Pigweed
 
Loading...
Searching...
No Matches
Pw_function

Typedefs

template<typename FunctionType >
using pw::Function = fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE >
 
template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize>
using pw::InlineFunction = fit::inline_function< FunctionType, inline_target_size >
 
template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize, typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
using pw::DynamicFunction = fit::function_impl< inline_target_size, false, FunctionType, Allocator >
 
using pw::Closure = Function< void()>
 void-returning pw::Function that takes no arguments.
 
template<typename FunctionType >
using pw::Callback = fit::callback_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE >
 
template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize>
using pw::InlineCallback = fit::inline_callback< FunctionType, inline_target_size >
 Version of pw::Callback that exclusively uses inline storage.
 
template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize, typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
using pw::DynamicCallback = fit::callback_impl< inline_target_size, false, FunctionType, Allocator >
 Version of pw::Callback that supports dynamic allocation.
 

Functions

template<auto method, typename T >
auto pw::bind_member (T *instance)
 

Detailed Description

Typedef Documentation

◆ Callback

template<typename FunctionType >
using pw::Callback = typedef fit::callback_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>

pw::Callback is identical to

embed:rst:inline :cpp:type:`pw::Function` 

except:

  1. On the first call to invoke a pw::Callback, the target function held by the pw::Callback cannot be called again.
  2. When a pw::Callback is invoked for the first time, the target function is released and destructed, along with any resources owned by that function (typically the objects captured by a lambda).

A pw::Callback in the "already called" state has the same state as a pw::Callback that has been assigned to nullptr.

◆ DynamicFunction

template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize, typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
using pw::DynamicFunction = typedef fit::function_impl<inline_target_size, false, FunctionType, Allocator>

Version of pw::Function that always supports dynamic allocation.

If dynamic allocation is enabled for pw::Function, pw::Function<FunctionType> is identical to pw::DynamicFunction<FunctionType>.

Template Parameters
AllocatorThe Allocator used to dynamically allocate the callable, if it exceeds inline_target_size. Its value_type is irrelevant, since it must support rebinding.

◆ Function

template<typename FunctionType >
using pw::Function = typedef fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>

pw::Function is a wrapper for an arbitrary callable object. It can be used by callback-based APIs to allow callers to provide any type of callable.

pw::Function is an alias shared across the Pigweed codebase. External users may configure pw::Function to meet their needs (e.g. more inline storage, support for dynamic memory). Having a shared alias ensures that function objects are interchangeable and keeps code efficient.

Because pw::Function is configurable, upstream Pigweed code must compile against pw::Function's default, minimal configuration: no dynamic memory and a PW_FUNCTION_INLINE_CALLABLE_SIZE of one word.

Almost all upstream Pigweed code should use the default pw::Function. If a different function configuration is needed, use pw::InlineFunction or pw::DynamicFunction. This is not recommended, since this results in increased code size, and conversions between function types are not well optimized.

Example:

template <typename T>
bool All(const pw::Vector<T>& items,
const pw::Function<bool(const T& item)>& predicate) {
for (const T& item : items) {
if (!predicate(item)) {
return false;
}
}
return true;
}
bool ElementsArePositive(const pw::Vector<int>& items) {
return All(items, [](const int& i) { return i > 0; });
}
bool IsEven(const int& i) { return i % 2 == 0; }
bool ElementsAreEven(const pw::Vector<int>& items) {
return All(items, IsEven);
}
Definition: vector.h:65
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74

◆ InlineFunction

template<typename FunctionType , std::size_t inline_target_size = function_internal::config::kInlineCallableSize>
using pw::InlineFunction = typedef fit::inline_function<FunctionType, inline_target_size>

Version of pw::Function that exclusively uses inline storage.

IMPORTANT: If pw::Function is configured to allow dynamic allocations then any attempt to convert pw::InlineFunction to pw::Function will ALWAYS allocate.

If dynamic allocation is disabled for pw::Function, pw::Function<FunctionType> is identical to pw::InlineFunction<FunctionType>.

Function Documentation

◆ bind_member()

template<auto method, typename T >
auto pw::bind_member ( T *  instance)

Returns a Callable which, when called, invokes method on instance using the arguments provided.

This is useful for binding the this argument of a callable.

pw::bind_member<&T::MethodName>(instance) is roughly equivalent to [instance](Arg arg1, ...) { instance->MethodName(arg1, ...) }, albeit with proper support for overloads and argument forwarding.