Debian Codegraph @

include/deal.II/base/std_cxx20/functional.h

raw
1 // ------------------------------------------------------------------------
2 //
3 // SPDX-License-Identifier: LGPL-2.1-or-later
4 // Copyright (C) 2023 by the deal.II authors
5 //
6 // This file is part of the deal.II library.
7 //
8 // Part of the source code is dual licensed under Apache-2.0 WITH
9 // LLVM-exception OR LGPL-2.1-or-later. Detailed license information
10 // governing the source code and code contributions can be found in
11 // LICENSE.md and CONTRIBUTING.md at the top level directory of deal.II.
12 //
13 // ------------------------------------------------------------------------
14 #ifndef dealii_cxx20_functional_h
15 #define dealii_cxx20_functional_h
16
17 #include <deal.II/base/config.h>
18
19 #ifdef DEAL_II_HAVE_CXX20
20 # include <functional>
21 #else
22 # include <functional>
23 # include <tuple>
24 # include <utility>
25 #endif
26
27 DEAL_II_NAMESPACE_OPEN
28
29 namespace std_cxx20
30 {
31 #ifndef DEAL_II_HAVE_CXX20
32 # ifndef DOXYGEN
33
34 namespace internal
35 {
36 /**
37 * This call is used to invoke the lambda with stored bound arguments.
38 *
39 * @note The extra call is required to pattern-match the index_sequence.
40 */
41 template <typename F,
42 size_t... Ind,
43 typename BoundArgsTuple,
44 typename... CallArgs>
45 constexpr decltype(auto)
46 call_bind(F &&function,
47 std::index_sequence<Ind...>,
48 BoundArgsTuple &&bound_args,
49 CallArgs &&...call_args)
50 {
51 return std::invoke(std::forward<F>(function),
52 std::get<Ind>(
53 std::forward<BoundArgsTuple>(bound_args))...,
54 std::forward<CallArgs>(call_args)...);
55 }
56
57 /**
58 * Return a callable which closely approximates what std::bind_front() is
59 * doing.
60 */
61 template <typename F, typename... BoundArgs>
62 decltype(auto)
63 make_bind_front(F &&f, BoundArgs &&...bound_args)
64 {
65 return [f = std::forward<F>(f),
66 bound_args = std::make_tuple(
67 std::forward<BoundArgs>(bound_args)...)](auto &&...call_args) {
68 // Perform actual call inside a helper functions which allows to use
69 // pattern-matching to the index sequence.
70 return call_bind(f,
71 std::index_sequence_for<BoundArgs...>{},
72 bound_args,
73 std::forward<decltype(call_args)>(call_args)...);
74 };
75 }
76
77 } // namespace internal
78
79 # endif
80
81 /**
82 * This function generates a forwarding call wrapper to the function @p f
83 * which has its first `n` arguments bound to the `n` arguments passed in
84 * @p bound_args. The implementation is an approximation to the functionality
85 * provided by `std::bind_front`, see
86 * https://en.cppreference.com/w/cpp/utility/functional/bind_front.
87 *
88 * This function allows to remove boilerplate in user code. Often functions in
89 * the deal.II library take a `std::function` object as an argument. While you
90 * can pass many different types of callable objects to a `std::function`
91 * directly, you cannot pass a pointer to a member function. Instead you can
92 * use the present function to create a compatible callable on-the-fly which
93 * can be assigned to `std::function`. This is demonstrated in the following
94 * example:
95 *
96 * @code
97 *
98 * // An exemplary function that takes an std::function as argument.
99 * void example_function(const std::function<int(double)>& fn);
100 *
101 * class MyClass
102 * {
103 * public:
104 * void do_something()
105 * {
106 * // Pass the member function
107 * example_function(
108 * std_cxx20::bind_front(&MyClass::my_function, this));
109 * }
110 *
111 * private:
112 * // This function has a signature which is compatible with the function
113 * // that is expected by example_function().
114 * int my_function(double);
115 * };
116 *
117 * @endcode
118 */
119 template <typename F, typename... BoundArgs>
120 decltype(auto)
121 bind_front(F &&f, BoundArgs &&...bound_args)
122 {
123 return internal::make_bind_front(std::forward<F>(f),
124 std::forward<BoundArgs>(bound_args)...);
125 }
126 #else
127 using std::bind_front;
128 #endif
129
130 } // namespace std_cxx20
131
132 DEAL_II_NAMESPACE_CLOSE
133
134 #endif