object.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #pragma once
  31. #include <godot_cpp/core/defs.hpp>
  32. #include <godot_cpp/core/object_id.hpp>
  33. #include <godot_cpp/core/property_info.hpp>
  34. #include <godot_cpp/variant/variant.hpp>
  35. #include <godot_cpp/templates/local_vector.hpp>
  36. #include <godot_cpp/classes/object.hpp>
  37. #include <godot_cpp/godot.hpp>
  38. #include <gdextension_interface.h>
  39. #define ADD_SIGNAL(m_signal) ::godot::ClassDB::add_signal(get_class_static(), m_signal)
  40. #define ADD_GROUP(m_name, m_prefix) ::godot::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  41. #define ADD_SUBGROUP(m_name, m_prefix) ::godot::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  42. #define ADD_PROPERTY(m_property, m_setter, m_getter) ::godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
  43. #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)
  44. namespace godot {
  45. namespace internal {
  46. Object *get_object_instance_binding(GodotObject *);
  47. } // namespace internal
  48. struct MethodInfo {
  49. StringName name;
  50. PropertyInfo return_val;
  51. uint32_t flags;
  52. int id = 0;
  53. LocalVector<PropertyInfo> arguments;
  54. LocalVector<Variant> default_arguments;
  55. GDExtensionClassMethodArgumentMetadata return_val_metadata;
  56. LocalVector<GDExtensionClassMethodArgumentMetadata> arguments_metadata;
  57. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  58. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  59. operator Dictionary() const;
  60. static MethodInfo from_dict(const Dictionary &p_dict);
  61. MethodInfo();
  62. MethodInfo(StringName p_name);
  63. template <typename... Args>
  64. MethodInfo(StringName p_name, const Args &...args);
  65. MethodInfo(Variant::Type ret);
  66. MethodInfo(Variant::Type ret, StringName p_name);
  67. template <typename... Args>
  68. MethodInfo(Variant::Type ret, StringName p_name, const Args &...args);
  69. MethodInfo(const PropertyInfo &p_ret, StringName p_name);
  70. template <typename... Args>
  71. MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...);
  72. };
  73. template <typename... Args>
  74. MethodInfo::MethodInfo(StringName p_name, const Args &...args) :
  75. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL), arguments({ args... }) {}
  76. template <typename... Args>
  77. MethodInfo::MethodInfo(Variant::Type ret, StringName p_name, const Args &...args) :
  78. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL), arguments({ args... }) {
  79. return_val.type = ret;
  80. }
  81. template <typename... Args>
  82. MethodInfo::MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...args) :
  83. name(p_name), return_val(p_ret), flags(GDEXTENSION_METHOD_FLAG_NORMAL), arguments({ args... }) {
  84. }
  85. class ObjectDB {
  86. public:
  87. static Object *get_instance(uint64_t p_object_id) {
  88. GDExtensionObjectPtr obj = internal::gdextension_interface_object_get_instance_from_id(p_object_id);
  89. if (obj == nullptr) {
  90. return nullptr;
  91. }
  92. return internal::get_object_instance_binding(obj);
  93. }
  94. };
  95. template <typename T>
  96. T *Object::cast_to(Object *p_object) {
  97. if (p_object == nullptr) {
  98. return nullptr;
  99. }
  100. StringName class_name = T::get_class_static();
  101. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  102. if (casted == nullptr) {
  103. return nullptr;
  104. }
  105. return dynamic_cast<T *>(internal::get_object_instance_binding(casted));
  106. }
  107. template <typename T>
  108. const T *Object::cast_to(const Object *p_object) {
  109. if (p_object == nullptr) {
  110. return nullptr;
  111. }
  112. StringName class_name = T::get_class_static();
  113. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  114. if (casted == nullptr) {
  115. return nullptr;
  116. }
  117. return dynamic_cast<const T *>(internal::get_object_instance_binding(casted));
  118. }
  119. } // namespace godot