wrapped.hpp 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. /**************************************************************************/
  2. /* wrapped.hpp */
  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 GODOT_WRAPPED_HPP
  31. #define GODOT_WRAPPED_HPP
  32. #include <godot_cpp/core/memory.hpp>
  33. #include <godot_cpp/core/property_info.hpp>
  34. #include <godot_cpp/templates/list.hpp>
  35. #include <godot_cpp/templates/vector.hpp>
  36. #include <godot_cpp/godot.hpp>
  37. #if defined(MACOS_ENABLED) && defined(HOT_RELOAD_ENABLED)
  38. #include <mutex>
  39. #define _GODOT_CPP_AVOID_THREAD_LOCAL
  40. #define _GODOT_CPP_THREAD_LOCAL
  41. #else
  42. #define _GODOT_CPP_THREAD_LOCAL thread_local
  43. #endif
  44. namespace godot {
  45. class ClassDB;
  46. typedef void GodotObject;
  47. template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool> = true>
  48. _ALWAYS_INLINE_ void _pre_initialize();
  49. // Base for all engine classes, to contain the pointer to the engine instance.
  50. class Wrapped {
  51. friend class GDExtensionBinding;
  52. friend class ClassDB;
  53. friend void postinitialize_handler(Wrapped *);
  54. template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool>>
  55. friend _ALWAYS_INLINE_ void _pre_initialize();
  56. #ifdef _GODOT_CPP_AVOID_THREAD_LOCAL
  57. static std::recursive_mutex _constructing_mutex;
  58. #endif
  59. _GODOT_CPP_THREAD_LOCAL static const StringName *_constructing_extension_class_name;
  60. _GODOT_CPP_THREAD_LOCAL static const GDExtensionInstanceBindingCallbacks *_constructing_class_binding_callbacks;
  61. #ifdef HOT_RELOAD_ENABLED
  62. _GODOT_CPP_THREAD_LOCAL static GDExtensionObjectPtr _constructing_recreate_owner;
  63. #endif
  64. template <typename T>
  65. _ALWAYS_INLINE_ static void _set_construct_info() {
  66. _constructing_extension_class_name = T::_get_extension_class_name();
  67. _constructing_class_binding_callbacks = &T::_gde_binding_callbacks;
  68. }
  69. protected:
  70. virtual bool _is_extension_class() const { return false; }
  71. static const StringName *_get_extension_class_name(); // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
  72. void _notification(int p_what) {}
  73. bool _set(const StringName &p_name, const Variant &p_property) { return false; }
  74. bool _get(const StringName &p_name, Variant &r_property) const { return false; }
  75. void _get_property_list(List<PropertyInfo> *p_list) const {}
  76. bool _property_can_revert(const StringName &p_name) const { return false; }
  77. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
  78. void _validate_property(PropertyInfo &p_property) const {}
  79. String _to_string() const { return "[" + String(get_class_static()) + ":" + itos(get_instance_id()) + "]"; }
  80. static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) {}
  81. static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { return false; }
  82. static GDExtensionBool get_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { return false; }
  83. static const GDExtensionPropertyInfo *get_property_list_bind(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) { return nullptr; }
  84. static void free_property_list_bind(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list, uint32_t p_count) {}
  85. static GDExtensionBool property_can_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) { return false; }
  86. static GDExtensionBool property_get_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { return false; }
  87. static GDExtensionBool validate_property_bind(GDExtensionClassInstancePtr p_instance, GDExtensionPropertyInfo *p_property) { return false; }
  88. static void to_string_bind(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out) {}
  89. // The only reason this has to be held here, is when we return results of `_get_property_list` to Godot, we pass
  90. // pointers to strings in this list. They have to remain valid to pass the bridge, until the list is freed by Godot...
  91. ::godot::List<::godot::PropertyInfo> plist_owned;
  92. void _postinitialize();
  93. Wrapped(const StringName p_godot_class);
  94. Wrapped(GodotObject *p_godot_object);
  95. virtual ~Wrapped() {}
  96. public:
  97. static const StringName &get_class_static() {
  98. static const StringName string_name = StringName("Wrapped");
  99. return string_name;
  100. }
  101. uint64_t get_instance_id() const {
  102. return 0;
  103. }
  104. // Must be public but you should not touch this.
  105. GodotObject *_owner = nullptr;
  106. };
  107. template <typename T, std::enable_if_t<std::is_base_of<::godot::Wrapped, T>::value, bool>>
  108. _ALWAYS_INLINE_ void _pre_initialize() {
  109. #ifdef _GODOT_CPP_AVOID_THREAD_LOCAL
  110. Wrapped::_constructing_mutex.lock();
  111. #endif
  112. Wrapped::_set_construct_info<T>();
  113. }
  114. _FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr) {
  115. }
  116. _FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr, const StringName &p_str) {
  117. arr.push_back(p_str);
  118. }
  119. template <typename... P>
  120. _FORCE_INLINE_ void snarray_add_str(Vector<StringName> &arr, const StringName &p_str, P... p_args) {
  121. arr.push_back(p_str);
  122. snarray_add_str(arr, p_args...);
  123. }
  124. template <typename... P>
  125. _FORCE_INLINE_ Vector<StringName> snarray(P... p_args) {
  126. Vector<StringName> arr;
  127. snarray_add_str(arr, p_args...);
  128. return arr;
  129. }
  130. namespace internal {
  131. GDExtensionPropertyInfo *create_c_property_list(const ::godot::List<::godot::PropertyInfo> &plist_cpp, uint32_t *r_size);
  132. void free_c_property_list(GDExtensionPropertyInfo *plist);
  133. typedef void (*EngineClassRegistrationCallback)();
  134. void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
  135. void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  136. void register_engine_classes();
  137. template <typename T>
  138. struct EngineClassRegistration {
  139. EngineClassRegistration() {
  140. add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
  141. }
  142. static void callback() {
  143. register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
  144. }
  145. };
  146. } // namespace internal
  147. } // namespace godot
  148. // Use this on top of your own classes.
  149. // Note: the trail of `***` is to keep sane diffs in PRs, because clang-format otherwise moves every `\` which makes
  150. // every line of the macro different
  151. #define GDCLASS(m_class, m_inherits) /***********************************************************************************************************************************************/ \
  152. private: \
  153. void operator=(const m_class & /*p_rval*/) {} \
  154. friend class ::godot::ClassDB; \
  155. friend class ::godot::Wrapped; \
  156. \
  157. protected: \
  158. virtual bool _is_extension_class() const override { return true; } \
  159. \
  160. static const ::godot::StringName *_get_extension_class_name() { \
  161. const ::godot::StringName &string_name = get_class_static(); \
  162. return &string_name; \
  163. } \
  164. \
  165. static void (*_get_bind_methods())() { \
  166. return &m_class::_bind_methods; \
  167. } \
  168. \
  169. static void (::godot::Wrapped::*_get_notification())(int) { \
  170. return (void(::godot::Wrapped::*)(int)) & m_class::_notification; \
  171. } \
  172. \
  173. static bool (::godot::Wrapped::*_get_set())(const ::godot::StringName &p_name, const ::godot::Variant &p_property) { \
  174. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, const ::godot::Variant &p_property)) & m_class::_set; \
  175. } \
  176. \
  177. static bool (::godot::Wrapped::*_get_get())(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const { \
  178. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const) & m_class::_get; \
  179. } \
  180. \
  181. static void (::godot::Wrapped::*_get_get_property_list())(::godot::List<::godot::PropertyInfo> * p_list) const { \
  182. return (void(::godot::Wrapped::*)(::godot::List<::godot::PropertyInfo> * p_list) const) & m_class::_get_property_list; \
  183. } \
  184. \
  185. static bool (::godot::Wrapped::*_get_property_can_revert())(const ::godot::StringName &p_name) const { \
  186. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name) const) & m_class::_property_can_revert; \
  187. } \
  188. \
  189. static bool (::godot::Wrapped::*_get_property_get_revert())(const ::godot::StringName &p_name, ::godot::Variant &) const { \
  190. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &) const) & m_class::_property_get_revert; \
  191. } \
  192. \
  193. static void (::godot::Wrapped::*_get_validate_property())(::godot::PropertyInfo & p_property) const { \
  194. return (void(::godot::Wrapped::*)(::godot::PropertyInfo & p_property) const) & m_class::_validate_property; \
  195. } \
  196. \
  197. static ::godot::String (::godot::Wrapped::*_get_to_string())() const { \
  198. return (::godot::String(::godot::Wrapped::*)() const) & m_class::_to_string; \
  199. } \
  200. \
  201. template <typename T, typename B> \
  202. static void register_virtuals() { \
  203. m_inherits::register_virtuals<T, B>(); \
  204. } \
  205. \
  206. public: \
  207. typedef m_class self_type; \
  208. typedef m_inherits parent_type; \
  209. \
  210. static void initialize_class() { \
  211. static bool initialized = false; \
  212. if (initialized) { \
  213. return; \
  214. } \
  215. m_inherits::initialize_class(); \
  216. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  217. _bind_methods(); \
  218. m_inherits::register_virtuals<m_class, m_inherits>(); \
  219. } \
  220. initialized = true; \
  221. } \
  222. \
  223. static const ::godot::StringName &get_class_static() { \
  224. static const ::godot::StringName string_name = ::godot::StringName(U## #m_class); \
  225. return string_name; \
  226. } \
  227. \
  228. static const ::godot::StringName &get_parent_class_static() { \
  229. return m_inherits::get_class_static(); \
  230. } \
  231. \
  232. static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what, GDExtensionBool p_reversed) { \
  233. if (p_instance && m_class::_get_notification()) { \
  234. if (!p_reversed) { \
  235. m_inherits::notification_bind(p_instance, p_what, p_reversed); \
  236. } \
  237. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  238. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  239. cls->_notification(p_what); \
  240. } \
  241. if (p_reversed) { \
  242. m_inherits::notification_bind(p_instance, p_what, p_reversed); \
  243. } \
  244. } \
  245. } \
  246. \
  247. static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { \
  248. if (p_instance) { \
  249. if (m_inherits::set_bind(p_instance, p_name, p_value)) { \
  250. return true; \
  251. } \
  252. if (m_class::_get_set() != m_inherits::_get_set()) { \
  253. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  254. return cls->_set(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<const ::godot::Variant *>(p_value)); \
  255. } \
  256. } \
  257. return false; \
  258. } \
  259. \
  260. static GDExtensionBool get_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { \
  261. if (p_instance) { \
  262. if (m_inherits::get_bind(p_instance, p_name, r_ret)) { \
  263. return true; \
  264. } \
  265. if (m_class::_get_get() != m_inherits::_get_get()) { \
  266. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  267. return cls->_get(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<::godot::Variant *>(r_ret)); \
  268. } \
  269. } \
  270. return false; \
  271. } \
  272. \
  273. static inline bool has_get_property_list() { \
  274. return m_class::_get_get_property_list() && m_class::_get_get_property_list() != m_inherits::_get_get_property_list(); \
  275. } \
  276. \
  277. static const GDExtensionPropertyInfo *get_property_list_bind(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) { \
  278. if (!p_instance) { \
  279. if (r_count) \
  280. *r_count = 0; \
  281. return nullptr; \
  282. } \
  283. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  284. ::godot::List<::godot::PropertyInfo> &plist_cpp = cls->plist_owned; \
  285. ERR_FAIL_COND_V_MSG(!plist_cpp.is_empty(), nullptr, "Internal error, property list was not freed by engine!"); \
  286. cls->_get_property_list(&plist_cpp); \
  287. return ::godot::internal::create_c_property_list(plist_cpp, r_count); \
  288. } \
  289. \
  290. static void free_property_list_bind(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list, uint32_t /*p_count*/) { \
  291. if (p_instance) { \
  292. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  293. cls->plist_owned.clear(); \
  294. ::godot::internal::free_c_property_list(const_cast<GDExtensionPropertyInfo *>(p_list)); \
  295. } \
  296. } \
  297. \
  298. static GDExtensionBool property_can_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) { \
  299. if (p_instance && m_class::_get_property_can_revert()) { \
  300. if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
  301. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  302. return cls->_property_can_revert(*reinterpret_cast<const ::godot::StringName *>(p_name)); \
  303. } \
  304. return m_inherits::property_can_revert_bind(p_instance, p_name); \
  305. } \
  306. return false; \
  307. } \
  308. \
  309. static GDExtensionBool property_get_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { \
  310. if (p_instance && m_class::_get_property_get_revert()) { \
  311. if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
  312. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  313. return cls->_property_get_revert(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<::godot::Variant *>(r_ret)); \
  314. } \
  315. return m_inherits::property_get_revert_bind(p_instance, p_name, r_ret); \
  316. } \
  317. return false; \
  318. } \
  319. \
  320. static GDExtensionBool validate_property_bind(GDExtensionClassInstancePtr p_instance, GDExtensionPropertyInfo *p_property) { \
  321. bool ret = false; \
  322. if (p_instance && m_class::_get_validate_property()) { \
  323. ret = m_inherits::validate_property_bind(p_instance, p_property); \
  324. if (m_class::_get_validate_property() != m_inherits::_get_validate_property()) { \
  325. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  326. ::godot::PropertyInfo info(p_property); \
  327. cls->_validate_property(info); \
  328. info._update(p_property); \
  329. return true; \
  330. } \
  331. } \
  332. return ret; \
  333. } \
  334. \
  335. static void to_string_bind(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out) { \
  336. if (p_instance && m_class::_get_to_string()) { \
  337. if (m_class::_get_to_string() != m_inherits::_get_to_string()) { \
  338. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  339. *reinterpret_cast<::godot::String *>(r_out) = cls->_to_string(); \
  340. *r_is_valid = true; \
  341. return; \
  342. } \
  343. m_inherits::to_string_bind(p_instance, r_is_valid, r_out); \
  344. } \
  345. } \
  346. \
  347. static void free(void * /*data*/, GDExtensionClassInstancePtr ptr) { \
  348. if (ptr) { \
  349. m_class *cls = reinterpret_cast<m_class *>(ptr); \
  350. cls->~m_class(); \
  351. ::godot::Memory::free_static(cls); \
  352. } \
  353. } \
  354. \
  355. static void *_gde_binding_create_callback(void * /*p_token*/, void * /*p_instance*/) { \
  356. return nullptr; \
  357. } \
  358. \
  359. static void _gde_binding_free_callback(void * /*p_token*/, void * /*p_instance*/, void * /*p_binding*/) { \
  360. } \
  361. \
  362. static GDExtensionBool _gde_binding_reference_callback(void * /*p_token*/, void * /*p_instance*/, GDExtensionBool /*p_reference*/) { \
  363. return true; \
  364. } \
  365. \
  366. static constexpr GDExtensionInstanceBindingCallbacks _gde_binding_callbacks = { \
  367. _gde_binding_create_callback, \
  368. _gde_binding_free_callback, \
  369. _gde_binding_reference_callback, \
  370. }; \
  371. \
  372. private:
  373. // Don't use this for your classes, use GDCLASS() instead.
  374. #define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) /******************************************************************************************************************/ \
  375. private: \
  376. inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
  377. void operator=(const m_class &p_rval) {} \
  378. friend class ::godot::ClassDB; \
  379. friend class ::godot::Wrapped; \
  380. \
  381. protected: \
  382. m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
  383. m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
  384. \
  385. static void _bind_methods() {} \
  386. \
  387. static void (*_get_bind_methods())() { \
  388. return nullptr; \
  389. } \
  390. \
  391. static void (Wrapped::*_get_notification())(int) { \
  392. return nullptr; \
  393. } \
  394. \
  395. static bool (Wrapped::*_get_set())(const ::godot::StringName &p_name, const Variant &p_property) { \
  396. return nullptr; \
  397. } \
  398. \
  399. static bool (Wrapped::*_get_get())(const ::godot::StringName &p_name, Variant &r_ret) const { \
  400. return nullptr; \
  401. } \
  402. \
  403. static inline bool has_get_property_list() { \
  404. return false; \
  405. } \
  406. \
  407. static void (Wrapped::*_get_get_property_list())(List<PropertyInfo> * p_list) const { \
  408. return nullptr; \
  409. } \
  410. \
  411. static bool (Wrapped::*_get_property_can_revert())(const ::godot::StringName &p_name) const { \
  412. return nullptr; \
  413. } \
  414. \
  415. static bool (Wrapped::*_get_property_get_revert())(const ::godot::StringName &p_name, Variant &) const { \
  416. return nullptr; \
  417. } \
  418. \
  419. static void (Wrapped::*_get_validate_property())(::godot::PropertyInfo & p_property) const { \
  420. return nullptr; \
  421. } \
  422. \
  423. static String (Wrapped::*_get_to_string())() const { \
  424. return nullptr; \
  425. } \
  426. \
  427. public: \
  428. typedef m_class self_type; \
  429. typedef m_inherits parent_type; \
  430. \
  431. static void initialize_class() {} \
  432. \
  433. static const ::godot::StringName &get_class_static() { \
  434. static const ::godot::StringName string_name = ::godot::StringName(#m_alias_for); \
  435. return string_name; \
  436. } \
  437. \
  438. static const ::godot::StringName &get_parent_class_static() { \
  439. return m_inherits::get_class_static(); \
  440. } \
  441. \
  442. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  443. } \
  444. \
  445. static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \
  446. /* Do not call memnew here, we don't want the post-initializer to be called */ \
  447. return new ("", "") m_class((GodotObject *)p_instance); \
  448. } \
  449. static void _gde_binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  450. /* Explicitly call the deconstructor to ensure proper lifecycle for non-trivial members */ \
  451. reinterpret_cast<m_class *>(p_binding)->~m_class(); \
  452. Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
  453. } \
  454. static GDExtensionBool _gde_binding_reference_callback(void *p_token, void *p_instance, GDExtensionBool p_reference) { \
  455. return true; \
  456. } \
  457. static constexpr GDExtensionInstanceBindingCallbacks _gde_binding_callbacks = { \
  458. _gde_binding_create_callback, \
  459. _gde_binding_free_callback, \
  460. _gde_binding_reference_callback, \
  461. }; \
  462. m_class() : m_class(#m_alias_for) {} \
  463. \
  464. private:
  465. // Don't use this for your classes, use GDCLASS() instead.
  466. #define GDEXTENSION_CLASS(m_class, m_inherits) GDEXTENSION_CLASS_ALIAS(m_class, m_class, m_inherits)
  467. #define GDVIRTUAL_CALL(m_name, ...) _gdvirtual_##m_name##_call<false>(__VA_ARGS__)
  468. #define GDVIRTUAL_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<false>(__VA_ARGS__)
  469. #define GDVIRTUAL_REQUIRED_CALL(m_name, ...) _gdvirtual_##m_name##_call<true>(__VA_ARGS__)
  470. #define GDVIRTUAL_REQUIRED_CALL_PTR(m_obj, m_name, ...) m_obj->_gdvirtual_##m_name##_call<true>(__VA_ARGS__)
  471. #define GDVIRTUAL_BIND(m_name, ...) ::godot::ClassDB::add_virtual_method(get_class_static(), _gdvirtual_##m_name##_get_method_info(), ::godot::snarray(__VA_ARGS__));
  472. #define GDVIRTUAL_IS_OVERRIDDEN(m_name) _gdvirtual_##m_name##_overridden()
  473. #define GDVIRTUAL_IS_OVERRIDDEN_PTR(m_obj, m_name) m_obj->_gdvirtual_##m_name##_overridden()
  474. #endif // GODOT_WRAPPED_HPP