Lift the invoke helper in Function.h
[folly.git] / folly / Function.h
index 55d7024ccae0cc26026e3b66c867c9487971c7f4..8cff8d8bb6b519e045c837f67462e10766ba46a6 100644 (file)
 #include <folly/CppAttributes.h>
 #include <folly/Portability.h>
 #include <folly/Traits.h>
+#include <folly/functional/Invoke.h>
 
 namespace folly {
 
@@ -405,19 +406,6 @@ bool execBig(Op o, Data* src, Data* dst) {
   return true;
 }
 
-// Invoke helper
-template <typename F, typename... Args>
-inline constexpr auto invoke(F&& f, Args&&... args)
-    -> decltype(std::forward<F>(f)(std::forward<Args>(args)...)) {
-  return std::forward<F>(f)(std::forward<Args>(args)...);
-}
-
-template <typename M, typename C, typename... Args>
-inline constexpr auto invoke(M(C::*d), Args&&... args)
-    -> decltype(std::mem_fn(d)(std::forward<Args>(args)...)) {
-  return std::mem_fn(d)(std::forward<Args>(args)...);
-}
-
 } // namespace function
 } // namespace detail
 
@@ -491,6 +479,12 @@ class Function final : private detail::function::FunctionTraits<FunctionType> {
   // not copyable
   Function(const Function&) = delete;
 
+#if __OBJC__
+  // Delete conversion from Objective-C blocks
+  template <class ReturnType, class... Args>
+  Function(ReturnType (^)(Args...)) = delete;
+#endif
+
   /**
    * Move constructor
    */
@@ -570,6 +564,12 @@ class Function final : private detail::function::FunctionTraits<FunctionType> {
 
   Function& operator=(const Function&) = delete;
 
+#if __OBJC__
+  // Delete conversion from Objective-C blocks
+  template <class ReturnType, class... Args>
+  Function& operator=(ReturnType (^)(Args...)) = delete;
+#endif
+
   /**
    * Move assignment operator
    *
@@ -790,7 +790,7 @@ class FunctionRef<ReturnType(Args...)> final {
   template <typename Fun>
   static ReturnType call(void* object, Args&&... args) {
     using Pointer = _t<std::add_pointer<Fun>>;
-    return static_cast<ReturnType>(detail::function::invoke(
+    return static_cast<ReturnType>(invoke(
         static_cast<Fun&&>(*static_cast<Pointer>(object)),
         static_cast<Args&&>(args)...));
   }