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