object.hpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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/variant/variant.hpp>
  34. #include <godot_cpp/classes/object.hpp>
  35. #include <godot_cpp/godot.hpp>
  36. #include <godot/gdnative_interface.h>
  37. #include <vector>
  38. #define ADD_SIGNAL(m_signal) godot::ClassDB::add_signal(get_class_static(), m_signal)
  39. #define ADD_GROUP(m_name, m_prefix) godot::ClassDB::add_property_group(get_class_static(), m_name, m_prefix)
  40. #define ADD_SUBGROUP(m_name, m_prefix) godot::ClassDB::add_property_subgroup(get_class_static(), m_name, m_prefix)
  41. #define ADD_PROPERTY(m_property, m_setter, m_getter) godot::ClassDB::add_property(get_class_static(), m_property, m_setter, m_getter)
  42. namespace godot {
  43. struct PropertyInfo {
  44. Variant::Type type = Variant::NIL;
  45. const char *name = nullptr;
  46. const char *class_name = nullptr;
  47. uint32_t hint = 0;
  48. const char *hint_string = nullptr;
  49. uint32_t usage = 7;
  50. operator GDNativePropertyInfo() const {
  51. GDNativePropertyInfo info;
  52. info.type = type;
  53. info.name = name;
  54. info.hint = hint;
  55. info.hint_string = hint_string;
  56. info.class_name = class_name;
  57. info.usage = usage;
  58. return info;
  59. }
  60. PropertyInfo() = default;
  61. PropertyInfo(Variant::Type p_type, const char *p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const char *p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const char *p_class_name = "") :
  62. type(p_type),
  63. name(p_name),
  64. hint(p_hint),
  65. hint_string(p_hint_string),
  66. usage(p_usage) {
  67. if (hint == PROPERTY_HINT_RESOURCE_TYPE) {
  68. class_name = hint_string;
  69. } else {
  70. class_name = p_class_name;
  71. }
  72. }
  73. PropertyInfo(GDNativeVariantType p_type, const char *p_name, PropertyHint p_hint = PROPERTY_HINT_NONE, const char *p_hint_string = "", uint32_t p_usage = PROPERTY_USAGE_DEFAULT, const char *p_class_name = "") :
  74. PropertyInfo((Variant::Type)p_type, p_name, p_hint, p_hint_string, p_usage, p_class_name) {}
  75. };
  76. struct MethodInfo {
  77. const char *name;
  78. PropertyInfo return_val;
  79. uint32_t flags;
  80. int id = 0;
  81. std::vector<PropertyInfo> arguments;
  82. std::vector<Variant> default_arguments;
  83. inline bool operator==(const MethodInfo &p_method) const { return id == p_method.id; }
  84. inline bool operator<(const MethodInfo &p_method) const { return id == p_method.id ? (name < p_method.name) : (id < p_method.id); }
  85. operator Dictionary() const;
  86. static MethodInfo from_dict(const Dictionary &p_dict);
  87. MethodInfo();
  88. MethodInfo(const char *p_name);
  89. template <class... Args>
  90. MethodInfo(const char *p_name, const Args &...args);
  91. MethodInfo(Variant::Type ret);
  92. MethodInfo(Variant::Type ret, const char *p_name);
  93. template <class... Args>
  94. MethodInfo(Variant::Type ret, const char *p_name, const Args &...args);
  95. MethodInfo(const PropertyInfo &p_ret, const char *p_name);
  96. template <class... Args>
  97. MethodInfo(const PropertyInfo &p_ret, const char *p_name, const Args &...);
  98. };
  99. template <class... Args>
  100. MethodInfo::MethodInfo(const char *p_name, const Args &...args) :
  101. name(p_name), flags(GDNATIVE_EXTENSION_METHOD_FLAG_NORMAL) {
  102. arguments = { args... };
  103. }
  104. template <class... Args>
  105. MethodInfo::MethodInfo(Variant::Type ret, const char *p_name, const Args &...args) :
  106. name(p_name), flags(GDNATIVE_EXTENSION_METHOD_FLAG_NORMAL) {
  107. return_val.type = ret;
  108. arguments = { args... };
  109. }
  110. template <class... Args>
  111. MethodInfo::MethodInfo(const PropertyInfo &p_ret, const char *p_name, const Args &...args) :
  112. name(p_name), return_val(p_ret), flags(GDNATIVE_EXTENSION_METHOD_FLAG_NORMAL) {
  113. arguments = { args... };
  114. }
  115. class ObjectID {
  116. uint64_t id = 0;
  117. public:
  118. _FORCE_INLINE_ bool is_ref_counted() const { return (id & (uint64_t(1) << 63)) != 0; }
  119. _FORCE_INLINE_ bool is_valid() const { return id != 0; }
  120. _FORCE_INLINE_ bool is_null() const { return id == 0; }
  121. _FORCE_INLINE_ operator uint64_t() const { return id; }
  122. _FORCE_INLINE_ operator int64_t() const { return id; }
  123. _FORCE_INLINE_ bool operator==(const ObjectID &p_id) const { return id == p_id.id; }
  124. _FORCE_INLINE_ bool operator!=(const ObjectID &p_id) const { return id != p_id.id; }
  125. _FORCE_INLINE_ bool operator<(const ObjectID &p_id) const { return id < p_id.id; }
  126. _FORCE_INLINE_ void operator=(int64_t p_int64) { id = p_int64; }
  127. _FORCE_INLINE_ void operator=(uint64_t p_uint64) { id = p_uint64; }
  128. _FORCE_INLINE_ ObjectID() {}
  129. _FORCE_INLINE_ explicit ObjectID(const uint64_t p_id) { id = p_id; }
  130. _FORCE_INLINE_ explicit ObjectID(const int64_t p_id) { id = p_id; }
  131. };
  132. class ObjectDB {
  133. public:
  134. static Object *get_instance(uint64_t p_object_id) {
  135. GDNativeObjectPtr obj = internal::gdn_interface->object_get_instance_from_id(p_object_id);
  136. if (obj == nullptr) {
  137. return nullptr;
  138. }
  139. return reinterpret_cast<Object *>(internal::gdn_interface->object_get_instance_binding(obj, internal::token, &Object::___binding_callbacks));
  140. }
  141. };
  142. template <class T>
  143. T *Object::cast_to(Object *p_object) {
  144. if (p_object == nullptr) {
  145. return nullptr;
  146. }
  147. GDNativeObjectPtr casted = internal::gdn_interface->object_cast_to(p_object->_owner, internal::gdn_interface->classdb_get_class_tag(T::get_class_static()));
  148. if (casted == nullptr) {
  149. return nullptr;
  150. }
  151. return reinterpret_cast<T *>(internal::gdn_interface->object_get_instance_binding(casted, internal::token, &T::___binding_callbacks));
  152. }
  153. } // namespace godot
  154. #endif // ! GODOT_OBJECT_HPP