F´ Flight Software - C/C++ Documentation
A framework for building embedded system applications to NASA flight quality standards.
SetConstIterator.hpp
Go to the documentation of this file.
1 // ======================================================================
2 // \title SetConstIterator
3 // \author bocchino
4 // \brief An abstract class template representing a const iterator for a set
5 // ======================================================================
6 
7 #ifndef Fw_SetConstIterator_HPP
8 #define Fw_SetConstIterator_HPP
9 
10 #include <new>
11 
15 #include "Fw/FPrimeBasicTypes.hpp"
16 
17 namespace Fw {
18 
19 template <typename T>
21  public:
22  // ----------------------------------------------------------------------
23  // Public types
24  // ----------------------------------------------------------------------
25 
28 
31 
32  private:
33  // ----------------------------------------------------------------------
34  // Private types
35  // ----------------------------------------------------------------------
36 
38  using ImplKind = typename SetOrMapImplConstIterator<T, Nil>::ImplKind;
39 
41  union Impl {
43  Impl() {}
45  Impl(const ArrayIterator& it) : array(it) {}
47  Impl(const RedBlackTreeIterator& it) : redBlackTree(it) {}
49  ArrayIterator array;
51  RedBlackTreeIterator redBlackTree;
52  // ! Destructor
53  ~Impl() {}
54  };
55 
56  public:
57  // ----------------------------------------------------------------------
58  // Constructors and destructors
59  // ----------------------------------------------------------------------
60 
62  SetConstIterator(const ArrayIterator& it) : m_impl(it), m_implIterator(&m_impl.array) {}
63 
65  SetConstIterator(const RedBlackTreeIterator& it) : m_impl(it), m_implIterator(&m_impl.redBlackTree) {}
66 
68  SetConstIterator(const SetConstIterator& it) : m_impl(), m_implIterator() {
69  const auto implKind = it.getImplIterator().implKind();
70  switch (implKind) {
71  case ImplKind::ARRAY:
72  this->m_implIterator = new (&this->m_impl.array) ArrayIterator(it.m_impl.array);
73  break;
74  case ImplKind::RED_BLACK_TREE:
75  this->m_implIterator = new (&this->m_impl.redBlackTree) RedBlackTreeIterator(it.m_impl.redBlackTree);
76  break;
77  default:
78  FW_ASSERT(0, static_cast<FwAssertArgType>(implKind));
79  break;
80  }
81  }
82 
85 
86  public:
87  // ----------------------------------------------------------------------
88  // Public member functions
89  // ----------------------------------------------------------------------
90 
92  SetConstIterator& operator=(const SetConstIterator&) = default;
93 
95  bool operator==(const SetConstIterator& it) {
96  bool result = false;
97  const auto implKind1 = this->getImplIterator().implKind();
98  const auto implKind2 = it.getImplIterator().implKind();
99  if (implKind1 == implKind2) {
100  switch (implKind1) {
101  case ImplKind::ARRAY:
102  result = this->m_impl.array.compareEqual(it.m_impl.array);
103  break;
104  case ImplKind::RED_BLACK_TREE:
105  result = this->m_impl.redBlackTree.compareEqual(it.m_impl.redBlackTree);
106  break;
107  default:
108  FW_ASSERT(0, static_cast<FwAssertArgType>(implKind1));
109  break;
110  }
111  }
112  return result;
113  }
114 
116  bool operator!=(const SetConstIterator& it) { return !(*this == it); };
117 
120  this->getImplIterator().increment();
121  return *this;
122  }
123 
126  SetConstIterator tmp = *this;
127  ++(*this);
128  return tmp;
129  }
130 
132  bool isInRange() const { return this->getImplIterator().isInRange(); }
133 
135  const T& operator*() const { return this->getImplIterator().getEntry().getKeyOrElement(); }
136 
138  const T* operator->() const { return &this->getImplIterator().getEntry().getKeyOrElement(); }
139 
140  private:
141  // ----------------------------------------------------------------------
142  // Private helper functions
143  // ----------------------------------------------------------------------
144 
146  SetOrMapImplConstIterator<T, Nil>& getImplIterator() {
147  FW_ASSERT(this->m_implIterator != nullptr);
148  return *this->m_implIterator;
149  }
150 
152  const SetOrMapImplConstIterator<T, Nil>& getImplIterator() const {
153  FW_ASSERT(this->m_implIterator != nullptr);
154  return *this->m_implIterator;
155  }
156 
157  private:
158  // ----------------------------------------------------------------------
159  // Private member variables
160  // ----------------------------------------------------------------------
161 
163  Impl m_impl;
164 
166  SetOrMapImplConstIterator<T, Nil>* m_implIterator = nullptr;
167 };
168 
169 } // namespace Fw
170 
171 #endif
bool operator!=(const SetConstIterator &it)
Inequality comparison operator.
typename RedBlackTreeSetOrMapImpl< T, Nil >::ConstIterator RedBlackTreeIterator
The type of a red-black tree iterator.
SetConstIterator(const ArrayIterator &it)
Constructor providing an array implementation.
typename ArraySetOrMapImpl< T, Nil >::ConstIterator ArrayIterator
The type of an array iterator.
SetConstIterator(const SetConstIterator &it)
Copy constructor.
bool isInRange() const
Check whether the iterator is in range.
SetConstIterator(const RedBlackTreeIterator &it)
Constructor providing a red-black tree implementation.
~SetConstIterator()
Destructor.
SetConstIterator & operator++()
Prefix increment.
bool operator==(const SetConstIterator &it)
Equality comparison operator.
SetConstIterator operator++(int)
Postfix increment.
ImplKind
The kind of a const iterator implementation.
SetConstIterator & operator=(const SetConstIterator &)=default
Copy assignment operator.
const T & operator*() const
Dereference.
const T * operator->() const
Pointer.
#define FW_ASSERT(...)
Definition: Assert.hpp:14