| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // ====================================================================== | ||
| 2 | // \title MapBase | ||
| 3 | // \author bocchino | ||
| 4 | // \brief An abstract base class template for a map | ||
| 5 | // ====================================================================== | ||
| 6 | |||
| 7 | #ifndef Fw_MapBase_HPP | ||
| 8 | #define Fw_MapBase_HPP | ||
| 9 | |||
| 10 | #include "Fw/DataStructures/MapConstIterator.hpp" | ||
| 11 | #include "Fw/DataStructures/SizedContainer.hpp" | ||
| 12 | #include "Fw/Types/Assert.hpp" | ||
| 13 | #include "Fw/Types/SuccessEnumAc.hpp" | ||
| 14 | |||
| 15 | namespace Fw { | ||
| 16 | |||
| 17 | template <typename K, typename V> | ||
| 18 | class MapBase : public SizedContainer { | ||
| 19 | private: | ||
| 20 | // ---------------------------------------------------------------------- | ||
| 21 | // Deleted elements | ||
| 22 | // ---------------------------------------------------------------------- | ||
| 23 | |||
| 24 | //! Copy constructor deleted in the base class | ||
| 25 | //! Behavior depends on the implementation | ||
| 26 | MapBase(const MapBase<K, V>&) = delete; | ||
| 27 | |||
| 28 | //! operator= deleted in the base class | ||
| 29 | //! Behavior depends on the implementation | ||
| 30 | //! We avoid virtual user-defined operators | ||
| 31 | MapBase<K, V>& operator=(const MapBase<K, V>&) = delete; | ||
| 32 | |||
| 33 | public: | ||
| 34 | // ---------------------------------------------------------------------- | ||
| 35 | // Public types | ||
| 36 | // ---------------------------------------------------------------------- | ||
| 37 | |||
| 38 | //! The type of a map const iterator | ||
| 39 | using ConstIterator = MapConstIterator<K, V>; | ||
| 40 | |||
| 41 | protected: | ||
| 42 | // ---------------------------------------------------------------------- | ||
| 43 | // Protected constructors and destructors | ||
| 44 | // ---------------------------------------------------------------------- | ||
| 45 | |||
| 46 | //! Zero-argument constructor | ||
| 47 | 112 | MapBase() : SizedContainer() {} | |
| 48 | |||
| 49 | //! Destructor | ||
| 50 | 224 | virtual ~MapBase() = default; | |
| 51 | |||
| 52 | public: | ||
| 53 | // ---------------------------------------------------------------------- | ||
| 54 | // Public member functions | ||
| 55 | // ---------------------------------------------------------------------- | ||
| 56 | |||
| 57 | //! Get the begin value of the iterator | ||
| 58 | //! \return The iterator | ||
| 59 | virtual ConstIterator begin() const = 0; | ||
| 60 | |||
| 61 | //! Copy data from another map | ||
| 62 | 16 | void copyDataFrom(const MapBase<K, V>& map) { | |
| 63 |
1/2✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
|
16 | if (&map != this) { |
| 64 |
1/1✓ Branch 10 taken 16 times.
|
16 | this->clear(); |
| 65 |
6/6✓ Branch 11 taken 16 times.
✓ Branch 23 taken 16 times.
✓ Branch 25 taken 8 times.
✓ Branch 26 taken 8 times.
✓ Branch 38 taken 8 times.
✓ Branch 50 taken 8 times.
|
16 | const FwSizeType size = FW_MIN(map.getSize(), this->getCapacity()); |
| 66 |
1/1✓ Branch 5 taken 16 times.
|
16 | auto it = map.begin(); |
| 67 |
2/2✓ Branch 3 taken 4140 times.
✓ Branch 4 taken 16 times.
|
8296 | for (FwSizeType i = 0; i < size; i++) { |
| 68 |
5/5✓ Branch 5 taken 4140 times.
✓ Branch 14 taken 4140 times.
✓ Branch 17 taken 4140 times.
✓ Branch 26 taken 4140 times.
✓ Branch 29 taken 4140 times.
|
4140 | const auto status = this->insert(it->getKey(), it->getValue()); |
| 69 | 4140 | FW_ASSERT(status == Success::SUCCESS, static_cast<FwAssertArgType>(status)); | |
| 70 |
1/1✓ Branch 2 taken 4140 times.
|
4140 | it++; |
| 71 | } | ||
| 72 | 16 | } | |
| 73 | 16 | } | |
| 74 | |||
| 75 | //! Get the end value of the iterator | ||
| 76 | //! \return The iterator | ||
| 77 | virtual ConstIterator end() const = 0; | ||
| 78 | |||
| 79 | //! Find the value associated with a key in the map | ||
| 80 | //! SUCCESS if the item was found | ||
| 81 | virtual Success find(const K& key, //!< The key (input) | ||
| 82 | V& value //!< The value (output) | ||
| 83 | ) const = 0; | ||
| 84 | |||
| 85 | //! Insert a (key, value) pair in the map | ||
| 86 | //! \return SUCCESS if there is room in the map | ||
| 87 | virtual Success insert(const K& key, //!< The key | ||
| 88 | const V& value //!< The value | ||
| 89 | ) = 0; | ||
| 90 | //! Remove a (key, value) pair from the map | ||
| 91 | //! Store the value into the value parameter if the key was there | ||
| 92 | //! \return SUCCESS if the key was there | ||
| 93 | virtual Success remove(const K& key, //!< The key (input) | ||
| 94 | V& value //!< The value (output) | ||
| 95 | ) = 0; | ||
| 96 | }; | ||
| 97 | |||
| 98 | } // namespace Fw | ||
| 99 | |||
| 100 | #endif | ||
| 101 |