wrapped.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*************************************************************************/
  2. /* wrapped.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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. // Must be public but you should not touch this.
  48. GodotObject *_owner = nullptr;
  49. };
  50. } // namespace godot
  51. #define GDCLASS(m_class, m_inherits) \
  52. private: \
  53. void operator=(const m_class &p_rval) {} \
  54. friend class ClassDB; \
  55. \
  56. protected: \
  57. virtual const char *_get_extension_class() const override { \
  58. return get_class_static(); \
  59. } \
  60. \
  61. virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  62. return &___binding_callbacks; \
  63. } \
  64. \
  65. static void (*_get_bind_methods())() { \
  66. return &m_class::_bind_methods; \
  67. } \
  68. \
  69. template <class T> \
  70. static void register_virtuals() { \
  71. m_inherits::register_virtuals<T>(); \
  72. } \
  73. \
  74. public: \
  75. static void initialize_class() { \
  76. static bool initialized = false; \
  77. if (initialized) { \
  78. return; \
  79. } \
  80. m_inherits::initialize_class(); \
  81. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  82. _bind_methods(); \
  83. m_inherits::register_virtuals<m_class>(); \
  84. } \
  85. initialized = true; \
  86. } \
  87. \
  88. static const char *get_class_static() { \
  89. return #m_class; \
  90. } \
  91. \
  92. static const char *get_parent_class_static() { \
  93. return #m_inherits; \
  94. } \
  95. \
  96. static GDNativeObjectPtr create(void *data) { \
  97. m_class *new_object = memnew(m_class); \
  98. return new_object->_owner; \
  99. } \
  100. \
  101. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  102. if (ptr) { \
  103. m_class *cls = reinterpret_cast<m_class *>(ptr); \
  104. cls->~m_class(); \
  105. ::godot::Memory::free_static(cls); \
  106. } \
  107. } \
  108. \
  109. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  110. return nullptr; \
  111. } \
  112. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  113. } \
  114. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  115. return true; \
  116. } \
  117. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  118. ___binding_create_callback, \
  119. ___binding_free_callback, \
  120. ___binding_reference_callback, \
  121. };
  122. // Don't use this for your classes, use GDCLASS() instead.
  123. #define GDNATIVE_CLASS(m_class, m_inherits) \
  124. private: \
  125. void operator=(const m_class &p_rval) {} \
  126. \
  127. protected: \
  128. virtual const GDNativeInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  129. return &___binding_callbacks; \
  130. } \
  131. \
  132. m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
  133. m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
  134. \
  135. static void (*_get_bind_methods())() { \
  136. return nullptr; \
  137. } \
  138. \
  139. public: \
  140. static void initialize_class() {} \
  141. \
  142. static const char *get_class_static() { \
  143. return #m_class; \
  144. } \
  145. \
  146. static const char *get_parent_class_static() { \
  147. return #m_inherits; \
  148. } \
  149. \
  150. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  151. return memnew(m_class((GodotObject *)p_instance)); \
  152. } \
  153. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  154. Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
  155. } \
  156. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  157. return true; \
  158. } \
  159. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  160. ___binding_create_callback, \
  161. ___binding_free_callback, \
  162. ___binding_reference_callback, \
  163. }; \
  164. m_class() : m_class(#m_class) {}
  165. #endif // ! GODOT_CPP_WRAPPED_HPP