| 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 Utility functions | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_UTILITIES_HPP_INCLUDED | ||
| 35 | #define GATE_UTILITIES_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/maps.hpp" | ||
| 39 | #include "gate/strings.hpp" | ||
| 40 | #include "gate/exceptions.hpp" | ||
| 41 | #include "gate/arrays.hpp" | ||
| 42 | #include "gate/values.hpp" | ||
| 43 | #include "gate/atomics.hpp" | ||
| 44 | #include "gate/structs.hpp" | ||
| 45 | #include "gate/streams.hpp" | ||
| 46 | |||
| 47 | namespace gate | ||
| 48 | { | ||
| 49 | typedef Array<String> StringArray; | ||
| 50 | typedef ArrayList<String> StringList; | ||
| 51 | typedef Set<String> StringSet; | ||
| 52 | typedef Map<String, String> StringMap; | ||
| 53 | |||
| 54 | namespace util | ||
| 55 | { | ||
| 56 | /// @brief | ||
| 57 | /// @param bytes | ||
| 58 | /// @return | ||
| 59 | GATE_CORE_CPP_API extern String printByteSize(uint64_t bytes); | ||
| 60 | |||
| 61 | |||
| 62 | /// @brief | ||
| 63 | /// @param resultCode | ||
| 64 | /// @return | ||
| 65 | GATE_CORE_CPP_API extern char const* printResult(result_t resultCode); | ||
| 66 | |||
| 67 | |||
| 68 | /// @brief | ||
| 69 | /// @param xcpt | ||
| 70 | /// @param addResultCode | ||
| 71 | /// @param addErrorCode | ||
| 72 | /// @return | ||
| 73 | GATE_CORE_CPP_API extern String printException(Throwable const& xcpt, bool_t addResultCode = false, bool_t addErrorCode = false); | ||
| 74 | |||
| 75 | |||
| 76 | /// @brief | ||
| 77 | /// @param addResultCode | ||
| 78 | /// @param addErrorCode | ||
| 79 | /// @return | ||
| 80 | GATE_CORE_CPP_API extern String printCurrentException(bool_t addResultCode = false, bool_t addErrorCode = false); | ||
| 81 | |||
| 82 | |||
| 83 | /// @brief | ||
| 84 | /// @param seconds | ||
| 85 | /// @param shortFormat | ||
| 86 | /// @return | ||
| 87 | GATE_CORE_CPP_API extern String printDuration(uint64_t seconds, bool_t shortFormat = false); | ||
| 88 | |||
| 89 | |||
| 90 | /// @brief | ||
| 91 | /// @param arr | ||
| 92 | /// @return | ||
| 93 | GATE_CORE_CPP_API extern ArrayList<String> convertStringArray(gate_array_t const& arr); | ||
| 94 | |||
| 95 | |||
| 96 | /// @brief | ||
| 97 | /// @param arr | ||
| 98 | /// @return | ||
| 99 | GATE_CORE_CPP_API extern ArrayList<String> convertStringArray(gate_arraylist_t const& arr); | ||
| 100 | |||
| 101 | |||
| 102 | /// @brief | ||
| 103 | /// @param arr | ||
| 104 | /// @return | ||
| 105 | GATE_CORE_CPP_API extern ArrayList<String> createStringArray(gate_array_t& arr); | ||
| 106 | |||
| 107 | |||
| 108 | /// @brief | ||
| 109 | /// @param arr | ||
| 110 | /// @return | ||
| 111 | GATE_CORE_CPP_API extern ArrayList<String> createStringArray(gate_arraylist_t& arr); | ||
| 112 | |||
| 113 | |||
| 114 | /// @brief | ||
| 115 | /// @param text | ||
| 116 | /// @param separator | ||
| 117 | /// @param maxCount | ||
| 118 | /// @return | ||
| 119 | GATE_CORE_CPP_API extern ArrayList<String> splitString(String const& text, String const& separator, size_t maxCount = String::npos); | ||
| 120 | |||
| 121 | |||
| 122 | /// @brief | ||
| 123 | /// @param arr | ||
| 124 | /// @return | ||
| 125 | GATE_CORE_CPP_API extern gate_arraylist_t convertStringArray(Array<String> const& arr); | ||
| 126 | |||
| 127 | |||
| 128 | /// @brief | ||
| 129 | /// @param arr | ||
| 130 | /// @return | ||
| 131 | GATE_CORE_CPP_API extern gate_arraylist_t convertStringArray(ArrayList<String> const& arr); | ||
| 132 | |||
| 133 | |||
| 134 | /// @brief | ||
| 135 | /// @param arr | ||
| 136 | /// @param strarr | ||
| 137 | /// @param strarr_max | ||
| 138 | /// @return | ||
| 139 | GATE_CORE_CPP_API extern size_t convertStringArray(Array<String> const& arr, gate_string_t* strarr, gate_size_t strarr_max); | ||
| 140 | |||
| 141 | |||
| 142 | /// @brief | ||
| 143 | /// @param strarr | ||
| 144 | /// @param strarr_length | ||
| 145 | /// @return | ||
| 146 | GATE_CORE_CPP_API extern void releaseStringArray(gate_string_t* strarr, gate_size_t strarr_length); | ||
| 147 | |||
| 148 | |||
| 149 | /// @brief | ||
| 150 | /// @param type | ||
| 151 | /// @param data | ||
| 152 | /// @param stream | ||
| 153 | /// @return | ||
| 154 | GATE_CORE_CPP_API extern VoidResult dumpType(type_id_t type, void const* data, Stream& stream); | ||
| 155 | |||
| 156 | |||
| 157 | /// @brief | ||
| 158 | /// @param ptrStruct | ||
| 159 | /// @param stream | ||
| 160 | /// @return | ||
| 161 | GATE_CORE_CPP_API extern VoidResult dumpStruct(gate_struct_t const* ptrStruct, Stream& stream); | ||
| 162 | |||
| 163 | |||
| 164 | /// @brief | ||
| 165 | /// @param state | ||
| 166 | /// @param value | ||
| 167 | /// @return | ||
| 168 | GATE_CORE_CPP_API extern VoidResult setState(AtomicInt& state, int32_t value); | ||
| 169 | |||
| 170 | |||
| 171 | /// @brief | ||
| 172 | /// @param state | ||
| 173 | /// @param from_value | ||
| 174 | /// @param to_value | ||
| 175 | /// @return | ||
| 176 | GATE_CORE_CPP_API extern VoidResult updateState(AtomicInt& state, int32_t from_value, int32_t to_value); | ||
| 177 | |||
| 178 | |||
| 179 | /// @brief | ||
| 180 | /// @param state | ||
| 181 | /// @param await_value | ||
| 182 | /// @param timeout_ms | ||
| 183 | /// @return | ||
| 184 | GATE_CORE_CPP_API extern Result<bool_t> awaitState(AtomicInt& state, int32_t await_value, uint32_t timeout_ms); | ||
| 185 | |||
| 186 | |||
| 187 | /// @brief | ||
| 188 | /// @param map | ||
| 189 | /// @return | ||
| 190 | GATE_CORE_CPP_API extern Map<String, Value> convertStringValueMap(gate_map_t const& map); | ||
| 191 | |||
| 192 | /// @brief | ||
| 193 | /// @param map | ||
| 194 | /// @return | ||
| 195 | GATE_CORE_CPP_API extern gate_map_t convertStringValueMap(Map<String, Value> const& map); | ||
| 196 | |||
| 197 | |||
| 198 | /// @brief Defers the execution of an external destructor-like function until the object's destructor is invoked | ||
| 199 | class GATE_CORE_CPP_API ScopeExit : private NonCopyable | ||
| 200 | { | ||
| 201 | public: | ||
| 202 | /// @brief Initializes a Scope-Exit instance | ||
| 203 | /// @param func Pointer to scope-exit cleanup function (signature of generic destructor function) | ||
| 204 | /// @param param Preserved parameter handed over to cleanup function | ||
| 205 | ScopeExit(gate_mem_dtor_t func, void* param) noexcept; | ||
| 206 | |||
| 207 | /// @brief Executes the stored Scope-Exit cleanup function and releases the object | ||
| 208 | ~ScopeExit() noexcept; | ||
| 209 | |||
| 210 | /// @brief Removes the scope-exit cleanup function and prohibits its execution in destructor | ||
| 211 | void release(); | ||
| 212 | private: | ||
| 213 | gate_mem_dtor_t onExitFunc; | ||
| 214 | void* onExitParam; | ||
| 215 | }; | ||
| 216 | |||
| 217 | template<class TO_ENTRY_TYPE, class FROM_ENTRY_TYPE> | ||
| 218 | 8 | ArrayList<TO_ENTRY_TYPE> convertArrayListTypes(gate_arraylist_t const& src) | |
| 219 | { | ||
| 220 | 8 | ArrayList<TO_ENTRY_TYPE> ret; | |
| 221 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | const size_t len = gate_arraylist_length(src); |
| 222 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4 times.
|
16 | for (size_t ndx = 0; ndx != len; ++ndx) |
| 223 | { | ||
| 224 |
1/2✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
|
8 | FROM_ENTRY_TYPE const* ptr = static_cast<FROM_ENTRY_TYPE const*>(gate_arraylist_get(src, ndx)); |
| 225 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
8 | if (ptr == NULL) |
| 226 | { | ||
| 227 | ✗ | continue; | |
| 228 | } | ||
| 229 |
2/4✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
|
8 | ret.add(TO_ENTRY_TYPE(*ptr)); |
| 230 | } | ||
| 231 | 8 | return ret; | |
| 232 | } | ||
| 233 | |||
| 234 | template<class TO_ENTRY_TYPE, class FROM_ENTRY_TYPE> | ||
| 235 | 16 | void convertArrayIntoCTypeArrayList(gate_arraylist_t& destArrayList, Array<FROM_ENTRY_TYPE> const& srcArray) | |
| 236 | { | ||
| 237 | 16 | Enumerator<FROM_ENTRY_TYPE const> e = srcArray.enumerate(); | |
| 238 |
2/2✓ Branch 1 taken 8 times.
✓ Branch 2 taken 8 times.
|
32 | for (; e.valid(); e.next()) |
| 239 | { | ||
| 240 | 16 | FROM_ENTRY_TYPE const* ptrSource = e.get(); | |
| 241 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | TO_ENTRY_TYPE const* ptrDest = cimpl_cast<TO_ENTRY_TYPE, FROM_ENTRY_TYPE>(*ptrSource); |
| 242 |
1/2✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
|
16 | gate_arraylist_add(destArrayList, ptrDest); |
| 243 | } | ||
| 244 | 16 | } | |
| 245 | |||
| 246 | |||
| 247 | } // end of namespace utils | ||
| 248 | } // end of namespace gate | ||
| 249 | |||
| 250 | #endif | ||
| 251 |