Pigweed
 
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
25
69template <typename FunctionType>
70using Function = fit::function_impl<
71 function_internal::config::kInlineCallableSize,
72 /*require_inline=*/!function_internal::config::kEnableDynamicAllocation,
73 FunctionType,
74 PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>;
75
86// TODO: b/252852651 - Remove warning above when conversion from
87// `fit::inline_function` to `fit::function` doesn't allocate anymore.
88template <typename FunctionType,
89 std::size_t inline_target_size =
90 function_internal::config::kInlineCallableSize>
91using InlineFunction = fit::inline_function<FunctionType, inline_target_size>;
92
102template <typename FunctionType,
103 std::size_t inline_target_size =
104 function_internal::config::kInlineCallableSize,
105 typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
106using DynamicFunction = fit::function_impl<inline_target_size,
107 /*require_inline=*/false,
108 FunctionType,
109 Allocator>;
110
112using Closure = Function<void()>;
113
124template <typename FunctionType>
125using Callback = fit::callback_impl<
126 function_internal::config::kInlineCallableSize,
127 /*require_inline=*/!function_internal::config::kEnableDynamicAllocation,
128 FunctionType,
129 PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>;
130
132template <typename FunctionType,
133 std::size_t inline_target_size =
134 function_internal::config::kInlineCallableSize>
135using InlineCallback = fit::inline_callback<FunctionType, inline_target_size>;
136
138template <typename FunctionType,
139 std::size_t inline_target_size =
140 function_internal::config::kInlineCallableSize,
141 typename Allocator = PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE>
142using DynamicCallback = fit::callback_impl<inline_target_size,
143 /*require_inline=*/false,
144 FunctionType,
145 Allocator>;
146
155template <auto method, typename T>
156auto bind_member(T* instance) {
157 return fit::bind_member<method, T>(instance);
158}
159
161
162} // namespace pw
Definition: allocator.h:34
fit::inline_callback< FunctionType, inline_target_size > InlineCallback
Version of pw::Callback that exclusively uses inline storage.
Definition: function.h:135
fit::function_impl< inline_target_size, false, FunctionType, Allocator > DynamicFunction
Definition: function.h:109
fit::function_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Function
Definition: function.h:74
fit::inline_function< FunctionType, inline_target_size > InlineFunction
Definition: function.h:91
auto bind_member(T *instance)
Definition: function.h:156
fit::callback_impl< function_internal::config::kInlineCallableSize, !function_internal::config::kEnableDynamicAllocation, FunctionType, PW_FUNCTION_DEFAULT_ALLOCATOR_TYPE > Callback
Definition: function.h:129
fit::callback_impl< inline_target_size, false, FunctionType, Allocator > DynamicCallback
Version of pw::Callback that supports dynamic allocation.
Definition: function.h:145
Function< void()> Closure
void-returning pw::Function that takes no arguments.
Definition: function.h:112
Provides basic helpers for reading and writing UTF-8 encoded strings.
Definition: alignment.h:27