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

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2