ROSE 0.11.145.147
AllocatingBuffer.h
1// WARNING: Changes to this file must be contributed back to Sawyer or else they will
2// be clobbered by the next update from Sawyer. The Sawyer repository is at
3// https://gitlab.com/charger7534/sawyer.git.
4
5
6
7
8#ifndef Sawyer_AllocatingBuffer_H
9#define Sawyer_AllocatingBuffer_H
10
11#include <Sawyer/Buffer.h>
12#include <Sawyer/Sawyer.h>
13
14#include <boost/lexical_cast.hpp>
15
16#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
17#include <boost/serialization/vector.hpp>
18#endif
19
20#ifdef SAWYER_HAVE_CEREAL
21#include <cereal/types/vector.hpp>
22#endif
23
24#include <cstring> // memcpy
25#include <string>
26
27namespace Sawyer {
28namespace Container {
29
34template<class A, class T>
35class AllocatingBuffer: public Buffer<A, T> {
36public:
37 typedef A Address;
38 typedef T Value;
41private:
42 std::vector<Value> values_;
43
44#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
45private:
46 friend class boost::serialization::access;
47
48 // Users: You'll need to register the subclass once you know its type, such as
49 // BOOST_CLASS_REGISTER(Sawyer::Container::AllocatingBuffer<size_t,uint8_t>);
50 template<class S>
51 void serialize(S &s, const unsigned /*version*/) {
52 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
53 s & BOOST_SERIALIZATION_NVP(values_);
54 }
55#endif
56
57#ifdef SAWYER_HAVE_CEREAL
58private:
59 friend class cereal::access;
60
61 template<class Archive>
62 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
63 archive(cereal::base_class<Super>());
64 archive(CEREAL_NVP(values_));
65 }
66#endif
67
68protected:
69 explicit AllocatingBuffer(Address size = 0): Super(".AllocatingBuffer"), values_(size) {}
70
71public:
77 return typename Buffer<A, T>::Ptr(new AllocatingBuffer(size));
78 }
79
83 static typename Buffer<A, T>::Ptr instance(const std::string &s) {
84 typename Buffer<A, T>::Ptr retval(new AllocatingBuffer(s.size()));
85 retval->write(s.c_str(), 0, s.size());
86 return retval;
87 }
88
89 typename Buffer<A, T>::Ptr copy() const /*override*/ {
90 typename Buffer<A, T>::Ptr newBuffer = instance(this->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"));
97 }
98 return newBuffer;
99 }
100
101 Address available(Address start) const /*override*/ {
102 return start < values_.size() ? values_.size() - start : 0;
103 }
104
105 void resize(Address newSize) /*override*/ {
106 values_.resize(newSize);
107 }
108
109 Address read(Value *buf, Address address, Address n) const /*override*/ {
110 n = std::min(n, available(address));
111 if (buf && n>0)
112 memcpy(buf, &values_[address], n*sizeof(values_[0]));
113 return n;
114 }
115
116 Address write(const Value *buf, Address address, Address n) /*override*/ {
117 n = std::min(n, available(address));
118 if (buf && n>0)
119 memcpy(&values_[address], buf, n*sizeof(values_[0]));
120 return n;
121 }
122
123 const Value* data() const /*override*/ {
124 return values_.size() > 0 ? &values_[0] : NULL;
125 }
126};
127
128} // namespace
129} // namespace
130
131#endif
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.
Definition Buffer.h:23
virtual Address size() const
Size of buffer.
Definition Buffer.h:93
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition Buffer.h:62
Reference-counting intrusive smart pointer.
Sawyer support library.