ROSE  0.9.9.109
NullBuffer.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_NullBuffer_H
9 #define Sawyer_NullBuffer_H
10 
11 #include <Sawyer/Buffer.h>
12 #include <Sawyer/Sawyer.h>
13 
14 #include <boost/serialization/access.hpp>
15 #include <boost/serialization/base_object.hpp>
16 #include <boost/serialization/nvp.hpp>
17 
18 namespace Sawyer {
19 namespace Container {
20 
25 template<class A, class T>
26 class NullBuffer: public Buffer<A, T> {
27 public:
28  typedef A Address;
29  typedef T Value;
30  typedef Buffer<A, T> Super;
32 private:
33  Address size_;
34 
35 private:
36  friend class boost::serialization::access;
37 
38  // Users: You'll need to register the subclass once you know its type, such as
39  // BOOST_CLASS_REGISTER(Sawyer::Container::NullBuffer<size_t,uint8_t>);
40  template<class S>
41  void serialize(S &s, const unsigned /*version*/) {
42  s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
43  s & BOOST_SERIALIZATION_NVP(size_);
44  }
45 
46 protected:
47  NullBuffer(): Super(".NullBuffer"), size_(0) {}
48  explicit NullBuffer(Address size): Super(".NullBuffer"), size_(size) {}
49 
50 public:
54  static typename Buffer<A, T>::Ptr instance(Address size) {
55  return typename Buffer<A, T>::Ptr(new NullBuffer(size));
56  }
57 
58  typename Buffer<A, T>::Ptr copy() const /*override*/ {
59  return instance(size_);
60  }
61 
62  Address available(Address start) const /*override*/ {
63  return start < size_ ? size_ - start : 0;
64  }
65 
66  void resize(Address newSize) /*override*/ {
67  size_ = newSize;
68  }
69 
70  Address read(Value *buf, Address address, Address n) const /*override*/ {
71  Address nread = std::min(available(address), n);
72  if (buf) {
73  for (Address i=0; i<n; ++i)
74  buf[i] = Value();
75  }
76  return nread;
77  }
78 
79  Address write(const Value */*buf*/, Address /*address*/, Address /*n*/) /*override*/ {
80  return 0;
81  }
82 
83  const Value* data() const /*override*/ {
84  return NULL;
85  }
86 };
87 
88 } // namespace
89 } // namespace
90 
91 #endif
T Value
Type of values.
Definition: NullBuffer.h:29
A Address
Type of addresses.
Definition: NullBuffer.h:28
static Buffer< A, T >::Ptr instance(Address size)
Construct a new buffer.
Definition: NullBuffer.h:54
void resize(Address newSize)
Change the size of the buffer.
Definition: NullBuffer.h:66
Address available(Address start) const
Distance to end of buffer.
Definition: NullBuffer.h:62
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
Definition: NullBuffer.h:70
Reference-counting smart pointer.
Definition: SharedPointer.h:34
Address write(const Value *, Address, Address)
Writes data to a buffer.
Definition: NullBuffer.h:79
Name space for the entire library.
Definition: Access.h:11
Buffer< A, T > Super
Type of base class.
Definition: NullBuffer.h:30
virtual Address size() const
Size of buffer.
Definition: Buffer.h:76
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition: Buffer.h:45
Buffer that has no data.
Definition: NullBuffer.h:26
Base class for all buffers.
Definition: Buffer.h:25
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
Definition: NullBuffer.h:58
const Value * data() const
Data for the buffer.
Definition: NullBuffer.h:83