wrapped.hpp 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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/godot.hpp>
  36. namespace godot {
  37. class ClassDB;
  38. typedef void GodotObject;
  39. // Base for all engine classes, to contain the pointer to the engine instance.
  40. class Wrapped {
  41. friend class GDExtensionBinding;
  42. friend void postinitialize_handler(Wrapped *);
  43. protected:
  44. virtual const StringName *_get_extension_class_name() const; // This is needed to retrieve the class name before the godot object has its _extension and _extension_instance members assigned.
  45. virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const = 0;
  46. void _notification(int p_what) {}
  47. bool _set(const StringName &p_name, const Variant &p_property) { return false; }
  48. bool _get(const StringName &p_name, Variant &r_property) const { return false; }
  49. void _get_property_list(List<PropertyInfo> *p_list) const {}
  50. bool _property_can_revert(const StringName &p_name) const { return false; }
  51. bool _property_get_revert(const StringName &p_name, Variant &r_property) const { return false; }
  52. String _to_string() const { return "[" + String(get_class_static()) + ":" + itos(get_instance_id()) + "]"; }
  53. static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what) {}
  54. static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { return false; }
  55. static GDExtensionBool get_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { return false; }
  56. static const GDExtensionPropertyInfo *get_property_list_bind(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) { return nullptr; }
  57. static void free_property_list_bind(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list) {}
  58. static GDExtensionBool property_can_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) { return false; }
  59. static GDExtensionBool property_get_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { return false; }
  60. static void to_string_bind(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out) {}
  61. ::godot::List<::godot::PropertyInfo> plist_owned;
  62. GDExtensionPropertyInfo *plist = nullptr;
  63. uint32_t plist_size = 0;
  64. void _postinitialize();
  65. Wrapped(const StringName p_godot_class);
  66. Wrapped(GodotObject *p_godot_object);
  67. virtual ~Wrapped() {}
  68. public:
  69. static StringName &get_class_static() {
  70. static StringName string_name = StringName("Wrapped");
  71. return string_name;
  72. }
  73. uint64_t get_instance_id() const {
  74. return 0;
  75. }
  76. // Must be public but you should not touch this.
  77. GodotObject *_owner = nullptr;
  78. };
  79. namespace internal {
  80. typedef void (*EngineClassRegistrationCallback)();
  81. void add_engine_class_registration_callback(EngineClassRegistrationCallback p_callback);
  82. void register_engine_class(const StringName &p_name, const GDExtensionInstanceBindingCallbacks *p_callbacks);
  83. void register_engine_classes();
  84. template <class T>
  85. struct EngineClassRegistration {
  86. EngineClassRegistration() {
  87. add_engine_class_registration_callback(&EngineClassRegistration<T>::callback);
  88. }
  89. static void callback() {
  90. register_engine_class(T::get_class_static(), &T::_gde_binding_callbacks);
  91. }
  92. };
  93. } // namespace internal
  94. } // namespace godot
  95. #define GDCLASS(m_class, m_inherits) \
  96. private: \
  97. void operator=(const m_class &p_rval) {} \
  98. friend class ::godot::ClassDB; \
  99. \
  100. protected: \
  101. virtual const ::godot::StringName *_get_extension_class_name() const override { \
  102. static ::godot::StringName string_name = get_class_static(); \
  103. return &string_name; \
  104. } \
  105. \
  106. virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  107. return &_gde_binding_callbacks; \
  108. } \
  109. \
  110. static void (*_get_bind_methods())() { \
  111. return &m_class::_bind_methods; \
  112. } \
  113. \
  114. static void (::godot::Wrapped::*_get_notification())(int) { \
  115. return (void(::godot::Wrapped::*)(int)) & m_class::_notification; \
  116. } \
  117. \
  118. static bool (::godot::Wrapped::*_get_set())(const ::godot::StringName &p_name, const ::godot::Variant &p_property) { \
  119. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, const ::godot::Variant &p_property)) & m_class::_set; \
  120. } \
  121. \
  122. static bool (::godot::Wrapped::*_get_get())(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const { \
  123. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &r_ret) const) & m_class::_get; \
  124. } \
  125. \
  126. static void (::godot::Wrapped::*_get_get_property_list())(::godot::List<::godot::PropertyInfo> * p_list) const { \
  127. return (void(::godot::Wrapped::*)(::godot::List<::godot::PropertyInfo> * p_list) const) & m_class::_get_property_list; \
  128. } \
  129. \
  130. static bool (::godot::Wrapped::*_get_property_can_revert())(const ::godot::StringName &p_name) const { \
  131. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name) const) & m_class::_property_can_revert; \
  132. } \
  133. \
  134. static bool (::godot::Wrapped::*_get_property_get_revert())(const ::godot::StringName &p_name, ::godot::Variant &) const { \
  135. return (bool(::godot::Wrapped::*)(const ::godot::StringName &p_name, ::godot::Variant &) const) & m_class::_property_get_revert; \
  136. } \
  137. \
  138. static ::godot::String (::godot::Wrapped::*_get_to_string())() const { \
  139. return (::godot::String(::godot::Wrapped::*)() const) & m_class::_to_string; \
  140. } \
  141. \
  142. template <class T, class B> \
  143. static void register_virtuals() { \
  144. m_inherits::register_virtuals<T, B>(); \
  145. } \
  146. \
  147. public: \
  148. static void initialize_class() { \
  149. static bool initialized = false; \
  150. if (initialized) { \
  151. return; \
  152. } \
  153. m_inherits::initialize_class(); \
  154. if (m_class::_get_bind_methods() != m_inherits::_get_bind_methods()) { \
  155. _bind_methods(); \
  156. m_inherits::register_virtuals<m_class, m_inherits>(); \
  157. } \
  158. initialized = true; \
  159. } \
  160. \
  161. static ::godot::StringName &get_class_static() { \
  162. static ::godot::StringName string_name = ::godot::StringName(#m_class); \
  163. return string_name; \
  164. } \
  165. \
  166. static ::godot::StringName &get_parent_class_static() { \
  167. return m_inherits::get_class_static(); \
  168. } \
  169. \
  170. static GDExtensionObjectPtr create(void *data) { \
  171. m_class *new_object = memnew(m_class); \
  172. return new_object->_owner; \
  173. } \
  174. \
  175. static void notification_bind(GDExtensionClassInstancePtr p_instance, int32_t p_what) { \
  176. if (p_instance && m_class::_get_notification()) { \
  177. if (m_class::_get_notification() != m_inherits::_get_notification()) { \
  178. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  179. return cls->_notification(p_what); \
  180. } \
  181. m_inherits::notification_bind(p_instance, p_what); \
  182. } \
  183. } \
  184. \
  185. static GDExtensionBool set_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionConstVariantPtr p_value) { \
  186. if (p_instance && m_class::_get_set()) { \
  187. if (m_class::_get_set() != m_inherits::_get_set()) { \
  188. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  189. return cls->_set(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<const ::godot::Variant *>(p_value)); \
  190. } \
  191. return m_inherits::set_bind(p_instance, p_name, p_value); \
  192. } \
  193. return false; \
  194. } \
  195. \
  196. static GDExtensionBool get_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { \
  197. if (p_instance && m_class::_get_get()) { \
  198. if (m_class::_get_get() != m_inherits::_get_get()) { \
  199. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  200. return cls->_get(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<::godot::Variant *>(r_ret)); \
  201. } \
  202. return m_inherits::get_bind(p_instance, p_name, r_ret); \
  203. } \
  204. return false; \
  205. } \
  206. \
  207. static const GDExtensionPropertyInfo *get_property_list_bind(GDExtensionClassInstancePtr p_instance, uint32_t *r_count) { \
  208. if (p_instance && m_class::_get_get_property_list()) { \
  209. if (m_class::_get_get_property_list() != m_inherits::_get_get_property_list()) { \
  210. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  211. ERR_FAIL_COND_V_MSG(!cls->plist_owned.is_empty() || cls->plist != nullptr || cls->plist_size != 0, nullptr, "Internal error, property list was not freed by engine!"); \
  212. cls->_get_property_list(&cls->plist_owned); \
  213. cls->plist = reinterpret_cast<GDExtensionPropertyInfo *>(memalloc(sizeof(GDExtensionPropertyInfo) * cls->plist_owned.size())); \
  214. cls->plist_size = 0; \
  215. for (const ::godot::PropertyInfo &E : cls->plist_owned) { \
  216. cls->plist[cls->plist_size].type = static_cast<GDExtensionVariantType>(E.type); \
  217. cls->plist[cls->plist_size].name = E.name._native_ptr(); \
  218. cls->plist[cls->plist_size].hint = E.hint; \
  219. cls->plist[cls->plist_size].hint_string = E.hint_string._native_ptr(); \
  220. cls->plist[cls->plist_size].class_name = E.class_name._native_ptr(); \
  221. cls->plist[cls->plist_size].usage = E.usage; \
  222. cls->plist_size++; \
  223. } \
  224. if (r_count) \
  225. *r_count = cls->plist_size; \
  226. return cls->plist; \
  227. } \
  228. return m_inherits::get_property_list_bind(p_instance, r_count); \
  229. } \
  230. return nullptr; \
  231. } \
  232. \
  233. static void free_property_list_bind(GDExtensionClassInstancePtr p_instance, const GDExtensionPropertyInfo *p_list) { \
  234. if (p_instance) { \
  235. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  236. ERR_FAIL_COND_MSG(cls->plist == nullptr, "Internal error, property list double free!"); \
  237. memfree(cls->plist); \
  238. cls->plist = nullptr; \
  239. cls->plist_size = 0; \
  240. cls->plist_owned.clear(); \
  241. } \
  242. } \
  243. \
  244. static GDExtensionBool property_can_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name) { \
  245. if (p_instance && m_class::_get_property_can_revert()) { \
  246. if (m_class::_get_property_can_revert() != m_inherits::_get_property_can_revert()) { \
  247. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  248. return cls->_property_can_revert(*reinterpret_cast<const ::godot::StringName *>(p_name)); \
  249. } \
  250. return m_inherits::property_can_revert_bind(p_instance, p_name); \
  251. } \
  252. return false; \
  253. } \
  254. \
  255. static GDExtensionBool property_get_revert_bind(GDExtensionClassInstancePtr p_instance, GDExtensionConstStringNamePtr p_name, GDExtensionVariantPtr r_ret) { \
  256. if (p_instance && m_class::_get_property_get_revert()) { \
  257. if (m_class::_get_property_get_revert() != m_inherits::_get_property_get_revert()) { \
  258. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  259. return cls->_property_get_revert(*reinterpret_cast<const ::godot::StringName *>(p_name), *reinterpret_cast<::godot::Variant *>(r_ret)); \
  260. } \
  261. return m_inherits::property_get_revert_bind(p_instance, p_name, r_ret); \
  262. } \
  263. return false; \
  264. } \
  265. \
  266. static void to_string_bind(GDExtensionClassInstancePtr p_instance, GDExtensionBool *r_is_valid, GDExtensionStringPtr r_out) { \
  267. if (p_instance && m_class::_get_to_string()) { \
  268. if (m_class::_get_to_string() != m_inherits::_get_to_string()) { \
  269. m_class *cls = reinterpret_cast<m_class *>(p_instance); \
  270. *reinterpret_cast<::godot::String *>(r_out) = cls->_to_string(); \
  271. *r_is_valid = true; \
  272. return; \
  273. } \
  274. m_inherits::to_string_bind(p_instance, r_is_valid, r_out); \
  275. } \
  276. } \
  277. \
  278. static void free(void *data, GDExtensionClassInstancePtr ptr) { \
  279. if (ptr) { \
  280. m_class *cls = reinterpret_cast<m_class *>(ptr); \
  281. cls->~m_class(); \
  282. ::godot::Memory::free_static(cls); \
  283. } \
  284. } \
  285. \
  286. static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \
  287. return nullptr; \
  288. } \
  289. \
  290. static void _gde_binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  291. } \
  292. \
  293. static GDExtensionBool _gde_binding_reference_callback(void *p_token, void *p_instance, GDExtensionBool p_reference) { \
  294. return true; \
  295. } \
  296. \
  297. static constexpr GDExtensionInstanceBindingCallbacks _gde_binding_callbacks = { \
  298. _gde_binding_create_callback, \
  299. _gde_binding_free_callback, \
  300. _gde_binding_reference_callback, \
  301. };
  302. // Don't use this for your classes, use GDCLASS() instead.
  303. #define GDEXTENSION_CLASS_ALIAS(m_class, m_alias_for, m_inherits) \
  304. private: \
  305. inline static ::godot::internal::EngineClassRegistration<m_class> _gde_engine_class_registration_helper; \
  306. void operator=(const m_class &p_rval) {} \
  307. \
  308. protected: \
  309. virtual const GDExtensionInstanceBindingCallbacks *_get_bindings_callbacks() const override { \
  310. return &_gde_binding_callbacks; \
  311. } \
  312. \
  313. m_class(const char *p_godot_class) : m_inherits(p_godot_class) {} \
  314. m_class(GodotObject *p_godot_object) : m_inherits(p_godot_object) {} \
  315. \
  316. static void (*_get_bind_methods())() { \
  317. return nullptr; \
  318. } \
  319. \
  320. static void (Wrapped::*_get_notification())(int) { \
  321. return nullptr; \
  322. } \
  323. \
  324. static bool (Wrapped::*_get_set())(const ::godot::StringName &p_name, const Variant &p_property) { \
  325. return nullptr; \
  326. } \
  327. \
  328. static bool (Wrapped::*_get_get())(const ::godot::StringName &p_name, Variant &r_ret) const { \
  329. return nullptr; \
  330. } \
  331. \
  332. static void (Wrapped::*_get_get_property_list())(List<PropertyInfo> * p_list) const { \
  333. return nullptr; \
  334. } \
  335. \
  336. static bool (Wrapped::*_get_property_can_revert())(const ::godot::StringName &p_name) const { \
  337. return nullptr; \
  338. } \
  339. \
  340. static bool (Wrapped::*_get_property_get_revert())(const ::godot::StringName &p_name, Variant &) const { \
  341. return nullptr; \
  342. } \
  343. \
  344. static String (Wrapped::*_get_to_string())() const { \
  345. return nullptr; \
  346. } \
  347. \
  348. public: \
  349. static void initialize_class() {} \
  350. \
  351. static ::godot::StringName &get_class_static() { \
  352. static ::godot::StringName string_name = ::godot::StringName(#m_alias_for); \
  353. return string_name; \
  354. } \
  355. \
  356. static ::godot::StringName &get_parent_class_static() { \
  357. return m_inherits::get_class_static(); \
  358. } \
  359. \
  360. static void *_gde_binding_create_callback(void *p_token, void *p_instance) { \
  361. /* Do not call memnew here, we don't want the post-initializer to be called */ \
  362. return new ("") m_class((GodotObject *)p_instance); \
  363. } \
  364. static void _gde_binding_free_callback(void *p_token, void *p_instance, void *p_binding) { \
  365. /* Explicitly call the deconstructor to ensure proper lifecycle for non-trivial members */ \
  366. reinterpret_cast<m_class *>(p_binding)->~m_class(); \
  367. Memory::free_static(reinterpret_cast<m_class *>(p_binding)); \
  368. } \
  369. static GDExtensionBool _gde_binding_reference_callback(void *p_token, void *p_instance, GDExtensionBool p_reference) { \
  370. return true; \
  371. } \
  372. static constexpr GDExtensionInstanceBindingCallbacks _gde_binding_callbacks = { \
  373. _gde_binding_create_callback, \
  374. _gde_binding_free_callback, \
  375. _gde_binding_reference_callback, \
  376. }; \
  377. m_class() : m_class(#m_alias_for) {}
  378. // Don't use this for your classes, use GDCLASS() instead.
  379. #define GDEXTENSION_CLASS(m_class, m_inherits) GDEXTENSION_CLASS_ALIAS(m_class, m_class, m_inherits)
  380. #endif // GODOT_WRAPPED_HPP