native_ptr.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. /**************************************************************************/
  2. /* native_ptr.h */
  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 NATIVE_PTR_H
  31. #define NATIVE_PTR_H
  32. #include "core/math/audio_frame.h"
  33. #include "core/variant/method_ptrcall.h"
  34. #include "core/variant/type_info.h"
  35. template <typename T>
  36. struct GDExtensionConstPtr {
  37. const T *data = nullptr;
  38. GDExtensionConstPtr(const T *p_assign) { data = p_assign; }
  39. static const char *get_name() { return "const void"; }
  40. operator const T *() const { return data; }
  41. operator Variant() const { return uint64_t(data); }
  42. };
  43. template <typename T>
  44. struct GDExtensionPtr {
  45. T *data = nullptr;
  46. GDExtensionPtr(T *p_assign) { data = p_assign; }
  47. static const char *get_name() { return "void"; }
  48. operator T *() const { return data; }
  49. operator Variant() const { return uint64_t(data); }
  50. };
  51. #define GDVIRTUAL_NATIVE_PTR(m_type) \
  52. template <> \
  53. struct GDExtensionConstPtr<const m_type> { \
  54. const m_type *data = nullptr; \
  55. GDExtensionConstPtr() {} \
  56. GDExtensionConstPtr(const m_type *p_assign) { data = p_assign; } \
  57. static const char *get_name() { return "const " #m_type; } \
  58. operator const m_type *() const { return data; } \
  59. operator Variant() const { return uint64_t(data); } \
  60. }; \
  61. template <> \
  62. struct VariantCaster<GDExtensionConstPtr<const m_type>> { \
  63. static _FORCE_INLINE_ GDExtensionConstPtr<const m_type> cast(const Variant &p_variant) { \
  64. return GDExtensionConstPtr<const m_type>((const m_type *)p_variant.operator uint64_t()); \
  65. } \
  66. }; \
  67. template <> \
  68. struct VariantInternalAccessor<GDExtensionConstPtr<const m_type>> { \
  69. static _FORCE_INLINE_ const GDExtensionConstPtr<const m_type> &get(const Variant *v) { return *reinterpret_cast<const GDExtensionConstPtr<const m_type> *>(VariantInternal::get_int(v)); } \
  70. static _FORCE_INLINE_ void set(Variant *v, const GDExtensionConstPtr<const m_type> &p_value) { *VariantInternal::get_int(v) = uint64_t(p_value.data); } \
  71. }; \
  72. template <> \
  73. struct GDExtensionPtr<m_type> { \
  74. m_type *data = nullptr; \
  75. GDExtensionPtr() {} \
  76. GDExtensionPtr(m_type *p_assign) { data = p_assign; } \
  77. static const char *get_name() { return #m_type; } \
  78. operator m_type *() const { return data; } \
  79. operator Variant() const { return uint64_t(data); } \
  80. }; \
  81. template <> \
  82. struct VariantCaster<GDExtensionPtr<m_type>> { \
  83. static _FORCE_INLINE_ GDExtensionPtr<m_type> cast(const Variant &p_variant) { \
  84. return GDExtensionPtr<m_type>((m_type *)p_variant.operator uint64_t()); \
  85. } \
  86. }; \
  87. template <> \
  88. struct VariantInternalAccessor<GDExtensionPtr<m_type>> { \
  89. static _FORCE_INLINE_ const GDExtensionPtr<m_type> &get(const Variant *v) { return *reinterpret_cast<const GDExtensionPtr<m_type> *>(VariantInternal::get_int(v)); } \
  90. static _FORCE_INLINE_ void set(Variant *v, const GDExtensionPtr<m_type> &p_value) { *VariantInternal::get_int(v) = uint64_t(p_value.data); } \
  91. };
  92. template <typename T>
  93. struct GetTypeInfo<GDExtensionConstPtr<T>> {
  94. static const Variant::Type VARIANT_TYPE = Variant::NIL;
  95. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  96. static inline PropertyInfo get_class_info() {
  97. return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_INT_IS_POINTER, GDExtensionConstPtr<T>::get_name());
  98. }
  99. };
  100. template <typename T>
  101. struct GetTypeInfo<GDExtensionPtr<T>> {
  102. static const Variant::Type VARIANT_TYPE = Variant::NIL;
  103. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  104. static inline PropertyInfo get_class_info() {
  105. return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_INT_IS_POINTER, GDExtensionPtr<T>::get_name());
  106. }
  107. };
  108. template <typename T>
  109. struct PtrToArg<GDExtensionConstPtr<T>> {
  110. _FORCE_INLINE_ static GDExtensionConstPtr<T> convert(const void *p_ptr) {
  111. return GDExtensionConstPtr<T>(reinterpret_cast<const T *>(p_ptr));
  112. }
  113. typedef const T *EncodeT;
  114. _FORCE_INLINE_ static void encode(GDExtensionConstPtr<T> p_val, void *p_ptr) {
  115. *((const T **)p_ptr) = p_val.data;
  116. }
  117. };
  118. template <typename T>
  119. struct PtrToArg<GDExtensionPtr<T>> {
  120. _FORCE_INLINE_ static GDExtensionPtr<T> convert(const void *p_ptr) {
  121. return GDExtensionPtr<T>(reinterpret_cast<const T *>(p_ptr));
  122. }
  123. typedef T *EncodeT;
  124. _FORCE_INLINE_ static void encode(GDExtensionPtr<T> p_val, void *p_ptr) {
  125. *((T **)p_ptr) = p_val.data;
  126. }
  127. };
  128. GDVIRTUAL_NATIVE_PTR(void)
  129. GDVIRTUAL_NATIVE_PTR(AudioFrame)
  130. GDVIRTUAL_NATIVE_PTR(bool)
  131. GDVIRTUAL_NATIVE_PTR(char)
  132. GDVIRTUAL_NATIVE_PTR(char16_t)
  133. GDVIRTUAL_NATIVE_PTR(char32_t)
  134. GDVIRTUAL_NATIVE_PTR(wchar_t)
  135. GDVIRTUAL_NATIVE_PTR(uint8_t)
  136. GDVIRTUAL_NATIVE_PTR(uint8_t *)
  137. GDVIRTUAL_NATIVE_PTR(int8_t)
  138. GDVIRTUAL_NATIVE_PTR(uint16_t)
  139. GDVIRTUAL_NATIVE_PTR(int16_t)
  140. GDVIRTUAL_NATIVE_PTR(uint32_t)
  141. GDVIRTUAL_NATIVE_PTR(int32_t)
  142. GDVIRTUAL_NATIVE_PTR(int64_t)
  143. GDVIRTUAL_NATIVE_PTR(uint64_t)
  144. GDVIRTUAL_NATIVE_PTR(float)
  145. GDVIRTUAL_NATIVE_PTR(double)
  146. #endif // NATIVE_PTR_H