class_db.h 19 KB

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