8 #ifndef Sawyer_StaticBuffer_H
9 #define Sawyer_StaticBuffer_H
11 #include <Sawyer/AllocatingBuffer.h>
12 #include <Sawyer/Assert.h>
13 #include <Sawyer/Buffer.h>
14 #include <Sawyer/Sawyer.h>
16 #include <boost/serialization/access.hpp>
17 #include <boost/serialization/array.hpp>
18 #include <boost/serialization/base_object.hpp>
19 #include <boost/serialization/nvp.hpp>
20 #include <boost/serialization/split_member.hpp>
29 template<
class A,
class T>
42 friend class boost::serialization::access;
45 void save(S &s,
const unsigned )
const {
46 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
47 s & BOOST_SERIALIZATION_NVP(size_);
48 s & BOOST_SERIALIZATION_NVP(rdonly_);
49 s & boost::serialization::make_nvp(
"values", boost::serialization::make_array(values_, size_));
53 void load(S &s,
const unsigned ) {
54 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
55 s & BOOST_SERIALIZATION_NVP(size_);
56 s & BOOST_SERIALIZATION_NVP(rdonly_);
57 values_ =
new Value[size_];
58 s & boost::serialization::make_nvp(
"values", boost::serialization::make_array(values_, size_));
61 BOOST_SERIALIZATION_SPLIT_MEMBER();
66 : values_(NULL), size_(0), rdonly_(false) {}
69 StaticBuffer(Value *values, Address
size)
70 : Super(
".StaticBuffer"), values_(values), size_(size), rdonly_(false) {
71 ASSERT_require(size==0 || values!=NULL);
73 StaticBuffer(
const Value *values, Address
size)
74 : Super(
".StaticBuffer"), values_(const_cast<Value*>(values)), size_(size), rdonly_(true) {
75 ASSERT_require(size==0 || values!=NULL);
104 Address nWritten = newBuffer->write(values_, 0, size_);
105 if (nWritten != size_) {
106 throw std::runtime_error(
"StaticBuffer::copy() failed after copying " +
107 boost::lexical_cast<std::string>(nWritten) +
" of " +
108 boost::lexical_cast<std::string>(size_) +
109 (1==size_?
" value":
" values"));
115 return start < size_ ? size_-start : 0;
119 if (newSize != size_)
120 throw std::runtime_error(
"unable to resize StaticBuffer");
123 Address
read(Value *buf, Address address, Address n)
const {
126 memcpy(buf, values_+address, n*
sizeof(values_[0]));
130 Address
write(
const Value *buf, Address address, Address n) {
135 memcpy(values_+address, buf, n*
sizeof(values_[0]));
A Address
Type of addresses.
static Buffer< A, T >::Ptr instance(Address size)
Allocating constructor.
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
Reference-counting intrusive smart pointer.
Name space for the entire library.
virtual Address size() const
Size of buffer.
static Buffer< A, T >::Ptr instance(Value *values, Address size)
Construct from caller-supplied data.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Base class for all buffers.
bool isReadOnly() const
True if the buffer is read-only.
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
void resize(Address newSize)
Change the size of the buffer.
Buffer< A, T > Super
Type of base class.
Address available(Address start) const
Distance to end of buffer.
static Buffer< A, T >::Ptr instance(const Value *values, Address size)
Construct from caller-supplied data.
const Value * data() const
Data for the buffer.
Address write(const Value *buf, Address address, Address n)
Writes data to a buffer.