native_ptr.h 11 KB

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