folly: fix clang's -Wundefined-var-template
[folly.git] / folly / ApplyTuple.h
index 5f535b9e054bfa16735db4aad5b898853b0b8ed1..3a8e14eea2255c913371d1aa3ce632cba8300d44 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2012 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 <utility>
 
 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...> {};
+
+template <std::size_t... Is>
+struct MakeIndexSequence<0, Is...> : IndexSequence<Is...> {};
+
+template <class Tuple>
+using MakeIndexSequenceFromTuple =
+    MakeIndexSequence<std::tuple_size<typename std::decay<Tuple>::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> F& makeCallable(F& f) { return f; }
-template<class R, class C, class ...A>
-auto makeCallable(R (C::*d)(A...)) -> decltype(std::mem_fn(d)) {
+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 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)...);
-  }
-};
-
-// 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 <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))...);
 }
 
+} // namespace apply_tuple
+} // namespace detail
+
 //////////////////////////////////////////////////////////////////////
 
-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 F, class Tuple>
+inline constexpr auto applyTuple(F&& f, Tuple&& t)
+    -> decltype(detail::apply_tuple::call(
+        detail::apply_tuple::makeCallable(std::forward<F>(f)),
+        std::forward<Tuple>(t),
+        detail::apply_tuple::MakeIndexSequenceFromTuple<Tuple>{})) {
+  return detail::apply_tuple::call(
+      detail::apply_tuple::makeCallable(std::forward<F>(f)),
+      std::forward<Tuple>(t),
+      detail::apply_tuple::MakeIndexSequenceFromTuple<Tuple>{});
 }
 
 //////////////////////////////////////////////////////////////////////
-
 }
-
-#endif