(file) Return to Buffer.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2