Function.h 6.9 KB

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