| 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 Object interface wrappers, utilities and implementations | ||
| 31 | * @ingroup gatecore_cpp | ||
| 32 | */ | ||
| 33 | |||
| 34 | |||
| 35 | #ifndef GATE_OBJECTS_HPP_INCLUDED | ||
| 36 | #define GATE_OBJECTS_HPP_INCLUDED | ||
| 37 | |||
| 38 | #include "gate/gate_core_api.hpp" | ||
| 39 | #include "gate/objects.h" | ||
| 40 | #include "gate/gatetypes.hpp" | ||
| 41 | #include "gate/strings.hpp" | ||
| 42 | #include "gate/memalloc.hpp" | ||
| 43 | #include "gate/atomics.hpp" | ||
| 44 | |||
| 45 | namespace gate | ||
| 46 | { | ||
| 47 | |||
| 48 | /// @brief | ||
| 49 | class GATE_CORE_CPP_API Object | ||
| 50 | { | ||
| 51 | public: | ||
| 52 | Object(gate_object_ptr_t obj_ptr) noexcept; // takes ownership of an object pointer | ||
| 53 | Object(Object const& obj) noexcept; | ||
| 54 | Object& operator=(Object const& obj) noexcept; | ||
| 55 | ~Object() noexcept; | ||
| 56 | |||
| 57 | String getInterfaceName(); | ||
| 58 | bool_t implementsInterface(char const* ifaceName); | ||
| 59 | gate_object_ptr_t c_impl() noexcept; | ||
| 60 | |||
| 61 | bool_t operator!() const noexcept; | ||
| 62 | bool_t empty() const noexcept; | ||
| 63 | |||
| 64 | protected: | ||
| 65 | void swap(Object& that) noexcept; | ||
| 66 | |||
| 67 | private: | ||
| 68 | gate_object_ptr_t object_ref_ptr; | ||
| 69 | }; | ||
| 70 | |||
| 71 | |||
| 72 | /// @brief | ||
| 73 | /// @tparam T | ||
| 74 | template<class T> | ||
| 75 | class GATE_CORE_CPP_TEMPLATE_API ObjectImpl : public Object | ||
| 76 | { | ||
| 77 | public: | ||
| 78 | typedef T impl_t; | ||
| 79 | typedef ObjectImpl<T> object_impl_t; | ||
| 80 | |||
| 81 | protected: | ||
| 82 | impl_t* impl_ptr; | ||
| 83 | |||
| 84 | protected: | ||
| 85 | 6 | void swap(object_impl_t& that) noexcept | |
| 86 | { | ||
| 87 | 6 | Object::swap(that); | |
| 88 | 6 | swapRefs(this->impl_ptr, that.impl_ptr); | |
| 89 | 6 | } | |
| 90 | |||
| 91 | public: | ||
| 92 | 626 | ObjectImpl(impl_t* impl) noexcept | |
| 93 | 626 | : Object((gate_object_ptr_t)impl), impl_ptr(impl) | |
| 94 | { | ||
| 95 | 626 | } | |
| 96 | 276 | ObjectImpl(ObjectImpl const& src) noexcept | |
| 97 | 276 | : Object(src), impl_ptr(src.impl_ptr) | |
| 98 | { | ||
| 99 | 276 | } | |
| 100 | 48 | ObjectImpl& operator=(ObjectImpl const& that) noexcept | |
| 101 | { | ||
| 102 | 48 | Object::operator=(that); | |
| 103 | 48 | this->impl_ptr = that.impl_ptr; | |
| 104 | 48 | return *this; | |
| 105 | } | ||
| 106 | 949 | ~ObjectImpl() noexcept | |
| 107 | { | ||
| 108 | 949 | } | |
| 109 | 918 | impl_t* c_impl() const noexcept | |
| 110 | { | ||
| 111 | 918 | return this->impl_ptr; | |
| 112 | } | ||
| 113 | }; | ||
| 114 | |||
| 115 | |||
| 116 | /// @brief | ||
| 117 | class GATE_CORE_CPP_API IObject | ||
| 118 | { | ||
| 119 | public: | ||
| 120 | virtual ~IObject(); | ||
| 121 | |||
| 122 | virtual void release() noexcept = 0; | ||
| 123 | virtual int retain() noexcept = 0; | ||
| 124 | virtual String getInterfaceName() noexcept = 0; | ||
| 125 | }; | ||
| 126 | |||
| 127 | |||
| 128 | /// @brief | ||
| 129 | /// @tparam ObjectStruct | ||
| 130 | /// @tparam VtblStruct | ||
| 131 | template<class ObjectStruct, class VtblStruct> | ||
| 132 | class GATE_CORE_CPP_TEMPLATE_API IObjectBuilder : public IObject | ||
| 133 | { | ||
| 134 | public: | ||
| 135 | typedef ObjectStruct obj_struct_t; | ||
| 136 | typedef VtblStruct obj_vtbl_t; | ||
| 137 | |||
| 138 | typedef IObjectBuilder<obj_struct_t, obj_vtbl_t> ObjectBuilder; | ||
| 139 | |||
| 140 | 32 | virtual ~IObjectBuilder() | |
| 141 | { | ||
| 142 | 32 | } | |
| 143 | |||
| 144 | 43 | virtual void release() noexcept override | |
| 145 | { | ||
| 146 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 20 times.
|
43 | if (--this->refCounter == 0) |
| 147 | { | ||
| 148 |
1/2✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
|
16 | delete this; |
| 149 | } | ||
| 150 | 43 | } | |
| 151 | 27 | virtual int retain() noexcept override | |
| 152 | { | ||
| 153 | 27 | return ++this->refCounter; | |
| 154 | } | ||
| 155 | ✗ | virtual String getInterfaceName() noexcept override | |
| 156 | { | ||
| 157 | ✗ | return this->interfaceName; | |
| 158 | } | ||
| 159 | |||
| 160 | protected: | ||
| 161 | struct GATE_CORE_CPP_TEMPLATE_API cpp_object : public obj_struct_t | ||
| 162 | { | ||
| 163 | ObjectBuilder* const parent; | ||
| 164 | |||
| 165 | 31 | cpp_object(obj_vtbl_t const* vtbl_ptr, ObjectBuilder* parent_ptr) | |
| 166 | 31 | : parent(parent_ptr) | |
| 167 | { | ||
| 168 | 31 | this->vtbl = vtbl_ptr; | |
| 169 | 31 | } | |
| 170 | }; | ||
| 171 | |||
| 172 | 65 | static void object_release(void* obj_ptr) noexcept | |
| 173 | { | ||
| 174 | 65 | cpp_object* const obj = static_cast<cpp_object*>(obj_ptr); | |
| 175 | 65 | obj->parent->release(); | |
| 176 | 65 | } | |
| 177 | 40 | static int object_retain(void* obj_ptr) noexcept | |
| 178 | { | ||
| 179 | 40 | cpp_object* const obj = static_cast<cpp_object*>(obj_ptr); | |
| 180 | 40 | return obj->parent->retain(); | |
| 181 | } | ||
| 182 | ✗ | static char const* object_get_interface_name(void* obj_ptr) noexcept | |
| 183 | { | ||
| 184 | ✗ | cpp_object* const obj = static_cast<cpp_object*>(obj_ptr); | |
| 185 | ✗ | return obj->parent->interfaceName.c_str(); | |
| 186 | } | ||
| 187 | |||
| 188 | 54 | template<class T> static T* getInterface(cpp_object* obj) | |
| 189 | { | ||
| 190 | 54 | T* ret = NULL; | |
| 191 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
54 | if (obj) |
| 192 | { | ||
| 193 | #if defined(GATEXX_NO_RTTI) | ||
| 194 | // TODO | ||
| 195 | ret = static_cast<T*>(obj->parent); | ||
| 196 | #else | ||
| 197 |
1/2✓ Branch 0 taken 27 times.
✗ Branch 1 not taken.
|
54 | ret = dynamic_cast<T*>(obj->parent); |
| 198 | #endif | ||
| 199 | } | ||
| 200 | 54 | return ret; | |
| 201 | } | ||
| 202 | |||
| 203 | |||
| 204 | struct GATE_CORE_CPP_TEMPLATE_API VtblObject : public obj_vtbl_t | ||
| 205 | { | ||
| 206 | 33 | VtblObject() | |
| 207 | { | ||
| 208 | 33 | GATE_INTERFACE_VTBL(gate_object)* this_ptr = reinterpret_cast<GATE_INTERFACE_VTBL(gate_object)*>(this); | |
| 209 | 33 | this_ptr->release = &object_release; | |
| 210 | 33 | this_ptr->retain = &object_retain; | |
| 211 | 33 | this_ptr->get_interface_name = &object_get_interface_name; | |
| 212 | 33 | } | |
| 213 | }; | ||
| 214 | |||
| 215 | private: | ||
| 216 | cpp_object c_interface; | ||
| 217 | AtomicInt refCounter; | ||
| 218 | String interfaceName; | ||
| 219 | |||
| 220 | protected: | ||
| 221 | 31 | IObjectBuilder(obj_vtbl_t const* vtbl_ptr, char const* name) | |
| 222 | 31 | : c_interface(vtbl_ptr, this), refCounter(1), interfaceName(String::createStatic(name)) | |
| 223 | { | ||
| 224 | //this->c_interface.parent = this; | ||
| 225 | 31 | } | |
| 226 | |||
| 227 | public: | ||
| 228 | 13 | virtual ObjectStruct* c_impl() | |
| 229 | { | ||
| 230 | 13 | return &this->c_interface; | |
| 231 | } | ||
| 232 | |||
| 233 | }; | ||
| 234 | |||
| 235 | |||
| 236 | /// @brief | ||
| 237 | class GATE_CORE_CPP_API Startable : public ObjectImpl<gate_startable_t> | ||
| 238 | { | ||
| 239 | public: | ||
| 240 | Startable(gate_startable_t* obj); | ||
| 241 | Startable(Startable const& src); | ||
| 242 | Startable& operator=(Startable const& src); | ||
| 243 | |||
| 244 | void start(); | ||
| 245 | void stop(); | ||
| 246 | enumint_t getStatus(); | ||
| 247 | }; | ||
| 248 | |||
| 249 | |||
| 250 | /// @brief | ||
| 251 | class GATE_CORE_CPP_API MemoryBlock : public ObjectImpl<gate_memoryblock_t> | ||
| 252 | { | ||
| 253 | public: | ||
| 254 | MemoryBlock(void const* sourceBlockBytes, size_t blockLength); | ||
| 255 | MemoryBlock(size_t blockLength = 0); | ||
| 256 | |||
| 257 | MemoryBlock(gate_memoryblock_t* obj); | ||
| 258 | MemoryBlock(MemoryBlock const& src); | ||
| 259 | MemoryBlock& operator=(MemoryBlock const& src); | ||
| 260 | |||
| 261 | size_t getSize() const; | ||
| 262 | void* getContent(); | ||
| 263 | void const* getContent() const; | ||
| 264 | |||
| 265 | intptr_t compare(MemoryBlock const& block) const; | ||
| 266 | intptr_t compare(void const* block, size_t blockLength) const; | ||
| 267 | |||
| 268 | bool equals(MemoryBlock const& block) const; | ||
| 269 | bool equals(void const* block, size_t blockLength) const; | ||
| 270 | |||
| 271 | }; | ||
| 272 | |||
| 273 | |||
| 274 | /// @brief | ||
| 275 | /// @tparam T | ||
| 276 | template<class T> | ||
| 277 | class GATE_CORE_CPP_TEMPLATE_API TypedMemoryBlock : public MemoryBlock | ||
| 278 | { | ||
| 279 | public: | ||
| 280 | typedef TypedMemoryBlock<T> self_t; | ||
| 281 | |||
| 282 | TypedMemoryBlock() | ||
| 283 | { | ||
| 284 | } | ||
| 285 | |||
| 286 | TypedMemoryBlock(self_t const& src) | ||
| 287 | : MemoryBlock(src) | ||
| 288 | { | ||
| 289 | } | ||
| 290 | |||
| 291 | self_t& operator=(self_t const& src) | ||
| 292 | { | ||
| 293 | MemoryBlock::operator=(src); | ||
| 294 | return *this; | ||
| 295 | } | ||
| 296 | |||
| 297 | T* getContent() | ||
| 298 | { | ||
| 299 | return static_cast<T*>(MemoryBlock::getContent()); | ||
| 300 | } | ||
| 301 | |||
| 302 | T const* getContent() const | ||
| 303 | { | ||
| 304 | return static_cast<T const*>(MemoryBlock::getContent()); | ||
| 305 | } | ||
| 306 | |||
| 307 | bool equals(MemoryBlock const& block) const; | ||
| 308 | bool equals(void const* block, size_t blockLength) const; | ||
| 309 | |||
| 310 | |||
| 311 | T* operator->() | ||
| 312 | { | ||
| 313 | return this->getContent(); | ||
| 314 | } | ||
| 315 | T const* operator->() const | ||
| 316 | { | ||
| 317 | return this->getContent(); | ||
| 318 | } | ||
| 319 | |||
| 320 | T& operator*() | ||
| 321 | { | ||
| 322 | return *this->getContent(); | ||
| 323 | } | ||
| 324 | T const& operator*() const | ||
| 325 | { | ||
| 326 | return *this->getContent(); | ||
| 327 | } | ||
| 328 | }; | ||
| 329 | |||
| 330 | } // end of namespace gate | ||
| 331 | |||
| 332 | #endif | ||
| 333 |