F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
MapConstIterator.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title MapConstIterator
3 // \author bocchino
4 // \brief An abstract class template representing a const iterator for a map
5 // ======================================================================
6 
7 #ifndef Fw_MapConstIterator_HPP
8 #define Fw_MapConstIterator_HPP
9 
10 #include <new>
11 
15 #include "Fw/FPrimeBasicTypes.hpp"
16 
17 namespace Fw {
18 
19 template <typename K, typename V>
21  public:
22  // ----------------------------------------------------------------------
23  // Public types
24  // ----------------------------------------------------------------------
25 
32 
33  private:
34  // ----------------------------------------------------------------------
35  // Private types
36  // ----------------------------------------------------------------------
37 
39  using ImplKind = typename SetOrMapImplConstIterator<K, V>::ImplKind;
40 
42  union Impl {
44  Impl() {}
46  Impl(const ArrayIterator& it) : array(it) {}
48  Impl(const RedBlackTreeIterator& it) : redBlackTree(it) {}
50  ArrayIterator array;
52  RedBlackTreeIterator redBlackTree;
53  // ! Destructor
54  ~Impl() {}
55  };
56 
57  public:
58  // ----------------------------------------------------------------------
59  // Constructors and destructors
60  // ----------------------------------------------------------------------
61 
63  MapConstIterator(const ArrayIterator& it) : m_impl(it), m_implIterator(&m_impl.array) {}
64 
66  MapConstIterator(const RedBlackTreeIterator& it) : m_impl(it), m_implIterator(&m_impl.redBlackTree) {}
67 
69  MapConstIterator(const MapConstIterator& it) : m_impl(), m_implIterator() {
70  const auto implKind = it.getImplIterator().implKind();
71  switch (implKind) {
72  case ImplKind::ARRAY:
73  this->m_implIterator = new (&this->m_impl.array) ArrayIterator(it.m_impl.array);
74  break;
75  case ImplKind::RED_BLACK_TREE:
76  this->m_implIterator = new (&this->m_impl.redBlackTree) RedBlackTreeIterator(it.m_impl.redBlackTree);
77  break;
78  default:
79  FW_ASSERT(0, static_cast<FwAssertArgType>(implKind));
80  break;
81  }
82  }
83 
86 
87  public:
88  // ----------------------------------------------------------------------
89  // Public member functions
90  // ----------------------------------------------------------------------
91 
93  MapConstIterator& operator=(const MapConstIterator&) = default;
94 
96  bool operator==(const MapConstIterator& it) {
97  bool result = false;
98  const auto implKind1 = this->getImplIterator().implKind();
99  const auto implKind2 = it.getImplIterator().implKind();
100  if (implKind1 == implKind2) {
101  switch (implKind1) {
102  case ImplKind::ARRAY:
103  result = this->m_impl.array.compareEqual(it.m_impl.array);
104  break;
105  case ImplKind::RED_BLACK_TREE:
106  result = this->m_impl.redBlackTree.compareEqual(it.m_impl.redBlackTree);
107  break;
108  default:
109  FW_ASSERT(0, static_cast<FwAssertArgType>(implKind1));
110  break;
111  }
112  }
113  return result;
114  }
115 
117  bool operator!=(const MapConstIterator& it) { return !(*this == it); };
118 
121  this->getImplIterator().increment();
122  return *this;
123  }
124 
127  MapConstIterator tmp = *this;
128  ++(*this);
129  return tmp;
130  }
131 
133  bool isInRange() const { return this->getImplIterator().isInRange(); }
134 
136  const EntryBase& operator*() const { return this->getImplIterator().getEntry(); }
137 
139  const EntryBase* operator->() const { return &this->getImplIterator().getEntry(); }
140 
141  private:
142  // ----------------------------------------------------------------------
143  // Private helper functions
144  // ----------------------------------------------------------------------
145 
147  SetOrMapImplConstIterator<K, V>& getImplIterator() {
148  FW_ASSERT(this->m_implIterator != nullptr);
149  return *this->m_implIterator;
150  }
151 
153  const SetOrMapImplConstIterator<K, V>& getImplIterator() const {
154  FW_ASSERT(this->m_implIterator != nullptr);
155  return *this->m_implIterator;
156  }
157 
158  private:
159  // ----------------------------------------------------------------------
160  // Private member variables
161  // ----------------------------------------------------------------------
162 
164  Impl m_impl;
165 
167  SetOrMapImplConstIterator<K, V>* m_implIterator = nullptr;
168 };
169 
170 } // namespace Fw
171 
172 #endif
virtual void increment()=0
Increment the iterator.
MapConstIterator & operator++()
Prefix increment.
typename RedBlackTreeSetOrMapImpl< K, V >::ConstIterator RedBlackTreeIterator
The type of a red-black tree iterator.
typename ArraySetOrMapImpl< K, V >::ConstIterator ArrayIterator
The type of an array iterator.
const EntryBase * operator->() const
Pointer.
virtual ImplKind implKind() const =0
MapConstIterator & operator=(const MapConstIterator &)=default
Copy assignment operator.
virtual const SetOrMapImplEntry< KE, VN > & getEntry() const =0
MapConstIterator operator++(int)
Postfix increment.
MapConstIterator(const RedBlackTreeIterator &it)
Constructor providing a red-black tree implementation.
bool operator==(const MapConstIterator &it)
Equality comparison operator.
MapConstIterator(const MapConstIterator &it)
Copy constructor.
bool isInRange() const
Check whether the iterator is in range.
bool operator!=(const MapConstIterator &it)
Inequality comparison operator.
~MapConstIterator()
Destructor.
const EntryBase & operator*() const
Dereference.
virtual bool isInRange() const =0
MapConstIterator(const ArrayIterator &it)
Constructor providing an array implementation.
#define FW_ASSERT(...)
Definition: Assert.hpp:14