wrapped.hpp 43 KB

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