wrapped.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. namespace godot {
  34. namespace internal {
  35. struct empty_constructor {};
  36. } // namespace internal
  37. typedef void GodotObject;
  38. // Base for all engine classes, to contain the pointer to the engine instance.
  39. class Wrapped {
  40. friend class GDExtensionBinding;
  41. // Private constructor, this should not be created directly by users.
  42. Wrapped(GodotObject *p_owner) :
  43. _owner(p_owner) {}
  44. protected:
  45. Wrapped() = default;
  46. Wrapped(internal::empty_constructor empty) {}
  47. public:
  48. // Must be public but you should not touch this.
  49. GodotObject *_owner = nullptr;
  50. };
  51. } // namespace godot
  52. #define GDCLASS(m_class, m_inherits) \
  53. private: \
  54. friend class ClassDB; \
  55. \
  56. using SelfType = m_class; \
  57. \
  58. protected: \
  59. static void (*_get_bind_methods())() { \
  60. return &m_class::_bind_methods; \
  61. } \
  62. \
  63. m_class(godot::GodotObject *owner) : m_inherits(godot::internal::empty_constructor()) { \
  64. _owner = owner; \
  65. } \
  66. \
  67. m_class(godot::internal::empty_constructor empty) : m_inherits(empty) {} \
  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 GDExtensionClassInstancePtr create(void *data) { \
  97. return (GDExtensionClassInstancePtr)godot::Memory::alloc_static(sizeof(m_class)); \
  98. } \
  99. \
  100. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  101. godot::memdelete(reinterpret_cast<m_class *>(ptr)); \
  102. } \
  103. \
  104. static void set_object_instance(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance) { \
  105. memnew_placement((void *)p_instance, m_class((godot::GodotObject *)p_object_instance)); \
  106. } \
  107. \
  108. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  109. return memnew(m_class((godot::GodotObject *)p_instance)); \
  110. } \
  111. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  112. memdelete((m_class *)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. \
  123. private:
  124. // Don't use this for your classes, use GDCLASS() instead.
  125. #define GDNATIVE_CLASS(m_class, m_inherits) \
  126. protected: \
  127. static void (*_get_bind_methods())() { \
  128. return nullptr; \
  129. } \
  130. m_class(godot::internal::empty_constructor empty) : m_inherits(empty) {} \
  131. \
  132. public: \
  133. static void initialize_class() {} \
  134. \
  135. static const char *get_class_static() { \
  136. return #m_class; \
  137. } \
  138. \
  139. static const char *get_parent_class_static() { \
  140. return #m_inherits; \
  141. } \
  142. \
  143. static void *___binding_create_callback(void *p_token, void *p_instance) { \
  144. m_class *obj = memnew(m_class(godot::internal::empty_constructor())); \
  145. obj->_owner = (godot::GodotObject *)p_instance; \
  146. return obj; \
  147. } \
  148. static void ___binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  149. memdelete((m_class *)p_binding); \
  150. } \
  151. static GDNativeBool ___binding_reference_callback(void *p_token, void *p_instance, GDNativeBool p_reference) { \
  152. return true; \
  153. } \
  154. static constexpr GDNativeInstanceBindingCallbacks ___binding_callbacks = { \
  155. ___binding_create_callback, \
  156. ___binding_free_callback, \
  157. ___binding_reference_callback, \
  158. }; \
  159. \
  160. private:
  161. #endif // ! GODOT_CPP_WRAPPED_HPP