Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(616)

Side by Side Diff: include/private/SkFunction.h

Issue 1273813002: Add skstd::move and skstd::forward. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Don't need to change animator anymore. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « include/core/SkTemplates.h ('k') | include/private/SkTLogic.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
(...skipping 17 matching lines...) Expand all
28 SkFunction(const SkFunction& other) { *this = other; } 28 SkFunction(const SkFunction& other) { *this = other; }
29 SkFunction& operator=(const SkFunction& other) { 29 SkFunction& operator=(const SkFunction& other) {
30 if (this != &other) { 30 if (this != &other) {
31 fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr ); 31 fFunction.reset(other.fFunction ? other.fFunction->clone() : nullptr );
32 } 32 }
33 return *this; 33 return *this;
34 } 34 }
35 35
36 R operator()(Args... args) const { 36 R operator()(Args... args) const {
37 SkASSERT(fFunction.get()); 37 SkASSERT(fFunction.get());
38 return fFunction->call(Forward(args)...); 38 return fFunction->call(skstd::forward<Args>(args)...);
39 } 39 }
40 40
41 private: 41 private:
42 // ~= std::forward. This moves its argument if possible, falling back to a copy if not.
43 template <typename T> static T&& Forward(T& v) { return (T&&)v; }
44
45 struct Interface { 42 struct Interface {
46 virtual ~Interface() {} 43 virtual ~Interface() {}
47 virtual R call(Args...) const = 0; 44 virtual R call(Args...) const = 0;
48 virtual Interface* clone() const = 0; 45 virtual Interface* clone() const = 0;
49 }; 46 };
50 47
51 template <typename Fn> 48 template <typename Fn>
52 class LambdaImpl final : public Interface { 49 class LambdaImpl final : public Interface {
53 public: 50 public:
54 LambdaImpl(const Fn& fn) : fFn(fn) {} 51 LambdaImpl(const Fn& fn) : fFn(fn) {}
55 52
56 R call(Args... args) const override { return fFn(Forward(args)...); } 53 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar gs)...); }
57 Interface* clone() const override { return SkNEW_ARGS(LambdaImpl<Fn>, (f Fn)); } 54 Interface* clone() const override { return SkNEW_ARGS(LambdaImpl<Fn>, (f Fn)); }
58 private: 55 private:
59 Fn fFn; 56 Fn fFn;
60 }; 57 };
61 58
62 class FnPtrImpl final : public Interface { 59 class FnPtrImpl final : public Interface {
63 public: 60 public:
64 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {} 61 FnPtrImpl(R (*fn)(Args...)) : fFn(fn) {}
65 62
66 R call(Args... args) const override { return fFn(Forward(args)...); } 63 R call(Args... args) const override { return fFn(skstd::forward<Args>(ar gs)...); }
67 Interface* clone() const override { return SkNEW_ARGS(FnPtrImpl, (fFn)); } 64 Interface* clone() const override { return SkNEW_ARGS(FnPtrImpl, (fFn)); }
68 private: 65 private:
69 R (*fFn)(Args...); 66 R (*fFn)(Args...);
70 }; 67 };
71 68
72 SkAutoTDelete<Interface> fFunction; 69 SkAutoTDelete<Interface> fFunction;
73 }; 70 };
74 71
75 #endif//SkFunction_DEFINED 72 #endif//SkFunction_DEFINED
OLDNEW
« no previous file with comments | « include/core/SkTemplates.h ('k') | include/private/SkTLogic.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698