1 #ifndef WIBBLE_SYS_BUFFER_H 2 #define WIBBLE_SYS_BUFFER_H 44 Data() throw () : _ref(0), _size(0), _data(0) {}
47 Data(
void* buf,
size_t size,
bool own =
true);
48 Data(
const void* buf,
size_t size);
56 bool unref()
const throw () {
return --_ref == 0; }
81 item =
new Data(size);
98 Buffer(
void* buf,
size_t size,
bool own =
true) : item(0)
102 item =
new Data(buf, size, own);
116 item =
new Data(buf, size);
129 if (item && item->
unref())
136 if (item && item->
unref())
143 void*
data() throw () {
return item ? item->
_data : 0; }
146 const void*
data()
const throw () {
return item ? item->
_data : 0; }
149 size_t size()
const throw () {
return item ? item->
_size : 0; }
154 if (
size() == newSize)
158 if (item && item->
unref())
164 item =
new Data(newSize);
172 if (item == 0 && buf.item == 0)
174 if (item == 0 || buf.item == 0)
176 return *item == *buf.item;
187 if (item == 0 && buf.item == 0)
193 return *item < *buf.item;
Variable-size, reference-counted memory buffer.
Definition: buffer.h:33
Buffer(const void *buf, size_t size)
Create a buffer with a copy of the given data.
Definition: buffer.h:112
void * _data
Definition: buffer.h:41
~Buffer()
Definition: buffer.h:127
bool operator<(const Buffer &buf) const
Compare the contents of two buffers.
Definition: buffer.h:185
std::string print_preview(unsigned size) const
Render a c-string escaped print preview of maximum size buffer bytes.
Definition: buffer.cpp:106
bool operator<(const Data &d) const
Compare the contents of two buffers.
Definition: buffer.cpp:91
Buffer(const Buffer &buf)
Definition: buffer.h:121
void resize(size_t size)
Resize (enlarging or shrinking it) the buffer to `size' bytes.
Definition: buffer.cpp:61
Data()
Definition: buffer.h:44
bool operator!=(const Buffer &buf) const
Definition: buffer.h:179
bool unref() const
Decrement the reference count for this object, returning true when it reaches 0.
Definition: buffer.h:56
Buffer(size_t size)
Create a buffer with the specified size.
Definition: buffer.h:77
size_t size() const
Return the buffer size.
Definition: buffer.h:149
size_t _size
Definition: buffer.h:40
int _ref
Definition: buffer.h:39
Data * item
Definition: buffer.h:70
const void * data() const
Return a pointer to the buffer.
Definition: buffer.h:146
std::ostream & operator<<(std::ostream &o, const Buffer &b)
Definition: buffer.cpp:118
void * data()
Return a pointer to the buffer.
Definition: buffer.h:143
Buffer(void *buf, size_t size, bool own=true)
Create a buffer from existing data.
Definition: buffer.h:98
~Data()
Definition: buffer.cpp:54
void ref() const
Increment the reference count for this object.
Definition: buffer.h:52
Buffer & operator=(const Buffer &buf)
Definition: buffer.h:132
bool operator==(const Buffer &buf) const
Compare the contents of two buffers.
Definition: buffer.h:170
bool operator==(const Data &d) const
Compare the contents of two buffers.
Definition: buffer.cpp:79
Buffer()
Create a 0-lenght buffer.
Definition: buffer.h:74
void resize(size_t newSize)
Resize the buffer to hold exactly the specified amount of bytes.
Definition: buffer.h:152