object.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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/property_info.hpp>
  34. #include <godot_cpp/variant/variant.hpp>
  35. #include <godot_cpp/classes/object.hpp>
  36. #include <godot_cpp/godot.hpp>
  37. #include <gdextension_interface.h>
  38. #include <vector>
  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. namespace godot {
  44. namespace internal {
  45. Object *get_object_instance_binding(GodotObject *);
  46. } // namespace internal
  47. struct MethodInfo {
  48. StringName name;
  49. PropertyInfo return_val;
  50. uint32_t flags;
  51. int id = 0;
  52. std::vector<PropertyInfo> arguments;
  53. std::vector<Variant> default_arguments;
  54. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  55. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  56. operator Dictionary() const;
  57. static MethodInfo from_dict(const Dictionary &p_dict);
  58. MethodInfo();
  59. MethodInfo(StringName p_name);
  60. template <typename... Args>
  61. MethodInfo(StringName p_name, const Args &...args);
  62. MethodInfo(Variant::Type ret);
  63. MethodInfo(Variant::Type ret, StringName p_name);
  64. template <typename... Args>
  65. MethodInfo(Variant::Type ret, StringName p_name, const Args &...args);
  66. MethodInfo(const PropertyInfo &p_ret, StringName p_name);
  67. template <typename... Args>
  68. MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...);
  69. };
  70. template <typename... Args>
  71. MethodInfo::MethodInfo(StringName p_name, const Args &...args) :
  72. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  73. arguments = { args... };
  74. }
  75. template <typename... Args>
  76. MethodInfo::MethodInfo(Variant::Type ret, StringName p_name, const Args &...args) :
  77. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  78. return_val.type = ret;
  79. arguments = { args... };
  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) {
  84. arguments = { args... };
  85. }
  86. class ObjectID {
  87. uint64_t id = 0;
  88. public:
  89. _FORCE_INLINE_ bool is_ref_counted() const { return (id & (uint64_t(1) << 63)) != 0; }
  90. _FORCE_INLINE_ bool is_valid() const { return id != 0; }
  91. _FORCE_INLINE_ bool is_null() const { return id == 0; }
  92. _FORCE_INLINE_ operator uint64_t() const { return id; }
  93. _FORCE_INLINE_ operator int64_t() const { return id; }
  94. _FORCE_INLINE_ bool operator==(const ObjectID &p_id) const { return id == p_id.id; }
  95. _FORCE_INLINE_ bool operator!=(const ObjectID &p_id) const { return id != p_id.id; }
  96. _FORCE_INLINE_ bool operator<(const ObjectID &p_id) const { return id < p_id.id; }
  97. _FORCE_INLINE_ void operator=(int64_t p_int64) { id = p_int64; }
  98. _FORCE_INLINE_ void operator=(uint64_t p_uint64) { id = p_uint64; }
  99. _FORCE_INLINE_ ObjectID() {}
  100. _FORCE_INLINE_ explicit ObjectID(const uint64_t p_id) { id = p_id; }
  101. _FORCE_INLINE_ explicit ObjectID(const int64_t p_id) { id = p_id; }
  102. };
  103. class ObjectDB {
  104. public:
  105. static Object *get_instance(uint64_t p_object_id) {
  106. GDExtensionObjectPtr obj = internal::gdextension_interface_object_get_instance_from_id(p_object_id);
  107. if (obj == nullptr) {
  108. return nullptr;
  109. }
  110. return internal::get_object_instance_binding(obj);
  111. }
  112. };
  113. template <typename T>
  114. T *Object::cast_to(Object *p_object) {
  115. if (p_object == nullptr) {
  116. return nullptr;
  117. }
  118. StringName class_name = T::get_class_static();
  119. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  120. if (casted == nullptr) {
  121. return nullptr;
  122. }
  123. return dynamic_cast<T *>(internal::get_object_instance_binding(casted));
  124. }
  125. template <typename T>
  126. const T *Object::cast_to(const Object *p_object) {
  127. if (p_object == nullptr) {
  128. return nullptr;
  129. }
  130. StringName class_name = T::get_class_static();
  131. GDExtensionObjectPtr casted = internal::gdextension_interface_object_cast_to(p_object->_owner, internal::gdextension_interface_classdb_get_class_tag(class_name._native_ptr()));
  132. if (casted == nullptr) {
  133. return nullptr;
  134. }
  135. return dynamic_cast<const T *>(internal::get_object_instance_binding(casted));
  136. }
  137. } // namespace godot
  138. #endif // GODOT_OBJECT_HPP