native_ptr.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*************************************************************************/
  2. /* native_ptr.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 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 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 <class T>
  36. struct GDNativeConstPtr {
  37. const T *data = nullptr;
  38. GDNativeConstPtr(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 <class T>
  44. struct GDNativePtr {
  45. T *data = nullptr;
  46. GDNativePtr(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 GDNativeConstPtr<const m_type> { \
  54. const m_type *data = nullptr; \
  55. GDNativeConstPtr(const m_type *p_assign) { data = p_assign; } \
  56. static const char *get_name() { return "const " #m_type; } \
  57. operator const m_type *() const { return data; } \
  58. operator Variant() const { return uint64_t(data); } \
  59. }; \
  60. template <> \
  61. struct GDNativePtr<m_type> { \
  62. m_type *data = nullptr; \
  63. GDNativePtr(m_type *p_assign) { data = p_assign; } \
  64. static const char *get_name() { return #m_type; } \
  65. operator m_type *() const { return data; } \
  66. operator Variant() const { return uint64_t(data); } \
  67. };
  68. template <class T>
  69. struct GetTypeInfo<GDNativeConstPtr<T>> {
  70. static const Variant::Type VARIANT_TYPE = Variant::NIL;
  71. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  72. static inline PropertyInfo get_class_info() {
  73. return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_INT_IS_POINTER, GDNativeConstPtr<T>::get_name());
  74. }
  75. };
  76. template <class T>
  77. struct GetTypeInfo<GDNativePtr<T>> {
  78. static const Variant::Type VARIANT_TYPE = Variant::NIL;
  79. static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
  80. static inline PropertyInfo get_class_info() {
  81. return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_INT_IS_POINTER, GDNativePtr<T>::get_name());
  82. }
  83. };
  84. template <class T>
  85. struct PtrToArg<GDNativeConstPtr<T>> {
  86. _FORCE_INLINE_ static GDNativeConstPtr<T> convert(const void *p_ptr) {
  87. return GDNativeConstPtr<T>(reinterpret_cast<const T *>(p_ptr));
  88. }
  89. typedef const T *EncodeT;
  90. _FORCE_INLINE_ static void encode(GDNativeConstPtr<T> p_val, void *p_ptr) {
  91. *((const T **)p_ptr) = p_val.data;
  92. }
  93. };
  94. template <class T>
  95. struct PtrToArg<GDNativePtr<T>> {
  96. _FORCE_INLINE_ static GDNativePtr<T> convert(const void *p_ptr) {
  97. return GDNativePtr<T>(reinterpret_cast<const T *>(p_ptr));
  98. }
  99. typedef T *EncodeT;
  100. _FORCE_INLINE_ static void encode(GDNativePtr<T> p_val, void *p_ptr) {
  101. *((T **)p_ptr) = p_val.data;
  102. }
  103. };
  104. GDVIRTUAL_NATIVE_PTR(AudioFrame)
  105. GDVIRTUAL_NATIVE_PTR(bool)
  106. GDVIRTUAL_NATIVE_PTR(char)
  107. GDVIRTUAL_NATIVE_PTR(char16_t)
  108. GDVIRTUAL_NATIVE_PTR(char32_t)
  109. GDVIRTUAL_NATIVE_PTR(wchar_t)
  110. GDVIRTUAL_NATIVE_PTR(uint8_t)
  111. GDVIRTUAL_NATIVE_PTR(uint8_t *)
  112. GDVIRTUAL_NATIVE_PTR(int8_t)
  113. GDVIRTUAL_NATIVE_PTR(uint16_t)
  114. GDVIRTUAL_NATIVE_PTR(int16_t)
  115. GDVIRTUAL_NATIVE_PTR(uint32_t)
  116. GDVIRTUAL_NATIVE_PTR(int32_t)
  117. GDVIRTUAL_NATIVE_PTR(int64_t)
  118. GDVIRTUAL_NATIVE_PTR(uint64_t)
  119. GDVIRTUAL_NATIVE_PTR(float)
  120. GDVIRTUAL_NATIVE_PTR(double)
  121. #endif // NATIVE_PTR_H