ROSE 2.1.0
Loading...
Searching...
No Matches
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
85 virtual std::vector<Value> to_vector() const = 0;
86
92 virtual Address available(Address address) const = 0;
93
98 virtual Address size() const { return available(Address(0)); }
99
103 virtual void resize(Address n) = 0;
104
109 virtual void sync() {}
110
116 const std::string& name() const { return name_; }
117 void name(const std::string &s) { name_ = s; }
129 virtual Address read(Value *buf, Address address, Address n) const = 0;
130
136 virtual Address write(const Value *buf, Address address, Address n) = 0;
137
141 virtual const Value* data() const = 0;
142
149 bool copyOnWrite() const { return copyOnWrite_; }
150 void copyOnWrite(bool b) { copyOnWrite_ = b; }
152};
153
154} // namespace
155} // namespace
156
157#endif
Base class for all buffers.
Definition Buffer.h:23
void name(const std::string &s)
Property: Name.
Definition Buffer.h:117
virtual Ptr copy() const =0
Create a new copy of buffer data.
virtual void sync()
Synchronize buffer with persistent storage.
Definition Buffer.h:109
virtual Address read(Value *buf, Address address, Address n) const =0
Reads data from a buffer.
virtual std::vector< Value > to_vector() const =0
Create a std::vector copy of the data.
void copyOnWrite(bool b)
Property: Copy on write.
Definition Buffer.h:150
A Address
Key type for addressing data.
Definition Buffer.h:67
virtual Address size() const
Size of buffer.
Definition Buffer.h:98
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:149
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:116
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.