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