GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_utilities.cpp
Date: 2026-12-03 19:36:00
Exec Total Coverage
Lines: 150 161 93.2%
Functions: 24 24 100.0%
Branches: 108 211 51.2%

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 #include "gate/utilities.hpp"
30 #include "gate/utilities.h"
31
32 #if !defined(GATEXX_NO_EXCEPTIONS)
33 # include <exception>
34 #endif
35
36 namespace gate
37 {
38
39 template class GATE_CORE_CPP_API Array<String>;
40 template class GATE_CORE_CPP_API ArrayList<String>;
41 template class GATE_CORE_CPP_API Set<String>;
42 template class GATE_CORE_CPP_API Map<String, String>;
43
44
45 namespace util
46 {
47 8 String printByteSize(uint64_t bytes)
48 {
49 char buffer[256];
50
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 size_t used = gate_util_print_byte_size(bytes, buffer, sizeof(buffer) - 16);
51
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
16 return String(buffer, used);
52 }
53 2 char const* printResult(result_t resultCode)
54 {
55 2 return gate_result_text(resultCode);
56 }
57
58
59 2 String printException(Throwable const& xcpt, bool_t addResultCode, bool_t addErrorCode)
60 {
61
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 StringBuilder builder;
62 2 char const* msg = xcpt.getMessage();
63
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 size_t msg_len = gate_str_length(msg);
64
65
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (msg_len > 0)
66 {
67
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 builder.append(msg);
68 }
69 else
70 {
71 ✗ addResultCode = true;
72 }
73
74
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (addResultCode)
75 {
76 2 result_t resultCode = xcpt.getResult();
77
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (GATE_FAILED(resultCode))
78 {
79
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (builder.length() != 0)
80 {
81
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 builder.appendNewLine();
82 }
83
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 builder.append("Result code #");
84
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 builder.appendHex(gate_uint16_t(resultCode));
85
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 builder.append(" ", 1);
86
3/6
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
2 builder.append(printResult(resultCode));
87 }
88 }
89
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (addErrorCode)
90 {
91 2 uint32_t errorCode = static_cast<uint32_t>(xcpt.getErrorCode());
92
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (errorCode != 0)
93 {
94
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 if (builder.length() != 0)
95 {
96
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 builder.appendNewLine();
97 }
98
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 builder.append("Error code 0x");
99
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 builder.appendHex(errorCode);
100 }
101 }
102
103
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 return builder.toString();
104 }
105
106 4 String printCurrentException(bool_t addResultCode, bool_t addErrorCode)
107 {
108 #if !defined(GATEXX_NO_EXCEPTIONS)
109
3/4
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
4 static StaticString const msgUnknownException = "Unknown Exception";
110
111 try
112 {
113 #if __cplusplus >= 201103L
114 8 std::exception_ptr xcpt_ptr = std::current_exception();
115
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 if (xcpt_ptr)
116 8 std::rethrow_exception(xcpt_ptr);
117 #else
118 // pre C++11 cannot check if exceptions are raised,
119 // std::terminate() is called, if this function if called from outside a catch-block
120 throw;
121 #endif
122 }
123 2 catch (Throwable const& throwable)
124 {
125
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return printException(throwable, addResultCode, addErrorCode);
126 }
127 2 catch (std::exception const& xcpt)
128 {
129
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 return String(xcpt.what());
130 }
131 2 catch (int num)
132 {
133
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 StringBuilder builder;
134
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 builder << "Numeric exception code #" << static_cast<int32_t>(num);
135
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return builder.toString();
136 }
137 2 catch (...)
138 {
139 1 return msgUnknownException;
140 }
141 #endif
142 ✗ return String(); // no exception was thrown
143 }
144
145
146 4 String printDuration(uint64_t seconds, bool_t shortFormat)
147 {
148 char buffer[1024];
149
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
4 size_t buffer_used = gate_util_print_duration(seconds, buffer, sizeof(buffer), shortFormat);
150
1/2
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
8 return String(buffer, buffer_used);
151 }
152
153 10 ArrayList<String> convertStringArray(gate_array_t const& arr)
154 {
155 10 size_t len = gate_array_length(&arr);
156 10 ArrayList<String> ret(len);
157
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 10 times.
36 for (size_t ndx = 0; ndx != len; ++ndx)
158 {
159
1/2
✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
26 gate_string_t const* ptr = (gate_string_t const*)gate_array_get(&arr, ndx);
160
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (ptr)
161 {
162
1/2
✓ Branch 2 taken 26 times.
✗ Branch 3 not taken.
26 ret.add(String::duplicate(*ptr));
163 }
164 }
165 10 return ret;
166 }
167
168 21 ArrayList<String> convertStringArray(gate_arraylist_t const& arr)
169 {
170 21 size_t len = gate_arraylist_length(arr);
171 21 ArrayList<String> ret(len);
172
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 21 times.
41 for (size_t ndx = 0; ndx != len; ++ndx)
173 {
174
1/2
✓ Branch 1 taken 20 times.
✗ Branch 2 not taken.
20 gate_string_t const* ptr = (gate_string_t const*)gate_arraylist_get(arr, ndx);
175
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 if (ptr)
176 {
177
1/2
✓ Branch 2 taken 20 times.
✗ Branch 3 not taken.
20 ret.add(String::duplicate(*ptr));
178 }
179 }
180 21 return ret;
181 }
182
183 7 ArrayList<String> createStringArray(gate_array_t& arr)
184 {
185 14 GenericArray genarr;
186 7 gate::swapRefsNoExcept(*genarr.c_impl(), arr);
187
1/2
✓ Branch 2 taken 7 times.
✗ Branch 3 not taken.
7 ArrayList<String> ret = convertStringArray(*genarr.c_impl());
188 14 return ret;
189 }
190
191 2 ArrayList<String> createStringArray(gate_arraylist_t& arr)
192 {
193
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
4 GenericArrayList genarr = GenericArrayList::createFrom(arr);
194
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 ArrayList<String> ret = convertStringArray(genarr.c_impl());
195 4 return ret;
196 }
197
198 1 ArrayList<String> splitString(String const& text, String const& separator, size_t maxCount)
199 {
200
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 gate_arraylist_t arr = gate_util_string_split(text.c_impl(), separator.c_impl(), maxCount);
201
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
1 if (gate_arraylist_is_null(arr))
202 {
203 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
204 }
205
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return createStringArray(arr);
206 }
207
208
209
210 3 gate_arraylist_t convertStringArray(Array<String> const& arr)
211 {
212
1/2
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
3 gate_arraylist_t lst = gate_util_stringarray_create();
213
214
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✗ Branch 4 not taken.
3 if (!gate_arraylist_is_null(lst))
215 {
216
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 3 times.
5 for (Array<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
217 {
218 2 gate_string_t const* ptr = it->c_impl();
219
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
2 if (NULL == gate_arraylist_add(lst, ptr))
220 {
221 ✗ gate_arraylist_release(&lst);
222 ✗ break;
223 }
224 }
225 }
226 3 return lst;
227 }
228
229 2 gate_arraylist_t convertStringArray(ArrayList<String> const& arr)
230 {
231
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 gate_arraylist_t lst = gate_util_stringarray_create();
232
233
2/4
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
2 if (!gate_arraylist_is_null(lst))
234 {
235
2/2
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
5 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
236 {
237 3 gate_string_t const* ptr = it->c_impl();
238
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (NULL == gate_arraylist_add(lst, ptr))
239 {
240 ✗ gate_arraylist_release(&lst);
241 ✗ break;
242 }
243 }
244 }
245 2 return lst;
246 }
247
248 15 size_t convertStringArray(Array<String> const& arr, gate_string_t* strarr, gate_size_t strarr_max)
249 {
250 15 size_t count = 0;
251
3/4
✓ Branch 2 taken 47 times.
✓ Branch 3 taken 15 times.
✓ Branch 4 taken 47 times.
✗ Branch 5 not taken.
62 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); (it != itend) && (strarr_max != 0); ++it)
252 {
253 47 gate_string_t const* ptr = it->c_impl();
254 47 gate_string_duplicate(strarr, ptr);
255 47 ++count;
256 47 ++strarr;
257 47 --strarr_max;
258 }
259 15 return count;
260 }
261
262 12 void releaseStringArray(gate_string_t* strarr, gate_size_t strarrlength)
263 {
264
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 while (strarrlength-- > 0)
265 {
266 4 gate_string_release(strarr);
267 4 ++strarr;
268 }
269 8 }
270
271 15 VoidResult dumpType(type_id_t type, void const* data, Stream& stream)
272 {
273
1/2
✓ Branch 2 taken 15 times.
✗ Branch 3 not taken.
15 result_t const result = gate_util_dump_type(stream.c_impl(), type, data);
274
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 return makeResult(result);
275 }
276
277 1 VoidResult dumpStruct(gate_struct_t const* ptrStruct, Stream& stream)
278 {
279
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t const result = gate_util_dump_struct(stream.c_impl(), ptrStruct);
280
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return makeResult(result);
281 }
282
283 1 VoidResult setState(AtomicInt& state, int32_t value)
284 {
285
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return makeResult(gate_util_state_set(state.c_impl(), value));
286 }
287
288 1 VoidResult updateState(AtomicInt& state, int32_t from_value, int32_t to_value)
289 {
290
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return makeResult(gate_util_state_update(state.c_impl(), from_value, to_value));
291 }
292
293 4 Result<bool_t> awaitState(AtomicInt& state, int32_t await_value, uint32_t timeout_ms)
294 {
295
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 result_t result = gate_util_state_await(state.c_impl(), await_value, timeout_ms);
296
2/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
4 switch(result)
297 {
298
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 case results::Ok: return makeOk(true);
299
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 case results::Timeout: return makeOk(false);
300 ✗ default: return makeErr(result);
301 }
302 }
303
304 5 Map<String, Value> convertStringValueMap(gate_map_t const& map)
305 {
306 5 Map<String, Value> ret;
307
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 for (gate_map_iterator_t iter = gate_map_first(&map);
308
3/4
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 5 times.
6 gate_map_iterator_valid(iter);
309 1 iter = gate_map_iterator_next(iter))
310 {
311
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_string_t const* const strKey = (gate_string_t const*)gate_map_iterator_key(iter);
312
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 gate_value_t const* const valValue = (gate_value_t const*)gate_map_iterator_value(iter);
313
4/8
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 12 taken 1 times.
✗ Branch 13 not taken.
1 ret.add(String(*strKey), Value(*valValue));
314 }
315 5 return ret;
316 }
317
318 14 gate_map_t convertStringValueMap(Map<String, Value> const& map)
319 {
320 14 gate_map_t ret = GATE_INIT_EMPTY;
321
2/4
✓ Branch 1 taken 14 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 14 times.
14 if (NULL == gate_util_stringvaluemap_create(&ret))
322 {
323 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
324 }
325 28 Map<String, Value>::const_iterator iter = map.begin();
326 28 Map<String, Value>::const_iterator iterEnd = map.end();
327
2/2
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 14 times.
17 for (; iter != iterEnd; ++iter)
328 {
329 3 Value const& val = iter.value();
330
1/2
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 gate_map_iterator_t newIter = gate_util_stringvaluemap_add_string(&ret,
331 3 iter.key().c_impl(),
332 3 val.c_impl()->value_type, &val.c_impl()->content);
333
2/4
✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
3 if (!gate_map_iterator_valid(newIter))
334 {
335 ✗ gate_map_destroy(&ret);
336 ✗ GATEXX_RAISE_ERROR(results::OutOfMemory);
337 }
338 }
339 28 return ret;
340 }
341
342
343
344 5 ScopeExit::ScopeExit(gate_mem_dtor_t func, gate_dataptr_t param) noexcept
345 : onExitFunc(func),
346 5 onExitParam(param)
347 {
348 5 }
349
350 10 ScopeExit::~ScopeExit() noexcept
351 {
352
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 if (onExitFunc != NULL)
353 {
354 4 onExitFunc(onExitParam);
355 }
356 5 }
357
358 1 void ScopeExit::release()
359 {
360 1 onExitFunc = NULL;
361 1 onExitParam = NULL;
362 1 }
363
364
365 } // end of namespace util;
366
367 } // end of namespace gate
368