ROSE  0.9.9.139
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://github.com/matzke1/sawyer.
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 <boost/serialization/access.hpp>
14 #include <boost/serialization/nvp.hpp>
15 #include <string>
16 
17 namespace Sawyer {
18 namespace Container {
19 
24 template<class A, class T>
25 class Buffer: public SharedObject {
26  std::string name_;
27  bool copyOnWrite_;
28 
29 protected:
30  explicit Buffer(const std::string &name = ""): name_(generateSequentialName()+name), copyOnWrite_(false) {}
31 
32 private:
33  friend class boost::serialization::access;
34 
35  template<class S>
36  void serialize(S &s, const unsigned /*version*/) {
37  s & BOOST_SERIALIZATION_NVP(name_);
38  s & BOOST_SERIALIZATION_NVP(copyOnWrite_);
39  }
40 
41 public:
46 
50  typedef A Address;
51 
53  typedef T Value;
54 
55 public:
56  virtual ~Buffer() {}
57 
63  virtual Ptr copy() const = 0;
64 
70  virtual Address available(Address address) const = 0;
71 
76  virtual Address size() const { return available(Address(0)); }
77 
81  virtual void resize(Address n) = 0;
82 
87  virtual void sync() {}
88 
94  const std::string& name() const { return name_; }
95  void name(const std::string &s) { name_ = s; }
107  virtual Address read(Value *buf, Address address, Address n) const = 0;
108 
114  virtual Address write(const Value *buf, Address address, Address n) = 0;
115 
119  virtual const Value* data() const = 0;
120 
127  bool copyOnWrite() const { return copyOnWrite_; }
128  void copyOnWrite(bool b) { copyOnWrite_ = b; }
130 };
131 
132 } // namespace
133 } // namespace
134 
135 #endif
A Address
Key type for addressing data.
Definition: Buffer.h:50
virtual Address available(Address address) const =0
Distance to end of buffer.
virtual Address read(Value *buf, Address address, Address n) const =0
Reads data from a buffer.
bool copyOnWrite() const
Property: Copy on write.
Definition: Buffer.h:127
virtual void resize(Address n)=0
Change the size of the buffer.
std::string generateSequentialName(size_t length=3)
Generate a sequential name.
Reference-counting smart pointer.
Definition: SharedPointer.h:34
Name space for the entire library.
Definition: Access.h:11
virtual Address size() const
Size of buffer.
Definition: Buffer.h:76
virtual Ptr copy() const =0
Create a new copy of buffer data.
SharedPointer< Buffer > Ptr
Reference counting smart pointer.
Definition: Buffer.h:45
T Value
Type of values stored in the buffer.
Definition: Buffer.h:53
Base class for all buffers.
Definition: Buffer.h:25
void name(const std::string &s)
Property: Name.
Definition: Buffer.h:95
const std::string & name() const
Property: Name.
Definition: Buffer.h:94
Base class for reference counted objects.
Definition: SharedObject.h:22
virtual void sync()
Synchronize buffer with persistent storage.
Definition: Buffer.h:87
virtual Address write(const Value *buf, Address address, Address n)=0
Writes data to a buffer.
void copyOnWrite(bool b)
Property: Copy on write.
Definition: Buffer.h:128
virtual const Value * data() const =0
Data for the buffer.