Fix SimpleBarrier
[folly.git] / folly / ApplyTuple.h
index 3fe30c3d1b996c04c9de8f0d210e89e3828b25f7..b22987766702fa4f023ae780abf30e0c55c7d4e9 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2014 Facebook, Inc.
+ * Copyright 2016 Facebook, Inc.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  *    ASSERT(x == 24);
  */
 
-#ifndef FOLLY_APPLYTUPLE_H_
-#define FOLLY_APPLYTUPLE_H_
+#pragma once
 
-#include <tuple>
 #include <functional>
-#include <type_traits>
+#include <tuple>
+#include <utility>
 
 namespace folly {
 
 //////////////////////////////////////////////////////////////////////
 
 namespace detail {
+namespace apply_tuple {
 
-// This is to allow using this with pointers to member functions,
-// where the first argument in the tuple will be the this pointer.
-template<class F> F& makeCallable(F& f) { return f; }
-template<class R, class C, class ...A>
-auto makeCallable(R (C::*d)(A...)) -> decltype(std::mem_fn(d)) {
-  return std::mem_fn(d);
+template <std::size_t...>
+struct IndexSequence {};
+
+template <std::size_t N, std::size_t... Is>
+struct MakeIndexSequence : MakeIndexSequence<N - 1, N - 1, Is...> {};
+
+template <std::size_t... Is>
+struct MakeIndexSequence<0, Is...> : IndexSequence<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<class Tuple>
-struct DerefSize
-  : std::tuple_size<typename std::remove_reference<Tuple>::type>
-{};
-
-// CallTuple recursively unpacks tuple arguments so we can forward
-// them into the function.
-template<class Ret>
-struct CallTuple {
-  template<class F, class Tuple, class ...Unpacked>
-  static typename std::enable_if<
-    (sizeof...(Unpacked) < DerefSize<Tuple>::value),
-    Ret
-  >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
-    typedef typename std::tuple_element<
-      sizeof...(Unpacked),
-      typename std::remove_reference<Tuple>::type
-    >::type ElementType;
-    return CallTuple<Ret>::call(f, std::forward<Tuple>(t),
-      std::forward<Unpacked>(unp)...,
-      std::forward<ElementType>(std::get<sizeof...(Unpacked)>(t))
-    );
-  }
-
-  template<class F, class Tuple, class ...Unpacked>
-  static typename std::enable_if<
-    (sizeof...(Unpacked) == DerefSize<Tuple>::value),
-    Ret
-  >::type call(const F& f, Tuple&& t, Unpacked&&... unp) {
-    return makeCallable(f)(std::forward<Unpacked>(unp)...);
-  }
+template <typename... Tuples>
+struct TupleSizeSum {
+  static constexpr auto value = sum(std::tuple_size<Tuples>::value...);
 };
 
-// The point of this meta function is to extract the contents of the
-// tuple as a parameter pack so we can pass it into std::result_of<>.
-template<class F, class Args> struct ReturnValue {};
-template<class F, class ...Args>
-struct ReturnValue<F,std::tuple<Args...>> {
-  typedef typename std::result_of<F (Args...)>::type type;
-};
+template <typename... Tuples>
+using MakeIndexSequenceFromTuple = MakeIndexSequence<
+    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.
+template <class F>
+inline constexpr F&& makeCallable(F&& f) {
+  return std::forward<F>(f);
+}
+template <class M, class C>
+inline constexpr auto makeCallable(M(C::*d)) -> decltype(std::mem_fn(d)) {
+  return std::mem_fn(d);
 }
 
-//////////////////////////////////////////////////////////////////////
+template <class F, class Tuple, std::size_t... Indexes>
+inline constexpr auto call(F&& f, Tuple&& t, IndexSequence<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 Callable, class Tuple>
-typename detail::ReturnValue<
-  typename std::decay<Callable>::type,
-  typename std::remove_reference<Tuple>::type
->::type
-applyTuple(const Callable& c, Tuple&& t) {
-  typedef typename detail::ReturnValue<
-    typename std::decay<Callable>::type,
-    typename std::remove_reference<Tuple>::type
-  >::type RetT;
-  return detail::CallTuple<RetT>::call(c, std::forward<Tuple>(t));
+template <class Tuple, std::size_t... Indexes>
+inline constexpr auto forwardTuple(Tuple&& t, IndexSequence<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
+
 //////////////////////////////////////////////////////////////////////
 
+/**
+ * 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::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::tuple_cat(detail::apply_tuple::forwardTuple(
+          std::forward<Tuples>(t),
+          detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples>{})...),
+      detail::apply_tuple::MakeIndexSequenceFromTuple<Tuples...>{});
 }
 
-#endif
+//////////////////////////////////////////////////////////////////////
+}