GCC Code Coverage Report


Directory: src/gate/
File: src/gate/cxx_utilities.cpp
Date: 2026-03-20 22:56:14
Exec Total Coverage
Lines: 119 129 92.2%
Functions: 19 19 100.0%
Branches: 82 155 52.9%

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 4 ArrayList<String> convertStringArray(gate_array_t const& arr)
154 {
155 4 size_t len = gate_array_length(&arr);
156 4 ArrayList<String> ret(len);
157
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 4 times.
14 for (size_t ndx = 0; ndx != len; ++ndx)
158 {
159
1/2
✓ Branch 1 taken 10 times.
✗ Branch 2 not taken.
10 gate_string_t const* ptr = (gate_string_t const*)gate_array_get(&arr, ndx);
160
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 if (ptr)
161 {
162
1/2
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
10 ret.add(String::duplicate(*ptr));
163 }
164 }
165 4 return ret;
166 }
167
168 6 ArrayList<String> convertStringArray(gate_arraylist_t const& arr)
169 {
170 6 size_t len = gate_arraylist_length(arr);
171 6 ArrayList<String> ret(len);
172
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 6 times.
17 for (size_t ndx = 0; ndx != len; ++ndx)
173 {
174
1/2
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
11 gate_string_t const* ptr = (gate_string_t const*)gate_arraylist_get(arr, ndx);
175
1/2
✓ Branch 0 taken 11 times.
✗ Branch 1 not taken.
11 if (ptr)
176 {
177
1/2
✓ Branch 2 taken 11 times.
✗ Branch 3 not taken.
11 ret.add(String::duplicate(*ptr));
178 }
179 }
180 6 return ret;
181 }
182
183 4 ArrayList<String> createStringArray(gate_array_t& arr)
184 {
185 8 GenericArray genarr;
186 4 gate::swapRefsNoExcept(*genarr.c_impl(), arr);
187
1/2
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
4 ArrayList<String> ret = convertStringArray(*genarr.c_impl());
188 8 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(arr);
194 2 arr = NULL;
195
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());
196 4 return ret;
197 }
198
199 1 ArrayList<String> splitString(String const& text, String const& separator, size_t maxCount)
200 {
201
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);
202
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!arr)
203 {
204 GATEXX_RAISE_ERROR(results::OutOfMemory);
205 }
206
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return createStringArray(arr);
207 }
208
209
210
211 1 gate_arraylist_t convertStringArray(Array<String> const& arr)
212 {
213 1 gate_arraylist_t lst = gate_util_stringarray_create();
214
215
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (lst != NULL)
216 {
217
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 for (Array<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
218 {
219 2 gate_string_t const* ptr = it->c_impl();
220
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_arraylist_add(lst, ptr))
221 {
222 gate_arraylist_release(lst);
223 lst = NULL;
224 break;
225 }
226 }
227 }
228 1 return lst;
229 }
230
231 1 gate_arraylist_t convertStringArray(ArrayList<String> const& arr)
232 {
233 1 gate_arraylist_t lst = gate_util_stringarray_create();
234
235
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (lst != NULL)
236 {
237
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 1 times.
3 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); it != itend; ++it)
238 {
239 2 gate_string_t const* ptr = it->c_impl();
240
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (NULL == gate_arraylist_add(lst, ptr))
241 {
242 gate_arraylist_release(lst);
243 lst = NULL;
244 break;
245 }
246 }
247 }
248 1 return lst;
249 }
250
251 9 size_t convertStringArray(Array<String> const& arr, gate_string_t* strarr, gate_size_t strarr_max)
252 {
253 9 size_t count = 0;
254
3/4
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 9 times.
✓ Branch 4 taken 23 times.
✗ Branch 5 not taken.
32 for (ArrayList<String>::const_iterator it(arr.begin()), itend(arr.end()); (it != itend) && (strarr_max != 0); ++it)
255 {
256 23 gate_string_t const* ptr = it->c_impl();
257 23 gate_string_duplicate(strarr, ptr);
258 23 ++count;
259 23 ++strarr;
260 23 --strarr_max;
261 }
262 9 return count;
263 }
264
265 8 void releaseStringArray(gate_string_t* strarr, gate_size_t strarrlength)
266 {
267
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 while (strarrlength-- > 0)
268 {
269 2 gate_string_release(strarr);
270 2 ++strarr;
271 }
272 6 }
273
274 15 VoidResult dumpType(type_id_t type, void const* data, Stream& stream)
275 {
276
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);
277
1/2
✓ Branch 1 taken 15 times.
✗ Branch 2 not taken.
30 return makeResult(result);
278 }
279
280 1 VoidResult dumpStruct(gate_struct_t const* ptrStruct, Stream& stream)
281 {
282
1/2
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
1 result_t const result = gate_util_dump_struct(stream.c_impl(), ptrStruct);
283
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 return makeResult(result);
284 }
285
286 1 VoidResult setState(AtomicInt& state, int32_t value)
287 {
288
1/2
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
1 return makeResult(gate_util_state_set(state.c_impl(), value));
289 }
290
291 1 VoidResult updateState(AtomicInt& state, int32_t from_value, int32_t to_value)
292 {
293
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));
294 }
295
296 4 Result<bool_t> awaitState(AtomicInt& state, int32_t await_value, uint32_t timeout_ms)
297 {
298
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);
299
2/3
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
4 switch(result)
300 {
301
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);
302
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);
303 default: return makeErr(result);
304 }
305 }
306
307 } // end of namespace util;
308
309 } // end of namespace gate
310