tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

front_binder.h (3599B)


      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/container/internal/compressed_tuple.h"
     25 #include "absl/meta/type_traits.h"
     26 #include "absl/utility/utility.h"
     27 
     28 namespace absl {
     29 ABSL_NAMESPACE_BEGIN
     30 namespace functional_internal {
     31 
     32 // Invoke the method, expanding the tuple of bound arguments.
     33 template <class R, class Tuple, size_t... Idx, class... Args>
     34 R Apply(Tuple&& bound, absl::index_sequence<Idx...>, Args&&... free) {
     35  return std::invoke(std::forward<Tuple>(bound).template get<Idx>()...,
     36                     std::forward<Args>(free)...);
     37 }
     38 
     39 template <class F, class... BoundArgs>
     40 class FrontBinder {
     41  using BoundArgsT = absl::container_internal::CompressedTuple<F, BoundArgs...>;
     42  using Idx = absl::make_index_sequence<sizeof...(BoundArgs) + 1>;
     43 
     44  BoundArgsT bound_args_;
     45 
     46 public:
     47  template <class... Ts>
     48  constexpr explicit FrontBinder(absl::in_place_t, Ts&&... ts)
     49      : bound_args_(std::forward<Ts>(ts)...) {}
     50 
     51  template <class... FreeArgs,
     52            class R = std::invoke_result_t<F&, BoundArgs&..., FreeArgs&&...>>
     53  R operator()(FreeArgs&&... free_args) & {
     54    return functional_internal::Apply<R>(bound_args_, Idx(),
     55                                         std::forward<FreeArgs>(free_args)...);
     56  }
     57 
     58  template <class... FreeArgs,
     59            class R = std::invoke_result_t<const F&, const BoundArgs&...,
     60                                           FreeArgs&&...>>
     61  R operator()(FreeArgs&&... free_args) const& {
     62    return functional_internal::Apply<R>(bound_args_, Idx(),
     63                                         std::forward<FreeArgs>(free_args)...);
     64  }
     65 
     66  template <class... FreeArgs,
     67            class R = std::invoke_result_t<F&&, BoundArgs&&..., FreeArgs&&...>>
     68  R operator()(FreeArgs&&... free_args) && {
     69    // This overload is called when *this is an rvalue. If some of the bound
     70    // arguments are stored by value or rvalue reference, we move them.
     71    return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
     72                                         std::forward<FreeArgs>(free_args)...);
     73  }
     74 
     75  template <class... FreeArgs,
     76            class R = std::invoke_result_t<const F&&, const BoundArgs&&...,
     77                                           FreeArgs&&...>>
     78  R operator()(FreeArgs&&... free_args) const&& {
     79    // This overload is called when *this is an rvalue. If some of the bound
     80    // arguments are stored by value or rvalue reference, we move them.
     81    return functional_internal::Apply<R>(std::move(bound_args_), Idx(),
     82                                         std::forward<FreeArgs>(free_args)...);
     83  }
     84 };
     85 
     86 template <class F, class... BoundArgs>
     87 using bind_front_t = FrontBinder<decay_t<F>, absl::decay_t<BoundArgs>...>;
     88 
     89 }  // namespace functional_internal
     90 ABSL_NAMESPACE_END
     91 }  // namespace absl
     92 
     93 #endif  // ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_