| 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 Wrappers and smart pointer objects | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | #ifndef GATE_WRAPPERS_HPP_INCLUDED | ||
| 35 | #define GATE_WRAPPERS_HPP_INCLUDED | ||
| 36 | |||
| 37 | #include "gate/gate_core_api.hpp" | ||
| 38 | #include "gate/wrappers.h" | ||
| 39 | #include "gate/atomics.h" | ||
| 40 | #include "gate/gatetypes.hpp" | ||
| 41 | #include "gate/results.hpp" | ||
| 42 | #include "gate/exceptions.hpp" | ||
| 43 | #include "gate/memalloc.hpp" | ||
| 44 | #include "gate/delegates.hpp" | ||
| 45 | #include "gate/cxx_typeinfo.hpp" | ||
| 46 | |||
| 47 | namespace gate | ||
| 48 | { | ||
| 49 | |||
| 50 | /// @brief | ||
| 51 | /// @tparam T | ||
| 52 | template<class T> | ||
| 53 | class GATE_CORE_CPP_TEMPLATE_API Ptr : public SafeBoolBase< Ptr<T> > | ||
| 54 | { | ||
| 55 | public: | ||
| 56 | typedef Ptr<T> self_t; | ||
| 57 | typedef T value_t; | ||
| 58 | typedef T const* const_ptr_t; | ||
| 59 | typedef T* ptr_t; | ||
| 60 | typedef T const& const_ref_t; | ||
| 61 | typedef T& ref_t; | ||
| 62 | |||
| 63 | |||
| 64 | private: | ||
| 65 | ptr_t ptr; | ||
| 66 | |||
| 67 | public: | ||
| 68 | |||
| 69 | 75 | Ptr(ptr_t newInstance = NULL) noexcept | |
| 70 | 75 | : ptr(newInstance) | |
| 71 | { | ||
| 72 | 75 | } | |
| 73 | 81 | void reset(ptr_t newInstance = NULL) noexcept | |
| 74 | { | ||
| 75 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 25 times.
|
81 | delete this->ptr; |
| 76 | 81 | this->ptr = newInstance; | |
| 77 | 81 | } | |
| 78 | 75 | ~Ptr() noexcept | |
| 79 | { | ||
| 80 | 75 | this->reset(); | |
| 81 | 75 | } | |
| 82 | 1 | void swap(self_t& that) noexcept | |
| 83 | { | ||
| 84 | 1 | gate::swapRefsNoExcept(this->ptr, that.ptr); | |
| 85 | 1 | } | |
| 86 | 37 | ptr_t release() noexcept | |
| 87 | { | ||
| 88 | 37 | ptr_t tmp = this->ptr; | |
| 89 | 37 | this->ptr = NULL; | |
| 90 | 37 | return tmp; | |
| 91 | } | ||
| 92 | 71 | ptr_t get() const noexcept | |
| 93 | { | ||
| 94 | 71 | return this->ptr; | |
| 95 | } | ||
| 96 | 1 | const_ptr_t operator->() const noexcept | |
| 97 | { | ||
| 98 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 99 | 1 | return this->get(); | |
| 100 | } | ||
| 101 | 1 | ptr_t operator->() noexcept | |
| 102 | { | ||
| 103 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 104 | 1 | return this->get(); | |
| 105 | } | ||
| 106 | 1 | const_ref_t operator*() const noexcept | |
| 107 | { | ||
| 108 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 109 | 1 | return *this->get(); | |
| 110 | } | ||
| 111 | 12 | ref_t operator*() noexcept | |
| 112 | { | ||
| 113 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 114 | 12 | return *this->get(); | |
| 115 | } | ||
| 116 | |||
| 117 | 6 | bool_t empty() const noexcept | |
| 118 | { | ||
| 119 | 6 | return this->ptr == NULL; | |
| 120 | } | ||
| 121 | 29 | bool_t hasValue() const noexcept | |
| 122 | { | ||
| 123 | 29 | return this->ptr != NULL; | |
| 124 | } | ||
| 125 | |||
| 126 | 2 | self_t const& andThen(Delegate1<value_t&> const& callback) const | |
| 127 | { | ||
| 128 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->ptr != NULL) |
| 129 | { | ||
| 130 | 1 | callback.invoke(*this->ptr); | |
| 131 | } | ||
| 132 | 2 | return *this; | |
| 133 | } | ||
| 134 | 2 | self_t const& orElse(Delegate0 const& callback) const | |
| 135 | { | ||
| 136 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->ptr == NULL) |
| 137 | { | ||
| 138 | 1 | callback.invoke(); | |
| 139 | } | ||
| 140 | 2 | return *this; | |
| 141 | } | ||
| 142 | 2 | self_t const& orThrow() const | |
| 143 | { | ||
| 144 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->ptr == NULL) |
| 145 | { | ||
| 146 | 1 | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 147 | } | ||
| 148 | 1 | return *this; | |
| 149 | } | ||
| 150 | |||
| 151 | 1 | ref_t value() noexcept | |
| 152 | { | ||
| 153 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->ptr != NULL); |
| 154 | 1 | return *this->ptr; | |
| 155 | } | ||
| 156 | 1 | const_ref_t value() const noexcept | |
| 157 | { | ||
| 158 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->ptr != NULL); |
| 159 | 1 | return *this->ptr; | |
| 160 | } | ||
| 161 | |||
| 162 | 1 | const_ref_t value(const_ref_t defaultValue) const | |
| 163 | { | ||
| 164 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (this->ptr) |
| 165 | { | ||
| 166 | ✗ | return *this->ptr; | |
| 167 | } | ||
| 168 | else | ||
| 169 | { | ||
| 170 | 1 | return defaultValue; | |
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | 2 | bool_t operator!() const noexcept | |
| 175 | { | ||
| 176 | 2 | return this->empty(); | |
| 177 | } | ||
| 178 | |||
| 179 | |||
| 180 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 181 | Ptr(self_t&& src) noexcept | ||
| 182 | : ptr(src.ptr) | ||
| 183 | { | ||
| 184 | src.ptr = NULL; | ||
| 185 | } | ||
| 186 | 1 | self_t& operator=(self_t&& src) noexcept | |
| 187 | { | ||
| 188 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 189 | { | ||
| 190 | 1 | this->reset(src.ptr); | |
| 191 | 1 | src.ptr = NULL; | |
| 192 | } | ||
| 193 | 1 | return *this; | |
| 194 | } | ||
| 195 | private: // new compilers will see copy-ctor and op= private | ||
| 196 | Ptr(self_t const& src) noexcept; | ||
| 197 | self_t& operator=(self_t const& src) noexcept; | ||
| 198 | public: | ||
| 199 | #else | ||
| 200 | Ptr(self_t const& src) noexcept | ||
| 201 | : ptr(src.ptr) | ||
| 202 | { | ||
| 203 | // dirty hack for old compilers: swap on copy-construction | ||
| 204 | const_cast<self_t&>(src).ptr = NULL; | ||
| 205 | } | ||
| 206 | self_t& operator=(self_t const& src) noexcept | ||
| 207 | { | ||
| 208 | if (this != &src) | ||
| 209 | { | ||
| 210 | // dirty hack for old compilers: transfer ownership on assignment | ||
| 211 | this->reset(src.ptr); | ||
| 212 | const_cast<self_t&>(src).ptr = NULL; | ||
| 213 | } | ||
| 214 | return *this; | ||
| 215 | } | ||
| 216 | #endif | ||
| 217 | }; | ||
| 218 | |||
| 219 | |||
| 220 | /// @brief | ||
| 221 | /// @tparam T | ||
| 222 | template<class T> | ||
| 223 | class GATE_CORE_CPP_TEMPLATE_API Optional : public SafeBoolBase<Optional<T> > | ||
| 224 | { | ||
| 225 | public: | ||
| 226 | typedef T value_t; | ||
| 227 | typedef T* ptr_t; | ||
| 228 | typedef T const* const_ptr_t; | ||
| 229 | typedef T& ref_t; | ||
| 230 | typedef T const& const_ref_t; | ||
| 231 | typedef Optional<T> self_t; | ||
| 232 | private: | ||
| 233 | union | ||
| 234 | { | ||
| 235 | gate_c_maxalign_t dummy; | ||
| 236 | char buffer[sizeof(value_t)]; | ||
| 237 | }; | ||
| 238 | |||
| 239 | ptr_t ptr; // points to initialized value in buffer, or NULL if empty | ||
| 240 | public: | ||
| 241 | 85 | Optional() | |
| 242 | 85 | : ptr(0) | |
| 243 | { | ||
| 244 | 85 | Mem::clear(&this->buffer[0], sizeof(this->buffer)); | |
| 245 | 85 | } | |
| 246 | |||
| 247 | 122 | void reset() noexcept | |
| 248 | { | ||
| 249 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 97 times.
|
122 | if (this->ptr) |
| 250 | { | ||
| 251 | 23 | this->ptr->~T(); | |
| 252 | 23 | this->ptr = NULL; | |
| 253 | } | ||
| 254 | 122 | } | |
| 255 | |||
| 256 | 1 | self_t& create() | |
| 257 | { | ||
| 258 | 1 | this->reset(); | |
| 259 | 1 | ptr_t p = reinterpret_cast<ptr_t>(&this->buffer[0]); | |
| 260 | 1 | constructType(p); | |
| 261 | 1 | this->ptr = p; | |
| 262 | 1 | return *this; | |
| 263 | } | ||
| 264 | 20 | self_t& create(const_ref_t src) | |
| 265 | { | ||
| 266 | 20 | this->reset(); | |
| 267 | 20 | ptr_t p = reinterpret_cast<ptr_t>(&this->buffer[0]); | |
| 268 | 20 | copyConstructType(p, src); | |
| 269 | 20 | this->ptr = p; | |
| 270 | 20 | return *this; | |
| 271 | } | ||
| 272 | |||
| 273 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 274 | 1 | template<class A1> void emplace(A1&& a1) | |
| 275 | { | ||
| 276 | 1 | this->reset(); | |
| 277 |
1/2✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | new (&buffer[0]) T(forwardRef<A1>(a1)); |
| 278 | 1 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | |
| 279 | 1 | } | |
| 280 | template<class A1, class A2> void emplace(A1&& a1, A2&& a2) | ||
| 281 | { | ||
| 282 | this->reset(); | ||
| 283 | new (&buffer[0]) T(forwardRef<A1>(a1), forwardRef<A2>(a2)); | ||
| 284 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 285 | } | ||
| 286 | template<class A1, class A2, class A3> void emplace(A1&& a1, A2&& a2, A3&& a3) | ||
| 287 | { | ||
| 288 | this->reset(); | ||
| 289 | new (&buffer[0]) T(forwardRef<A1>(a1), forwardRef<A2>(a2), forwardRef<A3>(a3)); | ||
| 290 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 291 | } | ||
| 292 | template<class A1, class A2, class A3, class A4> void emplace(A1&& a1, A2&& a2, A3&& a3, A4&& a4) | ||
| 293 | { | ||
| 294 | this->reset(); | ||
| 295 | new (&buffer[0]) T(forwardRef<A1>(a1), forwardRef<A2>(a2), forwardRef<A3>(a3), forwardRef<A4>(a4)); | ||
| 296 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 297 | } | ||
| 298 | template<class A1, class A2, class A3, class A4, class A5> void emplace(A1&& a1, A2&& a2, A3&& a3, A4&& a4, A5&& a5) | ||
| 299 | { | ||
| 300 | this->reset(); | ||
| 301 | new (&buffer[0]) T(forwardRef<A1>(a1), forwardRef<A2>(a2), forwardRef<A3>(a3), forwardRef<A4>(a4), forwardRef<A5>(a5)); | ||
| 302 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 303 | } | ||
| 304 | template<class A1, class A2, class A3, class A4, class A5, class A6> void emplace(A1&& a1, A2&& a2, A3&& a3, A4&& a4, A5&& a5, A6&& a6) | ||
| 305 | { | ||
| 306 | this->reset(); | ||
| 307 | new (&buffer[0]) T(forwardRef<A1>(a1), forwardRef<A2>(a2), forwardRef<A3>(a3), forwardRef<A4>(a4), forwardRef<A5>(a5), forwardRef<A6>(a6)); | ||
| 308 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 309 | } | ||
| 310 | #else | ||
| 311 | template<class A1> void emplace(A1& a1) | ||
| 312 | { | ||
| 313 | this->reset(); | ||
| 314 | new (&buffer[0]) T(a1); | ||
| 315 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 316 | } | ||
| 317 | template<class A1, class A2> void emplace(A1& a1, A2& a2) | ||
| 318 | { | ||
| 319 | this->reset(); | ||
| 320 | new (&buffer[0]) T(a1, a2); | ||
| 321 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 322 | } | ||
| 323 | template<class A1, class A2, class A3> void emplace(A1& a1, A2& a2, A3& a3) | ||
| 324 | { | ||
| 325 | this->reset(); | ||
| 326 | new (&buffer[0]) T(a1, a2, a3); | ||
| 327 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 328 | } | ||
| 329 | template<class A1, class A2, class A3, class A4> void emplace(A1& a1, A2& a2, A3& a3, A4& a4) | ||
| 330 | { | ||
| 331 | this->reset(); | ||
| 332 | new (&buffer[0]) T(a1, a2, a3, a4); | ||
| 333 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 334 | } | ||
| 335 | template<class A1, class A2, class A3, class A4, class A5> void emplace(A1& a1, A2& a2, A3& a3, A4& a4, A5& a5) | ||
| 336 | { | ||
| 337 | this->reset(); | ||
| 338 | new (&buffer[0]) T(a1, a2, a3, a4, a5); | ||
| 339 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 340 | } | ||
| 341 | template<class A1, class A2, class A3, class A4, class A5, class A6> void emplace(A1& a1, A2& a2, A3& a3, A4& a4, A5& a5, A6& a6) | ||
| 342 | { | ||
| 343 | this->reset(); | ||
| 344 | new (&buffer[0]) T(a1, a2, a3, a4, a5, a6); | ||
| 345 | this->ptr = reinterpret_cast<T*>(&this->buffer[0]); | ||
| 346 | } | ||
| 347 | #endif | ||
| 348 | |||
| 349 | 60 | ptr_t get() const noexcept | |
| 350 | { | ||
| 351 | 60 | return this->ptr; | |
| 352 | } | ||
| 353 | |||
| 354 | 82 | bool_t empty() const noexcept | |
| 355 | { | ||
| 356 | 82 | return this->ptr == NULL; | |
| 357 | } | ||
| 358 | |||
| 359 | 53 | bool_t hasValue() const noexcept | |
| 360 | { | ||
| 361 | 53 | return this->ptr != NULL; | |
| 362 | } | ||
| 363 | |||
| 364 | 1 | ref_t value() noexcept | |
| 365 | { | ||
| 366 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->ptr != NULL); |
| 367 | 1 | return *this->ptr; | |
| 368 | } | ||
| 369 | |||
| 370 | const_ref_t value() const noexcept | ||
| 371 | { | ||
| 372 | GATE_DEBUG_ASSERT(this->ptr != NULL); | ||
| 373 | return *this->ptr; | ||
| 374 | } | ||
| 375 | |||
| 376 | 1 | const_ref_t value(const_ref_t defaultValue) const | |
| 377 | { | ||
| 378 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (this->ptr == NULL) |
| 379 | { | ||
| 380 | ✗ | return defaultValue; | |
| 381 | } | ||
| 382 | else | ||
| 383 | { | ||
| 384 | 1 | return *this->ptr; | |
| 385 | } | ||
| 386 | } | ||
| 387 | |||
| 388 | 12 | ptr_t operator->() noexcept | |
| 389 | { | ||
| 390 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 391 | 12 | return this->get(); | |
| 392 | } | ||
| 393 | const_ptr_t operator->() const noexcept | ||
| 394 | { | ||
| 395 | GATE_DEBUG_ASSERT(this->hasValue()); | ||
| 396 | return this->get(); | ||
| 397 | } | ||
| 398 | 28 | ref_t operator*() noexcept | |
| 399 | { | ||
| 400 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 20 times.
|
28 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 401 | 28 | return *this->get(); | |
| 402 | } | ||
| 403 | const_ref_t operator*() const noexcept | ||
| 404 | { | ||
| 405 | GATE_DEBUG_ASSERT(this->hasValue()); | ||
| 406 | return *this->get(); | ||
| 407 | } | ||
| 408 | |||
| 409 | 4 | self_t const& andThen(Delegate1<value_t&> const& callback) const | |
| 410 | { | ||
| 411 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (this->ptr != NULL) |
| 412 | { | ||
| 413 | 2 | callback.invoke(*this->ptr); | |
| 414 | } | ||
| 415 | 4 | return *this; | |
| 416 | } | ||
| 417 | 4 | self_t const& orElse(Delegate0 const& callback) const | |
| 418 | { | ||
| 419 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (this->ptr == NULL) |
| 420 | { | ||
| 421 | 2 | callback.invoke(); | |
| 422 | } | ||
| 423 | 4 | return *this; | |
| 424 | } | ||
| 425 | 2 | self_t const& orThrow() const | |
| 426 | { | ||
| 427 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (this->ptr == NULL) |
| 428 | { | ||
| 429 | 1 | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 430 | } | ||
| 431 | 1 | return *this; | |
| 432 | } | ||
| 433 | |||
| 434 | ✗ | bool_t operator!() const noexcept | |
| 435 | { | ||
| 436 | ✗ | return this->empty(); | |
| 437 | } | ||
| 438 | |||
| 439 | 88 | ~Optional() noexcept | |
| 440 | { | ||
| 441 | 88 | this->reset(); | |
| 442 | 88 | } | |
| 443 | |||
| 444 | 1 | Optional(self_t const& src) | |
| 445 | 1 | : ptr(0) | |
| 446 | { | ||
| 447 | 1 | gate_mem_clear(&this->buffer[0], sizeof(this->buffer)); | |
| 448 | |||
| 449 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!src.empty()) |
| 450 | { | ||
| 451 | 1 | this->create(*src.get()); | |
| 452 | } | ||
| 453 | 1 | } | |
| 454 | |||
| 455 | 1 | Optional(const_ref_t native_src) | |
| 456 | 1 | : ptr(0) | |
| 457 | { | ||
| 458 | 1 | gate_mem_clear(&this->buffer[0], sizeof(this->buffer)); | |
| 459 | 1 | this->create(native_src); | |
| 460 | 1 | } | |
| 461 | |||
| 462 | 7 | self_t& operator=(self_t const& src) | |
| 463 | { | ||
| 464 |
1/2✓ Branch 0 taken 7 times.
✗ Branch 1 not taken.
|
7 | if (this != &src) |
| 465 | { | ||
| 466 | 7 | this->reset(); | |
| 467 |
2/2✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
|
7 | if (!src.empty()) |
| 468 | { | ||
| 469 | 6 | this->create(*src.get()); | |
| 470 | } | ||
| 471 | } | ||
| 472 | 7 | return *this; | |
| 473 | } | ||
| 474 | }; | ||
| 475 | |||
| 476 | /// @brief | ||
| 477 | /// @tparam T | ||
| 478 | /// @tparam E | ||
| 479 | template<class T, class E> | ||
| 480 | class GATE_CORE_CPP_TEMPLATE_API Expected | ||
| 481 | { | ||
| 482 | public: | ||
| 483 | typedef T value_t; | ||
| 484 | typedef T* ptr_t; | ||
| 485 | typedef T const* const_ptr_t; | ||
| 486 | typedef T& ref_t; | ||
| 487 | typedef T const& const_ref_t; | ||
| 488 | |||
| 489 | typedef E error_t; | ||
| 490 | typedef Expected<value_t, error_t> self_t; | ||
| 491 | |||
| 492 | private: | ||
| 493 | union | ||
| 494 | { | ||
| 495 | gate_c_maxalign_t dummy; | ||
| 496 | char bufferValue[sizeof(value_t)]; | ||
| 497 | char bufferError[sizeof(error_t)]; | ||
| 498 | }; | ||
| 499 | value_t* ptr; // pointer to expected value, or NULL if unexpected | ||
| 500 | #if defined(GATE_DEBUG_MODE) | ||
| 501 | error_t* ptrError; // pointer to unexpected value, or NULL if expected (just for debugging in IDEs) | ||
| 502 | #endif | ||
| 503 | |||
| 504 | 5 | void constructValue(value_t const& expectedValue) | |
| 505 | { | ||
| 506 | 5 | ptr_t p = reinterpret_cast<ptr_t>(&this->bufferValue[0]); | |
| 507 | 5 | copyConstructType(p, expectedValue); | |
| 508 | 5 | this->ptr = p; | |
| 509 | #if defined(GATE_DEBUG_MODE) | ||
| 510 | 5 | this->ptrError = NULL; | |
| 511 | #endif | ||
| 512 | 5 | } | |
| 513 | 2 | void constructError(error_t const& unexpectedValue) | |
| 514 | { | ||
| 515 | 2 | error_t* p = reinterpret_cast<error_t*>(&this->bufferError[0]); | |
| 516 | 2 | copyConstructType(p, unexpectedValue); | |
| 517 | 2 | this->ptr = NULL; | |
| 518 | #if defined(GATE_DEBUG_MODE) | ||
| 519 | 2 | this->ptrError = p; | |
| 520 | #endif | ||
| 521 | 2 | } | |
| 522 | 7 | void destruct() noexcept | |
| 523 | { | ||
| 524 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | if (this->ptr) |
| 525 | { | ||
| 526 | 5 | destructType(this->ptr); | |
| 527 | 5 | this->ptr = NULL; | |
| 528 | } | ||
| 529 | else | ||
| 530 | { | ||
| 531 | 2 | error_t* p = reinterpret_cast<error_t*>(&this->bufferError[0]); | |
| 532 | 2 | destructType(p); | |
| 533 | } | ||
| 534 | 7 | } | |
| 535 | |||
| 536 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 537 | void moveConstruct(GATE_MOVEREF(self_t) src) | ||
| 538 | { | ||
| 539 | if (src.hasValue()) | ||
| 540 | { | ||
| 541 | ptr_t p = reinterpret_cast<ptr_t>(&this->bufferValue[0]); | ||
| 542 | moveConstructType(p, src.value()); | ||
| 543 | this->ptr = p; | ||
| 544 | #if defined(GATE_DEBUG_MODE) | ||
| 545 | this->ptrError = NULL; | ||
| 546 | #endif | ||
| 547 | } | ||
| 548 | else | ||
| 549 | { | ||
| 550 | error_t* p = reinterpret_cast<error_t*>(&this->bufferError[0]); | ||
| 551 | moveConstructType(p, src.error()); | ||
| 552 | this->ptr = NULL; | ||
| 553 | #if defined(GATE_DEBUG_MODE) | ||
| 554 | this->ptrError = p; | ||
| 555 | #endif | ||
| 556 | } | ||
| 557 | } | ||
| 558 | #endif | ||
| 559 | |||
| 560 | public: | ||
| 561 | 2 | Expected(value_t const& expectedValue) | |
| 562 | : dummy() | ||
| 563 | , ptr(NULL) | ||
| 564 | #if defined(GATE_DEBUG_MODE) | ||
| 565 | 2 | , ptrError(NULL) | |
| 566 | #endif | ||
| 567 | { | ||
| 568 | 2 | constructValue(expectedValue); | |
| 569 | 2 | } | |
| 570 | |||
| 571 | 2 | Expected(error_t const& unexpectedValue) | |
| 572 | : dummy() | ||
| 573 | , ptr(NULL) | ||
| 574 | #if defined(GATE_DEBUG_MODE) | ||
| 575 | 2 | , ptrError(NULL) | |
| 576 | #endif | ||
| 577 | { | ||
| 578 | 2 | constructError(unexpectedValue); | |
| 579 | 2 | } | |
| 580 | |||
| 581 | 2 | Expected(self_t const& src) | |
| 582 | { | ||
| 583 |
1/2✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
|
2 | if (src.hasValue()) |
| 584 | { | ||
| 585 | 2 | constructValue(src.value()); | |
| 586 | } | ||
| 587 | else | ||
| 588 | { | ||
| 589 | ✗ | constructError(src.error()); | |
| 590 | } | ||
| 591 | 2 | } | |
| 592 | |||
| 593 | 1 | self_t& operator=(self_t const& src) | |
| 594 | { | ||
| 595 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 596 | { | ||
| 597 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | self_t backup(*this); |
| 598 | 1 | destruct(); | |
| 599 | |||
| 600 |
2/12✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✗ Branch 13 not taken.
✗ Branch 14 not taken.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 21 not taken.
✗ Branch 22 not taken.
|
1 | GATEXX_TRY_CATCHALL( |
| 601 | { | ||
| 602 | if (src.hasValue()) | ||
| 603 | { | ||
| 604 | constructValue(src.value()); | ||
| 605 | } | ||
| 606 | else | ||
| 607 | { | ||
| 608 | constructError(src.error()); | ||
| 609 | } | ||
| 610 | }, | ||
| 611 | { | ||
| 612 | // try to restore backup: | ||
| 613 | if (backup.hasValue()) | ||
| 614 | { | ||
| 615 | constructValue(backup.value()); | ||
| 616 | } | ||
| 617 | else | ||
| 618 | { | ||
| 619 | constructError(backup.error()); | ||
| 620 | } | ||
| 621 | GATEXX_RETHROW; | ||
| 622 | }); | ||
| 623 | } | ||
| 624 | 1 | return *this; | |
| 625 | } | ||
| 626 | |||
| 627 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 628 | Expected(GATE_MOVEREF(self_t) src) | ||
| 629 | { | ||
| 630 | moveConstruct(moveRef(src)); | ||
| 631 | } | ||
| 632 | |||
| 633 | self_t& operator=(GATE_MOVEREF(self_t) src) | ||
| 634 | { | ||
| 635 | if (this != &src) | ||
| 636 | { | ||
| 637 | destruct(); | ||
| 638 | moveConstruct(moveRef(src)); | ||
| 639 | } | ||
| 640 | return *this; | ||
| 641 | } | ||
| 642 | #endif | ||
| 643 | |||
| 644 | 6 | ~Expected() noexcept | |
| 645 | { | ||
| 646 | 6 | this->destruct(); | |
| 647 | 6 | } | |
| 648 | |||
| 649 | 29 | value_t* get() const noexcept | |
| 650 | { | ||
| 651 | 29 | return this->ptr; | |
| 652 | } | ||
| 653 | |||
| 654 | 18 | bool hasValue() const noexcept | |
| 655 | { | ||
| 656 | 18 | return this->get() != NULL; | |
| 657 | } | ||
| 658 | 1 | bool empty() const noexcept | |
| 659 | { | ||
| 660 | 1 | return this->get() == NULL; | |
| 661 | } | ||
| 662 | |||
| 663 | 1 | ref_t value() noexcept | |
| 664 | { | ||
| 665 | 1 | return *this->get(); | |
| 666 | } | ||
| 667 | 4 | const_ref_t value() const noexcept | |
| 668 | { | ||
| 669 | 4 | return *this->get(); | |
| 670 | } | ||
| 671 | 2 | const_ref_t value(const_ref_t defaultValue) const | |
| 672 | { | ||
| 673 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (this->hasValue()) |
| 674 | { | ||
| 675 | 1 | return this->value(); | |
| 676 | } | ||
| 677 | else | ||
| 678 | { | ||
| 679 | 1 | return defaultValue; | |
| 680 | } | ||
| 681 | } | ||
| 682 | |||
| 683 | 3 | error_t* getErrorPtr() const noexcept | |
| 684 | { | ||
| 685 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
|
3 | if (this->hasValue()) |
| 686 | { | ||
| 687 | 1 | return NULL; | |
| 688 | } | ||
| 689 | else | ||
| 690 | { | ||
| 691 | 2 | return const_cast<error_t*>(reinterpret_cast<error_t const*>(&this->bufferError[0])); | |
| 692 | } | ||
| 693 | } | ||
| 694 | 1 | error_t& error() noexcept | |
| 695 | { | ||
| 696 | 1 | return *reinterpret_cast<error_t*>(&this->bufferError[0]); | |
| 697 | } | ||
| 698 | ✗ | error_t const& error() const noexcept | |
| 699 | { | ||
| 700 | ✗ | return *reinterpret_cast<error_t const*>(&this->bufferError[0]); | |
| 701 | } | ||
| 702 | |||
| 703 | 1 | value_t* operator->() noexcept | |
| 704 | { | ||
| 705 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 706 | 1 | return this->get(); | |
| 707 | } | ||
| 708 | value_t const* operator->() const noexcept | ||
| 709 | { | ||
| 710 | GATE_DEBUG_ASSERT(this->hasValue()); | ||
| 711 | return this->get(); | ||
| 712 | } | ||
| 713 | |||
| 714 | 1 | value_t& operator*() noexcept | |
| 715 | { | ||
| 716 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 717 | 1 | return *this->get(); | |
| 718 | } | ||
| 719 | value_t const& operator*() const noexcept | ||
| 720 | { | ||
| 721 | GATE_DEBUG_ASSERT(this->hasValue()); | ||
| 722 | return *this->get(); | ||
| 723 | } | ||
| 724 | |||
| 725 | 1 | bool operator!() const noexcept | |
| 726 | { | ||
| 727 | 1 | return this->empty(); | |
| 728 | } | ||
| 729 | |||
| 730 | 2 | self_t const& andThen(Delegate1<value_t&> const& callback) const | |
| 731 | { | ||
| 732 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (this->hasValue()) |
| 733 | { | ||
| 734 | 1 | callback.invoke(*this->get()); | |
| 735 | } | ||
| 736 | 2 | return *this; | |
| 737 | } | ||
| 738 | 2 | self_t const& orElse(Delegate1<error_t&> const& callback) const | |
| 739 | { | ||
| 740 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (!this->hasValue()) |
| 741 | { | ||
| 742 | 1 | callback.invoke(*this->getErrorPtr()); | |
| 743 | } | ||
| 744 | 2 | return *this; | |
| 745 | } | ||
| 746 | 2 | self_t const& orThrow() const | |
| 747 | { | ||
| 748 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
|
2 | if (!this->hasValue()) |
| 749 | { | ||
| 750 | 1 | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 751 | } | ||
| 752 | 1 | return *this; | |
| 753 | } | ||
| 754 | }; | ||
| 755 | |||
| 756 | |||
| 757 | /// @brief | ||
| 758 | class GATE_CORE_CPP_API WrapperBase | ||
| 759 | { | ||
| 760 | public: | ||
| 761 | static gate_wrapper_t* createCppWrapper(size_t typeSize, cxx_type_info_t const& typeInfo, gate_mem_dtor_t dtor); | ||
| 762 | }; | ||
| 763 | |||
| 764 | template<class T> | ||
| 765 | class GATE_CORE_CPP_TEMPLATE_API Wrapper : public SafeBoolBase< Wrapper<T> > | ||
| 766 | { | ||
| 767 | public: | ||
| 768 | typedef T value_t; | ||
| 769 | typedef T* ptr_t; | ||
| 770 | typedef T const* const_ptr_t; | ||
| 771 | typedef T& ref_t; | ||
| 772 | typedef T const& const_ref_t; | ||
| 773 | typedef Wrapper<T> self_t; | ||
| 774 | |||
| 775 | private: | ||
| 776 | gate_wrapper_t* impl; | ||
| 777 | ptr_t ptr; | ||
| 778 | |||
| 779 | private: | ||
| 780 | 7 | static void GATE_CALL destruct_external_cpp(void* ptr) | |
| 781 | { | ||
| 782 | 7 | T* ptr_external = static_cast<T*>(ptr); | |
| 783 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
7 | delete ptr_external; |
| 784 | 7 | } | |
| 785 | |||
| 786 | public: | ||
| 787 | 2 | Wrapper() | |
| 788 | 2 | : impl(NULL), ptr(NULL) | |
| 789 | { | ||
| 790 | 2 | } | |
| 791 | Wrapper(gate_wrapper_t* wrapper) noexcept | ||
| 792 | : impl(wrapper), ptr(NULL) | ||
| 793 | { | ||
| 794 | if (wrapper != NULL) | ||
| 795 | { | ||
| 796 | void* content_ptr = gate_memoryblock_get_content(wrapper); | ||
| 797 | this->ptr = static_cast<ptr_t>(content_ptr); | ||
| 798 | } | ||
| 799 | } | ||
| 800 | Wrapper(gate_wrapper_t* wrapper, T* ptr_content) | ||
| 801 | : impl(wrapper), ptr(ptr_content) | ||
| 802 | { | ||
| 803 | } | ||
| 804 | 11 | Wrapper(self_t const& src) | |
| 805 | 11 | : impl(src.impl), ptr(src.ptr) | |
| 806 | { | ||
| 807 |
1/2✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
|
11 | if (this->impl) |
| 808 | { | ||
| 809 | 11 | gate_object_retain(this->impl); | |
| 810 | } | ||
| 811 | 11 | } | |
| 812 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 813 | 7 | Wrapper(Ptr<T>&& src) | |
| 814 | #else | ||
| 815 | Wrapper(Ptr<T>& src) | ||
| 816 | #endif | ||
| 817 | : impl(NULL), | ||
| 818 | 7 | ptr(NULL) | |
| 819 | { | ||
| 820 | 7 | ptr_t local_ptr = src.get(); | |
| 821 | 7 | cxx_type_info_t const& ti = get_cxx_type_info<value_t>(); | |
| 822 | 7 | this->impl = gate_wrapper_create(local_ptr, sizeof(value_t), ti.name(), &self_t::destruct_external_cpp, NULL); | |
| 823 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 5 times.
|
7 | if (this->impl == NULL) |
| 824 | { | ||
| 825 | ✗ | GATEXX_RAISE_ERROR(results::OutOfMemory); | |
| 826 | } | ||
| 827 | else | ||
| 828 | { | ||
| 829 | 7 | void* content_ptr = gate_memoryblock_get_content(this->impl); | |
| 830 | 7 | this->ptr = static_cast<ptr_t>(content_ptr); | |
| 831 | 7 | src.release(); | |
| 832 | } | ||
| 833 | 7 | } | |
| 834 | |||
| 835 | 22 | ~Wrapper() noexcept | |
| 836 | { | ||
| 837 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 2 times.
|
22 | if (this->impl) |
| 838 | { | ||
| 839 | 19 | gate_object_release(this->impl); | |
| 840 | } | ||
| 841 | #if defined(GATE_DEBUG_MODE) | ||
| 842 | 22 | this->ptr = NULL; | |
| 843 | 22 | this->impl = NULL; | |
| 844 | #endif | ||
| 845 | 22 | } | |
| 846 | 1 | void swap(self_t& that) | |
| 847 | { | ||
| 848 | 1 | gate::swapRefs(this->impl, that.impl); | |
| 849 | 1 | gate::swapRefs(this->ptr, that.ptr); | |
| 850 | 1 | } | |
| 851 | 1 | self_t& operator=(self_t const& src) | |
| 852 | { | ||
| 853 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (this != &src) |
| 854 | { | ||
| 855 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
2 | self_t that(src); |
| 856 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | this->swap(that); |
| 857 | } | ||
| 858 | 1 | return *this; | |
| 859 | } | ||
| 860 | 21 | bool hasValue() const noexcept | |
| 861 | { | ||
| 862 | 21 | return this->ptr != NULL; | |
| 863 | } | ||
| 864 | 5 | bool empty() const noexcept | |
| 865 | { | ||
| 866 | 5 | return this->ptr == NULL; | |
| 867 | } | ||
| 868 | 1 | bool operator!() const noexcept | |
| 869 | { | ||
| 870 | 1 | return this->ptr == NULL; | |
| 871 | } | ||
| 872 | |||
| 873 | 19 | ptr_t get() const noexcept | |
| 874 | { | ||
| 875 | 19 | return this->ptr; | |
| 876 | } | ||
| 877 | |||
| 878 | 1 | ref_t value() noexcept | |
| 879 | { | ||
| 880 | 1 | return *this->get(); | |
| 881 | } | ||
| 882 | 1 | const_ref_t value() const noexcept | |
| 883 | { | ||
| 884 | 1 | return *this->get(); | |
| 885 | } | ||
| 886 | |||
| 887 | 1 | const_ref_t value(const_ref_t defaultValue) const | |
| 888 | { | ||
| 889 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (this->hasValue()) |
| 890 | { | ||
| 891 | ✗ | return *this->get(); | |
| 892 | } | ||
| 893 | else | ||
| 894 | { | ||
| 895 | 1 | return defaultValue; | |
| 896 | } | ||
| 897 | } | ||
| 898 | |||
| 899 | 1 | void const* type() const noexcept | |
| 900 | { | ||
| 901 | 1 | return gate_wrapper_get_type(this->impl); | |
| 902 | } | ||
| 903 | 1 | bool equalsTypeOf(self_t const& that) const noexcept | |
| 904 | { | ||
| 905 | 1 | return gate_wrapper_is_type(this->impl, that.type()); | |
| 906 | } | ||
| 907 | 2 | size_t size() const noexcept | |
| 908 | { | ||
| 909 | 2 | return sizeof(value_t); | |
| 910 | } | ||
| 911 | |||
| 912 | self_t const& andThen(Delegate1<value_t&> const& callback) const | ||
| 913 | { | ||
| 914 | if (this->hasValue()) | ||
| 915 | { | ||
| 916 | callback.invoke(*this->get()); | ||
| 917 | } | ||
| 918 | return *this; | ||
| 919 | } | ||
| 920 | self_t const& orElse(Delegate0 const& callback) const | ||
| 921 | { | ||
| 922 | if (!this->hasValue()) | ||
| 923 | { | ||
| 924 | callback.invoke(); | ||
| 925 | } | ||
| 926 | return *this; | ||
| 927 | } | ||
| 928 | 1 | self_t const& orThrow() const | |
| 929 | { | ||
| 930 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | if (!this->hasValue()) |
| 931 | { | ||
| 932 | 1 | GATEXX_RAISE_ERROR(results::NullPointer); | |
| 933 | } | ||
| 934 | ✗ | return *this; | |
| 935 | } | ||
| 936 | |||
| 937 | |||
| 938 | 4 | ptr_t operator->() noexcept | |
| 939 | { | ||
| 940 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
|
4 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 941 | 4 | return this->get(); | |
| 942 | } | ||
| 943 | 1 | const_ptr_t operator->() const noexcept | |
| 944 | { | ||
| 945 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 946 | 1 | return this->get(); | |
| 947 | } | ||
| 948 | 1 | ref_t operator*() noexcept | |
| 949 | { | ||
| 950 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 951 | 1 | return *this->get(); | |
| 952 | } | ||
| 953 | 1 | const_ref_t operator*() const noexcept | |
| 954 | { | ||
| 955 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | GATE_DEBUG_ASSERT(this->hasValue()); |
| 956 | 1 | return *this->get(); | |
| 957 | } | ||
| 958 | 1 | bool operator==(self_t const& that) const noexcept | |
| 959 | { | ||
| 960 | 1 | return (this->impl == that.impl); | |
| 961 | } | ||
| 962 | 1 | bool operator!=(self_t const& that) const noexcept | |
| 963 | { | ||
| 964 | 1 | return (this->impl != that.impl); | |
| 965 | } | ||
| 966 | 1 | bool operator<(self_t const& that) const noexcept | |
| 967 | { | ||
| 968 | 1 | return this->impl < that.impl; | |
| 969 | } | ||
| 970 | 1 | bool operator<=(self_t const& that) const noexcept | |
| 971 | { | ||
| 972 | 1 | return this->impl <= that.impl; | |
| 973 | } | ||
| 974 | 1 | bool operator>(self_t const& that) const noexcept | |
| 975 | { | ||
| 976 | 1 | return this->impl > that.impl; | |
| 977 | } | ||
| 978 | 1 | bool operator>=(self_t const& that) const noexcept | |
| 979 | { | ||
| 980 | 1 | return this->impl >= that.impl; | |
| 981 | } | ||
| 982 | |||
| 983 | |||
| 984 | Wrapper(ptr_t ptr_external, gate_mem_dtor_t specialDestructor = NULL) | ||
| 985 | : impl(NULL), ptr(NULL) | ||
| 986 | { | ||
| 987 | void* content_ptr = NULL; | ||
| 988 | cxx_type_info_t const& ti = get_cxx_type_info<value_t>(); | ||
| 989 | this->impl = gate_wrapper_create(ptr_external, sizeof(value_t), ti.name(), | ||
| 990 | specialDestructor == NULL ? &self_t::destruct_external_cpp : specialDestructor, | ||
| 991 | NULL); | ||
| 992 | if (this->impl == NULL) | ||
| 993 | { | ||
| 994 | GATEXX_RAISE_ERROR(results::NullPointer); | ||
| 995 | } | ||
| 996 | else | ||
| 997 | { | ||
| 998 | content_ptr = gate_memoryblock_get_content(this->impl); | ||
| 999 | } | ||
| 1000 | this->ptr = static_cast<ptr_t>(content_ptr); | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | static self_t create(value_t const& src) | ||
| 1004 | { | ||
| 1005 | cxx_type_info_t const& ti = get_cxx_type_info<value_t>(); | ||
| 1006 | gate_wrapper_t* wrapper = gate_wrapper_create_new( | ||
| 1007 | sizeof(value_t), ti.name(), | ||
| 1008 | &TypeFunctions<value_t>::destruct); | ||
| 1009 | void* content_ptr = gate_memoryblock_get_content(wrapper); | ||
| 1010 | GATEXX_TRY_CATCHALL( | ||
| 1011 | { | ||
| 1012 | TypeFunctions<value_t>::copyConstruct(content_ptr, &src); | ||
| 1013 | }, | ||
| 1014 | { | ||
| 1015 | gate_mem_dealloc(wrapper); | ||
| 1016 | GATEXX_RETHROW; | ||
| 1017 | }); | ||
| 1018 | self_t ret; | ||
| 1019 | ret.impl = wrapper; | ||
| 1020 | ret.ptr = static_cast<ptr_t>(content_ptr); | ||
| 1021 | return ret; | ||
| 1022 | } | ||
| 1023 | |||
| 1024 | 1 | static self_t fromPtr(ptr_t ptr) | |
| 1025 | { | ||
| 1026 | 2 | Ptr<T> ptrwrapper(ptr); | |
| 1027 |
1/2✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
1 | self_t ret(gate::moveRef(ptrwrapper)); |
| 1028 | 2 | return ret; | |
| 1029 | } | ||
| 1030 | |||
| 1031 | template<class U> Wrapper<U> staticCastTo() const | ||
| 1032 | { | ||
| 1033 | Wrapper<U> ret(this->impl, static_cast<U*>(this->get())); | ||
| 1034 | if (this->impl) | ||
| 1035 | { | ||
| 1036 | gate_object_retain(this->impl); | ||
| 1037 | } | ||
| 1038 | return ret; | ||
| 1039 | } | ||
| 1040 | |||
| 1041 | template<class U> Wrapper<U> castTo() const | ||
| 1042 | { | ||
| 1043 | Wrapper<U> ret(this->impl, dynamic_cast<U*>(this->get())); | ||
| 1044 | if (this->impl) | ||
| 1045 | { | ||
| 1046 | gate_object_retain(this->impl); | ||
| 1047 | } | ||
| 1048 | return ret; | ||
| 1049 | } | ||
| 1050 | }; | ||
| 1051 | |||
| 1052 | #if __cplusplus >= 201103 | ||
| 1053 | template<class T> | ||
| 1054 | using Ref = Wrapper<T>; | ||
| 1055 | |||
| 1056 | #else | ||
| 1057 | // backwards compatibility with C++03/98 | ||
| 1058 | template<class T> | ||
| 1059 | class Ref : public Wrapper<T> | ||
| 1060 | { | ||
| 1061 | public: | ||
| 1062 | typedef Wrapper<T> base_t; | ||
| 1063 | typedef Ref<T> self_t; | ||
| 1064 | |||
| 1065 | Ref() | ||
| 1066 | : base_t() | ||
| 1067 | { | ||
| 1068 | } | ||
| 1069 | Ref(self_t const& src) | ||
| 1070 | : base_t(src) | ||
| 1071 | { | ||
| 1072 | } | ||
| 1073 | Ref(base_t const& src) | ||
| 1074 | : base_t(src) | ||
| 1075 | { | ||
| 1076 | } | ||
| 1077 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 1078 | Ref(Ptr<T>&& src) | ||
| 1079 | #else | ||
| 1080 | Ref(Ptr<T>& src) | ||
| 1081 | #endif | ||
| 1082 | : base_t(gate::moveRef(src)) | ||
| 1083 | { | ||
| 1084 | } | ||
| 1085 | |||
| 1086 | ~Ref() noexcept | ||
| 1087 | { | ||
| 1088 | } | ||
| 1089 | void swap(self_t& that) | ||
| 1090 | { | ||
| 1091 | base_t::swap(that); | ||
| 1092 | } | ||
| 1093 | self_t& operator=(self_t const& src) | ||
| 1094 | { | ||
| 1095 | if (this != &src) | ||
| 1096 | { | ||
| 1097 | self_t that(src); | ||
| 1098 | this->swap(that); | ||
| 1099 | } | ||
| 1100 | return *this; | ||
| 1101 | } | ||
| 1102 | |||
| 1103 | #if defined(GATE_COMPILER_SUPPORTS_CPP_MOVEREFS) | ||
| 1104 | self_t& operator=(Ptr<T>&& src) | ||
| 1105 | #else | ||
| 1106 | self_t& operator=(Ptr<T>& src) | ||
| 1107 | #endif | ||
| 1108 | { | ||
| 1109 | self_t that(gate::moveRef(src)); | ||
| 1110 | this->swap(that); | ||
| 1111 | return *this; | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | template<class U> Ref<U> staticCastTo() const | ||
| 1115 | { | ||
| 1116 | Wrapper<T> const& w = *this; | ||
| 1117 | #if defined(GATE_COMPILER_WATCOM) | ||
| 1118 | Wrapper<U> u = w.staticCastTo<U>(); | ||
| 1119 | #else | ||
| 1120 | Wrapper<U> u = w.template staticCastTo<U>(); | ||
| 1121 | #endif | ||
| 1122 | return Ref<U>(u); | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | template<class U> Ref<U> castTo() | ||
| 1126 | { | ||
| 1127 | Wrapper<T> const& w = *this; | ||
| 1128 | #if defined(GATE_COMPILER_WATCOM) | ||
| 1129 | Wrapper<U> u = w.castTo<U>(); | ||
| 1130 | #else | ||
| 1131 | Wrapper<U> u = w.template castTo<U>(); | ||
| 1132 | #endif | ||
| 1133 | return Ref<U>(u); | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | }; | ||
| 1137 | |||
| 1138 | #endif | ||
| 1139 | |||
| 1140 | |||
| 1141 | /// @brief | ||
| 1142 | class GATE_CORE_CPP_API Variant | ||
| 1143 | { | ||
| 1144 | protected: | ||
| 1145 | gate_variant_t* impl; | ||
| 1146 | |||
| 1147 | public: | ||
| 1148 | Variant(gate_variant_t* ptrVariant); | ||
| 1149 | Variant(Variant const& src); | ||
| 1150 | Variant& operator=(Variant const& src); | ||
| 1151 | ~Variant() noexcept; | ||
| 1152 | |||
| 1153 | gate_variant_t* c_impl() const; | ||
| 1154 | |||
| 1155 | size_t getSize() const; | ||
| 1156 | void const* getType() const; | ||
| 1157 | |||
| 1158 | 1 | template<class T> bool is() const noexcept | |
| 1159 | { | ||
| 1160 | 1 | cxx_type_info_t const& ti = get_cxx_type_info<T>(); | |
| 1161 | 1 | return (sizeof(T) == gate_variant_get_size(this->impl)) | |
| 1162 |
2/4✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | && (&ti == gate_variant_get_type(this->impl)); |
| 1163 | } | ||
| 1164 | |||
| 1165 | template<class T> T* get() noexcept | ||
| 1166 | { | ||
| 1167 | if (!this->is<T>()) | ||
| 1168 | { | ||
| 1169 | return NULL; | ||
| 1170 | } | ||
| 1171 | return static_cast<T*>(gate_variant_get_content(this->impl)); | ||
| 1172 | } | ||
| 1173 | template<class T> T const* get() const noexcept | ||
| 1174 | { | ||
| 1175 | if (!this->is<T>()) | ||
| 1176 | { | ||
| 1177 | return NULL; | ||
| 1178 | } | ||
| 1179 | return static_cast<T const*>(gate_variant_get_content(this->impl)); | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | template<class T> T& value() | ||
| 1183 | { | ||
| 1184 | T* ptr = this->get<T>(); | ||
| 1185 | if (!ptr) | ||
| 1186 | { | ||
| 1187 | GATEXX_RAISE_ERROR(results::IncorrectType); | ||
| 1188 | } | ||
| 1189 | return *ptr; | ||
| 1190 | } | ||
| 1191 | template<class T> T const& value() const | ||
| 1192 | { | ||
| 1193 | T const* ptr = this->get<T>(); | ||
| 1194 | if (!ptr) | ||
| 1195 | { | ||
| 1196 | GATEXX_RAISE_ERROR(results::IncorrectType); | ||
| 1197 | } | ||
| 1198 | return *ptr; | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | template<class T> void set(T const& value) | ||
| 1202 | { | ||
| 1203 | cxx_type_info_t const& ti = get_cxx_type_info<T>(); | ||
| 1204 | result_t result = gate_variant_set_content(this->impl, &value, sizeof(T), &ti); | ||
| 1205 | GATEXX_CHECK_ERROR(result); | ||
| 1206 | } | ||
| 1207 | }; | ||
| 1208 | |||
| 1209 | |||
| 1210 | |||
| 1211 | template<unsigned N> | ||
| 1212 | struct VariantNoneArg | ||
| 1213 | { | ||
| 1214 | }; | ||
| 1215 | |||
| 1216 | /// @brief | ||
| 1217 | /// @tparam T0 | ||
| 1218 | /// @tparam T1 | ||
| 1219 | /// @tparam T2 | ||
| 1220 | /// @tparam T3 | ||
| 1221 | /// @tparam T4 | ||
| 1222 | /// @tparam T5 | ||
| 1223 | /// @tparam T6 | ||
| 1224 | /// @tparam T7 | ||
| 1225 | /// @tparam T8 | ||
| 1226 | /// @tparam T9 | ||
| 1227 | template<class T0, | ||
| 1228 | class T1 = VariantNoneArg<1>, | ||
| 1229 | class T2 = VariantNoneArg<2>, | ||
| 1230 | class T3 = VariantNoneArg<3>, | ||
| 1231 | class T4 = VariantNoneArg<4>, | ||
| 1232 | class T5 = VariantNoneArg<5>, | ||
| 1233 | class T6 = VariantNoneArg<6>, | ||
| 1234 | class T7 = VariantNoneArg<7>, | ||
| 1235 | class T8 = VariantNoneArg<8>, | ||
| 1236 | class T9 = VariantNoneArg<9> | ||
| 1237 | > | ||
| 1238 | class VariantType | ||
| 1239 | { | ||
| 1240 | public: | ||
| 1241 | typedef VariantType<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> self_t; | ||
| 1242 | private: | ||
| 1243 | size_t type_index; | ||
| 1244 | size_t type_size; | ||
| 1245 | cxx_type_info_t const* ptr_type; | ||
| 1246 | gate_mem_copyctor_t cctor; | ||
| 1247 | gate_mem_dtor_t dtor; | ||
| 1248 | |||
| 1249 | union | ||
| 1250 | { | ||
| 1251 | gate_c_maxalign_t alignment_dummy; | ||
| 1252 | char t0[sizeof(T0)]; | ||
| 1253 | char t1[sizeof(T1)]; | ||
| 1254 | char t2[sizeof(T2)]; | ||
| 1255 | char t3[sizeof(T3)]; | ||
| 1256 | char t4[sizeof(T4)]; | ||
| 1257 | char t5[sizeof(T5)]; | ||
| 1258 | char t6[sizeof(T6)]; | ||
| 1259 | char t7[sizeof(T7)]; | ||
| 1260 | char t8[sizeof(T8)]; | ||
| 1261 | char t9[sizeof(T9)]; | ||
| 1262 | } content; | ||
| 1263 | |||
| 1264 | 2 | template<class U> void copyConstruct(size_t index, U const& src) | |
| 1265 | { | ||
| 1266 | 2 | this->type_index = index; | |
| 1267 | 2 | this->type_size = sizeof(U); | |
| 1268 | 2 | cxx_type_info_t const& ti = get_cxx_type_info<U>(); | |
| 1269 | 2 | this->ptr_type = &ti; | |
| 1270 | 2 | this->cctor = &TypeFunctions<U>::copyConstruct; | |
| 1271 | 2 | this->dtor = &TypeFunctions<U>::destruct; | |
| 1272 | 2 | result_t result = this->cctor(&this->content, &src); | |
| 1273 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
2 | GATEXX_CHECK_ERROR(result); |
| 1274 | 2 | } | |
| 1275 | 1 | void destruct() | |
| 1276 | { | ||
| 1277 | 1 | this->dtor(&this->content); | |
| 1278 | 1 | Mem::clear(this->content); | |
| 1279 | 1 | } | |
| 1280 | ✗ | template<class U> self_t& assign(size_t index, U const& src) | |
| 1281 | { | ||
| 1282 | ✗ | this->destruct(); | |
| 1283 | ✗ | this->copyConstruct(index, src); | |
| 1284 | ✗ | return *this; | |
| 1285 | } | ||
| 1286 | |||
| 1287 | public: | ||
| 1288 | |||
| 1289 | VariantType() { this->copyConstruct(0, T0()); } | ||
| 1290 | VariantType(T0 const& t) { this->copyConstruct(0, t); } | ||
| 1291 | 1 | VariantType(T1 const& t) { this->copyConstruct(1, t); } | |
| 1292 | VariantType(T2 const& t) { this->copyConstruct(2, t); } | ||
| 1293 | VariantType(T3 const& t) { this->copyConstruct(3, t); } | ||
| 1294 | VariantType(T4 const& t) { this->copyConstruct(4, t); } | ||
| 1295 | VariantType(T5 const& t) { this->copyConstruct(5, t); } | ||
| 1296 | VariantType(T6 const& t) { this->copyConstruct(6, t); } | ||
| 1297 | VariantType(T7 const& t) { this->copyConstruct(7, t); } | ||
| 1298 | VariantType(T8 const& t) { this->copyConstruct(8, t); } | ||
| 1299 | VariantType(T9 const& t) { this->copyConstruct(9, t); } | ||
| 1300 | |||
| 1301 | VariantType(self_t const& that) | ||
| 1302 | : type_index(that.type_index), | ||
| 1303 | type_size(that.type_size), | ||
| 1304 | ptr_type(that.ptr_type), | ||
| 1305 | cctor(that.cctor), | ||
| 1306 | dtor(that.dtor) | ||
| 1307 | { | ||
| 1308 | result_t result = this->cctor(&this->content, &that.content); | ||
| 1309 | GATEXX_CHECK_ERROR(result); | ||
| 1310 | } | ||
| 1311 | |||
| 1312 | self_t& operator=(self_t const& that) | ||
| 1313 | { | ||
| 1314 | if (this != &that) | ||
| 1315 | { | ||
| 1316 | this->destruct(); | ||
| 1317 | this->type_index = that.type_index; | ||
| 1318 | this->type_size = that.type_size; | ||
| 1319 | this->ptr_type = that.ptr_type; | ||
| 1320 | this->cctor = that.cctor; | ||
| 1321 | this->dtor = that.dtor; | ||
| 1322 | result_t result = this->cctor(&this->content, &that.content); | ||
| 1323 | GATEXX_CHECK_ERROR(result); | ||
| 1324 | } | ||
| 1325 | return *this; | ||
| 1326 | } | ||
| 1327 | |||
| 1328 | ✗ | self_t& operator=(T0 const& t) { return this->assign(0, t); } | |
| 1329 | ✗ | self_t& operator=(T1 const& t) { return this->assign(1, t); } | |
| 1330 | ✗ | self_t& operator=(T2 const& t) { return this->assign(2, t); } | |
| 1331 | ✗ | self_t& operator=(T3 const& t) { return this->assign(3, t); } | |
| 1332 | ✗ | self_t& operator=(T4 const& t) { return this->assign(4, t); } | |
| 1333 | ✗ | self_t& operator=(T5 const& t) { return this->assign(5, t); } | |
| 1334 | ✗ | self_t& operator=(T6 const& t) { return this->assign(6, t); } | |
| 1335 | ✗ | self_t& operator=(T7 const& t) { return this->assign(7, t); } | |
| 1336 | ✗ | self_t& operator=(T8 const& t) { return this->assign(8, t); } | |
| 1337 | ✗ | self_t& operator=(T9 const& t) { return this->assign(9, t); } | |
| 1338 | |||
| 1339 | 1 | ~VariantType() | |
| 1340 | { | ||
| 1341 | 1 | destruct(); | |
| 1342 | 1 | } | |
| 1343 | |||
| 1344 | 2 | cxx_type_info_t const& getTypeInfo() const | |
| 1345 | { | ||
| 1346 | 2 | return *this->ptr_type; | |
| 1347 | } | ||
| 1348 | 2 | size_t size() const | |
| 1349 | { | ||
| 1350 | 2 | return this->type_size; | |
| 1351 | } | ||
| 1352 | |||
| 1353 | template<class U> bool is() const | ||
| 1354 | { | ||
| 1355 | cxx_type_info_t const& ti = get_cxx_type_info<U>(); | ||
| 1356 | return this->getTypeInfo() == &ti; | ||
| 1357 | } | ||
| 1358 | |||
| 1359 | template<class U> U* get() | ||
| 1360 | { | ||
| 1361 | if (this->is<U>()) | ||
| 1362 | { | ||
| 1363 | return NULL; | ||
| 1364 | |||
| 1365 | } | ||
| 1366 | return reinterpret_cast<U*>(&this->content); | ||
| 1367 | } | ||
| 1368 | template<class U> U const* get() const | ||
| 1369 | { | ||
| 1370 | if (!this->is<U>()) | ||
| 1371 | { | ||
| 1372 | return NULL; | ||
| 1373 | |||
| 1374 | } | ||
| 1375 | return reinterpret_cast<U const*>(&this->content); | ||
| 1376 | } | ||
| 1377 | |||
| 1378 | ✗ | void* getContent() | |
| 1379 | { | ||
| 1380 | ✗ | return static_cast<void*>(&this->content); | |
| 1381 | } | ||
| 1382 | |||
| 1383 | size_t getIndex() const | ||
| 1384 | { | ||
| 1385 | return this->type_index; | ||
| 1386 | } | ||
| 1387 | |||
| 1388 | template<class U> U& value() | ||
| 1389 | { | ||
| 1390 | if (!this->is<U>()) | ||
| 1391 | { | ||
| 1392 | GATEXX_RAISE_ERROR(results::IncorrectType); | ||
| 1393 | } | ||
| 1394 | return *reinterpret_cast<U*>(&this->content); | ||
| 1395 | } | ||
| 1396 | |||
| 1397 | template<class U> U const& value() const | ||
| 1398 | { | ||
| 1399 | if (!this->is<U>()) | ||
| 1400 | { | ||
| 1401 | GATEXX_RAISE_ERROR(results::IncorrectType); | ||
| 1402 | } | ||
| 1403 | return *reinterpret_cast<U const*>(&this->content); | ||
| 1404 | } | ||
| 1405 | |||
| 1406 | ✗ | void set(cxx_type_info_t const* ti, void const* ptrContent) | |
| 1407 | { | ||
| 1408 | ✗ | if (ti == &get_cxx_type_info<T0>()) { *this = *static_cast<T0 const*>(ptrContent); } | |
| 1409 | ✗ | else if (ti == &get_cxx_type_info<T1>()) { *this = *static_cast<T1 const*>(ptrContent); } | |
| 1410 | ✗ | else if (ti == &get_cxx_type_info<T2>()) { *this = *static_cast<T2 const*>(ptrContent); } | |
| 1411 | ✗ | else if (ti == &get_cxx_type_info<T3>()) { *this = *static_cast<T3 const*>(ptrContent); } | |
| 1412 | ✗ | else if (ti == &get_cxx_type_info<T4>()) { *this = *static_cast<T4 const*>(ptrContent); } | |
| 1413 | ✗ | else if (ti == &get_cxx_type_info<T5>()) { *this = *static_cast<T5 const*>(ptrContent); } | |
| 1414 | ✗ | else if (ti == &get_cxx_type_info<T6>()) { *this = *static_cast<T6 const*>(ptrContent); } | |
| 1415 | ✗ | else if (ti == &get_cxx_type_info<T7>()) { *this = *static_cast<T7 const*>(ptrContent); } | |
| 1416 | ✗ | else if (ti == &get_cxx_type_info<T8>()) { *this = *static_cast<T8 const*>(ptrContent); } | |
| 1417 | ✗ | else if (ti == &get_cxx_type_info<T9>()) { *this = *static_cast<T9 const*>(ptrContent); } | |
| 1418 | ✗ | else { GATEXX_RAISE_ERROR(results::IncorrectType); } | |
| 1419 | ✗ | } | |
| 1420 | template<class U> void set(U const& content) | ||
| 1421 | { | ||
| 1422 | cxx_type_info_t const& ti = get_cxx_type_info<U>(); | ||
| 1423 | this->set(&ti, &content); | ||
| 1424 | } | ||
| 1425 | }; | ||
| 1426 | |||
| 1427 | |||
| 1428 | |||
| 1429 | |||
| 1430 | /// @brief | ||
| 1431 | /// @tparam T0 | ||
| 1432 | /// @tparam T1 | ||
| 1433 | /// @tparam T2 | ||
| 1434 | /// @tparam T3 | ||
| 1435 | /// @tparam T4 | ||
| 1436 | /// @tparam T5 | ||
| 1437 | /// @tparam T6 | ||
| 1438 | /// @tparam T7 | ||
| 1439 | /// @tparam T8 | ||
| 1440 | /// @tparam T9 | ||
| 1441 | template<class T0, | ||
| 1442 | class T1 = VariantNoneArg<1>, | ||
| 1443 | class T2 = VariantNoneArg<2>, | ||
| 1444 | class T3 = VariantNoneArg<3>, | ||
| 1445 | class T4 = VariantNoneArg<4>, | ||
| 1446 | class T5 = VariantNoneArg<5>, | ||
| 1447 | class T6 = VariantNoneArg<6>, | ||
| 1448 | class T7 = VariantNoneArg<7>, | ||
| 1449 | class T8 = VariantNoneArg<8>, | ||
| 1450 | class T9 = VariantNoneArg<9> | ||
| 1451 | > | ||
| 1452 | class VariantFactory | ||
| 1453 | { | ||
| 1454 | public: | ||
| 1455 | typedef VariantFactory<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> self_t; | ||
| 1456 | typedef VariantType<T0, T1, T2, T3, T4, T5, T6, T7, T8, T9> variant_type_t; | ||
| 1457 | |||
| 1458 | private: | ||
| 1459 | struct VariantImpl : gate_variant_t | ||
| 1460 | { | ||
| 1461 | gate_variant_vtbl_t vtbl_impl; | ||
| 1462 | gate_atomic_int_t ref_counter; | ||
| 1463 | |||
| 1464 | variant_type_t var; | ||
| 1465 | }; | ||
| 1466 | |||
| 1467 | ✗ | static char const* get_interface_name_impl(void* obj) | |
| 1468 | { | ||
| 1469 | ✗ | return GATE_INTERFACE_NAME_VARIANT; | |
| 1470 | } | ||
| 1471 | 4 | static void release_impl(void* obj) | |
| 1472 | { | ||
| 1473 | 4 | VariantImpl* self = (VariantImpl*)obj; | |
| 1474 |
2/2✓ Branch 1 taken 1 times.
✓ Branch 2 taken 3 times.
|
4 | if (gate_atomic_int_dec(&self->ref_counter) == 0) |
| 1475 | { | ||
| 1476 | 1 | destructType(&self->var); | |
| 1477 | 1 | gate_mem_dealloc(self); | |
| 1478 | } | ||
| 1479 | 4 | } | |
| 1480 | 3 | static int retain_impl(void* obj) | |
| 1481 | { | ||
| 1482 | 3 | VariantImpl* self = (VariantImpl*)obj; | |
| 1483 | 3 | return gate_atomic_int_inc(&self->ref_counter); | |
| 1484 | } | ||
| 1485 | 2 | static gate_size_t get_size_impl(void* obj) | |
| 1486 | { | ||
| 1487 | 2 | VariantImpl* self = (VariantImpl*)obj; | |
| 1488 | 2 | return self->var.size(); | |
| 1489 | } | ||
| 1490 | ✗ | static void* get_content_impl(void* obj) | |
| 1491 | { | ||
| 1492 | ✗ | VariantImpl* self = (VariantImpl*)obj; | |
| 1493 | ✗ | return self->var.getContent(); | |
| 1494 | } | ||
| 1495 | 2 | static void const* get_type_impl(void* obj) | |
| 1496 | { | ||
| 1497 | 2 | VariantImpl* self = (VariantImpl*)obj; | |
| 1498 | 2 | return &self->var.getTypeInfo(); | |
| 1499 | } | ||
| 1500 | ✗ | static gate_bool_t is_type_impl(void* obj, void const* compare_with) | |
| 1501 | { | ||
| 1502 | ✗ | VariantImpl* self = (VariantImpl*)obj; | |
| 1503 | ✗ | cxx_type_info_t const* ptrType = static_cast<cxx_type_info_t const*>(compare_with); | |
| 1504 | ✗ | return &self->var.getTypeInfo() == ptrType; | |
| 1505 | } | ||
| 1506 | ✗ | static gate_result_t set_content_impl(void* obj, void const* ptr_content, gate_size_t size, void const* ptr_type) | |
| 1507 | { | ||
| 1508 | ✗ | VariantImpl* self = (VariantImpl*)obj; | |
| 1509 | (void)size; | ||
| 1510 | ✗ | self->var.set(static_cast<cxx_type_info_t const*>(ptr_type), ptr_content); | |
| 1511 | ✗ | return GATE_RESULT_NOTSUPPORTED; | |
| 1512 | } | ||
| 1513 | |||
| 1514 | template<class V> | ||
| 1515 | 1 | static VariantImpl* createImpl(V const& value) | |
| 1516 | { | ||
| 1517 | 1 | VariantImpl* ptr = static_cast<VariantImpl*>(gate_mem_alloc(sizeof(VariantImpl))); | |
| 1518 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (!ptr) |
| 1519 | { | ||
| 1520 | ✗ | return NULL; | |
| 1521 | } | ||
| 1522 | 1 | ptr->vtbl = &ptr->vtbl_impl; | |
| 1523 | 1 | gate_atomic_int_init(&ptr->ref_counter, 1); | |
| 1524 | |||
| 1525 | 1 | ptr->vtbl_impl.get_interface_name = &get_interface_name_impl; | |
| 1526 | 1 | ptr->vtbl_impl.release = &release_impl; | |
| 1527 | 1 | ptr->vtbl_impl.retain = &retain_impl; | |
| 1528 | 1 | ptr->vtbl_impl.get_size = &get_size_impl; | |
| 1529 | 1 | ptr->vtbl_impl.get_content = &get_content_impl; | |
| 1530 | 1 | ptr->vtbl_impl.get_type = &get_type_impl; | |
| 1531 | 1 | ptr->vtbl_impl.is_type = &is_type_impl; | |
| 1532 | 1 | ptr->vtbl_impl.set_content = &set_content_impl; | |
| 1533 | |||
| 1534 |
1/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
|
1 | GATEXX_TRY_CATCHALL( |
| 1535 | { | ||
| 1536 | new(&ptr->var) variant_type_t(value); | ||
| 1537 | }, | ||
| 1538 | { | ||
| 1539 | gate_mem_dealloc(ptr); | ||
| 1540 | GATEXX_RETHROW; | ||
| 1541 | }); | ||
| 1542 | 1 | return ptr; | |
| 1543 | } | ||
| 1544 | |||
| 1545 | public: | ||
| 1546 | |||
| 1547 | static Variant create(T0 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1548 | 1 | static Variant create(T1 const& value) { return Variant(self_t::createImpl(value)); } | |
| 1549 | static Variant create(T2 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1550 | static Variant create(T3 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1551 | static Variant create(T4 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1552 | static Variant create(T5 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1553 | static Variant create(T6 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1554 | static Variant create(T7 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1555 | static Variant create(T8 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1556 | static Variant create(T9 const& value) { return Variant(self_t::createImpl(value)); } | ||
| 1557 | }; | ||
| 1558 | |||
| 1559 | |||
| 1560 | } // end of namespace gate | ||
| 1561 | |||
| 1562 | #endif | ||
| 1563 |