2018-10-17 11:55:43 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2016 Apple Inc. All rights reserved.
|
2021-05-19 15:41:17 +03:00
|
|
|
* Copyright (c) 2021, Gunnar Beutner <gbeutner@serenityos.org>
|
2023-08-18 19:21:33 +03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2018-10-17 11:55:43 +03:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
|
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-01-11 02:29:28 +03:00
|
|
|
#include <AK/Assertions.h>
|
2021-05-19 15:41:17 +03:00
|
|
|
#include <AK/Atomic.h>
|
|
|
|
#include <AK/BitCast.h>
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
#include <AK/ScopeGuard.h>
|
2023-08-18 19:21:33 +03:00
|
|
|
#include <AK/Span.h>
|
2021-01-11 02:29:28 +03:00
|
|
|
#include <AK/StdLibExtras.h>
|
2021-05-19 15:41:17 +03:00
|
|
|
#include <AK/Types.h>
|
2018-10-17 11:55:43 +03:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename>
|
|
|
|
class Function;
|
2018-10-17 11:55:43 +03:00
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
template<typename F>
|
|
|
|
inline constexpr bool IsFunctionPointer = (IsPointer<F> && IsFunction<RemovePointer<F>>);
|
|
|
|
|
|
|
|
// Not a function pointer, and not an lvalue reference.
|
|
|
|
template<typename F>
|
|
|
|
inline constexpr bool IsFunctionObject = (!IsFunctionPointer<F> && IsRvalueReference<F&&>);
|
|
|
|
|
2019-05-28 12:53:16 +03:00
|
|
|
template<typename Out, typename... In>
|
2018-10-17 11:55:43 +03:00
|
|
|
class Function<Out(In...)> {
|
2021-05-19 15:41:17 +03:00
|
|
|
AK_MAKE_NONCOPYABLE(Function);
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
public:
|
2023-08-18 19:21:33 +03:00
|
|
|
using FunctionType = Out(In...);
|
2022-11-21 17:49:24 +03:00
|
|
|
using ReturnType = Out;
|
|
|
|
|
2023-09-14 15:15:10 +03:00
|
|
|
#ifdef KERNEL
|
|
|
|
constexpr static auto AccommodateExcessiveAlignmentRequirements = false;
|
|
|
|
constexpr static size_t ExcessiveAlignmentThreshold = alignof(void*);
|
|
|
|
#else
|
|
|
|
constexpr static auto AccommodateExcessiveAlignmentRequirements = true;
|
|
|
|
constexpr static size_t ExcessiveAlignmentThreshold = 16;
|
|
|
|
#endif
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
Function() = default;
|
2022-12-13 09:59:30 +03:00
|
|
|
Function(nullptr_t)
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
{
|
|
|
|
}
|
2018-10-17 11:55:43 +03:00
|
|
|
|
2021-05-19 15:41:17 +03:00
|
|
|
~Function()
|
|
|
|
{
|
2021-06-04 13:59:53 +03:00
|
|
|
clear(false);
|
2021-05-19 15:41:17 +03:00
|
|
|
}
|
|
|
|
|
2023-08-18 19:21:33 +03:00
|
|
|
[[nodiscard]] ReadonlyBytes raw_capture_range() const
|
|
|
|
{
|
|
|
|
if (!m_size)
|
|
|
|
return {};
|
|
|
|
if (auto* wrapper = callable_wrapper())
|
|
|
|
return ReadonlyBytes { wrapper, m_size };
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
template<typename CallableType>
|
2022-10-17 01:06:11 +03:00
|
|
|
Function(CallableType&& callable)
|
2023-01-19 02:52:14 +03:00
|
|
|
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...> && !IsSame<RemoveCVReference<CallableType>, Function>))
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2023-08-18 19:21:33 +03:00
|
|
|
init_with_callable(forward<CallableType>(callable), CallableKind::FunctionObject);
|
2018-10-17 11:55:43 +03:00
|
|
|
}
|
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
template<typename FunctionType>
|
2022-10-17 01:06:11 +03:00
|
|
|
Function(FunctionType f)
|
2023-01-19 02:52:14 +03:00
|
|
|
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...> && !IsSame<RemoveCVReference<FunctionType>, Function>))
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2023-08-18 19:21:33 +03:00
|
|
|
init_with_callable(move(f), CallableKind::FunctionPointer);
|
2021-05-19 15:41:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
Function(Function&& other)
|
|
|
|
{
|
|
|
|
move_from(move(other));
|
2018-10-17 11:55:43 +03:00
|
|
|
}
|
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
// Note: Despite this method being const, a mutable lambda _may_ modify its own captures.
|
2018-10-23 12:57:38 +03:00
|
|
|
Out operator()(In... in) const
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2021-05-19 15:41:17 +03:00
|
|
|
auto* wrapper = callable_wrapper();
|
|
|
|
VERIFY(wrapper);
|
|
|
|
++m_call_nesting_level;
|
|
|
|
ScopeGuard guard([this] {
|
2021-06-04 13:59:53 +03:00
|
|
|
if (--m_call_nesting_level == 0 && m_deferred_clear)
|
|
|
|
const_cast<Function*>(this)->clear(false);
|
2021-05-19 15:41:17 +03:00
|
|
|
});
|
|
|
|
return wrapper->call(forward<In>(in)...);
|
2018-10-17 11:55:43 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 15:41:17 +03:00
|
|
|
explicit operator bool() const { return !!callable_wrapper(); }
|
2018-10-17 11:55:43 +03:00
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
template<typename CallableType>
|
2022-10-17 01:06:11 +03:00
|
|
|
Function& operator=(CallableType&& callable)
|
2023-01-19 02:52:14 +03:00
|
|
|
requires((IsFunctionObject<CallableType> && IsCallableWithArguments<CallableType, Out, In...>))
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2021-05-19 15:41:17 +03:00
|
|
|
clear();
|
2023-08-18 19:21:33 +03:00
|
|
|
init_with_callable(forward<CallableType>(callable), CallableKind::FunctionObject);
|
2018-10-17 11:55:43 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
template<typename FunctionType>
|
2022-10-17 01:06:11 +03:00
|
|
|
Function& operator=(FunctionType f)
|
2023-01-19 02:52:14 +03:00
|
|
|
requires((IsFunctionPointer<FunctionType> && IsCallableWithArguments<RemovePointer<FunctionType>, Out, In...>))
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2021-05-19 15:41:17 +03:00
|
|
|
clear();
|
|
|
|
if (f)
|
2023-08-18 19:21:33 +03:00
|
|
|
init_with_callable(move(f), CallableKind::FunctionPointer);
|
2018-10-17 11:55:43 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-12-13 09:59:30 +03:00
|
|
|
Function& operator=(nullptr_t)
|
2018-10-17 11:55:43 +03:00
|
|
|
{
|
2021-05-19 15:41:17 +03:00
|
|
|
clear();
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function& operator=(Function&& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
clear();
|
|
|
|
move_from(move(other));
|
|
|
|
}
|
2018-10-17 11:55:43 +03:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2023-08-18 19:21:33 +03:00
|
|
|
enum class CallableKind {
|
|
|
|
FunctionPointer,
|
|
|
|
FunctionObject,
|
|
|
|
};
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
class CallableWrapperBase {
|
|
|
|
public:
|
2021-01-11 02:29:28 +03:00
|
|
|
virtual ~CallableWrapperBase() = default;
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
// Note: This is not const to allow storing mutable lambdas.
|
|
|
|
virtual Out call(In...) = 0;
|
2021-05-19 15:41:17 +03:00
|
|
|
virtual void destroy() = 0;
|
|
|
|
virtual void init_and_swap(u8*, size_t) = 0;
|
2018-10-17 11:55:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
template<typename CallableType>
|
2018-11-08 17:39:26 +03:00
|
|
|
class CallableWrapper final : public CallableWrapperBase {
|
2021-05-19 15:41:17 +03:00
|
|
|
AK_MAKE_NONMOVABLE(CallableWrapper);
|
|
|
|
AK_MAKE_NONCOPYABLE(CallableWrapper);
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
public:
|
|
|
|
explicit CallableWrapper(CallableType&& callable)
|
|
|
|
: m_callable(move(callable))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
Out call(In... in) final override
|
2021-01-11 02:29:28 +03:00
|
|
|
{
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
return m_callable(forward<In>(in)...);
|
2021-01-11 02:29:28 +03:00
|
|
|
}
|
2018-10-17 11:55:43 +03:00
|
|
|
|
2021-05-19 15:41:17 +03:00
|
|
|
void destroy() final override
|
|
|
|
{
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2021-11-01 00:23:41 +03:00
|
|
|
// NOLINTNEXTLINE(readability-non-const-parameter) False positive; destination is used in a placement new expression
|
2021-05-19 15:41:17 +03:00
|
|
|
void init_and_swap(u8* destination, size_t size) final override
|
|
|
|
{
|
|
|
|
VERIFY(size >= sizeof(CallableWrapper));
|
|
|
|
new (destination) CallableWrapper { move(m_callable) };
|
|
|
|
}
|
|
|
|
|
2018-10-17 11:55:43 +03:00
|
|
|
private:
|
|
|
|
CallableType m_callable;
|
|
|
|
};
|
|
|
|
|
2021-05-19 15:41:17 +03:00
|
|
|
enum class FunctionKind {
|
|
|
|
NullPointer,
|
|
|
|
Inline,
|
|
|
|
Outline,
|
|
|
|
};
|
|
|
|
|
|
|
|
CallableWrapperBase* callable_wrapper() const
|
|
|
|
{
|
|
|
|
switch (m_kind) {
|
|
|
|
case FunctionKind::NullPointer:
|
|
|
|
return nullptr;
|
|
|
|
case FunctionKind::Inline:
|
|
|
|
return bit_cast<CallableWrapperBase*>(&m_storage);
|
|
|
|
case FunctionKind::Outline:
|
|
|
|
return *bit_cast<CallableWrapperBase**>(&m_storage);
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 13:59:53 +03:00
|
|
|
void clear(bool may_defer = true)
|
2021-05-19 15:41:17 +03:00
|
|
|
{
|
2021-06-04 13:59:53 +03:00
|
|
|
bool called_from_inside_function = m_call_nesting_level > 0;
|
2021-06-25 12:34:29 +03:00
|
|
|
// NOTE: This VERIFY could fail because a Function is destroyed from within itself.
|
2021-06-04 13:59:53 +03:00
|
|
|
VERIFY(may_defer || !called_from_inside_function);
|
|
|
|
if (called_from_inside_function && may_defer) {
|
|
|
|
m_deferred_clear = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
m_deferred_clear = false;
|
2021-05-19 15:41:17 +03:00
|
|
|
auto* wrapper = callable_wrapper();
|
|
|
|
if (m_kind == FunctionKind::Inline) {
|
|
|
|
VERIFY(wrapper);
|
|
|
|
wrapper->~CallableWrapperBase();
|
|
|
|
} else if (m_kind == FunctionKind::Outline) {
|
|
|
|
VERIFY(wrapper);
|
|
|
|
wrapper->destroy();
|
|
|
|
}
|
|
|
|
m_kind = FunctionKind::NullPointer;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Callable>
|
2023-08-18 19:21:33 +03:00
|
|
|
void init_with_callable(Callable&& callable, CallableKind callable_kind)
|
2021-05-19 15:41:17 +03:00
|
|
|
{
|
2023-09-14 15:15:10 +03:00
|
|
|
if constexpr (alignof(Callable) > ExcessiveAlignmentThreshold && !AccommodateExcessiveAlignmentRequirements) {
|
|
|
|
static_assert(
|
|
|
|
alignof(Callable) <= ExcessiveAlignmentThreshold,
|
|
|
|
"This callable object has a very large alignment requirement, "
|
|
|
|
"check your capture list if it is a lambda expression, "
|
|
|
|
"and make sure your callable object is not excessively aligned.");
|
|
|
|
}
|
2021-06-04 11:51:13 +03:00
|
|
|
VERIFY(m_call_nesting_level == 0);
|
2021-05-19 15:41:17 +03:00
|
|
|
using WrapperType = CallableWrapper<Callable>;
|
2022-11-01 11:28:44 +03:00
|
|
|
#ifndef KERNEL
|
2023-09-14 15:15:10 +03:00
|
|
|
if constexpr (alignof(Callable) > inline_alignment || sizeof(WrapperType) > inline_capacity) {
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
*bit_cast<CallableWrapperBase**>(&m_storage) = new WrapperType(forward<Callable>(callable));
|
2021-05-19 15:41:17 +03:00
|
|
|
m_kind = FunctionKind::Outline;
|
|
|
|
} else {
|
2022-11-01 11:28:44 +03:00
|
|
|
#endif
|
|
|
|
static_assert(sizeof(WrapperType) <= inline_capacity);
|
AK+Everywhere: Disallow constructing Functions from incompatible types
Previously, AK::Function would accept _any_ callable type, and try to
call it when called, first with the given set of arguments, then with
zero arguments, and if all of those failed, it would simply not call the
function and **return a value-constructed Out type**.
This lead to many, many, many hard to debug situations when someone
forgot a `const` in their lambda argument types, and many cases of
people taking zero arguments in their lambdas to ignore them.
This commit reworks the Function interface to not include any such
surprising behaviour, if your function instance is not callable with
the declared argument set of the Function, it can simply not be
assigned to that Function instance, end of story.
2021-06-05 21:34:31 +03:00
|
|
|
new (m_storage) WrapperType(forward<Callable>(callable));
|
2021-05-19 15:41:17 +03:00
|
|
|
m_kind = FunctionKind::Inline;
|
2022-11-01 11:28:44 +03:00
|
|
|
#ifndef KERNEL
|
2021-05-19 15:41:17 +03:00
|
|
|
}
|
2022-11-01 11:28:44 +03:00
|
|
|
#endif
|
2023-08-18 19:21:33 +03:00
|
|
|
if (callable_kind == CallableKind::FunctionObject)
|
|
|
|
m_size = sizeof(WrapperType);
|
|
|
|
else
|
|
|
|
m_size = 0;
|
2021-05-19 15:41:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void move_from(Function&& other)
|
|
|
|
{
|
|
|
|
VERIFY(m_call_nesting_level == 0 && other.m_call_nesting_level == 0);
|
|
|
|
auto* other_wrapper = other.callable_wrapper();
|
2023-08-18 19:21:33 +03:00
|
|
|
m_size = other.m_size;
|
2021-05-19 15:41:17 +03:00
|
|
|
switch (other.m_kind) {
|
|
|
|
case FunctionKind::NullPointer:
|
|
|
|
break;
|
|
|
|
case FunctionKind::Inline:
|
|
|
|
other_wrapper->init_and_swap(m_storage, inline_capacity);
|
|
|
|
m_kind = FunctionKind::Inline;
|
|
|
|
break;
|
|
|
|
case FunctionKind::Outline:
|
|
|
|
*bit_cast<CallableWrapperBase**>(&m_storage) = other_wrapper;
|
|
|
|
m_kind = FunctionKind::Outline;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
}
|
|
|
|
other.m_kind = FunctionKind::NullPointer;
|
|
|
|
}
|
|
|
|
|
2023-08-18 19:21:33 +03:00
|
|
|
size_t m_size { 0 };
|
2021-05-19 15:41:17 +03:00
|
|
|
FunctionKind m_kind { FunctionKind::NullPointer };
|
2021-06-04 13:59:53 +03:00
|
|
|
bool m_deferred_clear { false };
|
2021-05-19 15:41:17 +03:00
|
|
|
mutable Atomic<u16> m_call_nesting_level { 0 };
|
2023-08-18 19:21:33 +03:00
|
|
|
|
2023-09-14 15:15:10 +03:00
|
|
|
static constexpr size_t inline_alignment = max(alignof(CallableWrapperBase), alignof(CallableWrapperBase*));
|
2022-11-01 11:28:44 +03:00
|
|
|
#ifndef KERNEL
|
2021-05-19 15:41:17 +03:00
|
|
|
// Empirically determined to fit most lambdas and functions.
|
|
|
|
static constexpr size_t inline_capacity = 4 * sizeof(void*);
|
2022-11-01 11:28:44 +03:00
|
|
|
#else
|
|
|
|
// FIXME: Try to decrease this.
|
|
|
|
static constexpr size_t inline_capacity = 6 * sizeof(void*);
|
|
|
|
#endif
|
2023-09-14 15:15:10 +03:00
|
|
|
|
|
|
|
alignas(inline_alignment) u8 m_storage[inline_capacity];
|
2018-10-17 11:55:43 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2018-10-17 11:55:43 +03:00
|
|
|
using AK::Function;
|
2023-01-19 02:48:31 +03:00
|
|
|
using AK::IsCallableWithArguments;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|