CatapultServer  v0.5.0.1 (Elephant)
ModificationSafeIterableContainer.h
Go to the documentation of this file.
1 
21 #pragma once
22 #include <stddef.h>
23 
24 namespace catapult { namespace utils {
25 
27  template<typename TContainer>
29  public:
30  using value_type = typename TContainer::value_type;
31  using iterator = typename TContainer::iterator;
32 
33  public:
35  size_t size() const {
36  return m_container.size();
37  }
38 
40  bool empty () const {
41  return m_container.empty();
42  }
43 
44  public:
46  auto cbegin() const {
47  return m_container.cbegin();
48  }
49 
51  auto cend() const {
52  return m_container.cend();
53  }
54 
56  auto begin() {
57  return m_container.begin();
58  }
59 
61  auto end() {
62  return m_container.end();
63  }
64 
65  public:
68  if (empty())
69  return nullptr;
70 
71  if (m_container.end() == m_iter)
72  m_iter = m_container.begin();
73 
74  auto pNext = &*m_iter;
75  ++m_iter;
76  return pNext;
77  }
78 
81  template<typename TPredicate>
82  value_type* nextIf(TPredicate predicate) {
83  auto pValue = next();
84  if (!pValue)
85  return nullptr;
86 
87  auto pFirstValue = pValue;
88  while (!predicate(*pValue)) {
89  pValue = next();
90 
91  // all values failed the predicate
92  if (pFirstValue == pValue)
93  return nullptr;
94  }
95 
96  return pValue;
97  }
98 
99  public:
101  void push_back(const value_type& value) {
102  auto isEmpty = m_container.empty();
103  m_container.push_back(value);
104 
105  if (isEmpty)
106  m_iter = m_container.begin();
107  }
108 
110  void erase(iterator position) {
111  if (m_iter == position) {
112  if (m_container.end() == m_iter)
113  m_iter = m_container.begin();
114  else
115  ++m_iter;
116  }
117 
118  m_container.erase(position);
119  }
120 
122  void clear() {
123  m_container.clear();
124  }
125 
126  private:
127  TContainer m_container;
129  };
130 }}
catapult::utils::ModificationSafeIterableContainer::erase
void erase(iterator position)
Removes a single element at position from the container.
Definition: ModificationSafeIterableContainer.h:110
catapult::utils::ModificationSafeIterableContainer::size
size_t size() const
Gets the number of elements in the container.
Definition: ModificationSafeIterableContainer.h:35
catapult::predicate
std::function< bool(TArgs...)> predicate
A predicate function.
Definition: functions.h:31
catapult::utils::ModificationSafeIterableContainer::nextIf
value_type * nextIf(TPredicate predicate)
Definition: ModificationSafeIterableContainer.h:82
catapult::utils::ModificationSafeIterableContainer::m_iter
iterator m_iter
Definition: ModificationSafeIterableContainer.h:128
catapult::utils::ModificationSafeIterableContainer::m_container
TContainer m_container
Definition: ModificationSafeIterableContainer.h:127
catapult::utils::ModificationSafeIterableContainer::empty
bool empty() const
Gets a value indicating whether or not the container is empty.
Definition: ModificationSafeIterableContainer.h:40
catapult::utils::ModificationSafeIterableContainer::iterator
typename TContainer::iterator iterator
Definition: ModificationSafeIterableContainer.h:31
catapult::utils::ModificationSafeIterableContainer::next
value_type * next()
Gets a pointer to the next value or nullptr if this container is empty.
Definition: ModificationSafeIterableContainer.h:67
catapult::utils::ModificationSafeIterableContainer::value_type
typename TContainer::value_type value_type
Definition: ModificationSafeIterableContainer.h:30
catapult::utils::ModificationSafeIterableContainer::cbegin
auto cbegin() const
Returns a const iterator that represents the first entity.
Definition: ModificationSafeIterableContainer.h:46
catapult::utils::ModificationSafeIterableContainer::end
auto end()
Returns an iterator that represents one past the last entity.
Definition: ModificationSafeIterableContainer.h:61
catapult::utils::ModificationSafeIterableContainer::clear
void clear()
Removes all elements from the container.
Definition: ModificationSafeIterableContainer.h:122
catapult
Definition: AddressExtractionExtension.cpp:28
catapult::utils::ModificationSafeIterableContainer::cend
auto cend() const
Returns a const iterator that represents one past the last entity.
Definition: ModificationSafeIterableContainer.h:51
catapult::utils::ModificationSafeIterableContainer
Wraps a container type and provides modification-safe iteration support.
Definition: ModificationSafeIterableContainer.h:28
catapult::utils::ModificationSafeIterableContainer::push_back
void push_back(const value_type &value)
Adds a new element at the end of the container.
Definition: ModificationSafeIterableContainer.h:101
catapult::utils::ModificationSafeIterableContainer::begin
auto begin()
Returns an iterator that represents the first entity.
Definition: ModificationSafeIterableContainer.h:56