GCC Code Coverage Report


Directory: src/gate/
File: src/gate/enumerators.hpp
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 26 29 89.7%
Functions: 113 122 92.6%
Branches: 4 10 40.0%

Line Branch Exec Source
1 /* GATE PROJECT LICENSE:
2 +----------------------------------------------------------------------------+
3 | Copyright (c) 2018-2026, 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>
46 class GATE_CORE_CPP_TEMPLATE_API Enumerator : public SafeBoolBase< Enumerator<T> >
47 {
48 private:
49 gate_enumerator_t impl;
50 public:
51 typedef T type_t;
52 typedef Enumerator<T> self_t;
53
54 /// @brief
55 /// @param enumerator
56 77 Enumerator(gate_enumerator_t const& enumerator) noexcept
57 77 : impl(enumerator)
58 {
59 77 }
60
61 /// @brief
62 /// @param src
63 Enumerator(self_t const& src) noexcept
64 : impl(src.impl)
65 {
66 }
67
68 /// @brief
69 /// @param src
70 /// @return
71 self_t& operator=(self_t const& src) noexcept
72 {
73 if (this != &src)
74 {
75 this->impl = src.impl;
76 }
77 return *this;
78 }
79
80 /// @brief
81 /// @return
82 gate_enumerator_t const& c_impl() const noexcept
83 {
84 return this->impl;
85 }
86
87 /// @brief
88 /// @return
89 1 gate_enumerator_t& c_impl() noexcept
90 {
91 1 return this->impl;
92 }
93
94 /// @brief
95 /// @return
96 4484 bool_t valid() const noexcept
97 {
98 4484 return gate_enumerator_valid(&this->impl);
99 }
100
101 /// @brief
102 /// @return
103 4401 bool_t next() noexcept
104 {
105 4401 return gate_enumerator_next(&this->impl);
106 }
107
108 /// @brief
109 /// @return
110 4774 type_t const* get() const noexcept
111 {
112 4774 void const* ptr = gate_enumerator_get(&this->impl);
113 4774 return static_cast<type_t const*>(ptr);
114 }
115
116 /// @brief
117 /// @return
118 4272 type_t const& operator*() const
119 {
120 4272 type_t const* ptr = this->get();
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2086 times.
4272 if (!ptr)
122 {
123 ✗ GATEXX_RAISE_ERROR(results::NullPointer);
124 }
125 4272 return *ptr;
126 }
127
128 /// @brief
129 /// @return
130 256 type_t const* operator->() const
131 {
132 256 type_t const* ptr = this->get();
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 238 times.
256 if (!ptr)
134 {
135 ✗ GATEXX_RAISE_ERROR(results::NullPointer);
136 }
137 256 return ptr;
138 }
139
140 /// @brief
141 /// @return
142 4 bool operator!() const noexcept
143 {
144 4 return !this->valid();
145 }
146
147 /// @brief
148 /// @return
149 bool_t operator++() const noexcept
150 {
151 return this->next();
152 }
153 };
154
155
156 /// @brief Create an enumerator from a c-style array
157 /// @tparam T item type
158 /// @tparam N item count
159 /// @param arr array to be accessed
160 /// @return initialized Enumerator pointing to first element in array
161 template<class T, unsigned N>
162 1 Enumerator<T const> enumerate(T (&arr)[N])
163 {
164 1 gate_enumerator_t en = GATE_INIT_EMPTY;
165
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))
166 {
167 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
168 }
169 2 return Enumerator<T const>(en);
170 }
171
172 } // end of namespace gate
173
174 #define GATEXX_ENUMERATE(item_type, item_var, generator) \
175 for(gate::Enumerator< item_type > item_var = generator; item_var.valid(); item_var.next())
176
177 #endif
178