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>
16#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
17#include <boost/serialization/vector.hpp>
20#ifdef SAWYER_HAVE_CEREAL
21#include <cereal/types/vector.hpp>
34template<
class A,
class T>
42 std::vector<Value> values_;
44#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
46 friend class boost::serialization::access;
51 void serialize(S &s,
const unsigned ) {
52 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(
Super);
53 s & BOOST_SERIALIZATION_NVP(values_);
57#ifdef SAWYER_HAVE_CEREAL
59 friend class cereal::access;
61 template<
class Archive>
62 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
63 archive(cereal::base_class<Super>());
64 archive(CEREAL_NVP(values_));
85 retval->write(s.c_str(), 0, s.size());
91 Address nWritten = newBuffer->write(&values_[0], 0, this->
size());
92 if (nWritten != this->
size()) {
93 throw std::runtime_error(
"AllocatingBuffer::copy() failed after copying " +
94 boost::lexical_cast<std::string>(nWritten) +
" of " +
95 boost::lexical_cast<std::string>(this->
size()) +
96 (1==this->
size()?
" value":
" values"));
102 return start < values_.size() ? values_.size() - start : 0;
106 values_.resize(newSize);
112 memcpy(buf, &values_[address], n*
sizeof(values_[0]));
119 memcpy(&values_[address], buf, n*
sizeof(values_[0]));
124 return values_.size() > 0 ? &values_[0] : NULL;
Allocates memory as needed.
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
A Address
Type of addresses used to index the stored data.
const Value * data() const
Data for the buffer.
static Buffer< A, T >::Ptr instance(const std::string &s)
Allocating constructor.
Address write(const Value *buf, Address address, Address n)
Writes data to a buffer.
static Buffer< A, T >::Ptr instance(Address size)
Allocating constructor.
void resize(Address newSize)
Change the size of the buffer.
T Value
Type of data that is stored.
Address available(Address start) const
Distance to end of buffer.
Buffer< A, T > Super
Type of base class.
Base class for all buffers.
virtual Address size() const
Size of buffer.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Reference-counting intrusive smart pointer.