1 karl 1.4 //%2006////////////////////////////////////////////////////////////////////////
|
2 mike 1.2 //
3 // Copyright (c) 2000, 2001, 2002 BMC Software; Hewlett-Packard Development
4 // Company, L.P.; IBM Corp.; The Open Group; Tivoli Systems.
5 // Copyright (c) 2003 BMC Software; Hewlett-Packard Development Company, L.P.;
6 // IBM Corp.; EMC Corporation, The Open Group.
7 // Copyright (c) 2004 BMC Software; Hewlett-Packard Development Company, L.P.;
8 // IBM Corp.; EMC Corporation; VERITAS Software Corporation; The Open Group.
9 // Copyright (c) 2005 Hewlett-Packard Development Company, L.P.; IBM Corp.;
10 // EMC Corporation; VERITAS Software Corporation; The Open Group.
|
11 karl 1.4 // Copyright (c) 2006 Hewlett-Packard Development Company, L.P.; IBM Corp.;
12 // EMC Corporation; Symantec Corporation; The Open Group.
|
13 mike 1.2 //
14 // Permission is hereby granted, free of charge, to any person obtaining a copy
15 // of this software and associated documentation files (the "Software"), to
16 // deal in the Software without restriction, including without limitation the
17 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
18 // sell copies of the Software, and to permit persons to whom the Software is
19 // furnished to do so, subject to the following conditions:
20 //
21 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
22 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
23 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
24 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
25 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
26 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
27 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30 //==============================================================================
31 //
32 //%/////////////////////////////////////////////////////////////////////////////
33
34 mike 1.2 #ifndef Pegasus_Buffer_h
35 #define Pegasus_Buffer_h
36
37 #include <Pegasus/Common/Config.h>
38 #include <Pegasus/Common/Linkage.h>
39 #include <cstring>
40
41 PEGASUS_NAMESPACE_BEGIN
42
43 struct BufferRep
44 {
|
45 kumpf 1.10 Uint32 size;
46 Uint32 cap;
|
47 mike 1.2 char data[1];
48 };
49
50 class PEGASUS_COMMON_LINKAGE Buffer
51 {
52 public:
53
54 Buffer();
55
56 Buffer(const Buffer& x);
57
|
58 kumpf 1.10 Buffer(const char* data, Uint32 size);
|
59 mike 1.2
60 ~Buffer();
61
62 Buffer& operator=(const Buffer& x);
63
64 void swap(Buffer& x);
65
|
66 kumpf 1.10 Uint32 size() const;
|
67 mike 1.2
|
68 kumpf 1.10 Uint32 capacity() const;
|
69 mike 1.2
|
70 kumpf 1.5 /**
71 Returns a pointer to a character buffer with the Buffer contents.
72 The character buffer is null-terminated even if the Buffer contents
73 do not include a null termination character.
74 */
|
75 mike 1.2 const char* getData() const;
76
|
77 kumpf 1.10 char get(Uint32 i) const;
|
78 mike 1.2
|
79 kumpf 1.10 void set(Uint32 i, char x);
|
80 mike 1.2
|
81 kumpf 1.10 const char& operator[](Uint32 i) const;
|
82 mike 1.2
|
83 kumpf 1.10 void reserveCapacity(Uint32 cap);
|
84 mike 1.2
|
85 kumpf 1.10 void grow(Uint32 size, char x = '\0');
|
86 mike 1.2
87 void append(char x);
88
|
89 kumpf 1.10 void append(const char* data, Uint32 size);
|
90 mike 1.2
91 void append(char c1, char c2, char c3, char c4);
92
93 void append(
|
94 kumpf 1.9 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8);
|
95 mike 1.2
|
96 kumpf 1.10 void insert(Uint32 pos, const char* data, Uint32 size);
|
97 mike 1.2
|
98 kumpf 1.10 void remove(Uint32 pos, Uint32 size);
|
99 mike 1.2
|
100 kumpf 1.10 void remove(Uint32 pos);
|
101 mike 1.2
102 void clear();
103
104 private:
105
|
106 kumpf 1.10 void _reserve_aux(Uint32 cap);
|
107 mike 1.2
108 void _append_char_aux();
109
110 BufferRep* _rep;
111 static BufferRep _empty_rep;
112 };
113
114 inline Buffer::Buffer() : _rep(&_empty_rep)
115 {
116 }
117
118 inline Buffer::~Buffer()
119 {
|
120 gs.keenan 1.6 if (_rep->cap != 0)
|
121 kumpf 1.9 free(_rep);
|
122 mike 1.2 }
123
124 inline void Buffer::swap(Buffer& x)
125 {
126 BufferRep* tmp = _rep;
127 _rep = x._rep;
128 x._rep = tmp;
129 }
130
|
131 kumpf 1.10 inline Uint32 Buffer::size() const
|
132 mike 1.2 {
133 return _rep->size;
134 }
135
|
136 kumpf 1.10 inline Uint32 Buffer::capacity() const
|
137 mike 1.2 {
138 return _rep->cap;
139 }
140
141 inline const char* Buffer::getData() const
142 {
|
143 gs.keenan 1.6 if (_rep->cap == 0)
|
144 kumpf 1.5 {
145 const_cast<Buffer*>(this)->_append_char_aux();
146 }
147
148 _rep->data[_rep->size] = '\0';
149
|
150 mike 1.2 return _rep->data;
151 }
152
|
153 kumpf 1.10 inline char Buffer::get(Uint32 i) const
|
154 mike 1.2 {
155 return _rep->data[i];
156 }
157
|
158 kumpf 1.10 inline void Buffer::set(Uint32 i, char x)
|
159 mike 1.2 {
160 _rep->data[i] = x;
161 }
162
|
163 kumpf 1.10 inline const char& Buffer::operator[](Uint32 i) const
|
164 mike 1.2 {
165 return _rep->data[i];
166 }
167
|
168 kumpf 1.10 inline void Buffer::reserveCapacity(Uint32 cap)
|
169 mike 1.2 {
170 if (cap > _rep->cap)
|
171 kumpf 1.9 _reserve_aux(cap);
|
172 mike 1.2 }
173
|
174 kumpf 1.10 inline void Buffer::grow(Uint32 size, char x)
|
175 mike 1.2 {
|
176 kumpf 1.10 Uint32 cap = _rep->size + size;
|
177 mike 1.2
178 if (cap > _rep->cap)
|
179 kumpf 1.9 _reserve_aux(cap);
|
180 mike 1.2
181 memset(_rep->data + _rep->size, x, size);
182 _rep->size += size;
183 }
184
185 inline void Buffer::append(char x)
186 {
187 if (_rep->size == _rep->cap)
|
188 kumpf 1.9 _append_char_aux();
|
189 mike 1.2
190 _rep->data[_rep->size++] = x;
191 }
192
|
193 kumpf 1.10 inline void Buffer::append(const char* data, Uint32 size)
|
194 mike 1.2 {
|
195 kumpf 1.10 Uint32 cap = _rep->size + size;
|
196 mike 1.2
197 if (cap > _rep->cap)
|
198 kumpf 1.9 _reserve_aux(cap);
|
199 mike 1.2
200 memcpy(_rep->data + _rep->size, data, size);
201 _rep->size += size;
202 }
203
204 inline void Buffer::clear()
205 {
|
206 gs.keenan 1.6 if (_rep->cap != 0)
|
207 kumpf 1.9 _rep->size = 0;
|
208 mike 1.2 }
209
|
210 kumpf 1.10 inline void Buffer::remove(Uint32 pos)
|
211 mike 1.2 {
212 remove(pos, 1);
213 }
214
215 inline void Buffer::append(char c1, char c2, char c3, char c4)
216 {
|
217 kumpf 1.10 Uint32 cap = _rep->size + 4;
|
218 mike 1.2
219 if (cap > _rep->cap)
|
220 kumpf 1.9 _reserve_aux(cap);
|
221 mike 1.2
222 char* p = _rep->data + _rep->size;
223 p[0] = c1;
224 p[1] = c2;
225 p[2] = c3;
226 p[3] = c4;
227 _rep->size += 4;
228 }
229
230 inline void Buffer::append(
231 char c1, char c2, char c3, char c4, char c5, char c6, char c7, char c8)
232 {
|
233 kumpf 1.10 Uint32 cap = _rep->size + 8;
|
234 mike 1.2
235 if (cap > _rep->cap)
|
236 kumpf 1.9 _reserve_aux(cap);
|
237 mike 1.2
238 char* p = _rep->data + _rep->size;
239 p[0] = c1;
240 p[1] = c2;
241 p[2] = c3;
242 p[3] = c4;
243 p[4] = c5;
244 p[5] = c6;
245 p[6] = c7;
246 p[7] = c8;
247 _rep->size += 8;
248 }
249
250 inline bool operator==(const Buffer& x, const Buffer& y)
251 {
252 return memcmp(x.getData(), y.getData(), x.size()) == 0;
253 }
254
255 PEGASUS_NAMESPACE_END
256
257 #endif /* Pegasus_Buffer_h */
|