]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string-handle.icc
fc2efcb2b6e9ecd618d6c8f99f464bc560f957d7
[lilypond.git] / flower / include / string-handle.icc
1 /* -*-c++-*-
2
3 stringhandle.inl -- implement String_handle
4
5 source file of Flower lib
6
7 (c) 1997--2005 Han-Wen Nienhuys <hanwen@xs4all.nl>
8 */
9
10 #ifndef STRINGHANDLE_INL
11 #define STRINGHANDLE_INL
12
13 #include <cassert>
14 #include <memory>
15 using namespace std;
16
17 #include "string-data.hh"
18 #include "string-handle.hh"
19
20 INLINE void
21 String_handle::down ()
22 {
23   if (! (--data->ref_count_))
24     delete data;
25   data = 0;
26 }
27
28 /*
29   increase ref count
30
31   THIS does not have to be initialized.
32 */
33 INLINE void
34 String_handle::up (String_data *d)
35 {
36   data = d;
37   data->ref_count_++;
38 }
39
40 INLINE void
41 String_handle::copy ()
42 {
43   if (data->ref_count_ != 1)
44     {
45       String_data *newdata = new String_data (*data);
46       down ();
47       up (newdata);
48     }
49 }
50
51 INLINE
52 String_handle::String_handle ()
53 {
54   up (new String_data);
55 }
56
57 INLINE
58 String_handle::~String_handle ()
59 {
60   down ();
61 }
62
63 INLINE
64 String_handle::String_handle (String_handle const &src)
65 {
66   up (src.data);
67 }
68
69 INLINE Byte *
70 String_handle::get_bytes ()
71 {
72   copy ();
73   return data->get_bytes ();
74 }
75
76 INLINE char *
77 String_handle::get_str0 ()
78 {
79   copy ();
80   return (char *)data->get_bytes ();
81 }
82
83 INLINE Byte
84 const *String_handle::to_bytes () const
85 {
86   return data->to_bytes ();
87 }
88
89 INLINE char const *
90 String_handle::to_str0 () const
91 {
92   return (char const *)data->to_bytes ();
93 }
94
95 INLINE void
96 String_handle::operator = (String_handle const &src)
97 {
98   if (this == &src)
99     return;
100   down ();
101   up (src.data);
102 }
103
104 INLINE void
105 String_handle::operator += (char const *s)
106 {
107   copy ();
108   *data += s;
109 }
110
111 INLINE Byte
112 String_handle::operator [] (int j) const
113 {
114   return (*data)[j];
115 }
116
117 // !NOT SAFE!
118 // don't use this for loops. Use to_bytes ()
119 INLINE Byte &
120 String_handle::operator [] (int j)
121 {
122   copy ();      // hmm. Not efficient
123   return data->get_bytes ()[j];
124 }
125
126 INLINE void
127 String_handle::append (Byte const *byte, int length_i)
128 {
129   copy ();
130   data->append (byte, length_i);
131 }
132
133 INLINE void
134 String_handle::set (Byte const *byte, int length_i)
135 {
136   copy ();
137   data->set (byte, length_i);
138 }
139
140 INLINE void
141 String_handle::operator = (char const *p)
142 {
143   copy ();
144   data->set (p);
145 }
146
147 INLINE void
148 String_handle::trunc (int j)
149 {
150   copy (); data->trunc (j);
151 }
152
153 INLINE int
154 String_handle::length () const
155 {
156   return data->length_;
157 }
158
159 INLINE bool
160 String_handle::is_binary_bo () const
161 {
162   return data->is_binary_bo ();
163 }
164
165 #endif