bind_front.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. // -----------------------------------------------------------------------------
  16. // File: bind_front.h
  17. // -----------------------------------------------------------------------------
  18. //
  19. // `absl::bind_front()` returns a functor by binding a number of arguments to
  20. // the front of a provided (usually more generic) functor. Unlike `std::bind`,
  21. // it does not require the use of argument placeholders. The simpler syntax of
  22. // `absl::bind_front()` allows you to avoid known misuses with `std::bind()`.
  23. //
  24. // `absl::bind_front()` is meant as a drop-in replacement for C++20's upcoming
  25. // `std::bind_front()`, which similarly resolves these issues with
  26. // `std::bind()`. Both `bind_front()` alternatives, unlike `std::bind()`, allow
  27. // partial function application. (See
  28. // https://en.wikipedia.org/wiki/Partial_application).
  29. #ifndef ABSL_FUNCTIONAL_BIND_FRONT_H_
  30. #define ABSL_FUNCTIONAL_BIND_FRONT_H_
  31. #include "absl/functional/internal/front_binder.h"
  32. #include "absl/utility/utility.h"
  33. namespace absl {
  34. ABSL_NAMESPACE_BEGIN
  35. // bind_front()
  36. //
  37. // Binds the first N arguments of an invocable object and stores them by value.
  38. //
  39. // Like `std::bind()`, `absl::bind_front()` is implicitly convertible to
  40. // `std::function`. In particular, it may be used as a simpler replacement for
  41. // `std::bind()` in most cases, as it does not require placeholders to be
  42. // specified. More importantly, it provides more reliable correctness guarantees
  43. // than `std::bind()`; while `std::bind()` will silently ignore passing more
  44. // parameters than expected, for example, `absl::bind_front()` will report such
  45. // mis-uses as errors.
  46. //
  47. // absl::bind_front(a...) can be seen as storing the results of
  48. // std::make_tuple(a...).
  49. //
  50. // Example: Binding a free function.
  51. //
  52. // int Minus(int a, int b) { return a - b; }
  53. //
  54. // assert(absl::bind_front(Minus)(3, 2) == 3 - 2);
  55. // assert(absl::bind_front(Minus, 3)(2) == 3 - 2);
  56. // assert(absl::bind_front(Minus, 3, 2)() == 3 - 2);
  57. //
  58. // Example: Binding a member function.
  59. //
  60. // struct Math {
  61. // int Double(int a) const { return 2 * a; }
  62. // };
  63. //
  64. // Math math;
  65. //
  66. // assert(absl::bind_front(&Math::Double)(&math, 3) == 2 * 3);
  67. // // Stores a pointer to math inside the functor.
  68. // assert(absl::bind_front(&Math::Double, &math)(3) == 2 * 3);
  69. // // Stores a copy of math inside the functor.
  70. // assert(absl::bind_front(&Math::Double, math)(3) == 2 * 3);
  71. // // Stores std::unique_ptr<Math> inside the functor.
  72. // assert(absl::bind_front(&Math::Double,
  73. // std::unique_ptr<Math>(new Math))(3) == 2 * 3);
  74. //
  75. // Example: Using `absl::bind_front()`, instead of `std::bind()`, with
  76. // `std::function`.
  77. //
  78. // class FileReader {
  79. // public:
  80. // void ReadFileAsync(const std::string& filename, std::string* content,
  81. // const std::function<void()>& done) {
  82. // // Calls Executor::Schedule(std::function<void()>).
  83. // Executor::DefaultExecutor()->Schedule(
  84. // absl::bind_front(&FileReader::BlockingRead, this,
  85. // filename, content, done));
  86. // }
  87. //
  88. // private:
  89. // void BlockingRead(const std::string& filename, std::string* content,
  90. // const std::function<void()>& done) {
  91. // CHECK_OK(file::GetContents(filename, content, {}));
  92. // done();
  93. // }
  94. // };
  95. //
  96. // `absl::bind_front()` stores bound arguments explicitly using the type passed
  97. // rather than implicitly based on the type accepted by its functor.
  98. //
  99. // Example: Binding arguments explicitly.
  100. //
  101. // void LogStringView(absl::string_view sv) {
  102. // LOG(INFO) << sv;
  103. // }
  104. //
  105. // Executor* e = Executor::DefaultExecutor();
  106. // std::string s = "hello";
  107. // absl::string_view sv = s;
  108. //
  109. // // absl::bind_front(LogStringView, arg) makes a copy of arg and stores it.
  110. // e->Schedule(absl::bind_front(LogStringView, sv)); // ERROR: dangling
  111. // // string_view.
  112. //
  113. // e->Schedule(absl::bind_front(LogStringView, s)); // OK: stores a copy of
  114. // // s.
  115. //
  116. // To store some of the arguments passed to `absl::bind_front()` by reference,
  117. // use std::ref()` and `std::cref()`.
  118. //
  119. // Example: Storing some of the bound arguments by reference.
  120. //
  121. // class Service {
  122. // public:
  123. // void Serve(const Request& req, std::function<void()>* done) {
  124. // // The request protocol buffer won't be deleted until done is called.
  125. // // It's safe to store a reference to it inside the functor.
  126. // Executor::DefaultExecutor()->Schedule(
  127. // absl::bind_front(&Service::BlockingServe, this, std::cref(req),
  128. // done));
  129. // }
  130. //
  131. // private:
  132. // void BlockingServe(const Request& req, std::function<void()>* done);
  133. // };
  134. //
  135. // Example: Storing bound arguments by reference.
  136. //
  137. // void Print(const std::string& a, const std::string& b) {
  138. // std::cerr << a << b;
  139. // }
  140. //
  141. // std::string hi = "Hello, ";
  142. // std::vector<std::string> names = {"Chuk", "Gek"};
  143. // // Doesn't copy hi.
  144. // for_each(names.begin(), names.end(),
  145. // absl::bind_front(Print, std::ref(hi)));
  146. //
  147. // // DO NOT DO THIS: the functor may outlive "hi", resulting in
  148. // // dangling references.
  149. // foo->DoInFuture(absl::bind_front(Print, std::ref(hi), "Guest")); // BAD!
  150. // auto f = absl::bind_front(Print, std::ref(hi), "Guest"); // BAD!
  151. //
  152. // Example: Storing reference-like types.
  153. //
  154. // void Print(absl::string_view a, const std::string& b) {
  155. // std::cerr << a << b;
  156. // }
  157. //
  158. // std::string hi = "Hello, ";
  159. // // Copies "hi".
  160. // absl::bind_front(Print, hi)("Chuk");
  161. //
  162. // // Compile error: std::reference_wrapper<const string> is not implicitly
  163. // // convertible to string_view.
  164. // // absl::bind_front(Print, std::cref(hi))("Chuk");
  165. //
  166. // // Doesn't copy "hi".
  167. // absl::bind_front(Print, absl::string_view(hi))("Chuk");
  168. //
  169. template <class F, class... BoundArgs>
  170. constexpr functional_internal::bind_front_t<F, BoundArgs...> bind_front(
  171. F&& func, BoundArgs&&... args) {
  172. return functional_internal::bind_front_t<F, BoundArgs...>(
  173. absl::in_place, absl::forward<F>(func),
  174. absl::forward<BoundArgs>(args)...);
  175. }
  176. ABSL_NAMESPACE_END
  177. } // namespace absl
  178. #endif // ABSL_FUNCTIONAL_BIND_FRONT_H_