class_db.h 17 KB

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