C++/Tree Mapping Runtime Library
buffer.hxx
Go to the documentation of this file.
1// file : xsd/cxx/tree/buffer.hxx
2// license : GNU GPL v2 + exceptions; see accompanying LICENSE file
3
15#ifndef XSD_CXX_TREE_BUFFER_HXX
16#define XSD_CXX_TREE_BUFFER_HXX
17
18#include <new> // operator new/delete
19#include <cstddef> // std::size_t
20#include <cstring> // std::memcpy, std::memcmp
21
23
24namespace xsd
25{
26 namespace cxx
27 {
36 namespace tree
37 {
38 //@cond
39
40 class buffer_base
41 {
42 protected:
43 virtual
44 ~buffer_base ()
45 {
46 if (free_ && data_)
47 operator delete (data_);
48 }
49
50 buffer_base ()
51 : data_ (0), size_ (0), capacity_ (0), free_ (true)
52 {
53 }
54
55 protected:
56 char* data_;
57 size_t size_;
58 size_t capacity_;
59 bool free_;
60 };
61
62 //@endcond
63
79 template<typename C>
80 class buffer: protected buffer_base
81 {
82 public:
86 typedef std::size_t size_t;
87
88 public:
93
101 explicit
102 buffer (size_t size = 0);
103
111 buffer (size_t size, size_t capacity);
112
123 buffer (const void* data, size_t size);
124
137 buffer (const void* data, size_t size, size_t capacity);
138
153 buffer (void* data,
154 size_t size,
155 size_t capacity,
156 bool assume_ownership);
157
166 buffer (const buffer& x);
167
169
170 public:
180 buffer&
182
183 public:
190 size_t
191 capacity () const
192 {
193 return capacity_;
194 }
195
202 bool
203 capacity (size_t c)
204 {
205 return this->capacity (c, true);
206 }
207
208 public:
214 size_t
215 size () const {return size_;}
216
223 bool
224 size (size_t s)
225 {
226 bool r (false);
227
228 if (s > capacity_)
229 r = capacity (s);
230
231 size_ = s;
232
233 return r;
234 }
235
236 public:
242 const char*
243 data () const {return data_;}
244
250 char*
251 data () {return data_;}
252
259 const char*
260 begin () const {return data_;}
261
268 char*
269 begin () {return data_;}
270
277 const char*
278 end () const {return data_ + size_;}
279
286 char*
287 end () {return data_ + size_;}
288
289 public:
295 void
297
298 private:
299 bool
300 capacity (size_t capacity, bool copy);
301 };
302
309 template <typename C>
310 inline bool
311 operator== (const buffer<C>& a, const buffer<C>& b)
312 {
313 return a.size () == b.size () &&
314 std::memcmp (a.data (), b.data (), a.size ()) == 0;
315 }
316
323 template <typename C>
324 inline bool
325 operator!= (const buffer<C>& a, const buffer<C>& b)
326 {
327 return !(a == b);
328 }
329 }
330 }
331}
332
333#include <xsd/cxx/tree/buffer.txx>
334
335#endif // XSD_CXX_TREE_BUFFER_HXX
Simple binary buffer abstraction.
Definition buffer.hxx:81
size_t size() const
Get buffer's size.
Definition buffer.hxx:215
char * data()
Get the underlying memory region.
Definition buffer.hxx:251
buffer(void *data, size_t size, size_t capacity, bool assume_ownership)
Reuse an existing buffer.
buffer(const void *data, size_t size)
Allocate a buffer of the specified size and copy the data.
const char * begin() const
Get the beginning of the underlying memory region.
Definition buffer.hxx:260
buffer(const void *data, size_t size, size_t capacity)
Allocate a buffer of the specified size and capacity and copy the data.
buffer(size_t size=0)
Allocate a buffer of the specified size.
buffer(size_t size, size_t capacity)
Allocate a buffer of the specified size and capacity.
buffer(const buffer &x)
Copy constructor.
bool size(size_t s)
Set buffer's size.
Definition buffer.hxx:224
buffer & operator=(const buffer &x)
Copy assignment operator.
size_t capacity() const
Get buffer's capacity.
Definition buffer.hxx:191
void swap(buffer &x)
Swap data with another buffer.
bool capacity(size_t c)
Set buffer's capacity.
Definition buffer.hxx:203
std::size_t size_t
Size type.
Definition buffer.hxx:86
const char * end() const
Get the end of the underlying memory region.
Definition buffer.hxx:278
char * begin()
Get the beginning of the underlying memory region.
Definition buffer.hxx:269
char * end()
Get the end of the underlying memory region.
Definition buffer.hxx:287
const char * data() const
Get the underlying memory region.
Definition buffer.hxx:243
Contains exception definitions for the C++/Tree mapping.
bool operator==(const buffer< C > &a, const buffer< C > &b)
buffer comparison operator.
Definition buffer.hxx:311
bool operator!=(const buffer< C > &a, const buffer< C > &b)
buffer comparison operator.
Definition buffer.hxx:325

Copyright © 2005-2023 Code Synthesis.