object.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**************************************************************************/
  2. /* object.hpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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/core/object_id.hpp>
  34. #include <godot_cpp/core/property_info.hpp>
  35. #include <godot_cpp/variant/variant.hpp>
  36. #include <godot_cpp/classes/object.hpp>
  37. #include <godot_cpp/godot.hpp>
  38. #include <gdextension_interface.h>
  39. #include <vector>
  40. #define ADD_SIGNAL(m_signal) godot::ClassDB::add_signal(get_class_static(), m_signal)
  41. #define ADD_GROUP(m_name, m_prefix) godot::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  42. #define ADD_SUBGROUP(m_name, m_prefix) godot::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  43. #define ADD_PROPERTY(m_property, m_setter, m_getter) godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
  44. #define ADD_PROPERTYI(m_property, m_setter, m_getter, m_index) godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter, m_index)
  45. namespace godot {
  46. namespace internal {
  47. Object *get_object_instance_binding(GodotObject *);
  48. } // namespace internal
  49. struct MethodInfo {
  50. StringName name;
  51. PropertyInfo return_val;
  52. uint32_t flags;
  53. int id = 0;
  54. std::vector<PropertyInfo> arguments;
  55. std::vector<Variant> default_arguments;
  56. GDExtensionClassMethodArgumentMetadata return_val_metadata;
  57. std::vector<GDExtensionClassMethodArgumentMetadata> arguments_metadata;
  58. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  59. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  60. operator Dictionary() const;
  61. static MethodInfo from_dict(const Dictionary &p_dict);
  62. MethodInfo();
  63. MethodInfo(StringName p_name);
  64. template <typename... Args>
  65. MethodInfo(StringName p_name, const Args &...args);
  66. MethodInfo(Variant::Type ret);
  67. MethodInfo(Variant::Type ret, StringName p_name);
  68. template <typename... Args>
  69. MethodInfo(Variant::Type ret, StringName p_name, const Args &...args);
  70. MethodInfo(const PropertyInfo &p_ret, StringName p_name);
  71. template <typename... Args>
  72. MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...);
  73. };
  74. template <typename... Args>
  75. MethodInfo::MethodInfo(StringName p_name, const Args &...args) :
  76. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  77. arguments = { args... };
  78. }
  79. template <typename... Args>
  80. MethodInfo::MethodInfo(Variant::Type ret, StringName p_name, const Args &...args) :
  81. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  82. return_val.type = ret;
  83. arguments = { args... };
  84. }
  85. template <typename... Args>
  86. MethodInfo::MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...args) :
  87. name(p_name), return_val(p_ret), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  88. arguments = { args... };
  89. }
  90. class ObjectDB {
  91. public:
  92. static Object *get_instance(uint64_t p_object_id) {
  93. GDExtensionObjectPtr obj = internal::gdextension_interface_object_get_instance_from_id(p_object_id);
  94. if (obj == nullptr) {
  95. return nullptr;
  96. }
  97. return internal::get_object_instance_binding(obj);
  98. }
  99. };
  100. template <typename T>
  101. T *Object::cast_to(Object *p_object) {
  102. if (p_object == nullptr) {
  103. return nullptr;
  104. }
  105. StringName class_name = T::get_class_static();
  106. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  107. if (casted == nullptr) {
  108. return nullptr;
  109. }
  110. return dynamic_cast<T *>(internal::get_object_instance_binding(casted));
  111. }
  112. template <typename T>
  113. const T *Object::cast_to(const Object *p_object) {
  114. if (p_object == nullptr) {
  115. return nullptr;
  116. }
  117. StringName class_name = T::get_class_static();
  118. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  119. if (casted == nullptr) {
  120. return nullptr;
  121. }
  122. return dynamic_cast<const T *>(internal::get_object_instance_binding(casted));
  123. }
  124. } // namespace godot
  125. #endif // GODOT_OBJECT_HPP