tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
bind_front.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_FUNCTIONAL_BIND_FRONT_HPP
4#define TETL_FUNCTIONAL_BIND_FRONT_HPP
5
14#include <etl/_utility/move.hpp>
15
16namespace etl {
17
18namespace detail {
19
20template <typename Func, typename BoundArgsTuple, typename... CallArgs>
21constexpr auto bind_front_caller(Func&& func, BoundArgsTuple&& boundArgsTuple, CallArgs&&... callArgs) -> decltype(auto)
22{
23 return etl::apply([&func, &callArgs...]<typename... BoundArgs>(BoundArgs&&... boundArgs) -> decltype(auto) {
24 return etl::invoke(
25 etl::forward<Func>(func),
26 etl::forward<BoundArgs>(boundArgs)...,
27 etl::forward<CallArgs>(callArgs)...
28 );
29 }, etl::forward<BoundArgsTuple>(boundArgsTuple));
30}
31
32template <typename Func, typename... BoundArgs>
33class bind_front_t {
34public:
35 template <typename F, typename... BA>
36 requires(!(sizeof...(BA) == 0 && is_base_of_v<bind_front_t, decay_t<F>>))
37 explicit bind_front_t(F&& f, BA&&... ba)
38 : _func(etl::forward<F>(f))
39 , _boundArgs(etl::forward<BA>(ba)...)
40 {
41 }
42
43 // TODO: Add noexcept(is_nothrow_invocable_v<Func&, BoundArgs&..., CallArgs...>)
44 template <typename... CallArgs>
45 auto operator()(CallArgs&&... callArgs) & -> invoke_result_t<Func&, BoundArgs&..., CallArgs...>
46 {
47 return bind_front_caller(_func, _boundArgs, etl::forward<CallArgs>(callArgs)...);
48 }
49
50 // TODO: Add noexcept(is_nothrow_invocable_v<Func const&, BoundArgs const&...,CallArgs...>)
51 template <typename... CallArgs>
52 auto operator()(CallArgs&&... callArgs) const& -> invoke_result_t<Func const&, BoundArgs const&..., CallArgs...>
53 {
54 return bind_front_caller(_func, _boundArgs, etl::forward<CallArgs>(callArgs)...);
55 }
56
57 // TODO: Add noexcept(is_nothrow_invocable_v<Func, BoundArgs..., CallArgs...>)
58 template <typename... CallArgs>
59 auto operator()(CallArgs&&... callArgs) && -> invoke_result_t<Func, BoundArgs..., CallArgs...>
60 {
61 return bind_front_caller(etl::move(_func), etl::move(_boundArgs), etl::forward<CallArgs>(callArgs)...);
62 }
63
64 // TODO: noexcept(is_nothrow_invocable_v<Func const, BoundArgs const...,CallArgs...>)
65 template <typename... CallArgs>
66 auto operator()(CallArgs&&... callArgs) const&& -> invoke_result_t<Func const, BoundArgs const..., CallArgs...>
67 {
68 return bind_front_caller(etl::move(_func), etl::move(_boundArgs), etl::forward<CallArgs>(callArgs)...);
69 }
70
71private:
72 Func _func;
73 tuple<BoundArgs...> _boundArgs;
74};
75
76} // namespace detail
77
86template <typename Func, typename... BoundArgs>
87constexpr auto bind_front(Func&& func, BoundArgs&&... boundArgs)
88{
89 return detail::bind_front_t<decay_t<Func>, unwrap_ref_decay_t<BoundArgs>...>{
91 etl::forward<BoundArgs>(boundArgs)...
92 };
93}
94
95} // namespace etl
96
97#endif // TETL_FUNCTIONAL_BIND_FRONT_HPP
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
Definition adjacent_find.hpp:8
constexpr bool is_base_of_v
Definition is_base_of.hpp:39
constexpr auto apply(F &&f, Tuple &&t) -> decltype(auto)
Definition apply.hpp:16
constexpr auto bind_front(Func &&func, BoundArgs &&... boundArgs)
The function template bind_front generates a forwarding call wrapper for f. Calling this wrapper is e...
Definition bind_front.hpp:87
typename unwrap_ref_decay< T >::type unwrap_ref_decay_t
Definition unwrap_reference.hpp:27
constexpr auto forward(remove_reference_t< T > &param) noexcept -> T &&
Forwards lvalues as either lvalues or as rvalues, depending on T. When t is a forwarding reference (a...
Definition forward.hpp:18