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) {}
70 :
Super(
".StaticBuffer"), values_(values), size_(
size), rdonly_(false) {
71 ASSERT_require(
size==0 || values!=NULL);
74 :
Super(
".StaticBuffer"), values_(const_cast<
Value*>(values)), size_(
size), rdonly_(true) {
75 ASSERT_require(
size==0 || values!=NULL);
97 Address nWritten = newBuffer->write(values_, 0, size_);
98 if (nWritten != size_) {
99 throw std::runtime_error(
"StaticBuffer::copy() failed after copying " +
100 boost::lexical_cast<std::string>(nWritten) +
" of " +
101 boost::lexical_cast<std::string>(size_) +
102 (1==size_?
" value":
" values"));
108 return start < size_ ? size_-start : 0;
112 if (newSize != size_)
113 throw std::runtime_error(
"unable to resize StaticBuffer");
119 memcpy(buf, values_+address, n*
sizeof(values_[0]));
128 memcpy(values_+address, buf, n*
sizeof(values_[0]));