Skip to content

Commit f21cf2c

Browse files
Register custom converters for method binding
Signed-off-by: Martin Rodriguez Reboredo <yakoyoku@gmail.com>
1 parent 713481f commit f21cf2c

File tree

3 files changed

+140
-0
lines changed

3 files changed

+140
-0
lines changed

include/godot_cpp/core/method_ptrcall.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,62 @@ struct PtrToArg {};
113113
} \
114114
}
115115

116+
#define MAKE_PTRARG_CONVERTER(m_type, m_conv, m_to, m_from) \
117+
template <> \
118+
struct PtrToArg<m_type> { \
119+
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
120+
return m_to(*reinterpret_cast<const m_conv *>(p_ptr)); \
121+
} \
122+
typedef m_conv EncodeT; \
123+
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
124+
*reinterpret_cast<m_conv *>(p_ptr) = m_from(p_val); \
125+
} \
126+
_FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \
127+
return m_from(p_val); \
128+
} \
129+
}; \
130+
template <> \
131+
struct PtrToArg<const m_type &> { \
132+
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
133+
return m_to(*reinterpret_cast<const m_conv *>(p_ptr)); \
134+
} \
135+
typedef m_conv EncodeT; \
136+
_FORCE_INLINE_ static void encode(m_type p_val, void *p_ptr) { \
137+
*reinterpret_cast<m_conv *>(p_ptr) = m_from(p_val); \
138+
} \
139+
_FORCE_INLINE_ static m_conv encode_arg(m_type p_val) { \
140+
return m_from(p_val); \
141+
} \
142+
}
143+
144+
#define MAKE_PTRARG_CONVERTER_BY_REFERENCE(m_type, m_conv, m_to, m_from) \
145+
template <> \
146+
struct PtrToArg<m_type> { \
147+
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
148+
return m_to(*reinterpret_cast<const m_conv *>(p_ptr)); \
149+
} \
150+
typedef m_conv EncodeT; \
151+
_FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
152+
*reinterpret_cast<m_type *>(p_ptr) = m_from(p_val); \
153+
} \
154+
_FORCE_INLINE_ static m_conv encode_arg(const m_type &p_val) { \
155+
return m_from(p_val); \
156+
} \
157+
}; \
158+
template <> \
159+
struct PtrToArg<const m_type &> { \
160+
_FORCE_INLINE_ static m_type convert(const void *p_ptr) { \
161+
return m_to(*reinterpret_cast<const m_conv *>(p_ptr)); \
162+
} \
163+
typedef m_conv EncodeT; \
164+
_FORCE_INLINE_ static void encode(const m_type &p_val, void *p_ptr) { \
165+
*reinterpret_cast<m_type *>(p_ptr) = m_from(p_val); \
166+
} \
167+
_FORCE_INLINE_ static m_conv encode_arg(const m_type &p_val) { \
168+
return m_from(p_val); \
169+
} \
170+
}
171+
116172
MAKE_PTRARGCONV(bool, uint8_t);
117173
// Integer types.
118174
MAKE_PTRARGCONV(uint8_t, int64_t);
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/**************************************************************************/
2+
/* converter.hpp */
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 <godot_cpp/core/defs.hpp>
34+
#include <godot_cpp/core/method_ptrcall.hpp>
35+
#include <godot_cpp/core/type_info.hpp>
36+
#include <godot_cpp/variant/variant.hpp>
37+
38+
#include <gdextension_interface.h>
39+
40+
#define GD_REGISTER_CONVERSION(m_type, m_conv, m_to, m_from, m_var_type) \
41+
namespace godot { \
42+
template<> \
43+
struct VariantCaster<m_type> { \
44+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
45+
return m_to(p_variant); \
46+
} \
47+
template<> \
48+
struct VariantCaster<m_type &> { \
49+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
50+
return m_to(p_variant); \
51+
} \
52+
template<> \
53+
struct VariantCaster<const m_type &> { \
54+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
55+
return m_to(p_variant); \
56+
} \
57+
MAKE_TYPE_INFO(m_type, m_var_type) \
58+
MAKE_PTRARG_CONVERTER(m_type, m_conv, m_to, m_from); \
59+
}
60+
61+
#define GD_REGISTER_CONVERSION_BY_REFERENCE(m_type, m_conv, m_to, m_from, m_var_type) \
62+
namespace godot { \
63+
template<> \
64+
struct VariantCaster<m_type> { \
65+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
66+
return m_to(p_variant); \
67+
} \
68+
template<> \
69+
struct VariantCaster<m_type &> { \
70+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
71+
return m_to(p_variant); \
72+
} \
73+
template<> \
74+
struct VariantCaster<const m_type &> { \
75+
_FORCE_INLINE_ static m_type cast(const Variant &p_variant) { \
76+
return m_to(p_variant); \
77+
} \
78+
MAKE_TYPE_INFO(m_type, m_var_type) \
79+
MAKE_PTRARG_CONVERTER_BY_REFERENCE(m_type, m_conv, m_to, m_from); \
80+
}
81+

include/godot_cpp/variant/variant.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ class Variant {
215215
Variant(const PackedVector3Array &v);
216216
Variant(const PackedColorArray &v);
217217
Variant(const PackedVector4Array &v);
218+
template<typename T, typename = std::void_t<decltype(PtrToArg<T>::encode_arg(std::declval<T>()))>>
219+
Variant(T v) :
220+
Variant(PtrToArg<T>::encode_arg(v)) {}
218221
~Variant();
219222

220223
operator bool() const;

0 commit comments

Comments
 (0)