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