• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of 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,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Implementation details for `absl::bind_front()`.
16 
17 #ifndef ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
18 #define ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
19 
20 #include <cstddef>
21 #include <type_traits>
22 #include <utility>
23 
24 #include "absl/base/internal/invoke.h"
25 #include "absl/container/internal/compressed_tuple.h"
26 #include "absl/meta/type_traits.h"
27 #include "absl/utility/utility.h"
28 
29 namespace absl {
30 ABSL_NAMESPACE_BEGIN
31 namespace functional_internal {
32 
33 // Invoke the method, expanding the tuple of bound arguments.
34 template <class R, class Tuple, size_t... Idx, class... Args>
Apply(Tuple && bound,absl::index_sequence<Idx...>,Args &&...free)35 R Apply(Tuple&& bound, absl::index_sequence<Idx...>, Args&&... free) {
36   return base_internal::invoke(
37       absl::forward<Tuple>(bound).template get<Idx>()...,
38       absl::forward<Args>(free)...);
39 }
40 
41 template <class F, class... BoundArgs>
42 class FrontBinder {
43   using BoundArgsT = absl::container_internal::CompressedTuple<F, BoundArgs...>;
44   using Idx = absl::make_index_sequence<sizeof...(BoundArgs) + 1>;
45 
46   BoundArgsT bound_args_;
47 
48  public:
49   template <class... Ts>
FrontBinder(absl::in_place_t,Ts &&...ts)50   constexpr explicit FrontBinder(absl::in_place_t, Ts&&... ts)
51       : bound_args_(absl::forward<Ts>(ts)...) {}
52 
53   template <class... FreeArgs, class R = base_internal::invoke_result_t<
54                                    F&, BoundArgs&..., FreeArgs&&...>>
operator()55   R operator()(FreeArgs&&... free_args) & {
56     return functional_internal::Apply<R>(bound_args_, Idx(),
57                                          absl::forward<FreeArgs>(free_args)...);
58   }
59 
60   template <class... FreeArgs,
61             class R = base_internal::invoke_result_t<
62                 const F&, const BoundArgs&..., FreeArgs&&...>>
operator()63   R operator()(FreeArgs&&... free_args) const& {
64     return functional_internal::Apply<R>(bound_args_, Idx(),
65                                          absl::forward<FreeArgs>(free_args)...);
66   }
67 
68   template <class... FreeArgs, class R = base_internal::invoke_result_t<
69                                    F&&, BoundArgs&&..., FreeArgs&&...>>
operator()70   R operator()(FreeArgs&&... free_args) && {
71     // This overload is called when *this is an rvalue. If some of the bound
72     // arguments are stored by value or rvalue reference, we move them.
73     return functional_internal::Apply<R>(absl::move(bound_args_), Idx(),
74                                          absl::forward<FreeArgs>(free_args)...);
75   }
76 
77   template <class... FreeArgs,
78             class R = base_internal::invoke_result_t<
79                 const F&&, const BoundArgs&&..., FreeArgs&&...>>
operator()80   R operator()(FreeArgs&&... free_args) const&& {
81     // This overload is called when *this is an rvalue. If some of the bound
82     // arguments are stored by value or rvalue reference, we move them.
83     return functional_internal::Apply<R>(absl::move(bound_args_), Idx(),
84                                          absl::forward<FreeArgs>(free_args)...);
85   }
86 };
87 
88 template <class F, class... BoundArgs>
89 using bind_front_t = FrontBinder<decay_t<F>, absl::decay_t<BoundArgs>...>;
90 
91 }  // namespace functional_internal
92 ABSL_NAMESPACE_END
93 }  // namespace absl
94 
95 #endif  // ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
96