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 | 17 | Enumerator(gate_enumerator_t const& enumerator) noexcept | |
54 | 17 | : impl(enumerator) | |
55 | { | ||
56 | |||
57 | 17 | } | |
58 | Enumerator(self_t const& src) noexcept | ||
59 | : impl(src.impl) | ||
60 | { | ||
61 | |||
62 | } | ||
63 | self_t& operator=(self_t const& src) noexcept | ||
64 | { | ||
65 | if (this != &src) | ||
66 | { | ||
67 | this->impl = src.impl; | ||
68 | } | ||
69 | return *this; | ||
70 | } | ||
71 | |||
72 | gate_enumerator_t const& c_impl() const noexcept | ||
73 | { | ||
74 | return this->impl; | ||
75 | } | ||
76 | |||
77 | 1 | gate_enumerator_t& c_impl() noexcept | |
78 | { | ||
79 | 1 | return this->impl; | |
80 | } | ||
81 | |||
82 | 4167 | bool_t valid() const noexcept | |
83 | { | ||
84 | 4167 | return gate_enumerator_valid(&this->impl); | |
85 | } | ||
86 | 4144 | bool_t next() noexcept | |
87 | { | ||
88 | 4144 | return gate_enumerator_next(&this->impl); | |
89 | } | ||
90 | 4246 | type_t const* get() const noexcept | |
91 | { | ||
92 | 4246 | void const* ptr = gate_enumerator_get(&this->impl); | |
93 | 4246 | return static_cast<type_t const*>(ptr); | |
94 | } | ||
95 | |||
96 | 4050 | type_t const& operator*() const | |
97 | { | ||
98 | 4050 | type_t const* ptr = this->get(); | |
99 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2035 times.
|
4050 | if (!ptr) |
100 | { | ||
101 | ✗ | GATEXX_RAISE_ERROR(results::NullPointer); | |
102 | } | ||
103 | 4050 | return *ptr; | |
104 | } | ||
105 | 184 | type_t const* operator->() const | |
106 | { | ||
107 | 184 | type_t const* ptr = this->get(); | |
108 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 184 times.
|
184 | if (!ptr) |
109 | { | ||
110 | ✗ | GATEXX_RAISE_ERROR(results::NullPointer); | |
111 | } | ||
112 | 184 | return ptr; | |
113 | } | ||
114 | 4 | bool operator!() const noexcept | |
115 | { | ||
116 | 4 | return !this->valid(); | |
117 | } | ||
118 | bool_t operator++() const noexcept | ||
119 | { | ||
120 | return this->next(); | ||
121 | } | ||
122 | }; | ||
123 | |||
124 | |||
125 | /// @brief | ||
126 | /// @tparam T | ||
127 | /// @tparam N | ||
128 | /// @param arr | ||
129 | /// @return | ||
130 | template<class T, unsigned N> | ||
131 | Enumerator<T const> enumerate(T arr[N]) | ||
132 | { | ||
133 | gate_enumerator_t en = GATE_INIT_EMPTY; | ||
134 | if (NULL == gate_enumerator_for_array(&en, &arr[0], sizeof(T), N)) | ||
135 | { | ||
136 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
137 | } | ||
138 | return Enumerator<T const>(en); | ||
139 | } | ||
140 | |||
141 | } // end of namespace gate | ||
142 | |||
143 | #endif | ||
144 |