Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:40:51

0001 // Copyright 2018 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 
0015 // Implementation details for `absl::bind_front()`.
0016 
0017 #ifndef ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
0018 #define ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
0019 
0020 #include <cstddef>
0021 #include <type_traits>
0022 #include <utility>
0023 
0024 #include "absl/base/internal/invoke.h"
0025 #include "absl/container/internal/compressed_tuple.h"
0026 #include "absl/meta/type_traits.h"
0027 #include "absl/utility/utility.h"
0028 
0029 namespace absl {
0030 ABSL_NAMESPACE_BEGIN
0031 namespace functional_internal {
0032 
0033 // Invoke the method, expanding the tuple of bound arguments.
0034 template <class R, class Tuple, size_t... Idx, class... Args>
0035 R Apply(Tuple&& bound, absl::index_sequence<Idx...>, Args&&... free) {
0036   return base_internal::invoke(
0037       std::forward<Tuple>(bound).template get<Idx>()...,
0038       std::forward<Args>(free)...);
0039 }
0040 
0041 template <class F, class... BoundArgs>
0042 class FrontBinder {
0043   using BoundArgsT = absl::container_internal::CompressedTuple<F, BoundArgs...>;
0044   using Idx = absl::make_index_sequence<sizeof...(BoundArgs) + 1>;
0045 
0046   BoundArgsT bound_args_;
0047 
0048  public:
0049   template <class... Ts>
0050   constexpr explicit FrontBinder(absl::in_place_t, Ts&&... ts)
0051       : bound_args_(std::forward<Ts>(ts)...) {}
0052 
0053   template <class... FreeArgs, class R = base_internal::invoke_result_t<
0054                                    F&, BoundArgs&..., FreeArgs&&...>>
0055   R operator()(FreeArgs&&... free_args) & {
0056     return functional_internal::Apply<R>(bound_args_, Idx(),
0057                                          std::forward<FreeArgs>(free_args)...);
0058   }
0059 
0060   template <class... FreeArgs,
0061             class R = base_internal::invoke_result_t<
0062                 const F&, const BoundArgs&..., FreeArgs&&...>>
0063   R operator()(FreeArgs&&... free_args) const& {
0064     return functional_internal::Apply<R>(bound_args_, Idx(),
0065                                          std::forward<FreeArgs>(free_args)...);
0066   }
0067 
0068   template <class... FreeArgs, class R = base_internal::invoke_result_t<
0069                                    F&&, BoundArgs&&..., FreeArgs&&...>>
0070   R operator()(FreeArgs&&... free_args) && {
0071     // This overload is called when *this is an rvalue. If some of the bound
0072     // arguments are stored by value or rvalue reference, we move them.
0073     return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
0074                                          std::forward<FreeArgs>(free_args)...);
0075   }
0076 
0077   template <class... FreeArgs,
0078             class R = base_internal::invoke_result_t<
0079                 const F&&, const BoundArgs&&..., FreeArgs&&...>>
0080   R operator()(FreeArgs&&... free_args) const&& {
0081     // This overload is called when *this is an rvalue. If some of the bound
0082     // arguments are stored by value or rvalue reference, we move them.
0083     return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
0084                                          std::forward<FreeArgs>(free_args)...);
0085   }
0086 };
0087 
0088 template <class F, class... BoundArgs>
0089 using bind_front_t = FrontBinder<decay_t<F>, absl::decay_t<BoundArgs>...>;
0090 
0091 }  // namespace functional_internal
0092 ABSL_NAMESPACE_END
0093 }  // namespace absl
0094 
0095 #endif  // ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_