Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2024-11-15 09:01:03

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 // -----------------------------------------------------------------------------
0016 // File: bind_front.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // `absl::bind_front()` returns a functor by binding a number of arguments to
0020 // the front of a provided (usually more generic) functor. Unlike `std::bind`,
0021 // it does not require the use of argument placeholders. The simpler syntax of
0022 // `absl::bind_front()` allows you to avoid known misuses with `std::bind()`.
0023 //
0024 // `absl::bind_front()` is meant as a drop-in replacement for C++20's upcoming
0025 // `std::bind_front()`, which similarly resolves these issues with
0026 // `std::bind()`. Both `bind_front()` alternatives, unlike `std::bind()`, allow
0027 // partial function application. (See
0028 // https://en.wikipedia.org/wiki/Partial_application).
0029 
0030 #ifndef ABSL_FUNCTIONAL_BIND_FRONT_H_
0031 #define ABSL_FUNCTIONAL_BIND_FRONT_H_
0032 
0033 #if defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
0034 #include <functional>  // For std::bind_front.
0035 #endif  // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
0036 
0037 #include "absl/functional/internal/front_binder.h"
0038 #include "absl/utility/utility.h"
0039 
0040 namespace absl {
0041 ABSL_NAMESPACE_BEGIN
0042 
0043 // bind_front()
0044 //
0045 // Binds the first N arguments of an invocable object and stores them by value.
0046 //
0047 // Like `std::bind()`, `absl::bind_front()` is implicitly convertible to
0048 // `std::function`.  In particular, it may be used as a simpler replacement for
0049 // `std::bind()` in most cases, as it does not require placeholders to be
0050 // specified. More importantly, it provides more reliable correctness guarantees
0051 // than `std::bind()`; while `std::bind()` will silently ignore passing more
0052 // parameters than expected, for example, `absl::bind_front()` will report such
0053 // mis-uses as errors. In C++20, `absl::bind_front` is replaced by
0054 // `std::bind_front`.
0055 //
0056 // absl::bind_front(a...) can be seen as storing the results of
0057 // std::make_tuple(a...).
0058 //
0059 // Example: Binding a free function.
0060 //
0061 //   int Minus(int a, int b) { return a - b; }
0062 //
0063 //   assert(absl::bind_front(Minus)(3, 2) == 3 - 2);
0064 //   assert(absl::bind_front(Minus, 3)(2) == 3 - 2);
0065 //   assert(absl::bind_front(Minus, 3, 2)() == 3 - 2);
0066 //
0067 // Example: Binding a member function.
0068 //
0069 //   struct Math {
0070 //     int Double(int a) const { return 2 * a; }
0071 //   };
0072 //
0073 //   Math math;
0074 //
0075 //   assert(absl::bind_front(&Math::Double)(&math, 3) == 2 * 3);
0076 //   // Stores a pointer to math inside the functor.
0077 //   assert(absl::bind_front(&Math::Double, &math)(3) == 2 * 3);
0078 //   // Stores a copy of math inside the functor.
0079 //   assert(absl::bind_front(&Math::Double, math)(3) == 2 * 3);
0080 //   // Stores std::unique_ptr<Math> inside the functor.
0081 //   assert(absl::bind_front(&Math::Double,
0082 //                           std::unique_ptr<Math>(new Math))(3) == 2 * 3);
0083 //
0084 // Example: Using `absl::bind_front()`, instead of `std::bind()`, with
0085 //          `std::function`.
0086 //
0087 //   class FileReader {
0088 //    public:
0089 //     void ReadFileAsync(const std::string& filename, std::string* content,
0090 //                        const std::function<void()>& done) {
0091 //       // Calls Executor::Schedule(std::function<void()>).
0092 //       Executor::DefaultExecutor()->Schedule(
0093 //           absl::bind_front(&FileReader::BlockingRead, this,
0094 //                            filename, content, done));
0095 //     }
0096 //
0097 //    private:
0098 //     void BlockingRead(const std::string& filename, std::string* content,
0099 //                       const std::function<void()>& done) {
0100 //       CHECK_OK(file::GetContents(filename, content, {}));
0101 //       done();
0102 //     }
0103 //   };
0104 //
0105 // `absl::bind_front()` stores bound arguments explicitly using the type passed
0106 // rather than implicitly based on the type accepted by its functor.
0107 //
0108 // Example: Binding arguments explicitly.
0109 //
0110 //   void LogStringView(absl::string_view sv) {
0111 //     LOG(INFO) << sv;
0112 //   }
0113 //
0114 //   Executor* e = Executor::DefaultExecutor();
0115 //   std::string s = "hello";
0116 //   absl::string_view sv = s;
0117 //
0118 //   // absl::bind_front(LogStringView, arg) makes a copy of arg and stores it.
0119 //   e->Schedule(absl::bind_front(LogStringView, sv)); // ERROR: dangling
0120 //                                                     // string_view.
0121 //
0122 //   e->Schedule(absl::bind_front(LogStringView, s));  // OK: stores a copy of
0123 //                                                     // s.
0124 //
0125 // To store some of the arguments passed to `absl::bind_front()` by reference,
0126 //  use std::ref()` and `std::cref()`.
0127 //
0128 // Example: Storing some of the bound arguments by reference.
0129 //
0130 //   class Service {
0131 //    public:
0132 //     void Serve(const Request& req, std::function<void()>* done) {
0133 //       // The request protocol buffer won't be deleted until done is called.
0134 //       // It's safe to store a reference to it inside the functor.
0135 //       Executor::DefaultExecutor()->Schedule(
0136 //           absl::bind_front(&Service::BlockingServe, this, std::cref(req),
0137 //           done));
0138 //     }
0139 //
0140 //    private:
0141 //     void BlockingServe(const Request& req, std::function<void()>* done);
0142 //   };
0143 //
0144 // Example: Storing bound arguments by reference.
0145 //
0146 //   void Print(const std::string& a, const std::string& b) {
0147 //     std::cerr << a << b;
0148 //   }
0149 //
0150 //   std::string hi = "Hello, ";
0151 //   std::vector<std::string> names = {"Chuk", "Gek"};
0152 //   // Doesn't copy hi.
0153 //   for_each(names.begin(), names.end(),
0154 //            absl::bind_front(Print, std::ref(hi)));
0155 //
0156 //   // DO NOT DO THIS: the functor may outlive "hi", resulting in
0157 //   // dangling references.
0158 //   foo->DoInFuture(absl::bind_front(Print, std::ref(hi), "Guest"));  // BAD!
0159 //   auto f = absl::bind_front(Print, std::ref(hi), "Guest"); // BAD!
0160 //
0161 // Example: Storing reference-like types.
0162 //
0163 //   void Print(absl::string_view a, const std::string& b) {
0164 //     std::cerr << a << b;
0165 //   }
0166 //
0167 //   std::string hi = "Hello, ";
0168 //   // Copies "hi".
0169 //   absl::bind_front(Print, hi)("Chuk");
0170 //
0171 //   // Compile error: std::reference_wrapper<const string> is not implicitly
0172 //   // convertible to string_view.
0173 //   // absl::bind_front(Print, std::cref(hi))("Chuk");
0174 //
0175 //   // Doesn't copy "hi".
0176 //   absl::bind_front(Print, absl::string_view(hi))("Chuk");
0177 //
0178 #if defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
0179 using std::bind_front;
0180 #else   // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
0181 template <class F, class... BoundArgs>
0182 constexpr functional_internal::bind_front_t<F, BoundArgs...> bind_front(
0183     F&& func, BoundArgs&&... args) {
0184   return functional_internal::bind_front_t<F, BoundArgs...>(
0185       absl::in_place, absl::forward<F>(func),
0186       absl::forward<BoundArgs>(args)...);
0187 }
0188 #endif  // defined(__cpp_lib_bind_front) && __cpp_lib_bind_front >= 201907L
0189 
0190 ABSL_NAMESPACE_END
0191 }  // namespace absl
0192 
0193 #endif  // ABSL_FUNCTIONAL_BIND_FRONT_H_