cannam@147
|
1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
|
cannam@147
|
2 // Licensed under the MIT License:
|
cannam@147
|
3 //
|
cannam@147
|
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
|
cannam@147
|
5 // of this software and associated documentation files (the "Software"), to deal
|
cannam@147
|
6 // in the Software without restriction, including without limitation the rights
|
cannam@147
|
7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
cannam@147
|
8 // copies of the Software, and to permit persons to whom the Software is
|
cannam@147
|
9 // furnished to do so, subject to the following conditions:
|
cannam@147
|
10 //
|
cannam@147
|
11 // The above copyright notice and this permission notice shall be included in
|
cannam@147
|
12 // all copies or substantial portions of the Software.
|
cannam@147
|
13 //
|
cannam@147
|
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
cannam@147
|
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
cannam@147
|
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
cannam@147
|
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
cannam@147
|
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
cannam@147
|
19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
cannam@147
|
20 // THE SOFTWARE.
|
cannam@147
|
21
|
cannam@147
|
22 #ifndef KJ_FUNCTION_H_
|
cannam@147
|
23 #define KJ_FUNCTION_H_
|
cannam@147
|
24
|
cannam@147
|
25 #if defined(__GNUC__) && !KJ_HEADER_WARNINGS
|
cannam@147
|
26 #pragma GCC system_header
|
cannam@147
|
27 #endif
|
cannam@147
|
28
|
cannam@147
|
29 #include "memory.h"
|
cannam@147
|
30
|
cannam@147
|
31 namespace kj {
|
cannam@147
|
32
|
cannam@147
|
33 template <typename Signature>
|
cannam@147
|
34 class Function;
|
cannam@147
|
35 // Function wrapper using virtual-based polymorphism. Use this when template polymorphism is
|
cannam@147
|
36 // not possible. You can, for example, accept a Function as a parameter:
|
cannam@147
|
37 //
|
cannam@147
|
38 // void setFilter(Function<bool(const Widget&)> filter);
|
cannam@147
|
39 //
|
cannam@147
|
40 // The caller of `setFilter()` may then pass any callable object as the parameter. The callable
|
cannam@147
|
41 // object does not have to have the exact signature specified, just one that is "compatible" --
|
cannam@147
|
42 // i.e. the return type is covariant and the parameters are contravariant.
|
cannam@147
|
43 //
|
cannam@147
|
44 // Unlike `std::function`, `kj::Function`s are movable but not copyable, just like `kj::Own`. This
|
cannam@147
|
45 // is to avoid unexpected heap allocation or slow atomic reference counting.
|
cannam@147
|
46 //
|
cannam@147
|
47 // When a `Function` is constructed from an lvalue, it captures only a reference to the value.
|
cannam@147
|
48 // When constructed from an rvalue, it invokes the value's move constructor. So, for example:
|
cannam@147
|
49 //
|
cannam@147
|
50 // struct AddN {
|
cannam@147
|
51 // int n;
|
cannam@147
|
52 // int operator(int i) { return i + n; }
|
cannam@147
|
53 // }
|
cannam@147
|
54 //
|
cannam@147
|
55 // Function<int(int, int)> f1 = AddN{2};
|
cannam@147
|
56 // // f1 owns an instance of AddN. It may safely be moved out
|
cannam@147
|
57 // // of the local scope.
|
cannam@147
|
58 //
|
cannam@147
|
59 // AddN adder(2);
|
cannam@147
|
60 // Function<int(int, int)> f2 = adder;
|
cannam@147
|
61 // // f2 contains a reference to `adder`. Thus, it becomes invalid
|
cannam@147
|
62 // // when `adder` goes out-of-scope.
|
cannam@147
|
63 //
|
cannam@147
|
64 // AddN adder2(2);
|
cannam@147
|
65 // Function<int(int, int)> f3 = kj::mv(adder2);
|
cannam@147
|
66 // // f3 owns an insatnce of AddN moved from `adder2`. f3 may safely
|
cannam@147
|
67 // // be moved out of the local scope.
|
cannam@147
|
68 //
|
cannam@147
|
69 // Additionally, a Function may be bound to a class method using KJ_BIND_METHOD(object, methodName).
|
cannam@147
|
70 // For example:
|
cannam@147
|
71 //
|
cannam@147
|
72 // class Printer {
|
cannam@147
|
73 // public:
|
cannam@147
|
74 // void print(int i);
|
cannam@147
|
75 // void print(kj::StringPtr s);
|
cannam@147
|
76 // };
|
cannam@147
|
77 //
|
cannam@147
|
78 // Printer p;
|
cannam@147
|
79 //
|
cannam@147
|
80 // Function<void(uint)> intPrinter = KJ_BIND_METHOD(p, print);
|
cannam@147
|
81 // // Will call Printer::print(int).
|
cannam@147
|
82 //
|
cannam@147
|
83 // Function<void(const char*)> strPrinter = KJ_BIND_METHOD(p, print);
|
cannam@147
|
84 // // Will call Printer::print(kj::StringPtr).
|
cannam@147
|
85 //
|
cannam@147
|
86 // Notice how KJ_BIND_METHOD is able to figure out which overload to use depending on the kind of
|
cannam@147
|
87 // Function it is binding to.
|
cannam@147
|
88
|
cannam@147
|
89 template <typename Signature>
|
cannam@147
|
90 class ConstFunction;
|
cannam@147
|
91 // Like Function, but wraps a "const" (i.e. thread-safe) call.
|
cannam@147
|
92
|
cannam@147
|
93 template <typename Return, typename... Params>
|
cannam@147
|
94 class Function<Return(Params...)> {
|
cannam@147
|
95 public:
|
cannam@147
|
96 template <typename F>
|
cannam@147
|
97 inline Function(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
|
cannam@147
|
98 Function() = default;
|
cannam@147
|
99
|
cannam@147
|
100 // Make sure people don't accidentally end up wrapping a reference when they meant to return
|
cannam@147
|
101 // a function.
|
cannam@147
|
102 KJ_DISALLOW_COPY(Function);
|
cannam@147
|
103 Function(Function&) = delete;
|
cannam@147
|
104 Function& operator=(Function&) = delete;
|
cannam@147
|
105 template <typename T> Function(const Function<T>&) = delete;
|
cannam@147
|
106 template <typename T> Function& operator=(const Function<T>&) = delete;
|
cannam@147
|
107 template <typename T> Function(const ConstFunction<T>&) = delete;
|
cannam@147
|
108 template <typename T> Function& operator=(const ConstFunction<T>&) = delete;
|
cannam@147
|
109 Function(Function&&) = default;
|
cannam@147
|
110 Function& operator=(Function&&) = default;
|
cannam@147
|
111
|
cannam@147
|
112 inline Return operator()(Params... params) {
|
cannam@147
|
113 return (*impl)(kj::fwd<Params>(params)...);
|
cannam@147
|
114 }
|
cannam@147
|
115
|
cannam@147
|
116 Function reference() {
|
cannam@147
|
117 // Forms a new Function of the same type that delegates to this Function by reference.
|
cannam@147
|
118 // Therefore, this Function must outlive the returned Function, but otherwise they behave
|
cannam@147
|
119 // exactly the same.
|
cannam@147
|
120
|
cannam@147
|
121 return *impl;
|
cannam@147
|
122 }
|
cannam@147
|
123
|
cannam@147
|
124 private:
|
cannam@147
|
125 class Iface {
|
cannam@147
|
126 public:
|
cannam@147
|
127 virtual Return operator()(Params... params) = 0;
|
cannam@147
|
128 };
|
cannam@147
|
129
|
cannam@147
|
130 template <typename F>
|
cannam@147
|
131 class Impl final: public Iface {
|
cannam@147
|
132 public:
|
cannam@147
|
133 explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
|
cannam@147
|
134
|
cannam@147
|
135 Return operator()(Params... params) override {
|
cannam@147
|
136 return f(kj::fwd<Params>(params)...);
|
cannam@147
|
137 }
|
cannam@147
|
138
|
cannam@147
|
139 private:
|
cannam@147
|
140 F f;
|
cannam@147
|
141 };
|
cannam@147
|
142
|
cannam@147
|
143 Own<Iface> impl;
|
cannam@147
|
144 };
|
cannam@147
|
145
|
cannam@147
|
146 template <typename Return, typename... Params>
|
cannam@147
|
147 class ConstFunction<Return(Params...)> {
|
cannam@147
|
148 public:
|
cannam@147
|
149 template <typename F>
|
cannam@147
|
150 inline ConstFunction(F&& f): impl(heap<Impl<F>>(kj::fwd<F>(f))) {}
|
cannam@147
|
151 ConstFunction() = default;
|
cannam@147
|
152
|
cannam@147
|
153 // Make sure people don't accidentally end up wrapping a reference when they meant to return
|
cannam@147
|
154 // a function.
|
cannam@147
|
155 KJ_DISALLOW_COPY(ConstFunction);
|
cannam@147
|
156 ConstFunction(ConstFunction&) = delete;
|
cannam@147
|
157 ConstFunction& operator=(ConstFunction&) = delete;
|
cannam@147
|
158 template <typename T> ConstFunction(const ConstFunction<T>&) = delete;
|
cannam@147
|
159 template <typename T> ConstFunction& operator=(const ConstFunction<T>&) = delete;
|
cannam@147
|
160 template <typename T> ConstFunction(const Function<T>&) = delete;
|
cannam@147
|
161 template <typename T> ConstFunction& operator=(const Function<T>&) = delete;
|
cannam@147
|
162 ConstFunction(ConstFunction&&) = default;
|
cannam@147
|
163 ConstFunction& operator=(ConstFunction&&) = default;
|
cannam@147
|
164
|
cannam@147
|
165 inline Return operator()(Params... params) const {
|
cannam@147
|
166 return (*impl)(kj::fwd<Params>(params)...);
|
cannam@147
|
167 }
|
cannam@147
|
168
|
cannam@147
|
169 ConstFunction reference() const {
|
cannam@147
|
170 // Forms a new ConstFunction of the same type that delegates to this ConstFunction by reference.
|
cannam@147
|
171 // Therefore, this ConstFunction must outlive the returned ConstFunction, but otherwise they
|
cannam@147
|
172 // behave exactly the same.
|
cannam@147
|
173
|
cannam@147
|
174 return *impl;
|
cannam@147
|
175 }
|
cannam@147
|
176
|
cannam@147
|
177 private:
|
cannam@147
|
178 class Iface {
|
cannam@147
|
179 public:
|
cannam@147
|
180 virtual Return operator()(Params... params) const = 0;
|
cannam@147
|
181 };
|
cannam@147
|
182
|
cannam@147
|
183 template <typename F>
|
cannam@147
|
184 class Impl final: public Iface {
|
cannam@147
|
185 public:
|
cannam@147
|
186 explicit Impl(F&& f): f(kj::fwd<F>(f)) {}
|
cannam@147
|
187
|
cannam@147
|
188 Return operator()(Params... params) const override {
|
cannam@147
|
189 return f(kj::fwd<Params>(params)...);
|
cannam@147
|
190 }
|
cannam@147
|
191
|
cannam@147
|
192 private:
|
cannam@147
|
193 F f;
|
cannam@147
|
194 };
|
cannam@147
|
195
|
cannam@147
|
196 Own<Iface> impl;
|
cannam@147
|
197 };
|
cannam@147
|
198
|
cannam@147
|
199 #if 1
|
cannam@147
|
200
|
cannam@147
|
201 namespace _ { // private
|
cannam@147
|
202
|
cannam@147
|
203 template <typename T, typename Signature, Signature method>
|
cannam@147
|
204 class BoundMethod;
|
cannam@147
|
205
|
cannam@147
|
206 template <typename T, typename Return, typename... Params, Return (Decay<T>::*method)(Params...)>
|
cannam@147
|
207 class BoundMethod<T, Return (Decay<T>::*)(Params...), method> {
|
cannam@147
|
208 public:
|
cannam@147
|
209 BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
|
cannam@147
|
210
|
cannam@147
|
211 Return operator()(Params&&... params) {
|
cannam@147
|
212 return (t.*method)(kj::fwd<Params>(params)...);
|
cannam@147
|
213 }
|
cannam@147
|
214
|
cannam@147
|
215 private:
|
cannam@147
|
216 T t;
|
cannam@147
|
217 };
|
cannam@147
|
218
|
cannam@147
|
219 template <typename T, typename Return, typename... Params,
|
cannam@147
|
220 Return (Decay<T>::*method)(Params...) const>
|
cannam@147
|
221 class BoundMethod<T, Return (Decay<T>::*)(Params...) const, method> {
|
cannam@147
|
222 public:
|
cannam@147
|
223 BoundMethod(T&& t): t(kj::fwd<T>(t)) {}
|
cannam@147
|
224
|
cannam@147
|
225 Return operator()(Params&&... params) const {
|
cannam@147
|
226 return (t.*method)(kj::fwd<Params>(params)...);
|
cannam@147
|
227 }
|
cannam@147
|
228
|
cannam@147
|
229 private:
|
cannam@147
|
230 T t;
|
cannam@147
|
231 };
|
cannam@147
|
232
|
cannam@147
|
233 } // namespace _ (private)
|
cannam@147
|
234
|
cannam@147
|
235 #define KJ_BIND_METHOD(obj, method) \
|
cannam@147
|
236 ::kj::_::BoundMethod<KJ_DECLTYPE_REF(obj), \
|
cannam@147
|
237 decltype(&::kj::Decay<decltype(obj)>::method), \
|
cannam@147
|
238 &::kj::Decay<decltype(obj)>::method>(obj)
|
cannam@147
|
239 // Macro that produces a functor object which forwards to the method `obj.name`. If `obj` is an
|
cannam@147
|
240 // lvalue, the functor will hold a reference to it. If `obj` is an rvalue, the functor will
|
cannam@147
|
241 // contain a copy (by move) of it.
|
cannam@147
|
242 //
|
cannam@147
|
243 // The current implementation requires that the method is not overloaded.
|
cannam@147
|
244 //
|
cannam@147
|
245 // TODO(someday): C++14's generic lambdas may be able to simplify this code considerably, and
|
cannam@147
|
246 // probably make it work with overloaded methods.
|
cannam@147
|
247
|
cannam@147
|
248 #else
|
cannam@147
|
249 // Here's a better implementation of the above that doesn't work with GCC (but does with Clang)
|
cannam@147
|
250 // because it uses a local class with a template method. Sigh. This implementation supports
|
cannam@147
|
251 // overloaded methods.
|
cannam@147
|
252
|
cannam@147
|
253 #define KJ_BIND_METHOD(obj, method) \
|
cannam@147
|
254 ({ \
|
cannam@147
|
255 typedef KJ_DECLTYPE_REF(obj) T; \
|
cannam@147
|
256 class F { \
|
cannam@147
|
257 public: \
|
cannam@147
|
258 inline F(T&& t): t(::kj::fwd<T>(t)) {} \
|
cannam@147
|
259 template <typename... Params> \
|
cannam@147
|
260 auto operator()(Params&&... params) \
|
cannam@147
|
261 -> decltype(::kj::instance<T>().method(::kj::fwd<Params>(params)...)) { \
|
cannam@147
|
262 return t.method(::kj::fwd<Params>(params)...); \
|
cannam@147
|
263 } \
|
cannam@147
|
264 private: \
|
cannam@147
|
265 T t; \
|
cannam@147
|
266 }; \
|
cannam@147
|
267 (F(obj)); \
|
cannam@147
|
268 })
|
cannam@147
|
269 // Macro that produces a functor object which forwards to the method `obj.name`. If `obj` is an
|
cannam@147
|
270 // lvalue, the functor will hold a reference to it. If `obj` is an rvalue, the functor will
|
cannam@147
|
271 // contain a copy (by move) of it.
|
cannam@147
|
272
|
cannam@147
|
273 #endif
|
cannam@147
|
274
|
cannam@147
|
275 } // namespace kj
|
cannam@147
|
276
|
cannam@147
|
277 #endif // KJ_FUNCTION_H_
|