1 martin 1.12 //%LICENSE////////////////////////////////////////////////////////////////
|
2 martin 1.13 //
|
3 martin 1.12 // Licensed to The Open Group (TOG) under one or more contributor license
4 // agreements. Refer to the OpenPegasusNOTICE.txt file distributed with
5 // this work for additional information regarding copyright ownership.
6 // Each contributor licenses this file to you under the OpenPegasus Open
7 // Source License; you may not use this file except in compliance with the
8 // License.
|
9 martin 1.13 //
|
10 martin 1.12 // Permission is hereby granted, free of charge, to any person obtaining a
11 // copy of this software and associated documentation files (the "Software"),
12 // to deal in the Software without restriction, including without limitation
13 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 // and/or sell copies of the Software, and to permit persons to whom the
15 // Software is furnished to do so, subject to the following conditions:
|
16 martin 1.13 //
|
17 martin 1.12 // The above copyright notice and this permission notice shall be included
18 // in all copies or substantial portions of the Software.
|
19 martin 1.13 //
|
20 martin 1.12 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
21 martin 1.13 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22 martin 1.12 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27 martin 1.13 //
|
28 martin 1.12 //////////////////////////////////////////////////////////////////////////
|
29 mike 1.2 //
30 //%/////////////////////////////////////////////////////////////////////////////
31
32 #ifndef Pegasus_Buffer_h
33 #define Pegasus_Buffer_h
34
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/Linkage.h>
37 #include <cstring>
38
39 PEGASUS_NAMESPACE_BEGIN
40
41 struct BufferRep
42 {
|
43 kumpf 1.10 Uint32 size;
44 Uint32 cap;
|
45 mike 1.2 char data[1];
46 };
47
48 class PEGASUS_COMMON_LINKAGE Buffer
49 {
50 public:
51
|
52 marek 1.11 Buffer(Uint32 minCap=2048);
|
53 mike 1.2
54 Buffer(const Buffer& x);
55
|
56 marek 1.11 Buffer(const char* data, Uint32 size, Uint32 minCap=2048);
|
57 mike 1.2
58 ~Buffer();
59
60 Buffer& operator=(const Buffer& x);
61
62 void swap(Buffer& x);
63
|
64 kumpf 1.10 Uint32 size() const;
|
65 mike 1.2
|
66 kumpf 1.10 Uint32 capacity() const;
|
67 mike 1.2
|
68 kumpf 1.5 /**
69 Returns a pointer to a character buffer with the Buffer contents.
70 The character buffer is null-terminated even if the Buffer contents
71 do not include a null termination character.
72 */
|
73 mike 1.2 const char* getData() const;
74
|
75 kumpf 1.10 char get(Uint32 i) const;
|
76 mike 1.2
|
77 kumpf 1.10 void set(Uint32 i, char x);
|
78 mike 1.2
|
79 kumpf 1.10 const char& operator[](Uint32 i) const;
|
80 mike 1.2
|
81 kumpf 1.10 void reserveCapacity(Uint32 cap);
|
82 mike 1.2
|
83 kumpf 1.10 void grow(Uint32 size, char x = '\0');
|
84 mike 1.2
85 void append(char x);
86
|
87 thilo.boehm 1.15 void append_unchecked(char x);
88
|
89 kumpf 1.10 void append(const char* data, Uint32 size);
|
90 mike 1.2
|
91 thilo.boehm 1.15 void append_unchecked(const char* data, Uint32 size);
92
93 void append(char c1, char c2);
94
95 void append(char c1, char c2, char c3);
96
|
97 mike 1.2 void append(char c1, char c2, char c3, char c4);
98
99 void append(
|
100 kumpf 1.9 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8);
|
101 mike 1.2
|
102 kumpf 1.10 void insert(Uint32 pos, const char* data, Uint32 size);
|
103 mike 1.2
|
104 kumpf 1.10 void remove(Uint32 pos, Uint32 size);
|
105 mike 1.2
|
106 kumpf 1.10 void remove(Uint32 pos);
|
107 mike 1.2
108 void clear();
109
110 private:
111
|
112 kumpf 1.10 void _reserve_aux(Uint32 cap);
|
113 mike 1.2
114 void _append_char_aux();
115
116 BufferRep* _rep;
117 static BufferRep _empty_rep;
|
118 marek 1.11 Uint32 _minCap;
|
119 mike 1.2 };
120
|
121 marek 1.11 inline Buffer::Buffer(Uint32 minCap) : _rep(&_empty_rep), _minCap(minCap)
|
122 kumpf 1.14 {
|
123 mike 1.2 }
124
125 inline Buffer::~Buffer()
126 {
|
127 gs.keenan 1.6 if (_rep->cap != 0)
|
128 kumpf 1.9 free(_rep);
|
129 mike 1.2 }
130
131 inline void Buffer::swap(Buffer& x)
132 {
133 BufferRep* tmp = _rep;
134 _rep = x._rep;
135 x._rep = tmp;
136 }
137
|
138 kumpf 1.10 inline Uint32 Buffer::size() const
|
139 mike 1.2 {
140 return _rep->size;
141 }
142
|
143 kumpf 1.10 inline Uint32 Buffer::capacity() const
|
144 mike 1.2 {
145 return _rep->cap;
146 }
147
148 inline const char* Buffer::getData() const
149 {
|
150 gs.keenan 1.6 if (_rep->cap == 0)
|
151 kumpf 1.5 {
152 const_cast<Buffer*>(this)->_append_char_aux();
153 }
154
155 _rep->data[_rep->size] = '\0';
156
|
157 mike 1.2 return _rep->data;
158 }
159
|
160 kumpf 1.10 inline char Buffer::get(Uint32 i) const
|
161 mike 1.2 {
162 return _rep->data[i];
163 }
164
|
165 kumpf 1.10 inline void Buffer::set(Uint32 i, char x)
|
166 mike 1.2 {
167 _rep->data[i] = x;
168 }
169
|
170 kumpf 1.10 inline const char& Buffer::operator[](Uint32 i) const
|
171 mike 1.2 {
172 return _rep->data[i];
173 }
174
|
175 kumpf 1.10 inline void Buffer::reserveCapacity(Uint32 cap)
|
176 mike 1.2 {
177 if (cap > _rep->cap)
|
178 kumpf 1.9 _reserve_aux(cap);
|
179 mike 1.2 }
180
|
181 kumpf 1.10 inline void Buffer::grow(Uint32 size, char x)
|
182 mike 1.2 {
|
183 kumpf 1.10 Uint32 cap = _rep->size + size;
|
184 mike 1.2
185 if (cap > _rep->cap)
|
186 kumpf 1.9 _reserve_aux(cap);
|
187 mike 1.2
188 memset(_rep->data + _rep->size, x, size);
189 _rep->size += size;
190 }
191
192 inline void Buffer::append(char x)
193 {
194 if (_rep->size == _rep->cap)
|
195 kumpf 1.9 _append_char_aux();
|
196 mike 1.2
197 _rep->data[_rep->size++] = x;
198 }
199
|
200 thilo.boehm 1.15 inline void Buffer::append_unchecked(char x)
201 {
202 _rep->data[_rep->size++] = x;
203 }
204
|
205 kumpf 1.10 inline void Buffer::append(const char* data, Uint32 size)
|
206 mike 1.2 {
|
207 kumpf 1.10 Uint32 cap = _rep->size + size;
|
208 mike 1.2
209 if (cap > _rep->cap)
|
210 kumpf 1.9 _reserve_aux(cap);
|
211 mike 1.2
212 memcpy(_rep->data + _rep->size, data, size);
213 _rep->size += size;
214 }
215
|
216 thilo.boehm 1.15 inline void Buffer::append_unchecked(const char* data, Uint32 size)
217 {
218 memcpy(_rep->data + _rep->size, data, size);
219 _rep->size += size;
220 }
221
|
222 mike 1.2 inline void Buffer::clear()
223 {
|
224 gs.keenan 1.6 if (_rep->cap != 0)
|
225 kumpf 1.9 _rep->size = 0;
|
226 mike 1.2 }
227
|
228 kumpf 1.10 inline void Buffer::remove(Uint32 pos)
|
229 mike 1.2 {
230 remove(pos, 1);
231 }
232
|
233 thilo.boehm 1.15 inline void Buffer::append(char c1, char c2)
234 {
235 Uint32 cap = _rep->size + 2;
236
237 if (cap > _rep->cap)
238 _reserve_aux(cap);
239
240 char* p = _rep->data + _rep->size;
241 p[0] = c1;
242 p[1] = c2;
243 _rep->size += 2;
244 }
245
246 inline void Buffer::append(char c1, char c2, char c3)
247 {
248 Uint32 cap = _rep->size + 3;
249
250 if (cap > _rep->cap)
251 _reserve_aux(cap);
252
253 char* p = _rep->data + _rep->size;
254 thilo.boehm 1.15 p[0] = c1;
255 p[1] = c2;
256 p[2] = c3;
257 _rep->size += 3;
258 }
259
|
260 mike 1.2 inline void Buffer::append(char c1, char c2, char c3, char c4)
261 {
|
262 kumpf 1.10 Uint32 cap = _rep->size + 4;
|
263 mike 1.2
264 if (cap > _rep->cap)
|
265 kumpf 1.9 _reserve_aux(cap);
|
266 mike 1.2
267 char* p = _rep->data + _rep->size;
268 p[0] = c1;
269 p[1] = c2;
270 p[2] = c3;
271 p[3] = c4;
272 _rep->size += 4;
273 }
274
275 inline void Buffer::append(
276 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8)
277 {
|
278 kumpf 1.10 Uint32 cap = _rep->size + 8;
|
279 mike 1.2
280 if (cap > _rep->cap)
|
281 kumpf 1.9 _reserve_aux(cap);
|
282 mike 1.2
283 char* p = _rep->data + _rep->size;
284 p[0] = c1;
285 p[1] = c2;
286 p[2] = c3;
287 p[3] = c4;
288 p[4] = c5;
289 p[5] = c6;
290 p[6] = c7;
291 p[7] = c8;
292 _rep->size += 8;
293 }
294
295 inline bool operator==(const Buffer& x, const Buffer& y)
296 {
297 return memcmp(x.getData(), y.getData(), x.size()) == 0;
298 }
299
300 PEGASUS_NAMESPACE_END
301
302 #endif /* Pegasus_Buffer_h */
|