summaryrefslogtreecommitdiffstats
path: root/mfbt/Function.h
blob: d6de9c833514f4a8f9d387e723a4b1b7c80dbab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/* A type-erased callable wrapper. */

#ifndef mozilla_Function_h
#define mozilla_Function_h

#include "mozilla/Attributes.h"  // for MOZ_IMPLICIT
#include "mozilla/Move.h"
#include "mozilla/RefCounted.h"
#include "mozilla/RefPtr.h"

// |function<Signature>| is a wrapper that can hold any type of callable
// object that can be invoked in a way that's compatible with |Signature|.
// The standard "type erasure" technique is used to avoid the type of the
// wrapper depending on the concrete type of the wrapped callable.
//
// Supported callable types include non-member functions, static member
// functions, and function objects (that is to say, objects with an overloaded
// call operator; this includes C++11 lambdas). Member functions aren't
// directly supported; they first need to be wrapped into a function object
// using |std::mem_fn()| or an equivalent.
//
// |Signature| is a type of the form |ReturnType(Arguments...)|. Syntactically,
// this is a function type; it's not used in any way other than serving as a
// vehicle to encode the return and argument types into a single type.
//
// |function| is default-constructible. A default-constructed instance is
// considered "empty". Invoking an empty instance is undefined behaviour.
// An empty instance can be populated with a callable by assigning to it.
//
// This class is intended to provide functionality similar to the C++11
// standard library class |std::function|.

namespace mozilla {

namespace detail {

template<typename ReturnType, typename... Arguments>
class FunctionImplBase : public mozilla::RefCounted<FunctionImplBase<ReturnType, Arguments...>>
{
public:
  MOZ_DECLARE_REFCOUNTED_TYPENAME(FunctionImplBase)

  virtual ~FunctionImplBase() {}
  virtual ReturnType call(Arguments... aArguments) = 0;
};

// Normal Callable Object.
template <typename Callable, typename ReturnType, typename... Arguments>
class FunctionImpl : public FunctionImplBase<ReturnType, Arguments...>
{
  public:
    explicit FunctionImpl(const Callable& aCallable)
      : mCallable(aCallable) {}

    ReturnType call(Arguments... aArguments) override
    {
      return mCallable(Forward<Arguments>(aArguments)...);
    }
  private:
    Callable mCallable;
};

// Base class for passing pointer to member function.
template <typename Callable, typename ReturnType, typename... Arguments>
class MemberFunctionImplBase : public FunctionImplBase<ReturnType, Arguments...>
{
public:
  explicit MemberFunctionImplBase(const Callable& aCallable)
    : mCallable(aCallable) {}

  ReturnType call(Arguments... aArguments) override
  {
    return callInternal(Forward<Arguments>(aArguments)...);
  }
private:
  template<typename ThisType, typename... Args>
  ReturnType callInternal(ThisType* aThis, Args&&... aArguments)
  {
    return (aThis->*mCallable)(Forward<Args>(aArguments)...);
  }

  template<typename ThisType, typename... Args>
  ReturnType callInternal(ThisType&& aThis, Args&&... aArguments)
  {
    return (aThis.*mCallable)(Forward<Args>(aArguments)...);
  }
  Callable mCallable;
};

// For non-const member function specialization of FunctionImpl.
template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
class FunctionImpl<ReturnType(ThisType::*)(Args...),
                   ReturnType, Arguments...>
  : public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
                                  ReturnType, Arguments...>
{
public:
  explicit FunctionImpl(ReturnType(ThisType::*aMemberFunc)(Args...))
    : MemberFunctionImplBase<ReturnType(ThisType::*)(Args...),
                             ReturnType, Arguments...>(aMemberFunc)
  {}
};

// For const member function specialization of FunctionImpl.
template <typename ThisType, typename... Args, typename ReturnType, typename... Arguments>
class FunctionImpl<ReturnType(ThisType::*)(Args...) const,
                   ReturnType, Arguments...>
  : public MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
                                  ReturnType, Arguments...>
{
public:
  explicit FunctionImpl(ReturnType(ThisType::*aConstMemberFunc)(Args...) const)
    : MemberFunctionImplBase<ReturnType(ThisType::*)(Args...) const,
                             ReturnType, Arguments...>(aConstMemberFunc)
  {}
};

} // namespace detail

// The primary template is never defined. As |Signature| is required to be
// of the form |ReturnType(Arguments...)|, we only define a partial
// specialization that matches this form. This allows us to use |ReturnType|
// and |Arguments| in the definition of the specialization without having to
// introspect |Signature|.
template<typename Signature>
class function;

template<typename ReturnType, typename... Arguments>
class function<ReturnType(Arguments...)>
{
public:
  function() {}

  // This constructor is implicit to match the interface of |std::function|.
  template <typename Callable>
  MOZ_IMPLICIT function(const Callable& aCallable)
    : mImpl(new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable))
  {}
  MOZ_IMPLICIT function(const function& aFunction)
    : mImpl(aFunction.mImpl)
  {}
  MOZ_IMPLICIT function(decltype(nullptr))
  {}

  // Move constructor and move assingment operator.
  // These should be generated automatically, but MSVC doesn't do that yet.
  function(function&& aOther) : mImpl(Move(aOther.mImpl)) {}
  function& operator=(function&& aOther) {
    mImpl = Move(aOther.mImpl);
    return *this;
  }

  template <typename Callable>
  function& operator=(const Callable& aCallable)
  {
    mImpl = new detail::FunctionImpl<Callable, ReturnType, Arguments...>(aCallable);
    return *this;
  }
  function& operator=(const function& aFunction)
  {
    mImpl = aFunction.mImpl;
    return *this;
  }
  function& operator=(decltype(nullptr))
  {
    mImpl = nullptr;
    return *this;
  }

  template<typename... Args>
  ReturnType operator()(Args&&... aArguments) const
  {
    MOZ_ASSERT(mImpl);
    return mImpl->call(Forward<Args>(aArguments)...);
  }

  explicit operator bool() const
  {
    return bool(mImpl);
  }

private:
  // TODO: Consider implementing a small object optimization.
  RefPtr<detail::FunctionImplBase<ReturnType, Arguments...>> mImpl;
};

template<typename Signature>
bool
operator==(const function<Signature>& aX, decltype(nullptr))
{
  return !aX;
}

template<typename Signature>
bool
operator==(decltype(nullptr), const function<Signature>& aX)
{
  return !aX;
}

template<typename Signature>
bool
operator!=(const function<Signature>& aX, decltype(nullptr))
{
  return bool(aX);
}

template<typename Signature>
bool
operator!=(decltype(nullptr), const function<Signature>& aX)
{
  return bool(aX);
}

} // namespace mozilla

#endif /* mozilla_Function_h */