Function.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. /* A type-erased callable wrapper. */
  6. #ifndef mozilla_Function_h
  7. #define mozilla_Function_h
  8. #include "mozilla/Attributes.h" // for MOZ_IMPLICIT
  9. #include "mozilla/Move.h"
  10. #include "mozilla/RefCounted.h"
  11. #include "mozilla/RefPtr.h"
  12. // |function<Signature>| is a wrapper that can hold any type of callable
  13. // object that can be invoked in a way that's compatible with |Signature|.
  14. // The standard "type erasure" technique is used to avoid the type of the
  15. // wrapper depending on the concrete type of the wrapped callable.
  16. //
  17. // Supported callable types include non-member functions, static member
  18. // functions, and function objects (that is to say, objects with an overloaded
  19. // call operator; this includes C++11 lambdas). Member functions aren't
  20. // directly supported; they first need to be wrapped into a function object
  21. // using |std::mem_fn()| or an equivalent.
  22. //
  23. // |Signature| is a type of the form |ReturnType(Arguments...)|. Syntactically,
  24. // this is a function type; it's not used in any way other than serving as a
  25. // vehicle to encode the return and argument types into a single type.
  26. //
  27. // |function| is default-constructible. A default-constructed instance is
  28. // considered "empty". Invoking an empty instance is undefined behaviour.
  29. // An empty instance can be populated with a callable by assigning to it.
  30. //
  31. // This class is intended to provide functionality similar to the C++11
  32. // standard library class |std::function|.
  33. namespace mozilla {
  34. namespace detail {
  35. template<typename ReturnType, typename... Arguments>
  36. class FunctionImplBase : public mozilla::RefCounted<FunctionImplBase<ReturnType, Arguments...>>
  37. {
  38. public:
  39. MOZ_DECLARE_REFCOUNTED_TYPENAME(FunctionImplBase)
  40. virtual ~FunctionImplBase() {}
  41. virtual ReturnType call(Arguments... aArguments) = 0;
  42. };
  43. // Normal Callable Object.
  44. template <typename Callable, typename ReturnType, typename... Arguments>
  45. class FunctionImpl : public FunctionImplBase<ReturnType, Arguments...>
  46. {
  47. public:
  48. explicit FunctionImpl(const Callable& aCallable)
  49. : mCallable(aCallable) {}
  50. ReturnType call(Arguments... aArguments) override
  51. {
  52. return mCallable(Forward<Arguments>(aArguments)...);
  53. }
  54. private:
  55. Callable mCallable;
  56. };
  57. // Base class for passing pointer to member function.
  58. template <typename Callable, typename ReturnType, typename... Arguments>
  59. class MemberFunctionImplBase : public FunctionImplBase<ReturnType, Arguments...>
  60. {
  61. public:
  62. explicit MemberFunctionImplBase(const Callable& aCallable)
  63. : mCallable(aCallable) {}
  64. ReturnType call(Arguments... aArguments) override
  65. {
  66. return callInternal(Forward<Arguments>(aArguments)...);
  67. }
  68. private:
  69. template<typename ThisType, typename... Args>
  70. ReturnType callInternal(ThisType* aThis, Args&&... aArguments)
  71. {
  72. return (aThis->*mCallable)(Forward<Args>(aArguments)...);
  73. }
  74. template<typename ThisType, typename... Args>
  75. ReturnType callInternal(ThisType&& aThis, Args&&... aArguments)
  76. {
  77. return (aThis.*mCallable)(Forward<Args>(aArguments)...);
  78. }
  79. Callable mCallable;
  80. };
  81. // For non-const member function specialization of FunctionImpl.
  82. template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
  83. class FunctionImpl<ReturnType(ThisType::*)(Args...),
  84. ReturnType, Arguments...>
  85. : public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
  86. ReturnType, Arguments...>
  87. {
  88. public:
  89. explicit FunctionImpl(ReturnType(ThisType::*aMemberFunc)(Args...))
  90. : MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
  91. ReturnType, Arguments...>(aMemberFunc)
  92. {}
  93. };
  94. // For const member function specialization of FunctionImpl.
  95. template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
  96. class FunctionImpl<ReturnType(ThisType::*)(Args...) const,
  97. ReturnType, Arguments...>
  98. : public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
  99. ReturnType, Arguments...>
  100. {
  101. public:
  102. explicit FunctionImpl(ReturnType(ThisType::*aConstMemberFunc)(Args...) const)
  103. : MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
  104. ReturnType, Arguments...>(aConstMemberFunc)
  105. {}
  106. };
  107. } // namespace detail
  108. // The primary template is never defined. As |Signature| is required to be
  109. // of the form |ReturnType(Arguments...)|, we only define a partial
  110. // specialization that matches this form. This allows us to use |ReturnType|
  111. // and |Arguments| in the definition of the specialization without having to
  112. // introspect |Signature|.
  113. template<typename Signature>
  114. class function;
  115. template<typename ReturnType, typename... Arguments>
  116. class function<ReturnType(Arguments...)>
  117. {
  118. public:
  119. function() {}
  120. // This constructor is implicit to match the interface of |std::function|.
  121. template <typename Callable>
  122. MOZ_IMPLICIT function(const Callable& aCallable)
  123. : mImpl(new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable))
  124. {}
  125. MOZ_IMPLICIT function(const function& aFunction)
  126. : mImpl(aFunction.mImpl)
  127. {}
  128. MOZ_IMPLICIT function(decltype(nullptr))
  129. {}
  130. // Move constructor and move assingment operator.
  131. // These should be generated automatically, but MSVC doesn't do that yet.
  132. function(function&& aOther) : mImpl(Move(aOther.mImpl)) {}
  133. function& operator=(function&& aOther) {
  134. mImpl = Move(aOther.mImpl);
  135. return *this;
  136. }
  137. template <typename Callable>
  138. function& operator=(const Callable& aCallable)
  139. {
  140. mImpl = new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable);
  141. return *this;
  142. }
  143. function& operator=(const function& aFunction)
  144. {
  145. mImpl = aFunction.mImpl;
  146. return *this;
  147. }
  148. function& operator=(decltype(nullptr))
  149. {
  150. mImpl = nullptr;
  151. return *this;
  152. }
  153. template<typename... Args>
  154. ReturnType operator()(Args&&... aArguments) const
  155. {
  156. MOZ_ASSERT(mImpl);
  157. return mImpl->call(Forward<Args>(aArguments)...);
  158. }
  159. explicit operator bool() const
  160. {
  161. return bool(mImpl);
  162. }
  163. private:
  164. // TODO: Consider implementing a small object optimization.
  165. RefPtr<detail::FunctionImplBase<ReturnType, Arguments...>> mImpl;
  166. };
  167. template<typename Signature>
  168. bool
  169. operator==(const function<Signature>& aX, decltype(nullptr))
  170. {
  171. return !aX;
  172. }
  173. template<typename Signature>
  174. bool
  175. operator==(decltype(nullptr), const function<Signature>& aX)
  176. {
  177. return !aX;
  178. }
  179. template<typename Signature>
  180. bool
  181. operator!=(const function<Signature>& aX, decltype(nullptr))
  182. {
  183. return bool(aX);
  184. }
  185. template<typename Signature>
  186. bool
  187. operator!=(decltype(nullptr), const function<Signature>& aX)
  188. {
  189. return bool(aX);
  190. }
  191. } // namespace mozilla
  192. #endif /* mozilla_Function_h */