about summary refs log tree commit diff
path: root/absl/functional
diff options
context:
space:
mode:
Diffstat (limited to 'absl/functional')
-rw-r--r--absl/functional/BUILD.bazel26
-rw-r--r--absl/functional/bind_front.h152
-rw-r--r--absl/functional/bind_front_test.cc231
-rw-r--r--absl/functional/internal/front_binder.h95
4 files changed, 504 insertions, 0 deletions
diff --git a/absl/functional/BUILD.bazel b/absl/functional/BUILD.bazel
index 0a7b588e70e0..432546ce0c1b 100644
--- a/absl/functional/BUILD.bazel
+++ b/absl/functional/BUILD.bazel
@@ -27,6 +27,32 @@ package(default_visibility = ["//visibility:public"])
 licenses(["notice"])  # Apache 2.0
 
 cc_library(
+    name = "bind_front",
+    srcs = ["internal/front_binder.h"],
+    hdrs = ["bind_front.h"],
+    copts = ABSL_DEFAULT_COPTS,
+    linkopts = ABSL_DEFAULT_LINKOPTS,
+    deps = [
+        "//absl/base:base_internal",
+        "//absl/container:compressed_tuple",
+        "//absl/meta:type_traits",
+        "//absl/utility",
+    ],
+)
+
+cc_test(
+    name = "bind_front_test",
+    srcs = ["bind_front_test.cc"],
+    copts = ABSL_TEST_COPTS,
+    linkopts = ABSL_DEFAULT_LINKOPTS,
+    deps = [
+        ":bind_front",
+        "//absl/memory",
+        "@com_google_googletest//:gtest_main",
+    ],
+)
+
+cc_library(
     name = "function_ref",
     srcs = ["internal/function_ref.h"],
     hdrs = ["function_ref.h"],
diff --git a/absl/functional/bind_front.h b/absl/functional/bind_front.h
new file mode 100644
index 000000000000..4c61d0ecfd12
--- /dev/null
+++ b/absl/functional/bind_front.h
@@ -0,0 +1,152 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// `absl::bind_front()` returns a functor by binding a number of arguments to
+// the front of a provided functor, allowing you to avoid known problems with
+// `std::bind()`.  It is a form of partial function application
+// https://en.wikipedia.org/wiki/Partial_application.
+//
+// Like `std::bind()` it is implicitly convertible to `std::function`.  In
+// particular, it may be used as a simpler replacement for `std::bind()` in most
+// cases, as it does not require  placeholders to be specified.  More
+// importantly, it provides more reliable correctness guarantees than
+// `std::bind()`.
+//
+// absl::bind_front(a...) can be seen as storing the results of
+// std::make_tuple(a...).
+//
+// Example: Binding a free function.
+//
+//   int Minus(int a, int b) { return a - b; }
+//
+//   assert(absl::bind_front(Minus)(3, 2) == 3 - 2);
+//   assert(absl::bind_front(Minus, 3)(2) == 3 - 2);
+//   assert(absl::bind_front(Minus, 3, 2)() == 3 - 2);
+//
+// Example: Binding a member function.
+//
+//   struct Math {
+//     int Double(int a) const { return 2 * a; }
+//   };
+//
+//   Math math;
+//
+//   assert(absl::bind_front(&Math::Double)(&math, 3) == 2 * 3);
+//   // Stores a pointer to math inside the functor.
+//   assert(absl::bind_front(&Math::Double, &math)(3) == 2 * 3);
+//   // Stores a copy of math inside the functor.
+//   assert(absl::bind_front(&Math::Double, math)(3) == 2 * 3);
+//   // Stores std::unique_ptr<Math> inside the functor.
+//   assert(absl::bind_front(&Math::Double,
+//                           std::unique_ptr<Math>(new Math))(3) == 2 * 3);
+//
+// Example: Using `absl::bind_front()`, instead of `std::bind()`, with
+//          `std::function`.
+//
+//   class FileReader {
+//    public:
+//     void ReadFileAsync(const std::string& filename, std::string* content,
+//                        const std::function<void()>& done) {
+//       // Calls Executor::Schedule(std::function<void()>).
+//       Executor::DefaultExecutor()->Schedule(
+//           absl::bind_front(&FileReader::BlockingRead, this,
+//                            filename, content, done));
+//     }
+//
+//    private:
+//     void BlockingRead(const std::string& filename, std::string* content,
+//                       const std::function<void()>& done) {
+//       CHECK_OK(file::GetContents(filename, content, {}));
+//       done();
+//     }
+//   };
+//
+// `absl::bind_front()` stores bound arguments explicitly using the type passed
+// rather than implicitly based on the type accepted by its functor.
+//
+// Example: Binding arguments explicitly.
+//
+//   void LogStringView(absl::string_view sv) {
+//     LOG(INFO) << sv;
+//   }
+//
+//   Executor* e = Executor::DefaultExecutor();
+//   std::string s = "hello";
+//   absl::string_view sv = s;
+//
+//   // absl::bind_front(LogStringView, arg) makes a copy of arg and stores it.
+//   e->Schedule(absl::bind_front(LogStringView, sv)); // ERROR: dangling
+//                                                     // string_view.
+//
+//   e->Schedule(absl::bind_front(LogStringView, s));  // OK: stores a copy of
+//                                                     // s.
+//
+// To store some of the arguments passed to `absl::bind_front()` by reference,
+//  use std::ref()` and `std::cref()`.
+//
+// Example: Storing some of the bound arguments by reference.
+//
+//   class Service {
+//    public:
+//     void Serve(const Request& req, std::function<void()>* done) {
+//       // The request protocol buffer won't be deleted until done is called.
+//       // It's safe to store a reference to it inside the functor.
+//       Executor::DefaultExecutor()->Schedule(
+//           absl::bind_front(&Service::BlockingServe, this, std::cref(req),
+//           done));
+//     }
+//
+//    private:
+//     void BlockingServe(const Request& req, std::function<void()>* done);
+//   };
+//
+// Example: Storing bound arguments by reference.
+//
+//   void Print(const string& a, const string& b) { LOG(INFO) << a << b; }
+//
+//   std::string hi = "Hello, ";
+//   std::vector<std::string> names = {"Chuk", "Gek"};
+//   // Doesn't copy hi.
+//   for_each(names.begin(), names.end(),
+//            absl::bind_front(Print, std::ref(hi)));
+//
+//   // DO NOT DO THIS: the functor may outlive "hi", resulting in
+//   // dangling references.
+//   foo->DoInFuture(absl::bind_front(Print, std::ref(hi), "Guest"));  // BAD!
+//   auto f = absl::bind_front(Print, std::ref(hi), "Guest"); // BAD!
+
+#ifndef ABSL_FUNCTIONAL_BIND_FRONT_H_
+#define ABSL_FUNCTIONAL_BIND_FRONT_H_
+
+#include "absl/functional/internal/front_binder.h"
+#include "absl/utility/utility.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+
+// Binds the first N arguments of an invocable object and stores them by value,
+// except types of std::reference_wrapper which are 'unwound' and stored by
+// reference.
+template <class F, class... BoundArgs>
+constexpr functional_internal::bind_front_t<F, BoundArgs...> bind_front(
+    F&& func, BoundArgs&&... args) {
+  return functional_internal::bind_front_t<F, BoundArgs...>(
+      absl::in_place, absl::forward<F>(func),
+      absl::forward<BoundArgs>(args)...);
+}
+
+ABSL_NAMESPACE_END
+}  // namespace absl
+
+#endif  // ABSL_FUNCTIONAL_BIND_FRONT_H_
diff --git a/absl/functional/bind_front_test.cc b/absl/functional/bind_front_test.cc
new file mode 100644
index 000000000000..4801a81caf0d
--- /dev/null
+++ b/absl/functional/bind_front_test.cc
@@ -0,0 +1,231 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "absl/functional/bind_front.h"
+
+#include <stddef.h>
+
+#include <functional>
+#include <memory>
+#include <string>
+
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include "absl/memory/memory.h"
+
+namespace {
+
+char CharAt(const char* s, size_t index) { return s[index]; }
+
+TEST(BindTest, Basics) {
+  EXPECT_EQ('C', absl::bind_front(CharAt)("ABC", 2));
+  EXPECT_EQ('C', absl::bind_front(CharAt, "ABC")(2));
+  EXPECT_EQ('C', absl::bind_front(CharAt, "ABC", 2)());
+}
+
+TEST(BindTest, Lambda) {
+  auto lambda = [](int x, int y, int z) { return x + y + z; };
+  EXPECT_EQ(6, absl::bind_front(lambda)(1, 2, 3));
+  EXPECT_EQ(6, absl::bind_front(lambda, 1)(2, 3));
+  EXPECT_EQ(6, absl::bind_front(lambda, 1, 2)(3));
+  EXPECT_EQ(6, absl::bind_front(lambda, 1, 2, 3)());
+}
+
+struct Functor {
+  std::string operator()() & { return "&"; }
+  std::string operator()() const& { return "const&"; }
+  std::string operator()() && { return "&&"; }
+  std::string operator()() const&& { return "const&&"; }
+};
+
+TEST(BindTest, PerfectForwardingOfBoundArgs) {
+  auto f = absl::bind_front(Functor());
+  const auto& cf = f;
+  EXPECT_EQ("&", f());
+  EXPECT_EQ("const&", cf());
+  EXPECT_EQ("&&", std::move(f)());
+  EXPECT_EQ("const&&", std::move(cf)());
+}
+
+struct ArgDescribe {
+  std::string operator()(int&) const { return "&"; }             // NOLINT
+  std::string operator()(const int&) const { return "const&"; }  // NOLINT
+  std::string operator()(int&&) const { return "&&"; }
+  std::string operator()(const int&&) const { return "const&&"; }
+};
+
+TEST(BindTest, PerfectForwardingOfFreeArgs) {
+  ArgDescribe f;
+  int i;
+  EXPECT_EQ("&", absl::bind_front(f)(static_cast<int&>(i)));
+  EXPECT_EQ("const&", absl::bind_front(f)(static_cast<const int&>(i)));
+  EXPECT_EQ("&&", absl::bind_front(f)(static_cast<int&&>(i)));
+  EXPECT_EQ("const&&", absl::bind_front(f)(static_cast<const int&&>(i)));
+}
+
+struct NonCopyableFunctor {
+  NonCopyableFunctor() = default;
+  NonCopyableFunctor(const NonCopyableFunctor&) = delete;
+  NonCopyableFunctor& operator=(const NonCopyableFunctor&) = delete;
+  const NonCopyableFunctor* operator()() const { return this; }
+};
+
+TEST(BindTest, RefToFunctor) {
+  // It won't copy/move the functor and use the original object.
+  NonCopyableFunctor ncf;
+  auto bound_ncf = absl::bind_front(std::ref(ncf));
+  auto bound_ncf_copy = bound_ncf;
+  EXPECT_EQ(&ncf, bound_ncf_copy());
+}
+
+struct Struct {
+  std::string value;
+};
+
+TEST(BindTest, StoreByCopy) {
+  Struct s = {"hello"};
+  auto f = absl::bind_front(&Struct::value, s);
+  auto g = f;
+  EXPECT_EQ("hello", f());
+  EXPECT_EQ("hello", g());
+  EXPECT_NE(&s.value, &f());
+  EXPECT_NE(&s.value, &g());
+  EXPECT_NE(&g(), &f());
+}
+
+struct NonCopyable {
+  explicit NonCopyable(const std::string& s) : value(s) {}
+  NonCopyable(const NonCopyable&) = delete;
+  NonCopyable& operator=(const NonCopyable&) = delete;
+
+  std::string value;
+};
+
+const std::string& GetNonCopyableValue(const NonCopyable& n) { return n.value; }
+
+TEST(BindTest, StoreByRef) {
+  NonCopyable s("hello");
+  auto f = absl::bind_front(&GetNonCopyableValue, std::ref(s));
+  EXPECT_EQ("hello", f());
+  EXPECT_EQ(&s.value, &f());
+  auto g = std::move(f);  // NOLINT
+  EXPECT_EQ("hello", g());
+  EXPECT_EQ(&s.value, &g());
+  s.value = "goodbye";
+  EXPECT_EQ("goodbye", g());
+}
+
+TEST(BindTest, StoreByCRef) {
+  NonCopyable s("hello");
+  auto f = absl::bind_front(&GetNonCopyableValue, std::cref(s));
+  EXPECT_EQ("hello", f());
+  EXPECT_EQ(&s.value, &f());
+  auto g = std::move(f);  // NOLINT
+  EXPECT_EQ("hello", g());
+  EXPECT_EQ(&s.value, &g());
+  s.value = "goodbye";
+  EXPECT_EQ("goodbye", g());
+}
+
+const std::string& GetNonCopyableValueByWrapper(
+    std::reference_wrapper<NonCopyable> n) {
+  return n.get().value;
+}
+
+TEST(BindTest, StoreByRefInvokeByWrapper) {
+  NonCopyable s("hello");
+  auto f = absl::bind_front(GetNonCopyableValueByWrapper, std::ref(s));
+  EXPECT_EQ("hello", f());
+  EXPECT_EQ(&s.value, &f());
+  auto g = std::move(f);
+  EXPECT_EQ("hello", g());
+  EXPECT_EQ(&s.value, &g());
+  s.value = "goodbye";
+  EXPECT_EQ("goodbye", g());
+}
+
+TEST(BindTest, StoreByPointer) {
+  NonCopyable s("hello");
+  auto f = absl::bind_front(&NonCopyable::value, &s);
+  EXPECT_EQ("hello", f());
+  EXPECT_EQ(&s.value, &f());
+  auto g = std::move(f);
+  EXPECT_EQ("hello", g());
+  EXPECT_EQ(&s.value, &g());
+}
+
+int Sink(std::unique_ptr<int> p) {
+  return *p;
+}
+
+std::unique_ptr<int> Factory(int n) { return absl::make_unique<int>(n); }
+
+TEST(BindTest, NonCopyableArg) {
+  EXPECT_EQ(42, absl::bind_front(Sink)(absl::make_unique<int>(42)));
+  EXPECT_EQ(42, absl::bind_front(Sink, absl::make_unique<int>(42))());
+}
+
+TEST(BindTest, NonCopyableResult) {
+  EXPECT_THAT(absl::bind_front(Factory)(42), ::testing::Pointee(42));
+  EXPECT_THAT(absl::bind_front(Factory, 42)(), ::testing::Pointee(42));
+}
+
+// is_copy_constructible<FalseCopyable<unique_ptr<T>> is true but an attempt to
+// instantiate the copy constructor leads to a compile error. This is similar
+// to how standard containers behave.
+template <class T>
+struct FalseCopyable {
+  FalseCopyable() {}
+  FalseCopyable(const FalseCopyable& other) : m(other.m) {}
+  FalseCopyable(FalseCopyable&& other) : m(std::move(other.m)) {}
+  T m;
+};
+
+int GetMember(FalseCopyable<std::unique_ptr<int>> x) { return *x.m; }
+
+TEST(BindTest, WrappedMoveOnly) {
+  FalseCopyable<std::unique_ptr<int>> x;
+  x.m = absl::make_unique<int>(42);
+  auto f = absl::bind_front(&GetMember, std::move(x));
+  EXPECT_EQ(42, std::move(f)());
+}
+
+int Plus(int a, int b) { return a + b; }
+
+TEST(BindTest, ConstExpr) {
+  constexpr auto f = absl::bind_front(CharAt);
+  EXPECT_EQ(f("ABC", 1), 'B');
+  static constexpr int five = 5;
+  constexpr auto plus5 = absl::bind_front(Plus, five);
+  EXPECT_EQ(plus5(1), 6);
+
+  // There seems to be a bug in MSVC dealing constexpr construction of
+  // char[]. Notice 'plus5' above; 'int' works just fine.
+#if !(defined(_MSC_VER) && _MSC_VER < 1910)
+  static constexpr char data[] = "DEF";
+  constexpr auto g = absl::bind_front(CharAt, data);
+  EXPECT_EQ(g(1), 'E');
+#endif
+}
+
+struct ManglingCall {
+  int operator()(int, double, std::string) const { return 0; }
+};
+
+TEST(BindTest, Mangling) {
+  // We just want to generate a particular instantiation to see its mangling.
+  absl::bind_front(ManglingCall{}, 1, 3.3)("A");
+}
+
+}  // namespace
diff --git a/absl/functional/internal/front_binder.h b/absl/functional/internal/front_binder.h
new file mode 100644
index 000000000000..a4d95da44a7d
--- /dev/null
+++ b/absl/functional/internal/front_binder.h
@@ -0,0 +1,95 @@
+// Copyright 2018 The Abseil Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//      https://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Implementation details for `absl::bind_front()`.
+
+#ifndef ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
+#define ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_
+
+#include <cstddef>
+#include <type_traits>
+#include <utility>
+
+#include "absl/base/internal/invoke.h"
+#include "absl/container/internal/compressed_tuple.h"
+#include "absl/meta/type_traits.h"
+#include "absl/utility/utility.h"
+
+namespace absl {
+ABSL_NAMESPACE_BEGIN
+namespace functional_internal {
+
+// Invoke the method, expanding the tuple of bound arguments.
+template <class R, class Tuple, size_t... Idx, class... Args>
+R Apply(Tuple&& bound, absl::index_sequence<Idx...>, Args&&... free) {
+  return base_internal::Invoke(
+      absl::forward<Tuple>(bound).template get<Idx>()...,
+      absl::forward<Args>(free)...);
+}
+
+template <class F, class... BoundArgs>
+class FrontBinder {
+  using BoundArgsT = absl::container_internal::CompressedTuple<F, BoundArgs...>;
+  using Idx = absl::make_index_sequence<sizeof...(BoundArgs) + 1>;
+
+  BoundArgsT bound_args_;
+
+ public:
+  template <class... Ts>
+  constexpr explicit FrontBinder(absl::in_place_t, Ts&&... ts)
+      : bound_args_(absl::forward<Ts>(ts)...) {}
+
+  template <class... FreeArgs,
+            class R = base_internal::InvokeT<F&, BoundArgs&..., FreeArgs&&...>>
+  R operator()(FreeArgs&&... free_args) & {
+    return functional_internal::Apply<R>(bound_args_, Idx(),
+                                         absl::forward<FreeArgs>(free_args)...);
+  }
+
+  template <class... FreeArgs,
+            class R = base_internal::InvokeT<const F&, const BoundArgs&...,
+                                             FreeArgs&&...>>
+  R operator()(FreeArgs&&... free_args) const& {
+    return functional_internal::Apply<R>(bound_args_, Idx(),
+                                         absl::forward<FreeArgs>(free_args)...);
+  }
+
+  template <class... FreeArgs, class R = base_internal::InvokeT<
+                                   F&&, BoundArgs&&..., FreeArgs&&...>>
+  R operator()(FreeArgs&&... free_args) && {
+    // This overload is called when *this is an rvalue. If some of the bound
+    // arguments are stored by value or rvalue reference, we move them.
+    return functional_internal::Apply<R>(absl::move(bound_args_), Idx(),
+                                         absl::forward<FreeArgs>(free_args)...);
+  }
+
+  template <class... FreeArgs,
+            class R = base_internal::InvokeT<const F&&, const BoundArgs&&...,
+                                             FreeArgs&&...>>
+  R operator()(FreeArgs&&... free_args) const&& {
+    // This overload is called when *this is an rvalue. If some of the bound
+    // arguments are stored by value or rvalue reference, we move them.
+    return functional_internal::Apply<R>(absl::move(bound_args_), Idx(),
+                                         absl::forward<FreeArgs>(free_args)...);
+  }
+};
+
+template <class F, class... BoundArgs>
+using bind_front_t = FrontBinder<decay_t<F>, absl::decay_t<BoundArgs>...>;
+
+}  // namespace functional_internal
+ABSL_NAMESPACE_END
+}  // namespace absl
+
+#endif  // ABSL_FUNCTIONAL_INTERNAL_FRONT_BINDER_H_