You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

front_binder.h 3.9 kB

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