| 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 Standard value encapsulation | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | |||
| 35 | #ifndef GATE_VALUES_HPP_INCLUDED | ||
| 36 | #define GATE_VALUES_HPP_INCLUDED | ||
| 37 | |||
| 38 | #include "gate/gate_core_api.hpp" | ||
| 39 | #include "gate/gatetypes.hpp" | ||
| 40 | #include "gate/results.hpp" | ||
| 41 | #include "gate/exceptions.hpp" | ||
| 42 | |||
| 43 | #include "gate/values.h" | ||
| 44 | #include "gate/strings.hpp" | ||
| 45 | #include "gate/arrays.hpp" | ||
| 46 | #include "gate/properties.hpp" | ||
| 47 | #include "gate/blobs.hpp" | ||
| 48 | |||
| 49 | namespace gate | ||
| 50 | { | ||
| 51 | |||
| 52 | /// @brief | ||
| 53 | /// @tparam SRC | ||
| 54 | /// @tparam DST | ||
| 55 | template<class SRC, class DST> | ||
| 56 | struct ValueExtractor | ||
| 57 | { | ||
| 58 | static void get(SRC const& src, DST& dst) | ||
| 59 | { | ||
| 60 | dst = src; | ||
| 61 | } | ||
| 62 | }; | ||
| 63 | |||
| 64 | template<> struct ValueExtractor<gate_string_t, String> | ||
| 65 | { | ||
| 66 | static void get(gate_string_t const& src, String& dst) | ||
| 67 | { | ||
| 68 | dst = String(src); | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | template<> struct ValueExtractor<String, gate_string_t> | ||
| 72 | { | ||
| 73 | static void get(String const& src, gate_string_t& dst) | ||
| 74 | { | ||
| 75 | String::assign(dst, src); | ||
| 76 | } | ||
| 77 | }; | ||
| 78 | |||
| 79 | template<> struct ValueExtractor<gate_string_t, gate_string_t> | ||
| 80 | { | ||
| 81 | static void get(gate_string_t const& src, gate_string_t& dst) | ||
| 82 | { | ||
| 83 | if (NULL == gate_string_clone(&dst, &src)) | ||
| 84 | { | ||
| 85 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
| 86 | } | ||
| 87 | } | ||
| 88 | }; | ||
| 89 | |||
| 90 | template<> struct ValueExtractor<gate_array_t, gate_array_t> | ||
| 91 | { | ||
| 92 | static void get(gate_array_t const& src, gate_array_t& dst) | ||
| 93 | { | ||
| 94 | if (NULL == gate_array_copy(&dst, &src)) | ||
| 95 | { | ||
| 96 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
| 97 | } | ||
| 98 | } | ||
| 99 | }; | ||
| 100 | |||
| 101 | #if !defined(GATE_COMPILER_SUPPORTS_CPP_PARTIAL_TEMPLATE_SPECIALIZATION) | ||
| 102 | // TODO | ||
| 103 | |||
| 104 | #else | ||
| 105 | template<class T> struct ValueExtractor<Array<T>, gate_array_t> | ||
| 106 | { | ||
| 107 | static void get(Array<T> const& src, gate_array_t& dst) | ||
| 108 | { | ||
| 109 | if (NULL == gate_array_copy(&dst, src.c_impl())) | ||
| 110 | { | ||
| 111 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
| 112 | } | ||
| 113 | } | ||
| 114 | }; | ||
| 115 | |||
| 116 | template<class T> struct ValueExtractor<gate_array_t, Array<T> > | ||
| 117 | { | ||
| 118 | static void get(gate_array_t const& src, Array<T>& dst) | ||
| 119 | { | ||
| 120 | if (NULL == gate_array_copy(dst.c_impl(), &src)) | ||
| 121 | { | ||
| 122 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | }; | ||
| 126 | #endif /* GATE_COMPILER_SUPPORTS_CPP_PARTIAL_TEMPLATE_SPECIALIZATION */ | ||
| 127 | |||
| 128 | template<> struct ValueExtractor<gate_object_ptr_t, gate_object_ptr_t > | ||
| 129 | { | ||
| 130 | static void get(gate_object_ptr_t const& src, gate_object_ptr_t& dst) | ||
| 131 | { | ||
| 132 | dst = src; | ||
| 133 | if (dst) | ||
| 134 | { | ||
| 135 | gate_object_retain(dst); | ||
| 136 | } | ||
| 137 | } | ||
| 138 | }; | ||
| 139 | |||
| 140 | template<> struct ValueExtractor<gate_property_t, gate_property_t> | ||
| 141 | { | ||
| 142 | static void get(gate_property_t const& src, gate_property_t& dst) | ||
| 143 | { | ||
| 144 | if (NULL == gate_property_copy(&dst, &src)) | ||
| 145 | { | ||
| 146 | GATEXX_RAISE_ERROR(results::OutOfMemory); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | }; | ||
| 150 | |||
| 151 | |||
| 152 | |||
| 153 | /// @brief | ||
| 154 | /// @tparam T | ||
| 155 | template<class T> | ||
| 156 | struct ValueType | ||
| 157 | { | ||
| 158 | public: | ||
| 159 | static const gate_uint16_t Id; | ||
| 160 | static const gate_size_t Size; | ||
| 161 | |||
| 162 | static void load(void const* srcaddress, T& dst); | ||
| 163 | static void store(T const& src, void* dstaddress); | ||
| 164 | }; | ||
| 165 | |||
| 166 | template<class T, unsigned ID> | ||
| 167 | struct ValueTypePod | ||
| 168 | { | ||
| 169 | public: | ||
| 170 | static const gate_uint16_t Id = (gate_uint16_t)ID; | ||
| 171 | static const gate_size_t Size = sizeof(T); | ||
| 172 | |||
| 173 | 20 | static void load(void const* srcaddress, T& dst) | |
| 174 | { | ||
| 175 | 20 | gate_mem_copy(&dst, srcaddress, sizeof(T)); | |
| 176 | 20 | } | |
| 177 | 20 | static void store(T const& src, void* dstaddress) | |
| 178 | { | ||
| 179 | 20 | gate_mem_copy(dstaddress, &src, sizeof(T)); | |
| 180 | 20 | } | |
| 181 | }; | ||
| 182 | |||
| 183 | template<> struct ValueType<bool_t> : ValueTypePod<bool_t, GATE_TYPE_BOOL> {}; | ||
| 184 | template<> struct ValueType<int8_t> : ValueTypePod<int8_t, GATE_TYPE_I8> {}; | ||
| 185 | //template<> struct ValueType<uint8_t>: ValueTypePod<uint8_t, GATE_TYPE_UI8> {}; | ||
| 186 | template<> struct ValueType<int16_t> : ValueTypePod<int16_t, GATE_TYPE_I16> {}; | ||
| 187 | template<> struct ValueType<uint16_t> : ValueTypePod<uint16_t, GATE_TYPE_UI16> {}; | ||
| 188 | template<> struct ValueType<int32_t> : ValueTypePod<int32_t, GATE_TYPE_I32> {}; | ||
| 189 | template<> struct ValueType<uint32_t> : ValueTypePod<uint32_t, GATE_TYPE_UI32> {}; | ||
| 190 | template<> struct ValueType<int64_t> : ValueTypePod<int64_t, GATE_TYPE_I64> {}; | ||
| 191 | template<> struct ValueType<uint64_t> : ValueTypePod<uint64_t, GATE_TYPE_UI64> {}; | ||
| 192 | template<> struct ValueType<real32_t> : ValueTypePod<real32_t, GATE_TYPE_R32> {}; | ||
| 193 | template<> struct ValueType<real64_t> : ValueTypePod<real64_t, GATE_TYPE_R64> {}; | ||
| 194 | //template<> struct ValueType<uintptr_t>: ValueTypePod<uintptr_t, GATE_TYPE_ADDRESS> {}; | ||
| 195 | |||
| 196 | template<> struct ValueType<gate_ptr_t> : ValueTypePod<gate_ptr_t, GATE_TYPE_DATAPTR> {}; | ||
| 197 | template<> struct ValueType<gate_funcptr_t> : ValueTypePod<gate_funcptr_t, GATE_TYPE_FUNCPTR> {}; | ||
| 198 | template<> struct ValueType<gate_cstr_t> : ValueTypePod<gate_cstr_t, GATE_TYPE_CSTR> {}; | ||
| 199 | template<> struct ValueType<gate_wstr_t> : ValueTypePod<gate_wstr_t, GATE_TYPE_WSTR> {}; | ||
| 200 | template<> struct ValueType<gate_guid_t> : ValueTypePod<gate_guid_t, GATE_TYPE_GUID> {}; | ||
| 201 | template<> struct ValueType<gate_date_t> : ValueTypePod<gate_date_t, GATE_TYPE_DATE> {}; | ||
| 202 | template<> struct ValueType<gate_daytime_t> : ValueTypePod<gate_daytime_t, GATE_TYPE_DAYTIME> {}; | ||
| 203 | template<> struct ValueType<gate_datetime_t> : ValueTypePod<gate_datetime_t, GATE_TYPE_DATETIME> {}; | ||
| 204 | template<> struct ValueType<gate_time_t> : ValueTypePod<gate_time_t, GATE_TYPE_TIME> {}; | ||
| 205 | |||
| 206 | |||
| 207 | |||
| 208 | template<class CPPTYPE, class NATIVETYPE, unsigned ID> struct ValueTypeAccessor | ||
| 209 | { | ||
| 210 | public: | ||
| 211 | static const gate_uint16_t Id = (gate_uint16_t)ID; | ||
| 212 | static const gate_size_t Size = sizeof(NATIVETYPE); | ||
| 213 | |||
| 214 | static void load(void const* srcaddress, CPPTYPE& dst) | ||
| 215 | { | ||
| 216 | NATIVETYPE const* src = (NATIVETYPE const*)srcaddress; | ||
| 217 | ValueExtractor<NATIVETYPE, CPPTYPE>::get(*src, dst); | ||
| 218 | } | ||
| 219 | static void store(CPPTYPE const& src, void* dstaddress) | ||
| 220 | { | ||
| 221 | NATIVETYPE* dst = (NATIVETYPE*)dstaddress; | ||
| 222 | ValueExtractor<CPPTYPE, NATIVETYPE>::get(src, *dst); | ||
| 223 | } | ||
| 224 | }; | ||
| 225 | |||
| 226 | template<> struct ValueType<gate_string_t> : ValueTypeAccessor<gate_string_t, gate_string_t, GATE_TYPE_STRING> {}; | ||
| 227 | template<> struct ValueType<gate_array_t> : ValueTypeAccessor<gate_array_t, gate_array_t, GATE_TYPE_ARRAY> {}; | ||
| 228 | template<> struct ValueType<gate_blob_t> : ValueTypeAccessor<gate_blob_t, gate_blob_t, GATE_TYPE_BLOB> {}; | ||
| 229 | |||
| 230 | template<> struct ValueType<gate_struct_ptr_t> : ValueTypeAccessor<gate_struct_ptr_t, gate_struct_ptr_t, GATE_TYPE_STRUCT> {}; | ||
| 231 | template<> struct ValueType<gate_object_ptr_t> : ValueTypeAccessor< gate_object_ptr_t, gate_object_ptr_t, GATE_TYPE_OBJECT> {}; | ||
| 232 | template<> struct ValueType<gate_property_t> : ValueTypeAccessor<gate_property_t, gate_property_t, GATE_TYPE_PROPERTY> {}; | ||
| 233 | |||
| 234 | |||
| 235 | |||
| 236 | /// @brief | ||
| 237 | class GATE_CORE_CPP_API Value | ||
| 238 | { | ||
| 239 | private: | ||
| 240 | gate_value_t value; | ||
| 241 | |||
| 242 | public: | ||
| 243 | 28 | template<class T> Value(T const& t) | |
| 244 | { | ||
| 245 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 14 times.
|
28 | if (NULL == gate_value_create(ValueType<T>::Id, &t, &this->value)) |
| 246 | { | ||
| 247 | ✗ | GATEXX_RAISE_ERROR(results::InvalidData); | |
| 248 | } | ||
| 249 | 28 | } | |
| 250 | |||
| 251 | Value() noexcept; | ||
| 252 | Value(Value const& src); | ||
| 253 | Value(gate_value_t const& nativeValue); | ||
| 254 | Value& operator=(Value const& src); | ||
| 255 | ~Value() noexcept; | ||
| 256 | |||
| 257 | void swap(Value& src) noexcept; | ||
| 258 | |||
| 259 | gate_value_t* c_impl() noexcept; | ||
| 260 | gate_value_t const* c_impl() const noexcept; | ||
| 261 | |||
| 262 | gate_type_id_t Type() const noexcept; | ||
| 263 | |||
| 264 | 20 | template<class T> void get(T& value) const | |
| 265 | { | ||
| 266 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
20 | if (ValueType<T>::Id != this->value.value_type) |
| 267 | { | ||
| 268 | ✗ | GATEXX_RAISE_EXCEPTION(results::IncorrectType, NULL, 0); | |
| 269 | } | ||
| 270 | 20 | ValueType<T>::load(&this->value.content.ptr_value, value); | |
| 271 | 20 | } | |
| 272 | |||
| 273 | 20 | template<class T> void set(T const& value) | |
| 274 | { | ||
| 275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
|
20 | if (ValueType<T>::Id != this->value.value_type) |
| 276 | { | ||
| 277 | ✗ | GATEXX_RAISE_EXCEPTION(results::IncorrectType, NULL, 0); | |
| 278 | } | ||
| 279 | 20 | ValueType<T>::store(value, &this->value.content.ptr_value); | |
| 280 | 20 | } | |
| 281 | |||
| 282 | bool_t exportAs(String& value) const; | ||
| 283 | bool_t exportAs(bool_t& value) const; | ||
| 284 | bool_t exportAs(int8_t& value) const; | ||
| 285 | bool_t exportAs(int16_t& value) const; | ||
| 286 | bool_t exportAs(int32_t& value) const; | ||
| 287 | bool_t exportAs(int64_t& value) const; | ||
| 288 | bool_t exportAs(uint8_t& value) const; | ||
| 289 | bool_t exportAs(uint16_t& value) const; | ||
| 290 | bool_t exportAs(uint32_t& value) const; | ||
| 291 | bool_t exportAs(uint64_t& value) const; | ||
| 292 | bool_t exportAs(real32_t& value) const; | ||
| 293 | bool_t exportAs(real64_t& value) const; | ||
| 294 | |||
| 295 | String toString() const; | ||
| 296 | }; | ||
| 297 | |||
| 298 | } // end of namespace gate | ||
| 299 | |||
| 300 | |||
| 301 | #endif | ||
| 302 |