object.hpp 6.9 KB

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