class_db.h 17 KB

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