C/C++ API Reference
Loading...
Searching...
No Matches
function.h
1// Copyright 2021 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14#pragma once
15
16#include <cstddef>
17
18#include "lib/fit/function.h"
19#include "pw_function/config.h"
20
21namespace pw {
22
24
68template <typename FunctionType>
69using Function = fit::function_impl<
70 function_internal::config::kInlineCallableSize,
71 /*require_inline=*/!function_internal::config::kEnableDynamicAllocation,
72 FunctionType,
73 PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>;
74
85// TODO: b/252852651 - Remove warning above when conversion from
86// `fit::inline_function` to `fit::function` doesn't allocate anymore.
87template <typename FunctionType,
88 std::size_t inline_target_size =
89 function_internal::config::kInlineCallableSize>
90using InlineFunction = fit::inline_function<FunctionType, inline_target_size>;
91
101template <typename FunctionType,
102 std::size_t inline_target_size =
103 function_internal::config::kInlineCallableSize,
104 typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
105using DynamicFunction = fit::function_impl<inline_target_size,
106 /*require_inline=*/false,
107 FunctionType,
108 Allocator>;
109
111using Closure = Function<void()>;
112
123template <typename FunctionType>
124using Callback = fit::callback_impl<
125 function_internal::config::kInlineCallableSize,
126 /*require_inline=*/!function_internal::config::kEnableDynamicAllocation,
127 FunctionType,
128 PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>;
129
131template <typename FunctionType,
132 std::size_t inline_target_size =
133 function_internal::config::kInlineCallableSize>
134using InlineCallback = fit::inline_callback<FunctionType, inline_target_size>;
135
137template <typename FunctionType,
138 std::size_t inline_target_size =
139 function_internal::config::kInlineCallableSize,
140 typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
141using DynamicCallback = fit::callback_impl<inline_target_size,
142 /*require_inline=*/false,
143 FunctionType,
144 Allocator>;
145
154template <auto method, typename T>
155auto bind_member(T* instance) {
156 return fit::bind_member<method, T>(instance);
157}
158
159} // namespace pw
Definition: allocator.h:36
fit::inline_callback< FunctionType, inline_target_size > InlineCallback
Version of pw::Callback that exclusively uses inline storage.
Definition: function.h:134
fit::function_impl< inline_target_size, false, FunctionType, Allocator > DynamicFunction
Definition: function.h:108
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:73
fit::inline_function< FunctionType, inline_target_size > InlineFunction
Definition: function.h:90
auto bind_member(T *instance)
Definition: function.h:155
fit::callback_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Callback
Definition: function.h:128
fit::callback_impl< inline_target_size, false, FunctionType, Allocator > DynamicCallback
Version of pw::Callback that supports dynamic allocation.
Definition: function.h:144
Function< void()> Closure
void-returning pw::Function that takes no arguments.
Definition: function.h:111
The Pigweed namespace.
Definition: alignment.h:27