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);
94 memcpy(buf, &values_[address], n*
sizeof(values_[0]));
101 memcpy(&values_[address], buf, n*
sizeof(values_[0]));
106 return values_.size() > 0 ? &values_[0] : NULL;
Allocates memory as needed.
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
static Buffer< A, T >::Ptr instance(Address size)
Allocating constructor.
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
Buffer< A, T > Super
Type of base class.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
T Value
Type of data that is stored.
Address write(const Value *buf, Address address, Address n)
Writes data to a buffer.
Base class for all buffers.
Reference-counting intrusive smart pointer.
A Address
Type of addresses used to index the stored data.
Name space for the entire library.
Address available(Address start) const
Distance to end of buffer.
void resize(Address newSize)
Change the size of the buffer.
static Buffer< A, T >::Ptr instance(const std::string &s)
Allocating constructor.
const Value * data() const
Data for the buffer.
virtual Address size() const
Size of buffer.