| 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 "SkUtility.h" |
| 14 #include "SkUniquePtr.h" |
| 14 #include "SkTypes.h" | 15 #include "SkTypes.h" |
| 15 | 16 |
| 16 template <typename> class SkFunction; | 17 template <typename> class SkFunction; |
| 17 | 18 |
| 18 template <typename R, typename... Args> | 19 template <typename R, typename... Args> |
| 19 class SkFunction<R(Args...)> { | 20 class SkFunction<R(Args...)> { |
| 20 public: | 21 public: |
| 21 SkFunction() {} | 22 SkFunction() {} |
| 22 | 23 |
| 23 template <typename Fn> | 24 template <typename Fn> |
| 24 SkFunction(const Fn& fn) | 25 SkFunction(const Fn& fn) |
| 25 : fFunction(new LambdaImpl<Fn>(fn)) {} | 26 : fFunction(new LambdaImpl<Fn>(fn)) {} |
| 26 | 27 |
| 27 SkFunction(R (*fn)(Args...)) : fFunction(new FnPtrImpl(fn)) {} | 28 SkFunction(R (*fn)(Args...)) : fFunction(new FnPtrImpl(fn)) {} |
| 28 | 29 |
| 29 SkFunction(const SkFunction& other) { *this = other; } | 30 SkFunction(const SkFunction& other) { *this = other; } |
| 30 SkFunction& operator=(const SkFunction& other) { | 31 SkFunction& operator=(const SkFunction& other) { |
| 31 if (this != &other) { | 32 if (this != &other) { |
| 32 fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr
); | 33 fFunction.reset(other.fFunction.get() ? other.fFunction->clone() : n
ullptr); |
| 33 } | 34 } |
| 34 return *this; | 35 return *this; |
| 35 } | 36 } |
| 36 | 37 |
| 37 R operator()(Args... args) const { | 38 R operator()(Args... args) const { |
| 38 SkASSERT(fFunction.get()); | 39 SkASSERT(fFunction.get()); |
| 39 return fFunction->call(skstd::forward<Args>(args)...); | 40 return fFunction->call(skstd::forward<Args>(args)...); |
| 40 } | 41 } |
| 41 | 42 |
| 42 private: | 43 private: |
| (...skipping 19 matching lines...) Expand all Loading... |
| 62 public: | 63 public: |
| 63 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {} | 64 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {} |
| 64 | 65 |
| 65 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } | 66 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar
gs)...); } |
| 66 Interface* clone() const override { return new FnPtrImpl(fFn); } | 67 Interface* clone() const override { return new FnPtrImpl(fFn); } |
| 67 | 68 |
| 68 private: | 69 private: |
| 69 R (*fFn)(Args...); | 70 R (*fFn)(Args...); |
| 70 }; | 71 }; |
| 71 | 72 |
| 72 SkAutoTDelete<Interface> fFunction; | 73 skstd::unique_ptr<Interface> fFunction; |
| 73 }; | 74 }; |
| 74 | 75 |
| 75 #endif//SkFunction_DEFINED | 76 #endif//SkFunction_DEFINED |
| OLD | NEW |