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