diff options
author | 2022-11-09 13:56:40 -0800 | |
---|---|---|
committer | 2023-12-13 01:14:11 +0000 | |
commit | ace9af9bc7fc604df5c201c080bbad6ad5ec5529 (patch) | |
tree | e3298820ead3960999e6e15646665cf64c9489c2 /libs/ftl/Android.bp | |
parent | 0c73574b6dd34c87390f2ae1e577cdaf21f820b3 (diff) |
FTL: Introduce ftl::Function<F,N> et al.
ftl::Function<F, N> is a container for function object, and can mostly
be used in place of std::function<F>.
Unlike std::function<F>, a ftl::Function<F, N>:
* Uses a static amount of memory (controlled by N), and never any
dynamic allocation.
* Satisfies the std::is_trivially_copyable<> trait.
* Satisfies the std::is_trivially_destructible<> trait.
However to satisfy those constraints, the contained function object must
also satisfy those constraints, meaning certain types (like
std::unique_ptr's) cannot be part of the contained function object type.
The size of a ftl::Function<F, N> is guaranteed to be:
sizeof(std::intptr_t) * (N + 2)
If not specified, N defaults to zero, which is big enough to store a lambda
that captures a single pointer (such as "this" for forwarding to a
member function.
By comparison, sizeof(std::function) == sizeof(std::intptr_t) * 4, at
least with on x86-64 with clang 15.
Compile time checks are performed that the constraints are all satisfied,
and that the value of N is large enough to contain the desired function
object type.
ftl::make_function is a helper function to construct a ftl::Function,
and will deduce the template type arguments. In addition to constructing
a ftl::Function for a function object, ftl::make_function has overloads
for creating a ftl::Function which will invoke a member function or a free
(non-member) function.
ftl::no_op is a helper value to construct a ftl::Function<F, N> that
does nothing, except default construct a return value, if one is needed.
A unit test is also included to demonstrate and verify the
implementation, including asserting that function objects which don't
meet the requirements cannot be used. The test also asserts some
non-obvious corner cases for handling argument and return value
conversions to match how std::function behaves.
Bug: 279581095
Test: atest ftl_test
Change-Id: I268facb106a248d0766e931595291036bc606fb7
Diffstat (limited to 'libs/ftl/Android.bp')
-rw-r--r-- | libs/ftl/Android.bp | 1 |
1 files changed, 1 insertions, 0 deletions
diff --git a/libs/ftl/Android.bp b/libs/ftl/Android.bp index ea1b5e4998..918680d6a7 100644 --- a/libs/ftl/Android.bp +++ b/libs/ftl/Android.bp @@ -17,6 +17,7 @@ cc_test { "enum_test.cpp", "fake_guard_test.cpp", "flags_test.cpp", + "function_test.cpp", "future_test.cpp", "match_test.cpp", "mixins_test.cpp", |