object.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* object.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 GODOT_OBJECT_HPP
  31. #define GODOT_OBJECT_HPP
  32. #include <godot_cpp/core/defs.hpp>
  33. #include <godot_cpp/variant/variant.hpp>
  34. #include <godot_cpp/classes/object.hpp>
  35. #include <godot_cpp/godot.hpp>
  36. #include <godot/gdnative_interface.h>
  37. #include <vector>
  38. #define ADD_SIGNAL(m_signal) ClassDB::add_signal(get_class_static(), m_signal)
  39. #define ADD_PROPERTY(m_property, m_setter, m_getter) ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
  40. namespace godot {
  41. struct PropertyInfo {
  42. Variant::Type type = Variant::NIL;
  43. const char *name = nullptr;
  44. const char *class_name = nullptr;
  45. uint32_t hint = 0;
  46. const char *hint_string = nullptr;
  47. uint32_t usage = 7;
  48. operator GDNativePropertyInfo() const {
  49. GDNativePropertyInfo info;
  50. info.type = type;
  51. info.name = name;
  52. info.hint = hint;
  53. info.hint_string = hint_string;
  54. info.class_name = class_name;
  55. info.usage = usage;
  56. return info;
  57. }
  58. PropertyInfo() = default;
  59. PropertyInfo(Variant::Type p_type, const char *p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const char *p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const char *p_class_name = "") :
  60. type(p_type),
  61. name(p_name),
  62. hint(p_hint),
  63. hint_string(p_hint_string),
  64. usage(p_usage) {
  65. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  66. class_name = hint_string;
  67. } else {
  68. class_name = p_class_name;
  69. }
  70. }
  71. PropertyInfo(GDNativeVariantType p_type, const char *p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const char *p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const char *p_class_name = "") :
  72. PropertyInfo((Variant::Type)p_type, p_name, p_hint, p_hint_string, p_usage, p_class_name) {}
  73. };
  74. struct MethodInfo {
  75. const char *name;
  76. PropertyInfo return_val;
  77. uint32_t flags;
  78. int id = 0;
  79. std::vector<PropertyInfo> arguments;
  80. std::vector<Variant> default_arguments;
  81. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  82. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  83. operator Dictionary() const;
  84. static MethodInfo from_dict(const Dictionary &p_dict);
  85. MethodInfo();
  86. MethodInfo(const char *p_name);
  87. template <class... Args>
  88. MethodInfo(const char *p_name, const Args &...args);
  89. MethodInfo(Variant::Type ret);
  90. MethodInfo(Variant::Type ret, const char *p_name);
  91. template <class... Args>
  92. MethodInfo(Variant::Type ret, const char *p_name, const Args &...args);
  93. MethodInfo(const PropertyInfo &p_ret, const char *p_name);
  94. template <class... Args>
  95. MethodInfo(const PropertyInfo &p_ret, const char *p_name, const Args &...);
  96. };
  97. template <class... Args>
  98. MethodInfo::MethodInfo(const char *p_name, const Args &...args) :
  99. name(p_name), flags(METHOD_FLAG_NORMAL) {
  100. arguments = { args... };
  101. }
  102. template <class... Args>
  103. MethodInfo::MethodInfo(Variant::Type ret, const char *p_name, const Args &...args) :
  104. name(p_name), flags(METHOD_FLAG_NORMAL) {
  105. return_val.type = ret;
  106. arguments = { args... };
  107. }
  108. template <class... Args>
  109. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const char *p_name, const Args &...args) :
  110. name(p_name), return_val(p_ret), flags(METHOD_FLAG_NORMAL) {
  111. arguments = { args... };
  112. }
  113. template <class T>
  114. T *Object::cast_to(Object *p_object) {
  115. GDNativeObjectPtr casted = internal::interface->object_cast_to(p_object->_owner, internal::interface->classdb_get_class_tag(T::get_class_static()));
  116. if (casted == nullptr) {
  117. return nullptr;
  118. }
  119. return reinterpret_cast<T *>(internal::interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
  120. }
  121. } // namespace godot
  122. #endif // ! GODOT_OBJECT_HPP