Skip to content

Commit 32f4df3

Browse files
committed
Add variant_internal.hpp.
This module contains VariantInternalType, VariantInternal, VariantGetInternalPtr, VariantInternalAccessor and VariantDefaultInitializer, allowing to access and manipulate Variant's internal values.
1 parent f174b4a commit 32f4df3

File tree

9 files changed

+568
-0
lines changed

9 files changed

+568
-0
lines changed

include/godot_cpp/godot.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ extern "C" GDExtensionInterfaceVariantCanConvert gdextension_interface_variant_c
8888
extern "C" GDExtensionInterfaceVariantCanConvertStrict gdextension_interface_variant_can_convert_strict;
8989
extern "C" GDExtensionInterfaceGetVariantFromTypeConstructor gdextension_interface_get_variant_from_type_constructor;
9090
extern "C" GDExtensionInterfaceGetVariantToTypeConstructor gdextension_interface_get_variant_to_type_constructor;
91+
extern "C" GDExtensionInterfaceGetVariantGetInternalPtrFunc gdextension_interface_variant_get_ptr_internal_getter;
9192
extern "C" GDExtensionInterfaceVariantGetPtrOperatorEvaluator gdextension_interface_variant_get_ptr_operator_evaluator;
9293
extern "C" GDExtensionInterfaceVariantGetPtrBuiltinMethod gdextension_interface_variant_get_ptr_builtin_method;
9394
extern "C" GDExtensionInterfaceVariantGetPtrConstructor gdextension_interface_variant_get_ptr_constructor;

include/godot_cpp/variant/variant.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ class Variant {
4949

5050
friend class GDExtensionBinding;
5151
friend class MethodBind;
52+
friend class VariantInternal;
5253

5354
static void init_bindings();
5455

include/godot_cpp/variant/variant_internal.hpp

Lines changed: 503 additions & 0 deletions
Large diffs are not rendered by default.

src/godot.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ GDExtensionInterfaceVariantCanConvert gdextension_interface_variant_can_convert
9494
GDExtensionInterfaceVariantCanConvertStrict gdextension_interface_variant_can_convert_strict = nullptr;
9595
GDExtensionInterfaceGetVariantFromTypeConstructor gdextension_interface_get_variant_from_type_constructor = nullptr;
9696
GDExtensionInterfaceGetVariantToTypeConstructor gdextension_interface_get_variant_to_type_constructor = nullptr;
97+
GDExtensionInterfaceGetVariantGetInternalPtrFunc gdextension_interface_variant_get_ptr_internal_getter = nullptr;
9798
GDExtensionInterfaceVariantGetPtrOperatorEvaluator gdextension_interface_variant_get_ptr_operator_evaluator = nullptr;
9899
GDExtensionInterfaceVariantGetPtrBuiltinMethod gdextension_interface_variant_get_ptr_builtin_method = nullptr;
99100
GDExtensionInterfaceVariantGetPtrConstructor gdextension_interface_variant_get_ptr_constructor = nullptr;
@@ -375,6 +376,7 @@ GDExtensionBool GDExtensionBinding::init(GDExtensionInterfaceGetProcAddress p_ge
375376
LOAD_PROC_ADDRESS(variant_can_convert_strict, GDExtensionInterfaceVariantCanConvertStrict);
376377
LOAD_PROC_ADDRESS(get_variant_from_type_constructor, GDExtensionInterfaceGetVariantFromTypeConstructor);
377378
LOAD_PROC_ADDRESS(get_variant_to_type_constructor, GDExtensionInterfaceGetVariantToTypeConstructor);
379+
LOAD_PROC_ADDRESS(variant_get_ptr_internal_getter, GDExtensionInterfaceGetVariantGetInternalPtrFunc);
378380
LOAD_PROC_ADDRESS(variant_get_ptr_operator_evaluator, GDExtensionInterfaceVariantGetPtrOperatorEvaluator);
379381
LOAD_PROC_ADDRESS(variant_get_ptr_builtin_method, GDExtensionInterfaceVariantGetPtrBuiltinMethod);
380382
LOAD_PROC_ADDRESS(variant_get_ptr_constructor, GDExtensionInterfaceVariantGetPtrConstructor);

src/variant/variant.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <godot_cpp/core/binder_common.hpp>
3636
#include <godot_cpp/core/class_db.hpp>
3737
#include <godot_cpp/core/defs.hpp>
38+
#include <godot_cpp/variant/variant_internal.hpp>
3839

3940
#include <utility>
4041

@@ -49,6 +50,7 @@ void Variant::init_bindings() {
4950
from_type_constructor[i] = internal::gdextension_interface_get_variant_from_type_constructor((GDExtensionVariantType)i);
5051
to_type_constructor[i] = internal::gdextension_interface_get_variant_to_type_constructor((GDExtensionVariantType)i);
5152
}
53+
VariantInternal::init_bindings();
5254

5355
StringName::init_bindings();
5456
String::init_bindings();

src/variant/variant_internal.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**************************************************************************/
2+
/* variant_internal.cpp */
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+
#include <godot_cpp/variant/variant_internal.hpp>
32+
33+
namespace godot {
34+
35+
GDExtensionVariantGetInternalPtrFunc VariantInternal::get_internal_func[Variant::VARIANT_MAX]{};
36+
37+
void VariantInternal::init_bindings() {
38+
for (int i = 1; i < Variant::VARIANT_MAX; i++) {
39+
get_internal_func[i] = internal::gdextension_interface_variant_get_ptr_internal_getter((GDExtensionVariantType)i);
40+
}
41+
}
42+
43+
} // namespace godot

test/project/main.gd

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,9 @@ func _ready():
271271
# Test that we can access an engine singleton.
272272
assert_equal(example.test_use_engine_singleton(), OS.get_name())
273273

274+
assert_equal(example.test_get_internal(1), 1)
275+
assert_equal(example.test_get_internal(true), -1)
276+
274277
# Test that notifications happen on both parent and child classes.
275278
var example_child = $ExampleChild
276279
assert_equal(example_child.get_value1(), 11)

test/src/example.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ void Example::_bind_methods() {
243243
ClassDB::bind_method(D_METHOD("callable_bind"), &Example::callable_bind);
244244
ClassDB::bind_method(D_METHOD("test_post_initialize"), &Example::test_post_initialize);
245245

246+
ClassDB::bind_method(D_METHOD("test_get_internal", "a"), &Example::test_get_internal);
247+
246248
GDVIRTUAL_BIND(_do_something_virtual, "name", "value");
247249
ClassDB::bind_method(D_METHOD("test_virtual_implemented_in_script"), &Example::test_virtual_implemented_in_script);
248250
GDVIRTUAL_BIND(_do_something_virtual_with_control, "control");
@@ -741,6 +743,14 @@ String Example::test_library_path() {
741743
return library_path;
742744
}
743745

746+
int64_t Example::test_get_internal(const Variant &p_input) const {
747+
if (p_input.get_type() != Variant::INT) {
748+
return -1;
749+
}
750+
751+
return *VariantInternal::get_int(&p_input);
752+
}
753+
744754
void ExampleRuntime::_bind_methods() {
745755
ClassDB::bind_method(D_METHOD("set_prop_value", "value"), &ExampleRuntime::set_prop_value);
746756
ClassDB::bind_method(D_METHOD("get_prop_value"), &ExampleRuntime::get_prop_value);

test/src/example.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <godot_cpp/classes/tile_set.hpp>
2323
#include <godot_cpp/classes/viewport.hpp>
2424
#include <godot_cpp/variant/variant.hpp>
25+
#include <godot_cpp/variant/variant_internal.hpp>
2526

2627
#include <godot_cpp/core/binder_common.hpp>
2728
#include <godot_cpp/core/gdvirtual.gen.inc>
@@ -185,6 +186,8 @@ class Example : public Control {
185186

186187
bool test_post_initialize() const;
187188

189+
int64_t test_get_internal(const Variant &p_input) const;
190+
188191
// Static method.
189192
static int test_static(int p_a, int p_b);
190193
static void test_static2();

0 commit comments

Comments
 (0)