]> git.donarmstrong.com Git - lilypond.git/blob - flower/include/string-handle.icc
*** empty log message ***
[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--2006 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 namespace std {
21   
22 INLINE void
23 String_handle::down ()
24 {
25   if (! (--data->ref_count_))
26     delete data;
27   data = 0;
28 }
29
30 /*
31   increase ref count
32
33   THIS does not have to be initialized.
34 */
35 INLINE void
36 String_handle::up (String_data *d)
37 {
38   data = d;
39   data->ref_count_++;
40 }
41
42 INLINE void
43 String_handle::copy ()
44 {
45   if (data->ref_count_ != 1)
46     {
47       String_data *newdata = new String_data (*data);
48       down ();
49       up (newdata);
50     }
51 }
52
53 INLINE
54 String_handle::String_handle ()
55 {
56   up (new String_data);
57 }
58
59 INLINE
60 String_handle::~String_handle ()
61 {
62   down ();
63 }
64
65 INLINE
66 String_handle::String_handle (String_handle const &src)
67 {
68   up (src.data);
69 }
70
71 INLINE Byte *
72 String_handle::get_bytes ()
73 {
74   copy ();
75   return data->get_bytes ();
76 }
77
78 INLINE char *
79 String_handle::get_c_str ()
80 {
81   copy ();
82   return (char *)data->get_bytes ();
83 }
84
85 INLINE Byte
86 const *String_handle::to_bytes () const
87 {
88   return data->to_bytes ();
89 }
90
91 INLINE char const *
92 String_handle::c_str () const
93 {
94   return (char const *)data->to_bytes ();
95 }
96
97 INLINE void
98 String_handle::operator = (String_handle const &src)
99 {
100   if (this == &src)
101     return;
102   down ();
103   up (src.data);
104 }
105
106 INLINE void
107 String_handle::operator += (char const *s)
108 {
109   copy ();
110   *data += s;
111 }
112
113 INLINE Byte
114 String_handle::operator [] (int j) const
115 {
116   return (*data)[j];
117 }
118
119 // !NOT SAFE!
120 // don't use this for loops. Use to_bytes ()
121 INLINE Byte &
122 String_handle::operator [] (int j)
123 {
124   copy ();      // hmm. Not efficient
125   return data->get_bytes ()[j];
126 }
127
128 INLINE void
129 String_handle::append (Byte const *byte, int length_i)
130 {
131   copy ();
132   data->append (byte, length_i);
133 }
134
135 INLINE void
136 String_handle::set (Byte const *byte, int length_i)
137 {
138   copy ();
139   data->set (byte, length_i);
140 }
141
142 INLINE void
143 String_handle::operator = (char const *p)
144 {
145   copy ();
146   data->set (p);
147 }
148
149 INLINE void
150 String_handle::trunc (int j)
151 {
152   copy (); data->trunc (j);
153 }
154
155 INLINE int
156 String_handle::length () const
157 {
158   return data->length_;
159 }
160
161 INLINE bool
162 String_handle::is_binary_bo () const
163 {
164   return data->is_binary_bo ();
165 }
166
167 }
168
169 #endif