class_db.hpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* class_db.hpp */
  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_HPP
  31. #define CLASS_DB_HPP
  32. #include <godot/gdnative_interface.h>
  33. #include <godot_cpp/core/defs.hpp>
  34. #include <godot_cpp/core/error_macros.hpp>
  35. #include <godot_cpp/core/method_bind.hpp>
  36. #include <godot_cpp/core/object.hpp>
  37. #include <list>
  38. #include <string>
  39. #include <unordered_map>
  40. #include <vector>
  41. namespace godot {
  42. struct MethodDefinition {
  43. const char *name = nullptr;
  44. std::list<std::string> args;
  45. MethodDefinition() {}
  46. MethodDefinition(const char *p_name) :
  47. name(p_name) {}
  48. };
  49. MethodDefinition D_METHOD(const char *p_name);
  50. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1);
  51. template <typename... Args>
  52. MethodDefinition D_METHOD(const char *p_name, const char *p_arg1, Args... args) {
  53. MethodDefinition md = D_METHOD(p_name, args...);
  54. md.args.push_front(p_arg1);
  55. return md;
  56. }
  57. class ClassDB {
  58. static GDNativeInitializationLevel current_level;
  59. friend class godot::GDExtensionBinding;
  60. public:
  61. struct PropertySetGet {
  62. int index;
  63. const char *setter;
  64. const char *getter;
  65. MethodBind *_setptr;
  66. MethodBind *_getptr;
  67. Variant::Type type;
  68. };
  69. struct ClassInfo {
  70. const char *name = nullptr;
  71. const char *parent_name = nullptr;
  72. GDNativeInitializationLevel level = GDNATIVE_INITIALIZATION_SCENE;
  73. std::unordered_map<std::string, MethodBind *> method_map;
  74. std::unordered_map<std::string, MethodInfo> signal_map;
  75. std::list<MethodBind *> method_order;
  76. GDExtensionClassInstancePtr (*constructor)(void *data);
  77. std::unordered_map<std::string, GDNativeExtensionClassCallVirtual> virtual_methods;
  78. void (*destructor)(void *data, GDExtensionClassInstancePtr ptr);
  79. void (*object_instance)(GDExtensionClassInstancePtr p_instance, GDNativeObjectPtr p_object_instance);
  80. std::unordered_map<std::string, PropertySetGet> property_setget;
  81. std::list<PropertyInfo> property_list;
  82. std::unordered_map<std::string, std::pair<std::string, GDNativeInt>> constant_map; // String in pair is enum name.
  83. std::list<std::string> constant_order;
  84. ClassInfo *parent_ptr = nullptr;
  85. };
  86. private:
  87. static std::unordered_map<std::string, ClassInfo> classes;
  88. static MethodBind *bind_methodfi(uint32_t p_flags, MethodBind *p_bind, const MethodDefinition &method_name, const void **p_defs, int p_defcount);
  89. public:
  90. template <class T>
  91. static void register_class();
  92. template <class N, class M>
  93. static MethodBind *bind_method(N p_method_name, M p_method);
  94. template <class M>
  95. static MethodBind *bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info = MethodInfo(), const std::vector<Variant> &p_default_args = std::vector<Variant>{}, bool p_return_nil_is_variant = true);
  96. static void add_property_group(const char *p_class, const char *p_name, const char *p_prefix);
  97. static void add_property_subgroup(const char *p_class, const char *p_name, const char *p_prefix);
  98. static void add_property(const char *p_class, const PropertyInfo &p_pinfo, const char *p_setter, const char *p_getter, int p_index = -1);
  99. static void add_signal(const char *p_class, const MethodInfo &p_signal);
  100. static void bind_integer_constant(const char *p_class, const char *p_enum, const char *p_name, GDNativeInt p_constant);
  101. static void bind_virtual_method(const char *p_class, const char *p_method, GDNativeExtensionClassCallVirtual p_call);
  102. static MethodBind *get_method(const char *p_class, const char *p_method);
  103. static GDNativeExtensionClassCallVirtual get_virtual_func(void *p_userdata, const char *p_name);
  104. static void initialize(GDNativeInitializationLevel p_level);
  105. static void deinitialize(GDNativeInitializationLevel p_level);
  106. };
  107. #define BIND_CONSTANT(m_constant) \
  108. ClassDB::bind_integer_constant(get_class_static(), "", #m_constant, m_constant);
  109. #define BIND_ENUM_CONSTANT(m_constant) \
  110. ClassDB::bind_integer_constant(get_class_static(), __constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);
  111. #define BIND_VIRTUAL_METHOD(m_class, m_method) \
  112. { \
  113. auto ___call##m_method = [](GDNativeObjectPtr p_instance, const GDNativeTypePtr *p_args, GDNativeTypePtr p_ret) -> void { \
  114. call_with_ptr_args(reinterpret_cast<m_class *>(p_instance), &m_class::m_method, p_args, p_ret); \
  115. }; \
  116. ClassDB::bind_virtual_method(m_class::get_class_static(), #m_method, ___call##m_method); \
  117. }
  118. template <class T>
  119. void ClassDB::register_class() {
  120. ClassInfo cl;
  121. cl.name = T::get_class_static();
  122. cl.parent_name = T::get_parent_class_static();
  123. cl.level = current_level;
  124. cl.constructor = T::create;
  125. cl.destructor = T::free;
  126. cl.object_instance = T::set_object_instance;
  127. classes[cl.name] = cl;
  128. if (classes.find(cl.parent_name) != classes.end()) {
  129. cl.parent_ptr = &classes[cl.parent_name];
  130. }
  131. T::initialize_class();
  132. }
  133. template <class N, class M>
  134. MethodBind *ClassDB::bind_method(N p_method_name, M p_method) {
  135. MethodBind *bind = create_method_bind(p_method);
  136. return bind_methodfi(0, bind, p_method_name, nullptr, 0);
  137. }
  138. template <class M>
  139. MethodBind *ClassDB::bind_vararg_method(uint32_t p_flags, const char *p_name, M p_method, const MethodInfo &p_info, const std::vector<Variant> &p_default_args, bool p_return_nil_is_variant) {
  140. MethodBind *bind = create_vararg_method_bind(p_method, p_info, p_return_nil_is_variant);
  141. ERR_FAIL_COND_V(!bind, nullptr);
  142. bind->set_name(p_name);
  143. bind->set_default_arguments(p_default_args);
  144. const char *instance_type = bind->get_instance_class();
  145. std::unordered_map<std::string, ClassInfo>::iterator type_it = classes.find(instance_type);
  146. if (type_it == classes.end()) {
  147. memdelete(bind);
  148. ERR_FAIL_V_MSG(nullptr, "Class doesn't exist.");
  149. }
  150. ClassInfo &type = type_it->second;
  151. if (type.method_map.find(p_name) != type.method_map.end()) {
  152. memdelete(bind);
  153. ERR_FAIL_V_MSG(nullptr, "Binding duplicate method.");
  154. }
  155. type.method_map[p_name] = bind;
  156. type.method_order.push_back(bind);
  157. return bind;
  158. }
  159. } // namespace godot
  160. #endif // ! CLASS_DB_HPP