-
-
Notifications
You must be signed in to change notification settings - Fork 21.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Core: Allow methods of built-in
Variant
types to be used as Callables
- Loading branch information
Showing
9 changed files
with
230 additions
and
72 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/**************************************************************************/ | ||
/* variant_callable.cpp */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#include "variant_callable.h" | ||
|
||
#include "core/templates/hashfuncs.h" | ||
|
||
bool VariantCallable::compare_equal(const CallableCustom *p_a, const CallableCustom *p_b) { | ||
return p_a->hash() == p_b->hash(); | ||
} | ||
|
||
bool VariantCallable::compare_less(const CallableCustom *p_a, const CallableCustom *p_b) { | ||
return p_a->hash() < p_b->hash(); | ||
} | ||
|
||
uint32_t VariantCallable::hash() const { | ||
return h; | ||
} | ||
|
||
String VariantCallable::get_as_text() const { | ||
return vformat("%s::%s (Callable)", Variant::get_type_name(variant.get_type()), method); | ||
} | ||
|
||
CallableCustom::CompareEqualFunc VariantCallable::get_compare_equal_func() const { | ||
return compare_equal; | ||
} | ||
|
||
CallableCustom::CompareLessFunc VariantCallable::get_compare_less_func() const { | ||
return compare_less; | ||
} | ||
|
||
bool VariantCallable::is_valid() const { | ||
return Variant::has_builtin_method(variant.get_type(), method); | ||
} | ||
|
||
StringName VariantCallable::get_method() const { | ||
return method; | ||
} | ||
|
||
ObjectID VariantCallable::get_object() const { | ||
return ObjectID(); | ||
} | ||
|
||
void VariantCallable::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const { | ||
Variant v = variant; | ||
v.callp(method, p_arguments, p_argcount, r_return_value, r_call_error); | ||
} | ||
|
||
VariantCallable::VariantCallable(const Variant &p_variant, const StringName &p_method) { | ||
variant = p_variant; | ||
method = p_method; | ||
h = variant.hash(); | ||
h = hash_murmur3_one_64(Variant::get_builtin_method_hash(variant.get_type(), method), h); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/**************************************************************************/ | ||
/* variant_callable.h */ | ||
/**************************************************************************/ | ||
/* This file is part of: */ | ||
/* GODOT ENGINE */ | ||
/* https://godotengine.org */ | ||
/**************************************************************************/ | ||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ | ||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ | ||
/* */ | ||
/* Permission is hereby granted, free of charge, to any person obtaining */ | ||
/* a copy of this software and associated documentation files (the */ | ||
/* "Software"), to deal in the Software without restriction, including */ | ||
/* without limitation the rights to use, copy, modify, merge, publish, */ | ||
/* distribute, sublicense, and/or sell copies of the Software, and to */ | ||
/* permit persons to whom the Software is furnished to do so, subject to */ | ||
/* the following conditions: */ | ||
/* */ | ||
/* The above copyright notice and this permission notice shall be */ | ||
/* included in all copies or substantial portions of the Software. */ | ||
/* */ | ||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ | ||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ | ||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */ | ||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ | ||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ | ||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ | ||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ | ||
/**************************************************************************/ | ||
|
||
#ifndef VARIANT_CALLABLE_H | ||
#define VARIANT_CALLABLE_H | ||
|
||
#include "core/variant/callable.h" | ||
#include "core/variant/variant.h" | ||
|
||
class VariantCallable : public CallableCustom { | ||
Variant variant; | ||
StringName method; | ||
uint32_t h = 0; | ||
|
||
static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b); | ||
static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b); | ||
|
||
public: | ||
uint32_t hash() const override; | ||
String get_as_text() const override; | ||
CompareEqualFunc get_compare_equal_func() const override; | ||
CompareLessFunc get_compare_less_func() const override; | ||
bool is_valid() const override; | ||
StringName get_method() const override; | ||
ObjectID get_object() const override; | ||
void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override; | ||
|
||
VariantCallable(const Variant &p_variant, const StringName &p_method); | ||
}; | ||
|
||
#endif // VARIANT_CALLABLE_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
modules/gdscript/tests/scripts/runtime/features/builtin_method_as_callable.gd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
func test(): | ||
var array: Array = [1, 2, 3] | ||
print(array) | ||
var callable: Callable = array.clear | ||
callable.call() | ||
print(array) |
3 changes: 3 additions & 0 deletions
3
modules/gdscript/tests/scripts/runtime/features/builtin_method_as_callable.out
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
GDTEST_OK | ||
[1, 2, 3] | ||
[] |