0.2.0-dev Download ZIP
90 lines ·3.5 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* mono_gc_handle.h */ |
| 3 | /**************************************************************************/ |
| 4 | /* This file is part of: */ |
| 5 | /* GODOT ENGINE */ |
| 6 | /* https://godotengine.org */ |
| 7 | /**************************************************************************/ |
| 8 | /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 10 | /* */ |
| 11 | /* Permission is hereby granted, free of charge, to any person obtaining */ |
| 12 | /* a copy of this software and associated documentation files (the */ |
| 13 | /* "Software"), to deal in the Software without restriction, including */ |
| 14 | /* without limitation the rights to use, copy, modify, merge, publish, */ |
| 15 | /* distribute, sublicense, and/or sell copies of the Software, and to */ |
| 16 | /* permit persons to whom the Software is furnished to do so, subject to */ |
| 17 | /* the following conditions: */ |
| 18 | /* */ |
| 19 | /* The above copyright notice and this permission notice shall be */ |
| 20 | /* included in all copies or substantial portions of the Software. */ |
| 21 | /* */ |
| 22 | /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ |
| 23 | /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ |
| 24 | /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ |
| 25 | /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ |
| 26 | /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ |
| 27 | /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ |
| 28 | /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ |
| 29 | /**************************************************************************/ |
| 30 | |
| 31 | #pragma once |
| 32 | |
| 33 | #include "core/typedefs.h" |
| 34 | |
| 35 | #ifdef DEBUG_ENABLED |
| 36 | #include "core/error/error_macros.h" |
| 37 | #endif |
| 38 | |
| 39 | namespace gdmono { |
| 40 | |
| 41 | enum class GCHandleType : char { |
| 42 | NIL, |
| 43 | STRONG_HANDLE, |
| 44 | WEAK_HANDLE |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | extern "C" { |
| 49 | struct GCHandleIntPtr { |
| 50 | void *value; |
| 51 | |
| 52 | _FORCE_INLINE_ bool operator==(const GCHandleIntPtr &p_other) { return value == p_other.value; } |
| 53 | _FORCE_INLINE_ bool operator!=(const GCHandleIntPtr &p_other) { return value != p_other.value; } |
| 54 | |
| 55 | GCHandleIntPtr() = delete; |
| 56 | }; |
| 57 | } |
| 58 | |
| 59 | static_assert(sizeof(GCHandleIntPtr) == sizeof(void *)); |
| 60 | |
| 61 | // Manual release of the GC handle must be done when using this struct |
| 62 | struct MonoGCHandleData { |
| 63 | GCHandleIntPtr handle = { nullptr }; |
| 64 | gdmono::GCHandleType type = gdmono::GCHandleType::NIL; |
| 65 | |
| 66 | _FORCE_INLINE_ bool is_released() const { return !handle.value; } |
| 67 | _FORCE_INLINE_ bool is_weak() const { return type == gdmono::GCHandleType::WEAK_HANDLE; } |
| 68 | _FORCE_INLINE_ GCHandleIntPtr get_intptr() const { return handle; } |
| 69 | |
| 70 | void release(); |
| 71 | |
| 72 | static void free_gchandle(GCHandleIntPtr p_gchandle); |
| 73 | |
| 74 | void operator=(const MonoGCHandleData &p_other) { |
| 75 | #ifdef DEBUG_ENABLED |
| 76 | CRASH_COND(!is_released()); |
| 77 | #endif |
| 78 | handle = p_other.handle; |
| 79 | type = p_other.type; |
| 80 | } |
| 81 | |
| 82 | MonoGCHandleData(const MonoGCHandleData &) = default; |
| 83 | |
| 84 | MonoGCHandleData() {} |
| 85 | |
| 86 | MonoGCHandleData(GCHandleIntPtr p_handle, gdmono::GCHandleType p_type) : |
| 87 | handle(p_handle), |
| 88 | type(p_type) { |
| 89 | } |
| 90 | }; |