OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #ifndef SkFunction_DEFINED | 8 #ifndef SkFunction_DEFINED |
9 #define SkFunction_DEFINED | 9 #define SkFunction_DEFINED |
10 | 10 |
11 // TODO: document, more pervasive move support in constructors, small-Fn optimiz
ation | 11 // TODO: document, more pervasive move support in constructors, small-Fn optimiz
ation |
12 | 12 |
13 #include "SkTemplates.h" | 13 #include "SkTemplates.h" |
14 #include "SkTypes.h" | 14 #include "SkTypes.h" |
15 | 15 |
16 template <typename> class SkFunction; | 16 template <typename> class SkFunction; |
17 | 17 |
18 template <typename R, typename... Args> | 18 template <typename R, typename... Args> |
19 class SkFunction<R(Args...)> { | 19 class SkFunction<R(Args...)> { |
20 public: | 20 public: |
21 SkFunction() {} | 21 SkFunction() {} |
22 | 22 |
23 template <typename Fn> | 23 template <typename Fn> |
24 SkFunction(const Fn& fn) : fFunction(SkNEW_ARGS(LambdaImpl<Fn>, (fn))) {} | 24 SkFunction(const Fn& fn) |
| 25 : fFunction(new LambdaImpl<Fn>(fn)) {} |
25 | 26 |
26 SkFunction(R (*fn)(Args...)) : fFunction(SkNEW_ARGS(FnPtrImpl, (fn))) {} | 27 SkFunction(R (*fn)(Args...)) : fFunction(new FnPtrImpl(fn)) {} |
27 | 28 |
28 SkFunction(const SkFunction& other) { *this = other; } | 29 SkFunction(const SkFunction& other) { *this = other; } |
29 SkFunction& operator=(const SkFunction& other) { | 30 SkFunction& operator=(const SkFunction& other) { |
30 if (this != &other) { | 31 if (this != &other) { |
31 fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr
); | 32 fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr
); |
32 } | 33 } |
33 return *this; | 34 return *this; |
34 } | 35 } |
35 | 36 |
36 R operator()(Args... args) const { | 37 R operator()(Args... args) const { |
37 SkASSERT(fFunction.get()); | 38 SkASSERT(fFunction.get()); |
38 return fFunction->call(skstd::forward<Args>(args)...); | 39 return fFunction->call(skstd::forward<Args>(args)...); |
39 } | 40 } |
40 | 41 |
41 private: | 42 private: |
42 struct Interface { | 43 struct Interface { |
43 virtual ~Interface() {} | 44 virtual ~Interface() {} |
44 virtual R call(Args...) const = 0; | 45 virtual R call(Args...) const = 0; |
45 virtual Interface* clone() const = 0; | 46 virtual Interface* clone() const = 0; |
46 }; | 47 }; |
47 | 48 |
48 template <typename Fn> | 49 template <typename Fn> |
49 class LambdaImpl final : public Interface { | 50 class LambdaImpl final : public Interface { |
50 public: | 51 public: |
51 LambdaImpl(const Fn& fn) : fFn(fn) {} | 52 LambdaImpl(const Fn& fn) : fFn(fn) {} |
52 | 53 |
53 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } | 54 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } |
54 Interface* clone() const override { return SkNEW_ARGS(LambdaImpl<Fn>, (f
Fn)); } | 55 Interface* clone() const override { return new LambdaImpl<Fn>(fFn); } |
| 56 |
55 private: | 57 private: |
56 Fn fFn; | 58 Fn fFn; |
57 }; | 59 }; |
58 | 60 |
59 class FnPtrImpl final : public Interface { | 61 class FnPtrImpl final : public Interface { |
60 public: | 62 public: |
61 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {} | 63 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {} |
62 | 64 |
63 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } | 65 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } |
64 Interface* clone() const override { return SkNEW_ARGS(FnPtrImpl, (fFn));
} | 66 Interface* clone() const override { return new FnPtrImpl(fFn); } |
| 67 |
65 private: | 68 private: |
66 R (*fFn)(Args...); | 69 R (*fFn)(Args...); |
67 }; | 70 }; |
68 | 71 |
69 SkAutoTDelete<Interface> fFunction; | 72 SkAutoTDelete<Interface> fFunction; |
70 }; | 73 }; |
71 | 74 |
72 #endif//SkFunction_DEFINED | 75 #endif//SkFunction_DEFINED |
OLD | NEW |