ROSE 0.11.145.147
AddressSegment.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_AddressSegment_H
9#define Sawyer_AddressSegment_H
10
11#include <Sawyer/Access.h>
12#include <Sawyer/AllocatingBuffer.h>
13#include <Sawyer/MappedBuffer.h>
14#include <Sawyer/NullBuffer.h>
15#include <Sawyer/StaticBuffer.h>
16#include <Sawyer/Sawyer.h>
17
18#include <boost/cstdint.hpp>
19
20#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
21#include <boost/serialization/string.hpp>
22#endif
23
24#ifdef SAWYER_HAVE_CEREAL
25#include <cereal/types/string.hpp>
26#endif
27
28namespace Sawyer {
29namespace Container {
30
41template<class A = size_t, class T = boost::uint8_t>
43 typename Buffer<A, T>::Ptr buffer_; // reference counted buffer
44 A offset_; // initial offset into buffer
45 unsigned accessibility_; // uninterpreted bit mask
46 std::string name_; // for debugging
47
48public:
49 typedef A Address;
50 typedef T Value;
53 //-------------------------------------------------------------------------------------------------------------------
54 // Serialization
55 //-------------------------------------------------------------------------------------------------------------------
56#ifdef SAWYER_HAVE_BOOST_SERIALIZATION
57private:
58 friend class boost::serialization::access;
59
60 // Users: You'll need to register the subclass once you know its type, such as
61 // BOOST_CLASS_REGISTER(Sawyer::Container::AddressSegment<size_t,uint8_t>);
62 // You'll also need to register the most-derived buffer type.
63 template<class S>
64 void serialize(S &s, const unsigned /*version*/) {
65 s & BOOST_SERIALIZATION_NVP(buffer_);
66 s & BOOST_SERIALIZATION_NVP(offset_);
67 s & BOOST_SERIALIZATION_NVP(accessibility_);
68 s & BOOST_SERIALIZATION_NVP(name_);
69 }
70#endif
71
72#ifdef SAWYER_HAVE_CEREAL
73private:
74 friend class cereal::access;
75
76 template<class Archive>
77 void CEREAL_SERIALIZE_FUNCTION_NAME(Archive &archive) {
78 archive(CEREAL_NVP(buffer_));
79 archive(CEREAL_NVP(offset_));
80 archive(CEREAL_NVP(accessibility_));
81 archive(CEREAL_NVP(name_));
82 }
83#endif
84
85 //-------------------------------------------------------------------------------------------------------------------
86 // Constructors
87 //-------------------------------------------------------------------------------------------------------------------
88public:
89
94 AddressSegment(): offset_(0), accessibility_(0) {}
95
101 : buffer_(other.buffer_), offset_(other.offset_), accessibility_(other.accessibility_), name_(other.name_) {}
102
106 explicit AddressSegment(const typename Buffer<Address, Value>::Ptr &buffer, Address offset=0, unsigned accessBits=0,
107 const std::string &name="")
108 : buffer_(buffer), offset_(offset), accessibility_(accessBits), name_(name) {}
109
112 buffer_ = other.buffer_;
113 offset_ = other.offset_;
114 accessibility_ = other.accessibility_;
115 name_ = other.name_;
116 return *this;
117 }
118
119 //-------------------------------------------------------------------------------------------------------------------
120 // The following static methods are convenience wrappers around various buffer types.
121 //-------------------------------------------------------------------------------------------------------------------
122public:
123
129 static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="") {
130 return AddressSegment(NullBuffer<A, T>::instance(size), 0, accessBits, name);
131 }
132
137 static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="") {
138 return AddressSegment(AllocatingBuffer<A, T>::instance(size), 0, accessBits, name);
139 }
140
146 static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
147 return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits, name);
148 }
149 static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
150 return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits|Access::IMMUTABLE, name);
151 }
155 static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE,
156 const std::string &name="") {
157 boost::iostreams::mapped_file::mapmode mode = (accessBits & Access::PRIVATE ? boost::iostreams::mapped_file::priv
158 : (accessBits & Access::WRITABLE ? boost::iostreams::mapped_file::readwrite
159 : boost::iostreams::mapped_file::readonly));
160 return AddressSegment(MappedBuffer<A, T>::instance(fileName, mode), 0, accessBits, name);
161 }
162
163 //-------------------------------------------------------------------------------------------------------------------
164 // Properties
165 //-------------------------------------------------------------------------------------------------------------------
166public:
174 typename Buffer<A, T>::Ptr buffer() const { return buffer_; }
175 AddressSegment& buffer(const typename Buffer<A, T>::Ptr &b) { buffer_ = b; return *this; }
183 A offset() const { return offset_; }
184 AddressSegment& offset(A n) { offset_ = n; return *this; }
193 unsigned accessibility() const { return accessibility_; }
194 AddressSegment& accessibility(unsigned bits) { accessibility_ = bits; return *this; }
202 const std::string &name() const { return name_; }
203 AddressSegment& name(const std::string &s) { name_ = s; return *this; }
206 //-------------------------------------------------------------------------------------------------------------------
207 // Other methods
208 //-------------------------------------------------------------------------------------------------------------------
209public:
214 bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const {
215 return requiredAccess==(accessibility_ & requiredAccess) && 0==(accessibility_ & prohibitedAccess);
216 }
217
218};
219
220} // namespace
221} // namespace
222
223#endif
A homogeneous interval of an address space.
static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to no data.
static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE, const std::string &name="")
Map a file into an address space.
bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const
Determines whether this segment is accessible.
AddressSegment & name(const std::string &s)
Property: name.
AddressSegment & offset(A n)
Property: buffer offset.
static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
const std::string & name() const
Property: name.
static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment with no backing store.
A Address
Address types expected to be used by the underlying buffer.
AddressSegment()
Default constructor.
AddressSegment & operator=(const AddressSegment &other)
Assignment.
AddressSegment & accessibility(unsigned bits)
Property: access rights.
A offset() const
Property: buffer offset.
AddressSegment(const typename Buffer< Address, Value >::Ptr &buffer, Address offset=0, unsigned accessBits=0, const std::string &name="")
Construct a segment with buffer.
unsigned accessibility() const
Property: access rights.
AddressSegment(const AddressSegment &other)
Copy constructor.
Buffer< A, T >::Ptr buffer() const
Property: buffer.
static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
AddressSegment & buffer(const typename Buffer< A, T >::Ptr &b)
Property: buffer.
T Value
Type of values stored by the underlying buffer.
Allocates memory as needed.
Buffer that has no data.
Definition NullBuffer.h:22
Points to static data.
Reference-counting intrusive smart pointer.
Sawyer support library.