ROSE  0.9.9.139
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://github.com/matzke1/sawyer.
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 #include <boost/serialization/access.hpp>
16 #include <boost/serialization/base_object.hpp>
17 #include <boost/serialization/nvp.hpp>
18 #include <boost/serialization/vector.hpp>
19 #include <cstring> // memcpy
20 #include <string>
21 
22 namespace Sawyer {
23 namespace Container {
24 
29 template<class A, class T>
30 class AllocatingBuffer: public Buffer<A, T> {
31 public:
32  typedef A Address;
33  typedef T Value;
34  typedef Buffer<A, T> Super;
36 private:
37  Address size_;
38  std::vector<Value> values_;
39 
40 private:
41  friend class boost::serialization::access;
42 
43  // Users: You'll need to register the subclass once you know its type, such as
44  // BOOST_CLASS_REGISTER(Sawyer::Container::AllocatingBuffer<size_t,uint8_t>);
45  template<class S>
46  void serialize(S &s, const unsigned /*version*/) {
47  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
48  s & BOOST_SERIALIZATION_NVP(size_);
49  s & BOOST_SERIALIZATION_NVP(values_);
50  }
51 
52 protected:
53  explicit AllocatingBuffer(Address size = 0): Super(".AllocatingBuffer"), size_(size), values_(size) {}
54 
55 public:
60  static typename Buffer<A, T>::Ptr instance(Address size) {
61  return typename Buffer<A, T>::Ptr(new AllocatingBuffer(size));
62  }
63 
67  static typename Buffer<A, T>::Ptr instance(const std::string &s) {
68  typename Buffer<A, T>::Ptr retval(new AllocatingBuffer(s.size()));
69  retval->write(s.c_str(), 0, s.size());
70  return retval;
71  }
72 
73  typename Buffer<A, T>::Ptr copy() const /*override*/ {
74  typename Buffer<A, T>::Ptr newBuffer = instance(this->size());
75  Address nWritten = newBuffer->write(&values_[0], 0, this->size());
76  if (nWritten != this->size()) {
77  throw std::runtime_error("AllocatingBuffer::copy() failed after copying " +
78  boost::lexical_cast<std::string>(nWritten) + " of " +
79  boost::lexical_cast<std::string>(this->size()) +
80  (1==this->size()?" value":" values"));
81  }
82  return newBuffer;
83  }
84 
85  Address available(Address start) const /*override*/ {
86  return start < size_ ? size_-start : 0;
87  }
88 
89  void resize(Address newSize) /*override*/ {
90  values_.resize(newSize);
91  }
92 
93  Address read(Value *buf, Address address, Address n) const /*override*/ {
94  n = std::min(n, available(address));
95  if (buf && n>0)
96  memcpy(buf, &values_[address], n*sizeof(values_[0]));
97  return n;
98  }
99 
100  Address write(const Value *buf, Address address, Address n) /*override*/ {
101  n = std::min(n, available(address));
102  if (buf && n>0)
103  memcpy(&values_[address], buf, n*sizeof(values_[0]));
104  return n;
105  }
106 
107  const Value* data() const /*override*/ {
108  return size_ > 0 ? &values_[0] : NULL;
109  }
110 };
111 
112 } // namespace
113 } // namespace
114 
115 #endif
const Value * data() const
Data for the buffer.
T Value
Type of data that is stored.
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
A Address
Type of addresses used to index the stored data.
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
static Buffer< A, T >::Ptr instance(Address size)
Allocating constructor.
Reference-counting smart pointer.
Definition: SharedPointer.h:34
Name space for the entire library.
Definition: Access.h:11
Allocates memory as needed.
virtual Address size() const
Size of buffer.
Definition: Buffer.h:76
Address available(Address start) const
Distance to end of buffer.
Address write(const Value *buf, Address address, Address n)
Writes data to a buffer.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition: Buffer.h:45
static Buffer< A, T >::Ptr instance(const std::string &s)
Allocating constructor.
Base class for all buffers.
Definition: Buffer.h:25
Buffer< A, T > Super
Type of base class.
void resize(Address newSize)
Change the size of the buffer.