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