object.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*************************************************************************/
  2. /* object.hpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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/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 <godot/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. struct MethodInfo {
  45. StringName name;
  46. PropertyInfo return_val;
  47. uint32_t flags;
  48. int id = 0;
  49. std::vector<PropertyInfo> arguments;
  50. std::vector<Variant> default_arguments;
  51. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  52. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  53. operator Dictionary() const;
  54. static MethodInfo from_dict(const Dictionary &p_dict);
  55. MethodInfo();
  56. MethodInfo(StringName p_name);
  57. template <class... Args>
  58. MethodInfo(StringName p_name, const Args &...args);
  59. MethodInfo(Variant::Type ret);
  60. MethodInfo(Variant::Type ret, StringName p_name);
  61. template <class... Args>
  62. MethodInfo(Variant::Type ret, StringName p_name, const Args &...args);
  63. MethodInfo(const PropertyInfo &p_ret, StringName p_name);
  64. template <class... Args>
  65. MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...);
  66. };
  67. template <class... Args>
  68. MethodInfo::MethodInfo(StringName p_name, const Args &...args) :
  69. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  70. arguments = { args... };
  71. }
  72. template <class... Args>
  73. MethodInfo::MethodInfo(Variant::Type ret, StringName p_name, const Args &...args) :
  74. name(p_name), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  75. return_val.type = ret;
  76. arguments = { args... };
  77. }
  78. template <class... Args>
  79. MethodInfo::MethodInfo(const PropertyInfo &p_ret, StringName p_name, const Args &...args) :
  80. name(p_name), return_val(p_ret), flags(GDEXTENSION_METHOD_FLAG_NORMAL) {
  81. arguments = { args... };
  82. }
  83. class ObjectID {
  84. uint64_t id = 0;
  85. public:
  86. _FORCE_INLINE_ bool is_ref_counted() const { return (id & (uint64_t(1) << 63)) != 0; }
  87. _FORCE_INLINE_ bool is_valid() const { return id != 0; }
  88. _FORCE_INLINE_ bool is_null() const { return id == 0; }
  89. _FORCE_INLINE_ operator uint64_t() const { return id; }
  90. _FORCE_INLINE_ operator int64_t() const { return id; }
  91. _FORCE_INLINE_ bool operator==(const ObjectID &p_id) const { return id == p_id.id; }
  92. _FORCE_INLINE_ bool operator!=(const ObjectID &p_id) const { return id != p_id.id; }
  93. _FORCE_INLINE_ bool operator<(const ObjectID &p_id) const { return id < p_id.id; }
  94. _FORCE_INLINE_ void operator=(int64_t p_int64) { id = p_int64; }
  95. _FORCE_INLINE_ void operator=(uint64_t p_uint64) { id = p_uint64; }
  96. _FORCE_INLINE_ ObjectID() {}
  97. _FORCE_INLINE_ explicit ObjectID(const uint64_t p_id) { id = p_id; }
  98. _FORCE_INLINE_ explicit ObjectID(const int64_t p_id) { id = p_id; }
  99. };
  100. class ObjectDB {
  101. public:
  102. static Object *get_instance(uint64_t p_object_id) {
  103. GDExtensionObjectPtr obj = internal::gde_interface->object_get_instance_from_id(p_object_id);
  104. if (obj == nullptr) {
  105. return nullptr;
  106. }
  107. return reinterpret_cast<Object *>(internal::gde_interface->object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks));
  108. }
  109. };
  110. template <class T>
  111. T *Object::cast_to(Object *p_object) {
  112. if (p_object == nullptr) {
  113. return nullptr;
  114. }
  115. StringName class_name = T::get_class_static();
  116. GDExtensionObjectPtr casted = internal::gde_interface->object_cast_to(p_object->_owner, internal::gde_interface->classdb_get_class_tag(class_name._native_ptr()));
  117. if (casted == nullptr) {
  118. return nullptr;
  119. }
  120. return reinterpret_cast<T *>(internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
  121. }
  122. template <class T>
  123. const T *Object::cast_to(const Object *p_object) {
  124. if (p_object == nullptr) {
  125. return nullptr;
  126. }
  127. StringName class_name = T::get_class_static();
  128. GDExtensionObjectPtr casted = internal::gde_interface->object_cast_to(p_object->_owner, internal::gde_interface->classdb_get_class_tag(class_name._native_ptr()));
  129. if (casted == nullptr) {
  130. return nullptr;
  131. }
  132. return reinterpret_cast<const T *>(internal::gde_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
  133. }
  134. } // namespace godot
  135. #endif // GODOT_OBJECT_HPP