class_db.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*************************************************************************/
  2. /* class_db.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 CLASS_DB_H
  31. #define CLASS_DB_H
  32. #include "core/object/method_bind.h"
  33. #include "core/object/object.h"
  34. #include "core/string/print_string.h"
  35. /** To bind more then 6 parameters include this:
  36. *
  37. */
  38. // Makes callable_mp readily available in all classes connecting signals.
  39. // Needs to come after method_bind and object have been included.
  40. #include "core/object/callable_method_pointer.h"
  41. #define DEFVAL(m_defval) (m_defval)
  42. #ifdef DEBUG_METHODS_ENABLED
  43. struct MethodDefinition {
  44. StringName name;
  45. Vector<StringName> args;
  46. MethodDefinition() {}
  47. MethodDefinition(const char *p_name) :
  48. name(p_name) {}
  49. MethodDefinition(const StringName &p_name) :
  50. name(p_name) {}
  51. };
  52. MethodDefinition D_METHOD(const char *p_name);
  53. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1);
  54. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2);
  55. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3);
  56. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4);
  57. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5);
  58. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6);
  59. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7);
  60. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8);
  61. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9);
  62. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10);
  63. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11);
  64. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12);
  65. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4, const char *p_arg5, const char *p_arg6, const char *p_arg7, const char *p_arg8, const char *p_arg9, const char *p_arg10, const char *p_arg11, const char *p_arg12, const char *p_arg13);
  66. #else
  67. //#define NO_VARIADIC_MACROS
  68. #ifdef NO_VARIADIC_MACROS
  69. static _FORCE_INLINE_ const char *D_METHOD(const char *m_name, ...) {
  70. return m_name;
  71. }
  72. #else
  73. // When DEBUG_METHODS_ENABLED is set this will let the engine know
  74. // the argument names for easier debugging.
  75. #define D_METHOD(m_c, ...) m_c
  76. #endif
  77. #endif
  78. class ClassDB {
  79. public:
  80. enum APIType {
  81. API_CORE,
  82. API_EDITOR,
  83. API_NONE
  84. };
  85. public:
  86. struct PropertySetGet {
  87. int index;
  88. StringName setter;
  89. StringName getter;
  90. MethodBind *_setptr;
  91. MethodBind *_getptr;
  92. Variant::Type type;
  93. };
  94. struct ClassInfo {
  95. APIType api = API_NONE;
  96. ClassInfo *inherits_ptr = nullptr;
  97. void *class_ptr = nullptr;
  98. HashMap<StringName, MethodBind *> method_map;
  99. HashMap<StringName, int> constant_map;
  100. HashMap<StringName, List<StringName>> enum_map;
  101. HashMap<StringName, MethodInfo> signal_map;
  102. List<PropertyInfo> property_list;
  103. HashMap<StringName, PropertyInfo> property_map;
  104. #ifdef DEBUG_METHODS_ENABLED
  105. List<StringName> constant_order;
  106. List<StringName> method_order;
  107. Set<StringName> methods_in_properties;
  108. List<MethodInfo> virtual_methods;
  109. Map<StringName, MethodInfo> virtual_methods_map;
  110. StringName category;
  111. #endif
  112. HashMap<StringName, PropertySetGet> property_setget;
  113. StringName inherits;
  114. StringName name;
  115. bool disabled = false;
  116. bool exposed = false;
  117. Object *(*creation_func)() = nullptr;
  118. ClassInfo() {}
  119. ~ClassInfo() {}
  120. };
  121. template <class T>
  122. static Object *creator() {
  123. return memnew(T);
  124. }
  125. static RWLock lock;
  126. static HashMap<StringName, ClassInfo> classes;
  127. static HashMap<StringName, StringName> resource_base_extensions;
  128. static HashMap<StringName, StringName> compat_classes;
  129. #ifdef DEBUG_METHODS_ENABLED
  130. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount);
  131. #else
  132. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount);
  133. #endif
  134. static APIType current_api;
  135. static void _add_class2(const StringName &p_class, const StringName &p_inherits);
  136. static HashMap<StringName, HashMap<StringName, Variant>> default_values;
  137. static Set<StringName> default_values_cached;
  138. private:
  139. // Non-locking variants of get_parent_class and is_parent_class.
  140. static StringName _get_parent_class(const StringName &p_class);
  141. static bool _is_parent_class(const StringName &p_class, const StringName &p_inherits);
  142. public:
  143. // DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
  144. template <class T>
  145. static void _add_class() {
  146. _add_class2(T::get_class_static(), T::get_parent_class_static());
  147. }
  148. template <class T>
  149. static void register_class() {
  150. GLOBAL_LOCK_FUNCTION;
  151. T::initialize_class();
  152. ClassInfo *t = classes.getptr(T::get_class_static());
  153. ERR_FAIL_COND(!t);
  154. t->creation_func = &creator<T>;
  155. t->exposed = true;
  156. t->class_ptr = T::get_class_ptr_static();
  157. T::register_custom_data_to_otdb();
  158. }
  159. template <class T>
  160. static void register_virtual_class() {
  161. GLOBAL_LOCK_FUNCTION;
  162. T::initialize_class();
  163. ClassInfo *t = classes.getptr(T::get_class_static());
  164. ERR_FAIL_COND(!t);
  165. t->exposed = true;
  166. t->class_ptr = T::get_class_ptr_static();
  167. //nothing
  168. }
  169. template <class T>
  170. static Object *_create_ptr_func() {
  171. return T::create();
  172. }
  173. template <class T>
  174. static void register_custom_instance_class() {
  175. GLOBAL_LOCK_FUNCTION;
  176. T::initialize_class();
  177. ClassInfo *t = classes.getptr(T::get_class_static());
  178. ERR_FAIL_COND(!t);
  179. t->creation_func = &_create_ptr_func<T>;
  180. t->exposed = true;
  181. t->class_ptr = T::get_class_ptr_static();
  182. T::register_custom_data_to_otdb();
  183. }
  184. static void get_class_list(List<StringName> *p_classes);
  185. static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  186. static void get_direct_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  187. static StringName get_parent_class_nocheck(const StringName &p_class);
  188. static StringName get_parent_class(const StringName &p_class);
  189. static StringName get_compatibility_remapped_class(const StringName &p_class);
  190. static bool class_exists(const StringName &p_class);
  191. static bool is_parent_class(const StringName &p_class, const StringName &p_inherits);
  192. static bool can_instance(const StringName &p_class);
  193. static Object *instance(const StringName &p_class);
  194. static APIType get_api_type(const StringName &p_class);
  195. static uint64_t get_api_hash(APIType p_api);
  196. template <class N, class M>
  197. static MethodBind *bind_method(N p_method_name, M p_method) {
  198. MethodBind *bind = create_method_bind(p_method);
  199. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, nullptr, 0); //use static function, much smaller binary usage
  200. }
  201. template <class N, class M>
  202. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1) {
  203. MethodBind *bind = create_method_bind(p_method);
  204. const Variant *ptr[1] = { &p_def1 };
  205. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 1);
  206. }
  207. template <class N, class M>
  208. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2) {
  209. MethodBind *bind = create_method_bind(p_method);
  210. const Variant *ptr[2] = { &p_def1, &p_def2 };
  211. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 2);
  212. }
  213. template <class N, class M>
  214. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3) {
  215. MethodBind *bind = create_method_bind(p_method);
  216. const Variant *ptr[3] = { &p_def1, &p_def2, &p_def3 };
  217. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 3);
  218. }
  219. template <class N, class M>
  220. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3, const Variant &p_def4) {
  221. MethodBind *bind = create_method_bind(p_method);
  222. const Variant *ptr[4] = { &p_def1, &p_def2, &p_def3, &p_def4 };
  223. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 4);
  224. }
  225. template <class N, class M>
  226. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3, const Variant &p_def4, const Variant &p_def5) {
  227. MethodBind *bind = create_method_bind(p_method);
  228. const Variant *ptr[5] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5 };
  229. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 5);
  230. }
  231. template <class N, class M>
  232. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3, const Variant &p_def4, const Variant &p_def5, const Variant &p_def6) {
  233. MethodBind *bind = create_method_bind(p_method);
  234. const Variant *ptr[6] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5, &p_def6 };
  235. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 6);
  236. }
  237. template <class N, class M>
  238. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3, const Variant &p_def4, const Variant &p_def5, const Variant &p_def6, const Variant &p_def7) {
  239. MethodBind *bind = create_method_bind(p_method);
  240. const Variant *ptr[7] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5, &p_def6, &p_def7 };
  241. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 7);
  242. }
  243. template <class N, class M>
  244. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3, const Variant &p_def4, const Variant &p_def5, const Variant &p_def6, const Variant &p_def7, const Variant &p_def8) {
  245. MethodBind *bind = create_method_bind(p_method);
  246. const Variant *ptr[8] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5, &p_def6, &p_def7, &p_def8 };
  247. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 8);
  248. }
  249. template <class M>
  250. static MethodBind *bind_vararg_method(uint32_t p_flags, StringName p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const Vector<Variant> &p_default_args = Vector<Variant>(), bool p_return_nil_is_variant = true) {
  251. GLOBAL_LOCK_FUNCTION;
  252. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  253. ERR_FAIL_COND_V(!bind, nullptr);
  254. bind->set_name(p_name);
  255. bind->set_default_arguments(p_default_args);
  256. String instance_type = bind->get_instance_class();
  257. ClassInfo *type = classes.getptr(instance_type);
  258. if (!type) {
  259. memdelete(bind);
  260. ERR_FAIL_COND_V(!type, nullptr);
  261. }
  262. if (type->method_map.has(p_name)) {
  263. memdelete(bind);
  264. // overloading not supported
  265. ERR_FAIL_V_MSG(nullptr, "Method already bound: " + instance_type + "::" + p_name + ".");
  266. }
  267. type->method_map[p_name] = bind;
  268. #ifdef DEBUG_METHODS_ENABLED
  269. // FIXME: <reduz> set_return_type is no longer in MethodBind, so I guess it should be moved to vararg method bind
  270. //bind->set_return_type("Variant");
  271. type->method_order.push_back(p_name);
  272. #endif
  273. return bind;
  274. }
  275. static void add_signal(StringName p_class, const MethodInfo &p_signal);
  276. static bool has_signal(StringName p_class, StringName p_signal, bool p_no_inheritance = false);
  277. static bool get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal);
  278. static void get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance = false);
  279. static void add_property_group(StringName p_class, const String &p_name, const String &p_prefix = "");
  280. static void add_property_subgroup(StringName p_class, const String &p_name, const String &p_prefix = "");
  281. static void add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
  282. static void set_property_default_value(StringName p_class, const StringName &p_name, const Variant &p_default);
  283. static void get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance = false, const Object *p_validator = nullptr);
  284. static bool get_property_info(StringName p_class, StringName p_property, PropertyInfo *r_info, bool p_no_inheritance = false, const Object *p_validator = nullptr);
  285. static bool set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid = nullptr);
  286. static bool get_property(Object *p_object, const StringName &p_property, Variant &r_value);
  287. static bool has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance = false);
  288. static int get_property_index(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  289. static Variant::Type get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid = nullptr);
  290. static StringName get_property_setter(StringName p_class, const StringName &p_property);
  291. static StringName get_property_getter(StringName p_class, const StringName &p_property);
  292. static bool has_method(StringName p_class, StringName p_method, bool p_no_inheritance = false);
  293. static void set_method_flags(StringName p_class, StringName p_method, int p_flags);
  294. static void get_method_list(StringName p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  295. static bool get_method_info(StringName p_class, StringName p_method, MethodInfo *r_info, bool p_no_inheritance = false, bool p_exclude_from_properties = false);
  296. static MethodBind *get_method(StringName p_class, StringName p_name);
  297. static void add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual = true);
  298. static void get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);
  299. static void bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant);
  300. static void get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance = false);
  301. static int get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success = nullptr);
  302. static bool has_integer_constant(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  303. static StringName get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  304. static void get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance = false);
  305. static void get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance = false);
  306. static bool has_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
  307. static Variant class_get_default_property_value(const StringName &p_class, const StringName &p_property, bool *r_valid = nullptr);
  308. static StringName get_category(const StringName &p_node);
  309. static void set_class_enabled(StringName p_class, bool p_enable);
  310. static bool is_class_enabled(StringName p_class);
  311. static bool is_class_exposed(StringName p_class);
  312. static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
  313. static void get_resource_base_extensions(List<String> *p_extensions);
  314. static void get_extensions_for_type(const StringName &p_class, List<String> *p_extensions);
  315. static void add_compatibility_class(const StringName &p_class, const StringName &p_fallback);
  316. static void set_current_api(APIType p_api);
  317. static APIType get_current_api();
  318. static void cleanup_defaults();
  319. static void cleanup();
  320. };
  321. #ifdef DEBUG_METHODS_ENABLED
  322. #define BIND_CONSTANT(m_constant) \
  323. ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  324. #define BIND_ENUM_CONSTANT(m_constant) \
  325. ClassDB::bind_integer_constant(get_class_static(), __constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  326. #else
  327. #define BIND_CONSTANT(m_constant) \
  328. ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  329. #define BIND_ENUM_CONSTANT(m_constant) \
  330. ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);
  331. #endif
  332. #ifdef TOOLS_ENABLED
  333. #define BIND_VMETHOD(m_method) \
  334. ClassDB::add_virtual_method(get_class_static(), m_method);
  335. #else
  336. #define BIND_VMETHOD(m_method)
  337. #endif
  338. #endif // CLASS_DB_H