wrapped.hpp 54 KB

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