5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_FUNCTION_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_INLINE_CONTAINER_FUNCTION_HPP_
16#include "arene/base/stdlib_choice/cstddef.hpp"
17#include "arene/base/stdlib_choice/decay.hpp"
18#include "arene/base/stdlib_choice/enable_if.hpp"
19#include "arene/base/stdlib_choice/forward.hpp"
20#include "arene/base/stdlib_choice/is_base_of.hpp"
21#include "arene/base/stdlib_choice/is_move_constructible.hpp"
22#include "arene/base/stdlib_choice/is_same.hpp"
23#include "arene/base/stdlib_choice/move.hpp"
59 bool IsNoexcept =
false,
77namespace inline_function_detail {
94class inline_function_base {
99 using invoke_func_type = ReturnType(invoke_self_type self, Args&&... args);
104 struct operation_functions {
107 void (*
const destroy)(inline_function_base& self)
noexcept;
111 void (*
const move)(inline_function_base& target, inline_function_base& source)
noexcept;
115 invoke_func_type*
const invoke;
121 template <
typename T>
122 static void do_destroy(inline_function_base& self)
noexcept {
124 T& wrapped_invocable{*self.template get_ptr<T>()};
126 wrapped_invocable.~T();
127 self.operations_ =
nullptr;
135 template <
typename T>
136 static void do_move(inline_function_base& target, inline_function_base& source)
noexcept {
137 target.construct_from(
std::move(*source.template get_ptr<T>()));
138 target.operations_ = source.operations_;
139 do_destroy<T>(source);
148 template <
typename T>
149 static auto do_invoke(invoke_self_type self, Args&&... args) -> ReturnType {
150 auto& wrapped_invocable = *self.template get_ptr<T>();
158 template <
typename T>
159 static constexpr operation_functions call_table{&do_destroy<T>, &do_move<T>, &do_invoke<T>};
166 template <
typename T>
167 static constexpr bool
172 inline_function_base() noexcept
174 operations_{
nullptr} {}
177 inline_function_base(inline_function_base
const&) =
delete;
179 auto operator=(inline_function_base
const&) -> inline_function_base& =
delete;
196 inline_function_base(T&& invocable) noexcept
208 typename InvocableTooBigForBuffer,
214 inline_function_base(InvocableTooBigForBuffer&& buffer_too_small) =
delete;
230 void assign_from(T&& invocable)
noexcept {
232 operations_ = &call_table<std::decay_t<T>>;
250 void construct_from(T&& invocable)
noexcept {
255 "This invocable requires stricter alignment than the inline_function it "
256 "is stored in: increase the Alignment "
257 "template argument of inline_function to at least alignof(the invocable)."
264 ~inline_function_base() { destroy(); }
268 auto is_initialized()
const noexcept ->
bool {
return operations_ !=
nullptr; }
272 auto get_invoke()
const noexcept -> invoke_func_type* {
274 return operations_->invoke;
280 explicit operator bool()
const noexcept {
return is_initialized(); }
289 inline_function_base(inline_function_base&& other) noexcept
290 : inline_function_base{} {
304 auto operator=(inline_function_base&& other)
noexcept -> inline_function_base& {
305 if (&other !=
this) {
317 void destroy()
noexcept {
318 if (is_initialized()) {
319 operations_->destroy(*
this);
325 void move_from(inline_function_base& other) {
326 if (other.is_initialized()) {
327 other.operations_->move(*
this, other);
335 template <
typename T>
336 auto get_ptr()
noexcept -> T* {
337 return static_cast<T*
>(
static_cast<void*
>(buffer_.data()));
343 template <
typename T>
344 auto get_ptr()
const noexcept -> T
const* {
345 return static_cast<T const*
>(
static_cast<void const*
>(buffer_.data()));
352 operation_functions
const* operations_;
371 typename inline_function_base<BufferSize, Alignment, IsConst, IsNoexcept, ReturnType, Args...>::operation_functions
372 inline_function_base<BufferSize, Alignment, IsConst, IsNoexcept, ReturnType, Args...>::call_table;
385template <
typename ReturnType,
typename... Args,
std::size_t BufferSize,
bool IsNoexcept,
std::size_t Alignment>
387 : inline_function_detail::inline_function_base<BufferSize, Alignment, false, IsNoexcept, ReturnType, Args...> {
390 inline_function_detail::inline_function_base<BufferSize, Alignment,
false, IsNoexcept, ReturnType, Args...>;
394 using base_class::base_class;
421 std::enable_if_t<(sizeof(
std::decay_t<T>) <= BufferSize)>,
422 std::enable_if_t<
std::is_nothrow_move_constructible<
std::decay_t<T>>::value>,
423 std::enable_if_t<base_class::template is_invocable_n_r_v<
std::decay_t<T>>>> =
nullptr>
435 typename InvocableTooBigForBuffer,
439 auto operator=(InvocableTooBigForBuffer&& buffer_too_small) ->
inline_function& =
delete;
450 using base_class::operator bool;
456 auto operator()(Args... args)
noexcept(IsNoexcept) -> ReturnType {
473template <
typename ReturnType,
typename... Args,
std::size_t BufferSize,
bool IsNoexcept,
std::size_t Alignment>
475 : inline_function_detail::inline_function_base<BufferSize, Alignment, true, IsNoexcept, ReturnType, Args...> {
478 inline_function_detail::inline_function_base<BufferSize, Alignment,
true, IsNoexcept, ReturnType, Args...>;
482 using base_class::base_class;
509 std::enable_if_t<(sizeof(
std::decay_t<T>) <= BufferSize)>,
510 std::enable_if_t<
std::is_nothrow_move_constructible<
std::decay_t<T>>::value>,
511 std::enable_if_t<base_class::template is_invocable_n_r_v<
std::decay_t<T> const>>> =
nullptr>
523 typename InvocableTooBigForBuffer,
527 auto operator=(InvocableTooBigForBuffer&& buffer_too_small) ->
inline_function& =
delete;
538 using base_class::operator bool;
544 auto operator()(Args... args)
const noexcept(IsNoexcept) -> ReturnType {
A fixed-size array of T objects.
Definition array.hpp:77
auto operator=(inline_function const &) -> inline_function &=delete
Not copyable.
auto operator()(Args... args) noexcept(IsNoexcept) -> ReturnType
Invoke the wrapped invocable as a non-const object, with the supplied arguments.
Definition function.hpp:456
inline_function(inline_function const &)=delete
Not copyable.
inline_function() noexcept=default
Default construct with no stored invocable.
~inline_function()=default
Destroy the wrapped invocable.
auto operator()(Args... args) const noexcept(IsNoexcept) -> ReturnType
Invoke the wrapped invocable as a const object, with the supplied arguments.
Definition function.hpp:544
inline_function() noexcept=default
Default construct with no stored invocable.
auto operator=(inline_function const &) -> inline_function &=delete
Not copyable.
~inline_function()=default
Destroy the wrapped invocable.
inline_function(inline_function const &)=delete
Not copyable.
A function wrapper akin to std::function that always stores the wrapped invocable internally.
Definition function.hpp:61
Type trait to detect if Base is a base class of Derived.
Definition is_base_of.hpp:30
Type trait to detect if an instance of T is move constructible without throwing.
Definition is_move_constructible.hpp:70
Trait to detect if the two template parameters are the same or not. The value member is true if the t...
Definition is_same.hpp:35
#define ARENE_PRECONDITION(condition)
Check that a precondition holds, and terminate the application if it does not.
Definition contract.hpp:150
Definition accumulate.hpp:37
constexpr auto move(InIterator first, InIterator last, OutIterator output) noexcept(noexcept(*first++) &&noexcept(output++) &&//CODEQLFP(EXP52-CPP) CODEQLFP(A5-2-6) noexcept(first !=last) &&//CODEQLFP(A5-2-6) std::is_nothrow_assignable<, typename std::iterator_traits< InIterator >::value_type >::value) -> OutIterator
Moves the elements in the range [first, last), to another range beginning at output,...
Definition move.hpp:46
typename static_if< B >::template then_else< T, F > conditional_t
lazily select between two types based on a boolean condition
Definition conditional.hpp:28
constexpr bool is_invocable_r_v
Determine if a functor type is invocable with the specified return and argument types.
static constexpr std::size_t default_inline_function_size
The default size of the buffer used for inline_function .
Definition function.hpp:37
constexpr bool is_nothrow_invocable_r_v
Determine if a functor type is no-throw invocable with the specified return and argument types.
constexpr auto invoke(F &&func, Args &&... args) noexcept(is_nothrow_invocable_v< F, Args... >) ->
Implementation of std::invoke from C++23: invokes the specified callable with the specified arguments...
Definition invoke.hpp:281
static constexpr std::size_t default_inline_function_alignment
The default alignment of the buffer used for inline_function .
Definition function.hpp:42
inline_function< Signature, BufferSize, true, Alignment > noexcept_inline_function
A function wrapper to a noexcept-qualified invocable akin to std::function that always stores the wra...
Definition function.hpp:75
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition accumulate.hpp:36
constexpr auto move(T &&value) noexcept -> remove_reference_t< T > &&
Cast a value to an rvalue.
Definition move.hpp:30
::max_align_t max_align_t
A POD type which has the maximum alignment required for any built-in type.
Definition cstddef.hpp:35
::size_t size_t
A type suitable for holding the size of an object.
Definition cstddef.hpp:29
typename enable_if< Condition, Type >::type enable_if_t
Type alias to support constraining templates. Is an alias for the specified type if the Condition is ...
Definition enable_if.hpp:41
typename decay< Type >::type decay_t
A conversion equivalent to those performed when passing function arguments by value.
Definition decay.hpp:48
constexpr auto forward(remove_reference_t< T > &value) noexcept -> T &&
Cast a value to an rvalue if T is an rvalue or rvalue reference, otherwise forward an lvalue referenc...
Definition forward.hpp:24