ROSE 0.11.145.192
Buffer.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_Buffer_H
9#define Sawyer_Buffer_H
10
11#include <Sawyer/Sawyer.h>
12#include <Sawyer/SharedPointer.h>
13#include <string>
14
15namespace Sawyer {
16namespace Container {
17
22template<class A, class T>
23class Buffer: public SharedObject {
24 std::string name_;
25 bool copyOnWrite_;
26
27protected:
28 explicit Buffer(const std::string &name = ""): name_(generateSequentialName()+name), copyOnWrite_(false) {}
29
30#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
31private:
32 friend class boost::serialization::access;
33
34 template<class S>
35 void serialize(S &s, const unsigned /*version*/) {
36 s & BOOST_SERIALIZATION_NVP(name_);
37 s & BOOST_SERIALIZATION_NVP(copyOnWrite_);
38 }
39#endif
40
41#ifdef SAWYER_HAVE_CEREAL
42private:
43 friend class cereal::access;
44
45 template<class Archive>
46 void CEREAL_SAVE_FUNCTION_NAME(Archive &archive) const {
47 archive(CEREAL_NVP(name_));
48 archive(CEREAL_NVP(copyOnWrite_));
49 }
50
51 template<class Archive>
52 void CEREAL_LOAD_FUNCTION_NAME(Archive &archive) {
53 archive(CEREAL_NVP(name_));
54 archive(CEREAL_NVP(copyOnWrite_));
55 }
56#endif
57
58public:
63
67 typedef A Address;
68
70 typedef T Value;
71
72public:
73 virtual ~Buffer() {}
74
80 virtual Ptr copy() const = 0;
81
87 virtual Address available(Address address) const = 0;
88
93 virtual Address size() const { return available(Address(0)); }
94
98 virtual void resize(Address n) = 0;
99
104 virtual void sync() {}
105
111 const std::string& name() const { return name_; }
112 void name(const std::string &s) { name_ = s; }
124 virtual Address read(Value *buf, Address address, Address n) const = 0;
125
131 virtual Address write(const Value *buf, Address address, Address n) = 0;
132
136 virtual const Value* data() const = 0;
137
144 bool copyOnWrite() const { return copyOnWrite_; }
145 void copyOnWrite(bool b) { copyOnWrite_ = b; }
147};
148
149} // namespace
150} // namespace
151
152#endif
Base class for all buffers.
Definition Buffer.h:23
void name(const std::string &s)
Property: Name.
Definition Buffer.h:112
virtual Ptr copy() const =0
Create a new copy of buffer data.
virtual void sync()
Synchronize buffer with persistent storage.
Definition Buffer.h:104
virtual Address read(Value *buf, Address address, Address n) const =0
Reads data from a buffer.
void copyOnWrite(bool b)
Property: Copy on write.
Definition Buffer.h:145
A Address
Key type for addressing data.
Definition Buffer.h:67
virtual Address size() const
Size of buffer.
Definition Buffer.h:93
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition Buffer.h:62
virtual const Value * data() const =0
Data for the buffer.
virtual Address available(Address address) const =0
Distance to end of buffer.
T Value
Type of values stored in the buffer.
Definition Buffer.h:70
bool copyOnWrite() const
Property: Copy on write.
Definition Buffer.h:144
virtual Address write(const Value *buf, Address address, Address n)=0
Writes data to a buffer.
virtual void resize(Address n)=0
Change the size of the buffer.
const std::string & name() const
Property: Name.
Definition Buffer.h:111
Base class for reference counted objects.
Reference-counting intrusive smart pointer.
Sawyer support library.
std::string generateSequentialName(size_t length=3)
Generate a sequential name.