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 marek 1.14.4.1 void append_unchecked(char x);
88
|
89 kumpf 1.10 void append(const char* data, Uint32 size);
|
90 mike 1.2
|
91 marek 1.14.4.1 void append_unchecked(const char* data, Uint32 size);
92
|
93 mike 1.2 void append(char c1, char c2, char c3, char c4);
94
95 void append(
|
96 kumpf 1.9 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8);
|
97 mike 1.2
|
98 kumpf 1.10 void insert(Uint32 pos, const char* data, Uint32 size);
|
99 mike 1.2
|
100 kumpf 1.10 void remove(Uint32 pos, Uint32 size);
|
101 mike 1.2
|
102 kumpf 1.10 void remove(Uint32 pos);
|
103 mike 1.2
104 void clear();
105
106 private:
107
|
108 kumpf 1.10 void _reserve_aux(Uint32 cap);
|
109 mike 1.2
110 void _append_char_aux();
111
112 BufferRep* _rep;
113 static BufferRep _empty_rep;
|
114 marek 1.11 Uint32 _minCap;
|
115 mike 1.2 };
116
|
117 marek 1.11 inline Buffer::Buffer(Uint32 minCap) : _rep(&_empty_rep), _minCap(minCap)
|
118 kumpf 1.14 {
|
119 mike 1.2 }
120
121 inline Buffer::~Buffer()
122 {
|
123 gs.keenan 1.6 if (_rep->cap != 0)
|
124 kumpf 1.9 free(_rep);
|
125 mike 1.2 }
126
127 inline void Buffer::swap(Buffer& x)
128 {
129 BufferRep* tmp = _rep;
130 _rep = x._rep;
131 x._rep = tmp;
132 }
133
|
134 kumpf 1.10 inline Uint32 Buffer::size() const
|
135 mike 1.2 {
136 return _rep->size;
137 }
138
|
139 kumpf 1.10 inline Uint32 Buffer::capacity() const
|
140 mike 1.2 {
141 return _rep->cap;
142 }
143
144 inline const char* Buffer::getData() const
145 {
|
146 gs.keenan 1.6 if (_rep->cap == 0)
|
147 kumpf 1.5 {
148 const_cast<Buffer*>(this)->_append_char_aux();
149 }
150
151 _rep->data[_rep->size] = '\0';
152
|
153 mike 1.2 return _rep->data;
154 }
155
|
156 kumpf 1.10 inline char Buffer::get(Uint32 i) const
|
157 mike 1.2 {
158 return _rep->data[i];
159 }
160
|
161 kumpf 1.10 inline void Buffer::set(Uint32 i, char x)
|
162 mike 1.2 {
163 _rep->data[i] = x;
164 }
165
|
166 kumpf 1.10 inline const char& Buffer::operator[](Uint32 i) const
|
167 mike 1.2 {
168 return _rep->data[i];
169 }
170
|
171 kumpf 1.10 inline void Buffer::reserveCapacity(Uint32 cap)
|
172 mike 1.2 {
173 if (cap > _rep->cap)
|
174 kumpf 1.9 _reserve_aux(cap);
|
175 mike 1.2 }
176
|
177 kumpf 1.10 inline void Buffer::grow(Uint32 size, char x)
|
178 mike 1.2 {
|
179 kumpf 1.10 Uint32 cap = _rep->size + size;
|
180 mike 1.2
181 if (cap > _rep->cap)
|
182 kumpf 1.9 _reserve_aux(cap);
|
183 mike 1.2
184 memset(_rep->data + _rep->size, x, size);
185 _rep->size += size;
186 }
187
188 inline void Buffer::append(char x)
189 {
190 if (_rep->size == _rep->cap)
|
191 kumpf 1.9 _append_char_aux();
|
192 mike 1.2
193 _rep->data[_rep->size++] = x;
194 }
195
|
196 marek 1.14.4.1 inline void Buffer::append_unchecked(char x)
197 {
198 _rep->data[_rep->size++] = x;
199 }
200
|
201 kumpf 1.10 inline void Buffer::append(const char* data, Uint32 size)
|
202 mike 1.2 {
|
203 kumpf 1.10 Uint32 cap = _rep->size + size;
|
204 mike 1.2
205 if (cap > _rep->cap)
|
206 kumpf 1.9 _reserve_aux(cap);
|
207 mike 1.2
208 memcpy(_rep->data + _rep->size, data, size);
209 _rep->size += size;
210 }
211
|
212 marek 1.14.4.1 inline void Buffer::append_unchecked(const char* data, Uint32 size)
213 {
214 memcpy(_rep->data + _rep->size, data, size);
215 _rep->size += size;
216 }
217
|
218 mike 1.2 inline void Buffer::clear()
219 {
|
220 gs.keenan 1.6 if (_rep->cap != 0)
|
221 kumpf 1.9 _rep->size = 0;
|
222 mike 1.2 }
223
|
224 kumpf 1.10 inline void Buffer::remove(Uint32 pos)
|
225 mike 1.2 {
226 remove(pos, 1);
227 }
228
229 inline void Buffer::append(char c1, char c2, char c3, char c4)
230 {
|
231 kumpf 1.10 Uint32 cap = _rep->size + 4;
|
232 mike 1.2
233 if (cap > _rep->cap)
|
234 kumpf 1.9 _reserve_aux(cap);
|
235 mike 1.2
236 char* p = _rep->data + _rep->size;
237 p[0] = c1;
238 p[1] = c2;
239 p[2] = c3;
240 p[3] = c4;
241 _rep->size += 4;
242 }
243
244 inline void Buffer::append(
245 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8)
246 {
|
247 kumpf 1.10 Uint32 cap = _rep->size + 8;
|
248 mike 1.2
249 if (cap > _rep->cap)
|
250 kumpf 1.9 _reserve_aux(cap);
|
251 mike 1.2
252 char* p = _rep->data + _rep->size;
253 p[0] = c1;
254 p[1] = c2;
255 p[2] = c3;
256 p[3] = c4;
257 p[4] = c5;
258 p[5] = c6;
259 p[6] = c7;
260 p[7] = c8;
261 _rep->size += 8;
262 }
263
264 inline bool operator==(const Buffer& x, const Buffer& y)
265 {
266 return memcmp(x.getData(), y.getData(), x.size()) == 0;
267 }
268
269 PEGASUS_NAMESPACE_END
270
271 #endif /* Pegasus_Buffer_h */
|