native_ptr.h 11 KB

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