class_db.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. /*************************************************************************/
  2. /* class_db.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef CLASS_DB_H
  30. #define CLASS_DB_H
  31. #include "method_bind.h"
  32. #include "object.h"
  33. #include "print_string.h"
  34. /**
  35. @author Juan Linietsky <[email protected]>
  36. */
  37. struct ParamHint {
  38. String name;
  39. PropertyHint hint;
  40. String hint_text;
  41. Variant default_val;
  42. ParamHint(const String &p_name = "", PropertyHint p_hint = PROPERTY_HINT_NONE, const String &p_hint_text = "", Variant p_default_val = Variant()) {
  43. name = p_name;
  44. hint = p_hint;
  45. hint_text = p_hint_text;
  46. default_val = p_default_val;
  47. }
  48. };
  49. struct ParamDef {
  50. bool used;
  51. Variant val;
  52. _FORCE_INLINE_ ParamDef() { used = false; }
  53. ParamDef(const Variant &p_variant);
  54. };
  55. //#define DEFVAL( m_defval ) ParamDef(m_defval)
  56. #define DEFVAL(m_defval) (m_defval)
  57. //#define SIMPLE_METHODDEF
  58. #ifdef DEBUG_METHODS_ENABLED
  59. struct MethodDefinition {
  60. StringName name;
  61. Vector<StringName> args;
  62. MethodDefinition() {}
  63. MethodDefinition(const char *p_name) { name = p_name; }
  64. MethodDefinition(const StringName &p_name) { name = p_name; }
  65. };
  66. MethodDefinition D_METHOD(const char *p_name);
  67. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1);
  68. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2);
  69. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3);
  70. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, const char *p_arg2, const char *p_arg3, const char *p_arg4);
  71. 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);
  72. 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);
  73. 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);
  74. 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);
  75. 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);
  76. 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);
  77. #else
  78. //#define NO_VARIADIC_MACROS
  79. #ifdef NO_VARIADIC_MACROS
  80. static _FORCE_INLINE_ const char *D_METHOD(const char *m_name, ...) {
  81. return m_name;
  82. }
  83. #else
  84. // When DEBUG_METHODS_ENABLED is set this will let the engine know
  85. // the argument names for easier debugging.
  86. #define D_METHOD(m_c, ...) m_c
  87. #endif
  88. #endif
  89. class ClassDB {
  90. public:
  91. enum APIType {
  92. API_CORE,
  93. API_EDITOR,
  94. API_NONE
  95. };
  96. public:
  97. struct PropertySetGet {
  98. int index;
  99. StringName setter;
  100. StringName getter;
  101. MethodBind *_setptr;
  102. MethodBind *_getptr;
  103. Variant::Type type;
  104. };
  105. struct ClassInfo {
  106. APIType api;
  107. ClassInfo *inherits_ptr;
  108. HashMap<StringName, MethodBind *, StringNameHasher> method_map;
  109. HashMap<StringName, int, StringNameHasher> constant_map;
  110. HashMap<StringName, MethodInfo, StringNameHasher> signal_map;
  111. List<PropertyInfo> property_list;
  112. #ifdef DEBUG_METHODS_ENABLED
  113. List<StringName> constant_order;
  114. List<StringName> method_order;
  115. List<MethodInfo> virtual_methods;
  116. StringName category;
  117. #endif
  118. HashMap<StringName, PropertySetGet, StringNameHasher> property_setget;
  119. StringName inherits;
  120. StringName name;
  121. bool disabled;
  122. Object *(*creation_func)();
  123. ClassInfo();
  124. ~ClassInfo();
  125. };
  126. template <class T>
  127. static Object *creator() {
  128. return memnew(T);
  129. }
  130. static RWLock *lock;
  131. static HashMap<StringName, ClassInfo, StringNameHasher> classes;
  132. static HashMap<StringName, StringName, StringNameHasher> resource_base_extensions;
  133. static HashMap<StringName, StringName, StringNameHasher> compat_classes;
  134. #ifdef DEBUG_METHODS_ENABLED
  135. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const Variant **p_defs, int p_defcount);
  136. #else
  137. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const char *method_name, const Variant **p_defs, int p_defcount);
  138. #endif
  139. static APIType current_api;
  140. static void _add_class2(const StringName &p_class, const StringName &p_inherits);
  141. public:
  142. // DO NOT USE THIS!!!!!! NEEDS TO BE PUBLIC BUT DO NOT USE NO MATTER WHAT!!!
  143. template <class T>
  144. static void _add_class() {
  145. _add_class2(T::get_class_static(), T::get_parent_class_static());
  146. #if 0
  147. GLOBAL_LOCK_FUNCTION;
  148. StringName name = T::get_class_static();
  149. ERR_FAIL_COND(types.has(name));
  150. types[name]=TypeInfo();
  151. TypeInfo &ti=types[name];
  152. ti.name=name;
  153. ti.inherits=T::get_parent_class_static();
  154. if (ti.inherits) {
  155. ERR_FAIL_COND( !types.has(ti.inherits) ); //it MUST be registered.
  156. ti.inherits_ptr = &types[ti.inherits];
  157. } else {
  158. ti.inherits_ptr=NULL;
  159. }
  160. #endif
  161. }
  162. template <class T>
  163. static void register_class() {
  164. GLOBAL_LOCK_FUNCTION;
  165. T::initialize_class();
  166. ClassInfo *t = classes.getptr(T::get_class_static());
  167. ERR_FAIL_COND(!t);
  168. t->creation_func = &creator<T>;
  169. T::register_custom_data_to_otdb();
  170. }
  171. template <class T>
  172. static void register_virtual_class() {
  173. GLOBAL_LOCK_FUNCTION;
  174. T::initialize_class();
  175. //nothing
  176. }
  177. template <class T>
  178. static Object *_create_ptr_func() {
  179. return T::create();
  180. }
  181. template <class T>
  182. static void register_custom_instance_class() {
  183. GLOBAL_LOCK_FUNCTION;
  184. T::initialize_class();
  185. ClassInfo *t = classes.getptr(T::get_class_static());
  186. ERR_FAIL_COND(!t);
  187. t->creation_func = &_create_ptr_func<T>;
  188. T::register_custom_data_to_otdb();
  189. }
  190. static void get_class_list(List<StringName> *p_classes);
  191. static void get_inheriters_from_class(const StringName &p_class, List<StringName> *p_classes);
  192. static StringName get_parent_class_nocheck(const StringName &p_class);
  193. static StringName get_parent_class(const StringName &p_class);
  194. static bool class_exists(const StringName &p_class);
  195. static bool is_parent_class(const StringName &p_class, const StringName &p_inherits);
  196. static bool can_instance(const StringName &p_class);
  197. static Object *instance(const StringName &p_class);
  198. static APIType get_api_type(const StringName &p_class);
  199. static uint64_t get_api_hash(APIType p_api);
  200. #if 0
  201. template<class N, class M>
  202. static MethodBind* bind_method(N p_method_name, M p_method,
  203. //default arguments
  204. ParamDef d1=ParamDef(),
  205. ParamDef d2=ParamDef(),
  206. ParamDef d3=ParamDef(),
  207. ParamDef d4=ParamDef(),
  208. ParamDef d5=ParamDef()
  209. ) {
  210. return bind_methodf(METHOD_FLAGS_DEFAULT,p_method_name, p_method, d1,d2,d3,d4,d5);
  211. }
  212. template<class N, class M>
  213. static MethodBind* bind_methodf(uint32_t p_flags, N p_method_name, M p_method,
  214. //default arguments
  215. const ParamDef &d1=ParamDef(),
  216. const ParamDef &d2=ParamDef(),
  217. const ParamDef &d3=ParamDef(),
  218. const ParamDef &d4=ParamDef(),
  219. const ParamDef &d5=ParamDef()
  220. ) {
  221. GLOBAL_LOCK_FUNCTION;
  222. MethodDefinition method_name=p_method_name;
  223. MethodBind *bind = create_method_bind(p_method);
  224. bind->set_name(method_name.name);
  225. ERR_FAIL_COND_V(!bind,NULL);
  226. String instance_type=bind->get_instance_type();
  227. TypeInfo *type=types.getptr(instance_type);
  228. if (!type) {
  229. memdelete(bind);
  230. ERR_FAIL_COND_V(!type,NULL);
  231. }
  232. if (type->method_map.has(method_name.name)) {
  233. memdelete(bind);
  234. // overloading not supported
  235. ERR_EXPLAIN("Method already bound: "+instance_type+"::"+method_name.name);
  236. ERR_FAIL_V(NULL);
  237. }
  238. bind->set_argument_names(method_name.args);
  239. type->method_map[method_name.name]=bind;
  240. Vector<Variant> defvals;
  241. #define PARSE_DEFVAL(m_defval) \
  242. if (d##m_defval.used) \
  243. defvals.insert(0, d##m_defval.val); \
  244. else \
  245. goto set_defvals;
  246. PARSE_DEFVAL(1);
  247. PARSE_DEFVAL(2);
  248. PARSE_DEFVAL(3);
  249. PARSE_DEFVAL(4);
  250. PARSE_DEFVAL(5);
  251. set_defvals:
  252. bind->set_default_arguments(defvals);
  253. bind->set_hint_flags(p_flags);
  254. return bind;
  255. #undef PARSE_DEFVAL
  256. }
  257. #else
  258. #if 0
  259. template<class N, class M>
  260. static MethodBind* bind_method(N p_method_name, M p_method,
  261. //default arguments
  262. const ParamDef &d1=ParamDef(),
  263. const ParamDef &d2=ParamDef(),
  264. const ParamDef &d3=ParamDef(),
  265. const ParamDef &d4=ParamDef(),
  266. const ParamDef &d5=ParamDef()
  267. ) {
  268. MethodDefinition method_name=p_method_name;
  269. MethodBind *bind = create_method_bind(p_method);
  270. return bind_methodfi(METHOD_FLAGS_DEFAULT,bind,method_name,d1,d2,d3,d4,d5); //use static function, much smaller binary usage
  271. }
  272. #endif
  273. template <class N, class M>
  274. static MethodBind *bind_method(N p_method_name, M p_method) {
  275. MethodBind *bind = create_method_bind(p_method);
  276. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, NULL, 0); //use static function, much smaller binary usage
  277. }
  278. template <class N, class M>
  279. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1) {
  280. MethodBind *bind = create_method_bind(p_method);
  281. const Variant *ptr[1] = { &p_def1 };
  282. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 1);
  283. }
  284. template <class N, class M>
  285. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2) {
  286. MethodBind *bind = create_method_bind(p_method);
  287. const Variant *ptr[2] = { &p_def1, &p_def2 };
  288. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 2);
  289. }
  290. template <class N, class M>
  291. static MethodBind *bind_method(N p_method_name, M p_method, const Variant &p_def1, const Variant &p_def2, const Variant &p_def3) {
  292. MethodBind *bind = create_method_bind(p_method);
  293. const Variant *ptr[3] = { &p_def1, &p_def2, &p_def3 };
  294. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 3);
  295. }
  296. template <class N, class M>
  297. 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) {
  298. MethodBind *bind = create_method_bind(p_method);
  299. const Variant *ptr[4] = { &p_def1, &p_def2, &p_def3, &p_def4 };
  300. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 4);
  301. }
  302. template <class N, class M>
  303. 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) {
  304. MethodBind *bind = create_method_bind(p_method);
  305. const Variant *ptr[5] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5 };
  306. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 5);
  307. }
  308. template <class N, class M>
  309. 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) {
  310. MethodBind *bind = create_method_bind(p_method);
  311. const Variant *ptr[6] = { &p_def1, &p_def2, &p_def3, &p_def4, &p_def5, &p_def6 };
  312. return bind_methodfi(METHOD_FLAGS_DEFAULT, bind, p_method_name, ptr, 6);
  313. }
  314. #if 0
  315. template<class N, class M>
  316. static MethodBind* bind_methodf(uint32_t p_flags, N p_method_name, M p_method,
  317. const ParamDef& d1=ParamDef(),
  318. const ParamDef& d2=ParamDef(),
  319. const ParamDef& d3=ParamDef(),
  320. const ParamDef& d4=ParamDef(),
  321. const ParamDef& d5=ParamDef()
  322. ) {
  323. MethodDefinition method_name=p_method_name;
  324. MethodBind *bind = create_method_bind(p_method);
  325. return bind_methodfi(p_flags,bind,method_name,d1,d2,d3,d4,d5); //use static function, much smaller binary usage
  326. }
  327. #endif
  328. #endif
  329. template <class M>
  330. 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>()) {
  331. GLOBAL_LOCK_FUNCTION;
  332. MethodBind *bind = create_vararg_method_bind(p_method, p_info);
  333. ERR_FAIL_COND_V(!bind, NULL);
  334. String rettype;
  335. if (p_name.operator String().find(":") != -1) {
  336. rettype = p_name.operator String().get_slice(":", 1);
  337. p_name = p_name.operator String().get_slice(":", 0);
  338. }
  339. bind->set_name(p_name);
  340. bind->set_default_arguments(p_default_args);
  341. String instance_type = bind->get_instance_class();
  342. ClassInfo *type = classes.getptr(instance_type);
  343. if (!type) {
  344. memdelete(bind);
  345. ERR_FAIL_COND_V(!type, NULL);
  346. }
  347. if (type->method_map.has(p_name)) {
  348. memdelete(bind);
  349. // overloading not supported
  350. ERR_EXPLAIN("Method already bound: " + instance_type + "::" + p_name);
  351. ERR_FAIL_V(NULL);
  352. }
  353. type->method_map[p_name] = bind;
  354. #ifdef DEBUG_METHODS_ENABLED
  355. if (!rettype.empty())
  356. bind->set_return_type(rettype);
  357. type->method_order.push_back(p_name);
  358. #endif
  359. return bind;
  360. }
  361. static void add_signal(StringName p_class, const MethodInfo &p_signal);
  362. static bool has_signal(StringName p_class, StringName p_signal);
  363. static bool get_signal(StringName p_class, StringName p_signal, MethodInfo *r_signal);
  364. static void get_signal_list(StringName p_class, List<MethodInfo> *p_signals, bool p_no_inheritance = false);
  365. static void add_property_group(StringName p_class, const String &p_name, const String &p_prefix = "");
  366. static void add_property(StringName p_class, const PropertyInfo &p_pinfo, const StringName &p_setter, const StringName &p_getter, int p_index = -1);
  367. static void get_property_list(StringName p_class, List<PropertyInfo> *p_list, bool p_no_inheritance = false, const Object *p_validator = NULL);
  368. static bool set_property(Object *p_object, const StringName &p_property, const Variant &p_value, bool *r_valid = NULL);
  369. static bool get_property(Object *p_object, const StringName &p_property, Variant &r_value);
  370. static bool has_property(const StringName &p_class, const StringName &p_property, bool p_no_inheritance = false);
  371. static Variant::Type get_property_type(const StringName &p_class, const StringName &p_property, bool *r_is_valid = NULL);
  372. static StringName get_property_setter(StringName p_class, const StringName p_property);
  373. static StringName get_property_getter(StringName p_class, const StringName p_property);
  374. static bool has_method(StringName p_class, StringName p_method, bool p_no_inheritance = false);
  375. static void set_method_flags(StringName p_class, StringName p_method, int p_flags);
  376. static void get_method_list(StringName p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);
  377. static MethodBind *get_method(StringName p_class, StringName p_name);
  378. static void add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual = true);
  379. static void get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);
  380. static void bind_integer_constant(const StringName &p_class, const StringName &p_name, int p_constant);
  381. static void get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance = false);
  382. static int get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success = NULL);
  383. static StringName get_category(const StringName &p_node);
  384. static bool get_setter_and_type_for_property(const StringName &p_class, const StringName &p_prop, StringName &r_class, StringName &r_setter);
  385. static void set_class_enabled(StringName p_class, bool p_enable);
  386. static bool is_class_enabled(StringName p_class);
  387. static void add_resource_base_extension(const StringName &p_extension, const StringName &p_class);
  388. static void get_resource_base_extensions(List<String> *p_extensions);
  389. static void get_extensions_for_type(const StringName &p_class, List<String> *p_extensions);
  390. static void add_compatibility_class(const StringName &p_class, const StringName &p_fallback);
  391. static void init();
  392. static void set_current_api(APIType p_api);
  393. static void cleanup();
  394. };
  395. #define BIND_CONSTANT(m_constant) \
  396. ClassDB::bind_integer_constant(get_class_static(), #m_constant, m_constant);
  397. #ifdef TOOLS_ENABLED
  398. #define BIND_VMETHOD(m_method) \
  399. ClassDB::add_virtual_method(get_class_static(), m_method);
  400. #else
  401. #define BIND_VMETHOD(m_method)
  402. #endif
  403. #endif // CLASS_DB_H