mono_gc_handle.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. #ifndef MONO_GC_HANDLE_H
  31. #define MONO_GC_HANDLE_H
  32. #include "core/object/ref_counted.h"
  33. namespace gdmono {
  34. enum class GCHandleType : char {
  35. NIL,
  36. STRONG_HANDLE,
  37. WEAK_HANDLE
  38. };
  39. }
  40. extern "C" {
  41. struct GCHandleIntPtr {
  42. void *value;
  43. _FORCE_INLINE_ bool operator==(const GCHandleIntPtr &p_other) { return value == p_other.value; }
  44. _FORCE_INLINE_ bool operator!=(const GCHandleIntPtr &p_other) { return value != p_other.value; }
  45. GCHandleIntPtr() = delete;
  46. };
  47. }
  48. static_assert(sizeof(GCHandleIntPtr) == sizeof(void *));
  49. // Manual release of the GC handle must be done when using this struct
  50. struct MonoGCHandleData {
  51. GCHandleIntPtr handle = { nullptr };
  52. gdmono::GCHandleType type = gdmono::GCHandleType::NIL;
  53. _FORCE_INLINE_ bool is_released() const { return !handle.value; }
  54. _FORCE_INLINE_ bool is_weak() const { return type == gdmono::GCHandleType::WEAK_HANDLE; }
  55. _FORCE_INLINE_ GCHandleIntPtr get_intptr() const { return handle; }
  56. void release();
  57. static void free_gchandle(GCHandleIntPtr p_gchandle);
  58. void operator=(const MonoGCHandleData &p_other) {
  59. #ifdef DEBUG_ENABLED
  60. CRASH_COND(!is_released());
  61. #endif
  62. handle = p_other.handle;
  63. type = p_other.type;
  64. }
  65. MonoGCHandleData(const MonoGCHandleData &) = default;
  66. MonoGCHandleData() {}
  67. MonoGCHandleData(GCHandleIntPtr p_handle, gdmono::GCHandleType p_type) :
  68. handle(p_handle),
  69. type(p_type) {
  70. }
  71. };
  72. #endif // MONO_GC_HANDLE_H