ROSE 0.11.145.192
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://gitlab.com/charger7534/sawyer.git.
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
14namespace Sawyer {
15namespace Container {
16
21template<class A, class T>
22class NullBuffer: public Buffer<A, T> {
23public:
24 typedef A Address;
25 typedef T Value;
28private:
29 Address size_;
30
31#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
32private:
33 friend class boost::serialization::access;
34
35 // Users: You'll need to register the subclass once you know its type, such as
36 // BOOST_CLASS_REGISTER(Sawyer::Container::NullBuffer<size_t,uint8_t>);
37 template<class S>
38 void serialize(S &s, const unsigned /*version*/) {
39 s & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Super);
40 s & BOOST_SERIALIZATION_NVP(size_);
41 }
42#endif
43
44#ifdef SAWYER_HAVE_CEREAL
45private:
46 friend class cereal::access;
47
48 template<class Archive>
49 void CEREAL_SAVE_FUNCTION_NAME(Archive &archive) const {
50 archive(cereal::base_class<Super>());
51 archive(CEREAL_NVP(size_));
52 }
53
54 template<class Archive>
55 void CEREAL_LOAD_FUNCTION_NAME(Archive &archive) {
56 archive(cereal::base_class<Super>());
57 archive(CEREAL_NVP(size_));
58 }
59#endif
60
61protected:
62 NullBuffer(): Super(".NullBuffer"), size_(0) {}
63 explicit NullBuffer(Address size): Super(".NullBuffer"), size_(size) {}
64
65public:
70 return typename Buffer<A, T>::Ptr(new NullBuffer(size));
71 }
72
73 typename Buffer<A, T>::Ptr copy() const /*override*/ {
74 return instance(size_);
75 }
76
77 Address available(Address start) const /*override*/ {
78 return start < size_ ? size_ - start : 0;
79 }
80
81 void resize(Address newSize) /*override*/ {
82 size_ = newSize;
83 }
84
85 Address read(Value *buf, Address address, Address n) const /*override*/ {
86 Address nread = std::min(available(address), n);
87 if (buf) {
88 for (Address i=0; i<n; ++i)
89 buf[i] = Value();
90 }
91 return nread;
92 }
93
94 Address write(const Value */*buf*/, Address /*address*/, Address /*n*/) /*override*/ {
95 return 0;
96 }
97
98 const Value* data() const /*override*/ {
99 return NULL;
100 }
101};
102
103} // namespace
104} // namespace
105
106#endif
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
Buffer that has no data.
Definition NullBuffer.h:22
const Value * data() const
Data for the buffer.
Definition NullBuffer.h:98
Address read(Value *buf, Address address, Address n) const
Reads data from a buffer.
Definition NullBuffer.h:85
Buffer< A, T >::Ptr copy() const
Create a new copy of buffer data.
Definition NullBuffer.h:73
Address write(const Value *, Address, Address)
Writes data to a buffer.
Definition NullBuffer.h:94
static Buffer< A, T >::Ptr instance(Address size)
Construct a new buffer.
Definition NullBuffer.h:69
void resize(Address newSize)
Change the size of the buffer.
Definition NullBuffer.h:81
Address available(Address start) const
Distance to end of buffer.
Definition NullBuffer.h:77
A Address
Type of addresses.
Definition NullBuffer.h:24
Buffer< A, T > Super
Type of base class.
Definition NullBuffer.h:26
Reference-counting intrusive smart pointer.
Sawyer support library.