8 #ifndef Sawyer_AllocatingBuffer_H
9 #define Sawyer_AllocatingBuffer_H
11 #include <Sawyer/Buffer.h>
12 #include <Sawyer/Sawyer.h>
14 #include <boost/lexical_cast.hpp>
15 #include <boost/serialization/access.hpp>
16 #include <boost/serialization/base_object.hpp>
17 #include <boost/serialization/nvp.hpp>
18 #include <boost/serialization/vector.hpp>
29 template<
class A,
class T>
37 std::vector<Value> values_;
40 friend class boost::serialization::access;
45 void serialize(S &s,
const unsigned ) {
46 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
47 s & BOOST_SERIALIZATION_NVP(values_);
67 retval->write(s.c_str(), 0, s.size());
73 Address nWritten = newBuffer->write(&values_[0], 0, this->
size());
74 if (nWritten != this->
size()) {
75 throw std::runtime_error(
"AllocatingBuffer::copy() failed after copying " +
76 boost::lexical_cast<std::string>(nWritten) +
" of " +
77 boost::lexical_cast<std::string>(this->
size()) +
78 (1==this->
size()?
" value":
" values"));
84 return start < values_.size() ? values_.size() - start : 0;
88 values_.resize(newSize);
91 Address
read(Value *buf, Address address, Address n)
const {
94 memcpy(buf, &values_[address], n*
sizeof(values_[0]));
98 Address
write(
const Value *buf, Address address, Address n) {
101 memcpy(&values_[address], buf, n*
sizeof(values_[0]));
106 return values_.size() > 0 ? &values_[0] : NULL;
const Value * data() const
Data for the buffer.
T Value
Type of data that is stored.
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
A Address
Type of addresses used to index the stored data.
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
static Buffer< A, T >::Ptr instance(Address size)
Allocating constructor.
Reference-counting intrusive smart pointer.
Name space for the entire library.
Allocates memory as needed.
virtual Address size() const
Size of buffer.
Address available(Address start) const
Distance to end of buffer.
Address write(const Value *buf, Address address, Address n)
Writes data to a buffer.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
static Buffer< A, T >::Ptr instance(const std::string &s)
Allocating constructor.
Base class for all buffers.
Buffer< A, T > Super
Type of base class.
void resize(Address newSize)
Change the size of the buffer.