CatapultServer  v0.5.0.1 (Elephant)
ContiguousEntityContainer.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include "catapult/exceptions.h"
23 #include "catapult/types.h"
24 
25 namespace catapult { namespace model {
26 
30  Throw,
31 
33  Suppress
34  };
35 
38  template<typename TEntity>
40  public:
41  using value_type = TEntity;
42 
43  public:
46  constexpr BasicContiguousEntityContainer(TEntity* pEntity, size_t entitiesSize, EntityContainerErrorPolicy errorPolicy)
47  : m_pStart(pEntity)
48  , m_state(entitiesSize, EntityContainerErrorPolicy::Throw == errorPolicy)
49  {}
50 
51  private:
52  struct State {
53  public:
54  const size_t Size;
55  const bool ThrowOnError;
56  bool HasError;
57 
58  public:
59  constexpr State(size_t size, bool throwOnError)
60  : Size(size)
61  , ThrowOnError(throwOnError)
62  , HasError(false)
63  {}
64  };
65 
66  public:
68  template<typename TIteratorEntity>
69  class iterator {
70  public:
71  using difference_type = std::ptrdiff_t;
72  using value_type = TIteratorEntity;
73  using pointer = TIteratorEntity*;
74  using reference = TIteratorEntity&;
75  using iterator_category = std::forward_iterator_tag;
76 
77  private:
78  template<typename T>
79  static constexpr auto ToBytePointer(T* pEntity) {
80  return reinterpret_cast<uint8_t*>(pEntity);
81  }
82 
83  template<typename T>
84  static constexpr auto ToBytePointer(const T* pEntity) {
85  return reinterpret_cast<const uint8_t*>(pEntity);
86  }
87 
88  static constexpr auto Advance(value_type* pEntity, size_t numBytes) {
89  return reinterpret_cast<value_type*>(ToBytePointer(pEntity) + numBytes);
90  }
91 
92  public:
94  iterator(value_type* pStart, value_type* pCurrent, State& state)
95  : m_pStart(pStart)
96  , m_pCurrent(pCurrent ? pCurrent : Advance(m_pStart, state.Size))
97  , m_state(state) {
98  checkError();
99  }
100 
101  public:
103  bool operator==(const iterator& rhs) const {
104  return m_pStart == rhs.m_pStart && m_state.Size == rhs.m_state.Size && m_pCurrent == rhs.m_pCurrent;
105  }
106 
108  bool operator!=(const iterator& rhs) const {
109  return !(*this == rhs);
110  }
111 
112  public:
115  if (isEnd(m_pCurrent))
116  CATAPULT_THROW_OUT_OF_RANGE("cannot advance iterator beyond end");
117 
119  if (isEnd(m_pCurrent))
120  return *this;
121 
122  checkError();
123  return *this;
124  }
125 
128  auto copy = *this;
129  ++*this;
130  return copy;
131  }
132 
133  public:
136  return *(this->operator->());
137  }
138 
140  pointer operator->() const {
141  if (isEnd(m_pCurrent))
142  CATAPULT_THROW_OUT_OF_RANGE("cannot dereference at end");
143 
144  return m_pCurrent;
145  }
146 
147  private:
148  constexpr bool isEntityInBuffer(value_type* pEntity) const noexcept {
149  return
150  ToBytePointer(pEntity) <= endBytePointer() &&
151  ToBytePointer(pEntity) + sizeof(pEntity->Size) <= endBytePointer() && // ensure Size is readable
152  ToBytePointer(pEntity) + pEntity->Size <= endBytePointer();
153  }
154 
155  constexpr bool isEnd(value_type* pEntity) const noexcept {
156  return endBytePointer() == ToBytePointer(pEntity);
157  }
158 
159  constexpr auto endBytePointer() const noexcept {
161  }
162 
163  void checkError() {
164  if (isEnd(m_pCurrent) || (isEntityInBuffer(m_pCurrent) && m_pCurrent->Size >= sizeof(TEntity)))
165  return;
166 
167  m_pCurrent = reinterpret_cast<value_type*>(endBytePointer());
168  m_state.HasError = true;
169  if (m_state.ThrowOnError)
170  CATAPULT_THROW_RUNTIME_ERROR("error encountered while iterating");
171  }
172 
173  private:
177  };
178 
179  public:
181  auto cbegin() const {
183  }
184 
186  auto cend() const {
187  return iterator<const TEntity>(m_pStart, nullptr, m_state);
188  }
189 
191  auto begin() const {
193  }
194 
196  auto end() const {
197  return iterator<TEntity>(m_pStart, nullptr, m_state);
198  }
199 
200  public:
202  bool hasError() const {
203  return m_state.HasError;
204  }
205 
206  private:
207  TEntity* m_pStart;
208  mutable State m_state;
209  };
210 
213  template<typename TEntity>
215  TEntity* pEntity,
216  size_t size,
218  return BasicContiguousEntityContainer<TEntity>(pEntity, size, errorPolicy);
219  }
220 }}
catapult::model::MakeContiguousEntityContainer
constexpr BasicContiguousEntityContainer< TEntity > MakeContiguousEntityContainer(TEntity *pEntity, size_t size, EntityContainerErrorPolicy errorPolicy=EntityContainerErrorPolicy::Throw)
Definition: ContiguousEntityContainer.h:214
catapult::model::EntityContainerErrorPolicy
EntityContainerErrorPolicy
Polcies for handling iteration errors.
Definition: ContiguousEntityContainer.h:28
exceptions.h
catapult::model::BasicContiguousEntityContainer::iterator::checkError
void checkError()
Definition: ContiguousEntityContainer.h:163
catapult::model::BasicContiguousEntityContainer::BasicContiguousEntityContainer
constexpr BasicContiguousEntityContainer(TEntity *pEntity, size_t entitiesSize, EntityContainerErrorPolicy errorPolicy)
Definition: ContiguousEntityContainer.h:46
catapult::model::BasicContiguousEntityContainer::begin
auto begin() const
Returns an iterator that represents the first entity.
Definition: ContiguousEntityContainer.h:191
catapult::model::BasicContiguousEntityContainer::iterator
Actual iterator.
Definition: ContiguousEntityContainer.h:69
catapult::model::BasicContiguousEntityContainer::iterator::ToBytePointer
static constexpr auto ToBytePointer(T *pEntity)
Definition: ContiguousEntityContainer.h:79
catapult::model::BasicContiguousEntityContainer::iterator::operator==
bool operator==(const iterator &rhs) const
Returns true if this iterator and rhs are equal.
Definition: ContiguousEntityContainer.h:103
catapult::model::BasicContiguousEntityContainer::value_type
TEntity value_type
Definition: ContiguousEntityContainer.h:41
catapult::model::EntityContainerErrorPolicy::Throw
Immediately throw when an iteration error is encounted.
catapult::model::BasicContiguousEntityContainer
Definition: ContiguousEntityContainer.h:39
catapult::model::BasicContiguousEntityContainer::iterator::isEntityInBuffer
constexpr bool isEntityInBuffer(value_type *pEntity) const noexcept
Definition: ContiguousEntityContainer.h:148
catapult::model::BasicContiguousEntityContainer::State::HasError
bool HasError
Definition: ContiguousEntityContainer.h:56
catapult::model::BasicContiguousEntityContainer::end
auto end() const
Returns an iterator that represents one past the last entity.
Definition: ContiguousEntityContainer.h:196
catapult::model::BasicContiguousEntityContainer::iterator::operator!=
bool operator!=(const iterator &rhs) const
Returns true if this iterator and rhs are not equal.
Definition: ContiguousEntityContainer.h:108
catapult::model::BasicContiguousEntityContainer::iterator::m_pCurrent
value_type * m_pCurrent
Definition: ContiguousEntityContainer.h:175
catapult::model::BasicContiguousEntityContainer::m_pStart
TEntity * m_pStart
Definition: ContiguousEntityContainer.h:207
catapult::model::BasicContiguousEntityContainer::iterator::difference_type
std::ptrdiff_t difference_type
Definition: ContiguousEntityContainer.h:71
CATAPULT_THROW_OUT_OF_RANGE
#define CATAPULT_THROW_OUT_OF_RANGE(MESSAGE)
Macro used to throw a catapult out of range.
Definition: exceptions.h:191
catapult::model::BasicContiguousEntityContainer::iterator::operator++
iterator operator++(int)
Advances the iterator to the next position.
Definition: ContiguousEntityContainer.h:127
catapult::model::BasicContiguousEntityContainer::iterator::m_state
State & m_state
Definition: ContiguousEntityContainer.h:176
catapult::model::BasicContiguousEntityContainer::iterator::operator++
iterator & operator++()
Advances the iterator to the next position.
Definition: ContiguousEntityContainer.h:114
catapult::model::BasicContiguousEntityContainer::iterator::iterator
iterator(value_type *pStart, value_type *pCurrent, State &state)
Creates an iterator around pStart and state with specified current position (pCurrent).
Definition: ContiguousEntityContainer.h:94
catapult::model::BasicContiguousEntityContainer::iterator::Advance
static constexpr auto Advance(value_type *pEntity, size_t numBytes)
Definition: ContiguousEntityContainer.h:88
catapult::model::BasicContiguousEntityContainer::iterator::pointer
TIteratorEntity * pointer
Definition: ContiguousEntityContainer.h:73
size
uint64_t size
Definition: MemoryCounters.cpp:65
catapult::model::BasicContiguousEntityContainer::iterator::m_pStart
value_type * m_pStart
Definition: ContiguousEntityContainer.h:174
catapult::model::BasicContiguousEntityContainer::iterator::operator*
reference operator*() const
Returns a reference to the current entity.
Definition: ContiguousEntityContainer.h:135
catapult::model::BasicContiguousEntityContainer::State::Size
const size_t Size
Definition: ContiguousEntityContainer.h:54
catapult::model::BasicContiguousEntityContainer::iterator::value_type
TIteratorEntity value_type
Definition: ContiguousEntityContainer.h:72
catapult::model::BasicContiguousEntityContainer::State::ThrowOnError
const bool ThrowOnError
Definition: ContiguousEntityContainer.h:55
catapult::model::BasicContiguousEntityContainer::cend
auto cend() const
Returns a const iterator that represents one past the last entity.
Definition: ContiguousEntityContainer.h:186
CATAPULT_THROW_RUNTIME_ERROR
#define CATAPULT_THROW_RUNTIME_ERROR(MESSAGE)
Macro used to throw a catapult runtime error.
Definition: exceptions.h:167
catapult::model::BasicContiguousEntityContainer::cbegin
auto cbegin() const
Returns a const iterator that represents the first entity.
Definition: ContiguousEntityContainer.h:181
types.h
catapult::model::BasicContiguousEntityContainer::iterator::isEnd
constexpr bool isEnd(value_type *pEntity) const noexcept
Definition: ContiguousEntityContainer.h:155
catapult::model::EntityContainerErrorPolicy::Suppress
Do not throw when an iteration error is encountered (but error flag will still be set).
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::model::BasicContiguousEntityContainer::m_state
State m_state
Definition: ContiguousEntityContainer.h:208
catapult::model::BasicContiguousEntityContainer::iterator::endBytePointer
constexpr auto endBytePointer() const noexcept
Definition: ContiguousEntityContainer.h:159
catapult::model::BasicContiguousEntityContainer::State
Definition: ContiguousEntityContainer.h:52
catapult::model::BasicContiguousEntityContainer::State::State
constexpr State(size_t size, bool throwOnError)
Definition: ContiguousEntityContainer.h:59
catapult::model::BasicContiguousEntityContainer::iterator::iterator_category
std::forward_iterator_tag iterator_category
Definition: ContiguousEntityContainer.h:75
catapult::model::BasicContiguousEntityContainer::iterator::reference
TIteratorEntity & reference
Definition: ContiguousEntityContainer.h:74
catapult::model::BasicContiguousEntityContainer::hasError
bool hasError() const
Gets a value indicating whether or not there was an iteration error.
Definition: ContiguousEntityContainer.h:202
catapult::model::BasicContiguousEntityContainer::iterator::operator->
pointer operator->() const
Returns a pointer to the current entity.
Definition: ContiguousEntityContainer.h:140
catapult::model::BasicContiguousEntityContainer::iterator::ToBytePointer
static constexpr auto ToBytePointer(const T *pEntity)
Definition: ContiguousEntityContainer.h:84