ROSE  0.9.9.109
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://github.com/matzke1/sawyer.
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 #include <boost/serialization/access.hpp>
20 #include <boost/serialization/nvp.hpp>
21 #include <boost/serialization/string.hpp>
22 
23 namespace Sawyer {
24 namespace Container {
25 
36 template<class A = size_t, class T = boost::uint8_t>
38  typename Buffer<A, T>::Ptr buffer_; // reference counted buffer
39  A offset_; // initial offset into buffer
40  unsigned accessibility_; // uninterpreted bit mask
41  std::string name_; // for debugging
42 
43 public:
44  typedef A Address;
45  typedef T Value;
47 private:
48  friend class boost::serialization::access;
49 
50  // Users: You'll need to register the subclass once you know its type, such as
51  // BOOST_CLASS_REGISTER(Sawyer::Container::AddressSegment<size_t,uint8_t>);
52  // You'll also need to register the most-derived buffer type.
53  template<class S>
54  void serialize(S &s, const unsigned /*version*/) {
55  s & BOOST_SERIALIZATION_NVP(buffer_);
56  s & BOOST_SERIALIZATION_NVP(offset_);
57  s & BOOST_SERIALIZATION_NVP(accessibility_);
58  s & BOOST_SERIALIZATION_NVP(name_);
59  }
60 
61  //-------------------------------------------------------------------------------------------------------------------
62  // Constructors
63  //-------------------------------------------------------------------------------------------------------------------
64 public:
65 
70  AddressSegment(): offset_(0), accessibility_(0) {}
71 
77  : buffer_(other.buffer_), offset_(other.offset_), accessibility_(other.accessibility_), name_(other.name_) {}
78 
82  explicit AddressSegment(const typename Buffer<Address, Value>::Ptr &buffer, Address offset=0, unsigned accessBits=0,
83  const std::string &name="")
84  : buffer_(buffer), offset_(offset), accessibility_(accessBits), name_(name) {}
85 
86  //-------------------------------------------------------------------------------------------------------------------
87  // The following static methods are convenience wrappers around various buffer types.
88  //-------------------------------------------------------------------------------------------------------------------
89 public:
90 
96  static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="") {
97  return AddressSegment(NullBuffer<A, T>::instance(size), 0, accessBits, name);
98  }
99 
104  static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="") {
105  return AddressSegment(AllocatingBuffer<A, T>::instance(size), 0, accessBits, name);
106  }
107 
113  static AddressSegment staticInstance(Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
114  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits, name);
115  }
116  static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="") {
117  return AddressSegment(StaticBuffer<A, T>::instance(buffer, size), 0, accessBits|Access::IMMUTABLE, name);
118  }
122  static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE,
123  const std::string &name="") {
124  boost::iostreams::mapped_file::mapmode mode = (accessBits & Access::WRITABLE)!=0 ?
125  boost::iostreams::mapped_file::readwrite :
126  boost::iostreams::mapped_file::readonly;
127  return AddressSegment(MappedBuffer<A, T>::instance(fileName, mode), 0, accessBits, name);
128  }
129 
130  //-------------------------------------------------------------------------------------------------------------------
131  // Properties
132  //-------------------------------------------------------------------------------------------------------------------
133 public:
141  typename Buffer<A, T>::Ptr buffer() const { return buffer_; }
142  AddressSegment& buffer(const typename Buffer<A, T>::Ptr &b) { buffer_ = b; return *this; }
150  A offset() const { return offset_; }
151  AddressSegment& offset(A n) { offset_ = n; return *this; }
160  unsigned accessibility() const { return accessibility_; }
161  AddressSegment& accessibility(unsigned bits) { accessibility_ = bits; return *this; }
169  const std::string &name() const { return name_; }
170  AddressSegment& name(const std::string &s) { name_ = s; return *this; }
173  //-------------------------------------------------------------------------------------------------------------------
174  // Other methods
175  //-------------------------------------------------------------------------------------------------------------------
176 public:
181  bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const {
182  return requiredAccess==(accessibility_ & requiredAccess) && 0==(accessibility_ & prohibitedAccess);
183  }
184 
185 };
186 
187 } // namespace
188 } // namespace
189 
190 #endif
static AddressSegment staticInstance(const Value *buffer, Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to a static buffer.
AddressSegment(const typename Buffer< Address, Value >::Ptr &buffer, Address offset=0, unsigned accessBits=0, const std::string &name="")
Construct a segment with buffer.
T Value
Type of values stored by the underlying buffer.
Points to static data.
Definition: StaticBuffer.h:30
AddressSegment(const AddressSegment &other)
Copy constructor.
Buffer< A, T >::Ptr buffer() const
Property: buffer.
A homogeneous interval of an address space.
AddressSegment & accessibility(unsigned bits)
Property: access rights.
AddressSegment & buffer(const typename Buffer< A, T >::Ptr &b)
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.
Memory mapped file.
Definition: MappedBuffer.h:43
Reference-counting smart pointer.
Definition: SharedPointer.h:34
Name space for the entire library.
Definition: Access.h:11
AddressSegment & name(const std::string &s)
Property: name.
Allocates memory as needed.
AddressSegment & offset(A n)
Property: buffer offset.
const std::string & name() const
Property: name.
unsigned accessibility() const
Property: access rights.
Buffer that has no data.
Definition: NullBuffer.h:26
A Address
Address types expected to be used by the underlying buffer.
static AddressSegment nullInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment that points to no data.
static AddressSegment anonymousInstance(Address size, unsigned accessBits=0, const std::string &name="")
Create a segment with no backing store.
static AddressSegment fileInstance(const std::string &fileName, unsigned accessBits=Access::READABLE, const std::string &name="")
Map a file into an address space.
A offset() const
Property: buffer offset.
bool isAccessible(unsigned requiredAccess, unsigned prohibitedAccess) const
Determines whether this segment is accessible.
AddressSegment()
Default constructor.