Fixing opt-asan/ubsan fail for folly contbuild
[folly.git] / folly / ApplyTuple.h
index 3a8e14eea2255c913371d1aa3ce632cba8300d44..cce6fec07d6e3e23bf0e6a574f8ac352535953db 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 #pragma once
 
 #include <functional>
+#include <tuple>
 #include <utility>
 
+#include <folly/Utility.h>
+
 namespace folly {
 
 //////////////////////////////////////////////////////////////////////
@@ -37,18 +40,22 @@ namespace folly {
 namespace detail {
 namespace apply_tuple {
 
-template <std::size_t...>
-struct IndexSequence {};
-
-template <std::size_t N, std::size_t... Is>
-struct MakeIndexSequence : MakeIndexSequence<N - 1, N - 1, Is...> {};
+inline constexpr std::size_t sum() {
+  return 0;
+}
+template <typename... Args>
+inline constexpr std::size_t sum(std::size_t v1, Args... vs) {
+  return v1 + sum(vs...);
+}
 
-template <std::size_t... Is>
-struct MakeIndexSequence<0, Is...> : IndexSequence<Is...> {};
+template <typename... Tuples>
+struct TupleSizeSum {
+  static constexpr auto value = sum(std::tuple_size<Tuples>::value...);
+};
 
-template <class Tuple>
-using MakeIndexSequenceFromTuple =
-    MakeIndexSequence<std::tuple_size<typename std::decay<Tuple>::type>::value>;
+template <typename... Tuples>
+using MakeIndexSequenceFromTuple = folly::make_index_sequence<
+    TupleSizeSum<typename std::decay<Tuples>::type...>::value>;
 
 // This is to allow using this with pointers to member functions,
 // where the first argument in the tuple will be the this pointer.
@@ -62,27 +69,95 @@ inline constexpr auto makeCallable(M(C::*d)) -> decltype(std::mem_fn(d)) {
 }
 
 template <class F, class Tuple, std::size_t... Indexes>
-inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<Indexes...>)
+inline constexpr auto call(F&& f, Tuple&& t, folly::index_sequence<Indexes...>)
     -> decltype(
         std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...)) {
   return std::forward<F>(f)(std::get<Indexes>(std::forward<Tuple>(t))...);
 }
 
+template <class Tuple, std::size_t... Indexes>
+inline constexpr auto forwardTuple(Tuple&& t, folly::index_sequence<Indexes...>)
+    -> decltype(
+        std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...)) {
+  return std::forward_as_tuple(std::get<Indexes>(std::forward<Tuple>(t))...);
+}
+
 } // namespace apply_tuple
 } // namespace detail
 
 //////////////////////////////////////////////////////////////////////
 
-template <class F, class Tuple>
-inline constexpr auto applyTuple(F&& f, Tuple&& t)
+/**
+ * Invoke a callable object with a set of arguments passed as a tuple, or a
+ *     series of tuples
+ *
+ * Example: the following lines are equivalent
+ *     func(1, 2, 3, "foo");
+ *     applyTuple(func, std::make_tuple(1, 2, 3, "foo"));
+ *     applyTuple(func, std::make_tuple(1, 2), std::make_tuple(3, "foo"));
+ */
+
+template <class F, class... Tuples>
+inline constexpr auto applyTuple(F&& f, Tuples&&... t)
     -> decltype(detail::apply_tuple::call(
         detail::apply_tuple::makeCallable(std::forward<F>(f)),
-        std::forward<Tuple>(t),
-        detail::apply_tuple::MakeIndexSequenceFromTuple<Tuple>{})) {
+        std::tuple_cat(detail::apply_tuple::forwardTuple(
+            std::forward<Tuples>(t),
+            detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
+        detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{})) {
   return detail::apply_tuple::call(
       detail::apply_tuple::makeCallable(std::forward<F>(f)),
-      std::forward<Tuple>(t),
-      detail::apply_tuple::MakeIndexSequenceFromTuple<Tuple>{});
+      std::tuple_cat(detail::apply_tuple::forwardTuple(
+          std::forward<Tuples>(t),
+          detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
+      detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{});
+}
+
+namespace detail {
+namespace apply_tuple {
+
+template <class F>
+class Uncurry {
+ public:
+  explicit Uncurry(F&& func) : func_(std::move(func)) {}
+  explicit Uncurry(const F& func) : func_(func) {}
+
+  template <class Tuple>
+  auto operator()(Tuple&& tuple) const
+      -> decltype(applyTuple(std::declval<F>(), std::forward<Tuple>(tuple))) {
+    return applyTuple(func_, std::forward<Tuple>(tuple));
+  }
+
+ private:
+  F func_;
+};
+} // namespace apply_tuple
+} // namespace detail
+
+/**
+ * Wraps a function taking N arguments into a function which accepts a tuple of
+ * N arguments. Note: This function will also accept an std::pair if N == 2.
+ *
+ * For example, given the below code:
+ *
+ *    std::vector<std::tuple<int, int, int>> rows = ...;
+ *    auto test = [](std::tuple<int, int, int>& row) {
+ *      return std::get<0>(row) * std::get<1>(row) * std::get<2>(row) == 24;
+ *    };
+ *    auto found = std::find_if(rows.begin(), rows.end(), test);
+ *
+ *
+ * 'test' could be rewritten as:
+ *
+ *    auto test =
+ *        folly::uncurry([](int a, int b, int c) { return a * b * c == 24; });
+ *
+ */
+template <class F>
+auto uncurry(F&& f)
+    -> detail::apply_tuple::Uncurry<typename std::decay<F>::type> {
+  return detail::apply_tuple::Uncurry<typename std::decay<F>::type>(
+      std::forward<F>(f));
 }
 
 //////////////////////////////////////////////////////////////////////