| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2025, Stefan Meislinger <sm@opengate.at> | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | /** @file | ||
| 30 | * @brief Enumerator objects allow to walk through a set of elements | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_ENUMERATORS_HPP_INCLUDED | ||
| 35 | #define GATE_ENUMERATORS_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/enumerators.h" | ||
| 38 | #include "gate/gatetypes.hpp" | ||
| 39 | |||
| 40 | namespace gate | ||
| 41 | { | ||
| 42 | |||
| 43 | /// @brief | ||
| 44 | /// @tparam T | ||
| 45 | template<class T> class Enumerator : public SafeBoolBase< Enumerator<T> > | ||
| 46 | { | ||
| 47 | private: | ||
| 48 | gate_enumerator_t impl; | ||
| 49 | public: | ||
| 50 | typedef T type_t; | ||
| 51 | typedef Enumerator<T> self_t; | ||
| 52 | |||
| 53 | /// @brief | ||
| 54 | /// @param enumerator | ||
| 55 | 33 | Enumerator(gate_enumerator_t const& enumerator) noexcept | |
| 56 | 33 | : impl(enumerator) | |
| 57 | { | ||
| 58 | 33 | } | |
| 59 | |||
| 60 | /// @brief | ||
| 61 | /// @param src | ||
| 62 | Enumerator(self_t const& src) noexcept | ||
| 63 | : impl(src.impl) | ||
| 64 | { | ||
| 65 | } | ||
| 66 | |||
| 67 | /// @brief | ||
| 68 | /// @param src | ||
| 69 | /// @return | ||
| 70 | self_t& operator=(self_t const& src) noexcept | ||
| 71 | { | ||
| 72 | if (this != &src) | ||
| 73 | { | ||
| 74 | this->impl = src.impl; | ||
| 75 | } | ||
| 76 | return *this; | ||
| 77 | } | ||
| 78 | |||
| 79 | /// @brief | ||
| 80 | /// @return | ||
| 81 | gate_enumerator_t const& c_impl() const noexcept | ||
| 82 | { | ||
| 83 | return this->impl; | ||
| 84 | } | ||
| 85 | |||
| 86 | /// @brief | ||
| 87 | /// @return | ||
| 88 | 1 | gate_enumerator_t& c_impl() noexcept | |
| 89 | { | ||
| 90 | 1 | return this->impl; | |
| 91 | } | ||
| 92 | |||
| 93 | /// @brief | ||
| 94 | /// @return | ||
| 95 | 4351 | bool_t valid() const noexcept | |
| 96 | { | ||
| 97 | 4351 | return gate_enumerator_valid(&this->impl); | |
| 98 | } | ||
| 99 | |||
| 100 | /// @brief | ||
| 101 | /// @return | ||
| 102 | 4312 | bool_t next() noexcept | |
| 103 | { | ||
| 104 | 4312 | return gate_enumerator_next(&this->impl); | |
| 105 | } | ||
| 106 | |||
| 107 | /// @brief | ||
| 108 | /// @return | ||
| 109 | 4650 | type_t const* get() const noexcept | |
| 110 | { | ||
| 111 | 4650 | void const* ptr = gate_enumerator_get(&this->impl); | |
| 112 | 4650 | return static_cast<type_t const*>(ptr); | |
| 113 | } | ||
| 114 | |||
| 115 | /// @brief | ||
| 116 | /// @return | ||
| 117 | 4229 | type_t const& operator*() const | |
| 118 | { | ||
| 119 | 4229 | type_t const* ptr = this->get(); | |
| 120 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2053 times.
|
4229 | if (!ptr) |
| 121 | { | ||
| 122 | ✗ | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 123 | } | ||
| 124 | 4229 | return *ptr; | |
| 125 | } | ||
| 126 | |||
| 127 | /// @brief | ||
| 128 | /// @return | ||
| 129 | 225 | type_t const* operator->() const | |
| 130 | { | ||
| 131 | 225 | type_t const* ptr = this->get(); | |
| 132 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 203 times.
|
225 | if (!ptr) |
| 133 | { | ||
| 134 | ✗ | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 135 | } | ||
| 136 | 225 | return ptr; | |
| 137 | } | ||
| 138 | |||
| 139 | /// @brief | ||
| 140 | /// @return | ||
| 141 | 4 | bool operator!() const noexcept | |
| 142 | { | ||
| 143 | 4 | return !this->valid(); | |
| 144 | } | ||
| 145 | |||
| 146 | /// @brief | ||
| 147 | /// @return | ||
| 148 | bool_t operator++() const noexcept | ||
| 149 | { | ||
| 150 | return this->next(); | ||
| 151 | } | ||
| 152 | }; | ||
| 153 | |||
| 154 | |||
| 155 | /// @brief Create an enumerator from a c-style array | ||
| 156 | /// @tparam T item type | ||
| 157 | /// @tparam N item count | ||
| 158 | /// @param arr array to be accessed | ||
| 159 | /// @return initialized Enumerator pointing to first element in array | ||
| 160 | template<class T, unsigned N> | ||
| 161 | 1 | Enumerator<T const> enumerate(T (&arr)[N]) | |
| 162 | { | ||
| 163 | 1 | gate_enumerator_t en = GATE_INIT_EMPTY; | |
| 164 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
|
1 | if (NULL == gate_enumerator_for_array(&en, &arr[0], sizeof(T), N)) |
| 165 | { | ||
| 166 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 167 | } | ||
| 168 | 2 | return Enumerator<T const>(en); | |
| 169 | } | ||
| 170 | |||
| 171 | } // end of namespace gate | ||
| 172 | |||
| 173 | #define GATEXX_ENUMERATE(item_type, item_var, generator) \ | ||
| 174 | for(gate::Enumerator< item_type > item_var = generator; item_var.valid(); item_var.next()) | ||
| 175 | |||
| 176 | #endif | ||
| 177 |