Path: blob/master/modules/gdscript/gdscript_lambda_callable.h
10277 views
/**************************************************************************/1/* gdscript_lambda_callable.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "gdscript.h"3334#include "core/object/ref_counted.h"35#include "core/templates/vector.h"36#include "core/variant/callable.h"37#include "core/variant/variant.h"3839class GDScriptFunction;40class GDScriptInstance;4142class GDScriptLambdaCallable : public CallableCustom {43GDScript::UpdatableFuncPtr function;44Ref<GDScript> script;45uint32_t h;4647Vector<Variant> captures;4849static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);50static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);5152public:53bool is_valid() const override;54uint32_t hash() const override;55String get_as_text() const override;56CompareEqualFunc get_compare_equal_func() const override;57CompareLessFunc get_compare_less_func() const override;58ObjectID get_object() const override;59StringName get_method() const override;60int get_argument_count(bool &r_is_valid) const override;61void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;6263GDScriptLambdaCallable(GDScriptLambdaCallable &) = delete;64GDScriptLambdaCallable(const GDScriptLambdaCallable &) = delete;65GDScriptLambdaCallable(Ref<GDScript> p_script, GDScriptFunction *p_function, const Vector<Variant> &p_captures);66virtual ~GDScriptLambdaCallable() = default;67};6869// Lambda callable that references a particular object, so it can use `self` in the body.70class GDScriptLambdaSelfCallable : public CallableCustom {71GDScript::UpdatableFuncPtr function;72Ref<RefCounted> reference; // For objects that are RefCounted, keep a reference.73Object *object = nullptr; // For non RefCounted objects, use a direct pointer.74uint32_t h;7576Vector<Variant> captures;7778static bool compare_equal(const CallableCustom *p_a, const CallableCustom *p_b);79static bool compare_less(const CallableCustom *p_a, const CallableCustom *p_b);8081public:82bool is_valid() const override;83uint32_t hash() const override;84String get_as_text() const override;85CompareEqualFunc get_compare_equal_func() const override;86CompareLessFunc get_compare_less_func() const override;87ObjectID get_object() const override;88StringName get_method() const override;89int get_argument_count(bool &r_is_valid) const override;90void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;9192GDScriptLambdaSelfCallable(GDScriptLambdaSelfCallable &) = delete;93GDScriptLambdaSelfCallable(const GDScriptLambdaSelfCallable &) = delete;94GDScriptLambdaSelfCallable(Ref<RefCounted> p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures);95GDScriptLambdaSelfCallable(Object *p_self, GDScriptFunction *p_function, const Vector<Variant> &p_captures);96virtual ~GDScriptLambdaSelfCallable() = default;97};9899100