wrapped.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. // Private constructor, this should not be created directly by users.
  40. Wrapped(GodotObject *p_owner) :
  41. _owner(p_owner) {}
  42. protected:
  43. Wrapped() = default;
  44. public:
  45. // Must be public but you should not touch this.
  46. GodotObject *_owner = nullptr;
  47. static Wrapped *_new() {
  48. return nullptr;
  49. }
  50. };
  51. namespace internal {
  52. template <class T, class Enable = void>
  53. struct Creator {
  54. static T *_new() { return nullptr; }
  55. };
  56. template <class T>
  57. struct Creator<T, typename std::enable_if<std::is_base_of_v<godot::Wrapped, T>>::type> {
  58. static T *_new() { return T::_new(); }
  59. };
  60. // template <class T>
  61. // struct Creator<T, std::false_type> {
  62. // };
  63. // template <class T>
  64. // struct Creator<T, std::enable_if_t<std::is_base_of_v<godot::Wrapped, T>, bool>> {
  65. // static T *_new() { return T::_new(); }
  66. // };
  67. }; // namespace internal
  68. } // namespace godot
  69. #ifdef DEBUG_ENABLED
  70. #define CHECK_CLASS_CONSTRUCTOR(m_constructor, m_class) \
  71. if (unlikely(!m_constructor)) { \
  72. ERR_PRINT_ONCE("Constructor for class " #m_class "not found. Likely wasn't registered in ClassDB."); \
  73. return nullptr; \
  74. } else \
  75. ((void)0)
  76. #else
  77. #define CHECK_CLASS_CONSTRUCTOR(m_constructor, m_class)
  78. #endif
  79. #define GDCLASS(m_class, m_inherits) \
  80. private: \
  81. friend class ClassDB; \
  82. \
  83. using SelfType = m_class; \
  84. \
  85. protected: \
  86. static void (*_get_bind_methods())() { \
  87. return &m_class::_bind_methods; \
  88. } \
  89. \
  90. template <class T> \
  91. static void register_virtuals() { \
  92. m_inherits::register_virtuals<T>(); \
  93. } \
  94. \
  95. public: \
  96. static void initialize_class() { \
  97. static bool initialized = false; \
  98. if (initialized) { \
  99. return; \
  100. } \
  101. m_inherits::initialize_class(); \
  102. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  103. _bind_methods(); \
  104. m_inherits::register_virtuals<m_class>(); \
  105. } \
  106. initialized = true; \
  107. } \
  108. \
  109. static const char *get_class_static() { \
  110. return #m_class; \
  111. } \
  112. \
  113. static const char *get_parent_class_static() { \
  114. return #m_inherits; \
  115. } \
  116. \
  117. static GDExtensionClassInstancePtr create(void *data) { \
  118. return reinterpret_cast<GDExtensionClassInstancePtr>(new ("") m_class); \
  119. } \
  120. \
  121. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  122. if (ptr) { \
  123. m_class *cls = reinterpret_cast<m_class *>(ptr); \
  124. cls->~m_class(); \
  125. ::godot::Memory::free_static(cls); \
  126. } \
  127. } \
  128. \
  129. static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \
  130. godot::internal::gdn_interface->object_set_instance_binding(p_object_instance, godot::internal::token, p_instance, &m_class::___binding_callbacks); \
  131. reinterpret_cast<m_class *>(p_instance)->_owner = reinterpret_cast<godot::GodotObject *>(p_object_instance); \
  132. } \
  133. \
  134. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  135. return nullptr; \
  136. } \
  137. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  138. } \
  139. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  140. return true; \
  141. } \
  142. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  143. ___binding_create_callback, \
  144. ___binding_free_callback, \
  145. ___binding_reference_callback, \
  146. }; \
  147. \
  148. static m_class *_new() { \
  149. static GDNativeExtensionPtr ___extension = nullptr; \
  150. static GDNativeClassConstructor ___constructor = godot::internal::gdn_interface->classdb_get_constructor(#m_class, &___extension); \
  151. CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \
  152. GDNativeObjectPtr obj = godot::internal::gdn_interface->classdb_construct_object(___constructor, ___extension); \
  153. return reinterpret_cast<m_class *>(godot::internal::gdn_interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \
  154. } \
  155. \
  156. private:
  157. // Don't use this for your classes, use GDCLASS() instead.
  158. #define GDNATIVE_CLASS(m_class, m_inherits) \
  159. protected: \
  160. static void (*_get_bind_methods())() { \
  161. return nullptr; \
  162. } \
  163. \
  164. public: \
  165. static void initialize_class() {} \
  166. \
  167. static const char *get_class_static() { \
  168. return #m_class; \
  169. } \
  170. \
  171. static const char *get_parent_class_static() { \
  172. return #m_inherits; \
  173. } \
  174. \
  175. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  176. m_class *obj = new ("") m_class; \
  177. obj->_owner = (godot::GodotObject *)p_instance; \
  178. return obj; \
  179. } \
  180. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  181. Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
  182. } \
  183. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  184. return true; \
  185. } \
  186. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  187. ___binding_create_callback, \
  188. ___binding_free_callback, \
  189. ___binding_reference_callback, \
  190. }; \
  191. static m_class *_new() { \
  192. static GDNativeClassConstructor ___constructor = godot::internal::gdn_interface->classdb_get_constructor(#m_class, nullptr); \
  193. CHECK_CLASS_CONSTRUCTOR(___constructor, m_class); \
  194. GDNativeObjectPtr obj = ___constructor(); \
  195. return reinterpret_cast<m_class *>(godot::internal::gdn_interface->object_get_instance_binding(obj, godot::internal::token, &m_class::___binding_callbacks)); \
  196. } \
  197. \
  198. private:
  199. #endif // ! GODOT_CPP_WRAPPED_HPP