wrapped.hpp 61 KB

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