buffer.h
Go to the documentation of this file.
#ifndef WIBBLE_SYS_BUFFER_H
#define WIBBLE_SYS_BUFFER_H
/*
* Variable-size, reference-counted memory buffer
*
* Copyright (C) 2003--2013 Enrico Zini <enrico@debian.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stddef.h> // for size_t
#include <ostream>
namespace wibble {
namespace sys {
class Buffer
{
public:
class Data
{
protected:
mutable int _ref;
size_t _size;
void* _data;
public:
Data() throw () : _ref(0), _size(0), _data(0) {}
Data(size_t size);
// if own == true, take possession of the memory buffer, else copy it
Data(void* buf, size_t size, bool own = true);
Data(const void* buf, size_t size);
~Data();
void ref() const throw () { ++_ref; }
bool unref() const throw () { return --_ref == 0; }
void resize(size_t size);
bool operator==(const Data& d) const throw();
bool operator<(const Data& d) const throw();
friend class Buffer;
};
Data* item;
public:
Buffer() throw () : item(0) {}
Buffer(size_t size) : item(0)
{
if (size)
{
item = new Data(size);
item->ref();
}
}
Buffer(void* buf, size_t size, bool own = true) : item(0)
{
if (size)
{
item = new Data(buf, size, own);
item->ref();
}
}
Buffer(const void* buf, size_t size) : item(0)
{
if (size)
{
item = new Data(buf, size);
item->ref();
}
}
Buffer(const Buffer& buf) throw ()
{
if (buf.item)
buf.item->ref();
item = buf.item;
}
~Buffer()
{
if (item && item->unref())
delete item;
}
Buffer& operator=(const Buffer& buf)
{
if (buf.item)
buf.item->ref(); // Do it early to correctly handle the case of x = x;
if (item && item->unref())
delete item;
item = buf.item;
return *this;
}
void* data() throw () { return item ? item->_data : 0; }
const void* data() const throw () { return item ? item->_data : 0; }
size_t size() const throw () { return item ? item->_size : 0; }
void resize(size_t newSize)
{
if (size() == newSize)
return;
if (!newSize)
{
if (item && item->unref())
delete item;
item = 0;
} else if (item) {
item->resize(newSize);
} else {
item = new Data(newSize);
item->ref();
}
}
bool operator==(const Buffer& buf) const throw ()
{
if (item == 0 && buf.item == 0)
return true;
if (item == 0 || buf.item == 0)
return false;
return *item == *buf.item;
}
bool operator!=(const Buffer& buf) const throw ()
{
return !operator==(buf);
}
bool operator<(const Buffer& buf) const throw ()
{
if (item == 0 && buf.item == 0)
return false;
if (item == 0)
return true;
if (buf.item == 0)
return false;
return *item < *buf.item;
}
std::string print_preview(unsigned size) const;
};
std::ostream& operator<<(std::ostream& o, const Buffer& b);
}
}
// vim:set ts=4 sw=4:
#endif
wibble::sys::Buffer
Variable-size, reference-counted memory buffer.
Definition: buffer.h:33
wibble::sys::Buffer::Buffer
Buffer(const void *buf, size_t size)
Create a buffer with a copy of the given data.
Definition: buffer.h:112
wibble::sys::Buffer::Data
Definition: buffer.h:36
wibble::sys::Buffer::Data::_data
void * _data
Definition: buffer.h:41
wibble::sys::Buffer::~Buffer
~Buffer()
Definition: buffer.h:127
wibble::sys::Buffer::operator<
bool operator<(const Buffer &buf) const
Compare the contents of two buffers.
Definition: buffer.h:185
wibble::sys::Buffer::print_preview
std::string print_preview(unsigned size) const
Render a c-string escaped print preview of maximum size buffer bytes.
Definition: buffer.cpp:106
wibble::sys::Buffer::Data::operator<
bool operator<(const Data &d) const
Compare the contents of two buffers.
Definition: buffer.cpp:91
wibble::sys::Buffer::Buffer
Buffer(const Buffer &buf)
Definition: buffer.h:121
wibble::sys::Buffer::Data::resize
void resize(size_t size)
Resize (enlarging or shrinking it) the buffer to `size&#39; bytes.
Definition: buffer.cpp:61
wibble::sys::Buffer::Data::Data
Data()
Definition: buffer.h:44
wibble::sys::Buffer::operator!=
bool operator!=(const Buffer &buf) const
Definition: buffer.h:179
wibble::sys::Buffer::Data::unref
bool unref() const
Decrement the reference count for this object, returning true when it reaches 0.
Definition: buffer.h:56
wibble::sys::Buffer::Buffer
Buffer(size_t size)
Create a buffer with the specified size.
Definition: buffer.h:77
wibble::sys::Buffer::size
size_t size() const
Return the buffer size.
Definition: buffer.h:149
wibble::sys::Buffer::Data::_size
size_t _size
Definition: buffer.h:40
wibble::sys::Buffer::Data::_ref
int _ref
Definition: buffer.h:39
wibble::sys::Buffer::item
Data * item
Definition: buffer.h:70
wibble::sys::Buffer::data
const void * data() const
Return a pointer to the buffer.
Definition: buffer.h:146
wibble::sys::operator<<
std::ostream & operator<<(std::ostream &o, const Buffer &b)
Definition: buffer.cpp:118
wibble::sys::Buffer::data
void * data()
Return a pointer to the buffer.
Definition: buffer.h:143
wibble::sys::Buffer::Buffer
Buffer(void *buf, size_t size, bool own=true)
Create a buffer from existing data.
Definition: buffer.h:98
wibble::sys::Buffer::Data::~Data
~Data()
Definition: buffer.cpp:54
wibble
Definition: amorph.h:17
wibble::sys::Buffer::Data::ref
void ref() const
Increment the reference count for this object.
Definition: buffer.h:52
wibble::sys::Buffer::operator=
Buffer & operator=(const Buffer &buf)
Definition: buffer.h:132
wibble::sys::Buffer::operator==
bool operator==(const Buffer &buf) const
Compare the contents of two buffers.
Definition: buffer.h:170
wibble::sys::Buffer::Data::operator==
bool operator==(const Data &d) const
Compare the contents of two buffers.
Definition: buffer.cpp:79
wibble::sys::Buffer::Buffer
Buffer()
Create a 0-lenght buffer.
Definition: buffer.h:74
wibble::sys::Buffer::resize
void resize(size_t newSize)
Resize the buffer to hold exactly the specified amount of bytes.
Definition: buffer.h:152