wrapped.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*************************************************************************/
  2. /* wrapped.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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 GODOT_CPP_WRAPPED_HPP
  31. #define GODOT_CPP_WRAPPED_HPP
  32. #include <godot_cpp/core/memory.hpp>
  33. #include <godot_cpp/godot.hpp>
  34. namespace godot {
  35. typedef void GodotObject;
  36. // Base for all engine classes, to contain the pointer to the engine instance.
  37. class Wrapped {
  38. friend class GDExtensionBinding;
  39. friend void postinitialize_handler(Wrapped *);
  40. protected:
  41. virtual const char *_get_extension_class() const; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
  42. virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const = 0;
  43. void _postinitialize();
  44. Wrapped(const char *p_godot_class);
  45. Wrapped(GodotObject *p_godot_object);
  46. public:
  47. static const char *get_class_static() {
  48. return "Wrapped";
  49. }
  50. // Must be public but you should not touch this.
  51. GodotObject *_owner = nullptr;
  52. };
  53. } // namespace godot
  54. #define GDCLASS(m_class, m_inherits) \
  55. private: \
  56. void operator=(const m_class &p_rval) {} \
  57. friend class ::godot::ClassDB; \
  58. \
  59. protected: \
  60. virtual const char *_get_extension_class() const override { \
  61. return get_class_static(); \
  62. } \
  63. \
  64. virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  65. return &___binding_callbacks; \
  66. } \
  67. \
  68. static void (*_get_bind_methods())() { \
  69. return &m_class::_bind_methods; \
  70. } \
  71. \
  72. template <class T> \
  73. static void register_virtuals() { \
  74. m_inherits::register_virtuals<T>(); \
  75. } \
  76. \
  77. public: \
  78. static void initialize_class() { \
  79. static bool initialized = false; \
  80. if (initialized) { \
  81. return; \
  82. } \
  83. m_inherits::initialize_class(); \
  84. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  85. _bind_methods(); \
  86. m_inherits::register_virtuals<m_class>(); \
  87. } \
  88. initialized = true; \
  89. } \
  90. \
  91. static const char *get_class_static() { \
  92. return #m_class; \
  93. } \
  94. \
  95. static const char *get_parent_class_static() { \
  96. return m_inherits::get_class_static(); \
  97. } \
  98. \
  99. static GDNativeObjectPtr create(void *data) { \
  100. m_class *new_object = memnew(m_class); \
  101. return new_object->_owner; \
  102. } \
  103. \
  104. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  105. if (ptr) { \
  106. m_class *cls = reinterpret_cast<m_class *>(ptr); \
  107. cls->~m_class(); \
  108. ::godot::Memory::free_static(cls); \
  109. } \
  110. } \
  111. \
  112. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  113. return nullptr; \
  114. } \
  115. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  116. } \
  117. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  118. return true; \
  119. } \
  120. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  121. ___binding_create_callback, \
  122. ___binding_free_callback, \
  123. ___binding_reference_callback, \
  124. };
  125. // Don't use this for your classes, use GDCLASS() instead.
  126. #define GDNATIVE_CLASS(m_class, m_inherits) \
  127. private: \
  128. void operator=(const m_class &p_rval) {} \
  129. \
  130. protected: \
  131. virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  132. return &___binding_callbacks; \
  133. } \
  134. \
  135. m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
  136. m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
  137. \
  138. static void (*_get_bind_methods())() { \
  139. return nullptr; \
  140. } \
  141. \
  142. public: \
  143. static void initialize_class() {} \
  144. \
  145. static const char *get_class_static() { \
  146. return #m_class; \
  147. } \
  148. \
  149. static const char *get_parent_class_static() { \
  150. return m_inherits::get_class_static(); \
  151. } \
  152. \
  153. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  154. /* Do not call memnew here, we don't want the postinitializer to be called */ \
  155. return new ("") m_class((GodotObject *)p_instance); \
  156. } \
  157. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  158. /* Explicitly call the deconstructor to ensure proper lifecycle for non-trivial members */ \
  159. reinterpret_cast<m_class *>(p_binding)->~m_class(); \
  160. Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
  161. } \
  162. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  163. return true; \
  164. } \
  165. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  166. ___binding_create_callback, \
  167. ___binding_free_callback, \
  168. ___binding_reference_callback, \
  169. }; \
  170. m_class() : m_class(#m_class) {}
  171. #endif // ! GODOT_CPP_WRAPPED_HPP