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

Side by Side Diff: src/core/SkValue.cpp

Issue 1585813004: SkValue: SkXfermode (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-01-15 (Friday) 14:43:14 EST Created 4 years, 11 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
OLDNEW
(Empty)
1 /*
2 * Copyright 2016 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include <unordered_map>
9
10 #include "SkData.h"
11 #include "SkValue.h"
12
13 class SkValue::Obj {
14 public:
15 std::unordered_map<SkValue::Key, SkValue> fMap;
16 };
17
18 SkValue::SkValue() : fType(Null) {}
19
20 SkValue::SkValue(const SkValue& o) {
21 memcpy(this, &o, sizeof(o));
22 if (Bytes == fType) {
23 fBytes->ref();
24 } else if (this->isObject()) {
25 fObject = new Obj(*fObject);
26 }
27 }
28
29 SkValue::SkValue(SkValue&& o) {
30 memcpy(this, &o, sizeof(o));
31 new (&o) SkValue();
32 }
33
34 SkValue& SkValue::operator=(const SkValue& o) {
35 if (this != &o) {
36 this->~SkValue();
37 new (this) SkValue(o);
38 }
39 return *this;
40 }
41
42 SkValue& SkValue::operator=(SkValue&& o) {
43 if (this != &o) {
44 this->~SkValue();
45 new (this) SkValue(std::move(o));
46 }
47 return *this;
48 }
49
50 SkValue::~SkValue() {
51 if (Bytes == fType) {
52 fBytes->unref();
53 } else if (this->isObject()) {
54 delete fObject;
55 }
56 }
57
58 #define FN(NAME, LNAME, TYPE) \
59 SkValue SkValue::From##NAME(TYPE x) { \
60 SkValue v; \
61 v.fType = NAME; \
62 v.f##NAME = x; \
63 return v; \
64 } \
65 TYPE SkValue::LNAME() const { \
66 SkASSERT(NAME == fType); \
67 return f##NAME; \
68 }
69 FN(S32, s32, int32_t)
70 FN(U32, u32, uint32_t)
71 FN(F32, f32, float)
72 #undef FN
73
74 SkValue SkValue::FromBytes(SkData* data) {
75 if (!data) {
76 return SkValue();
77 }
78 SkValue v;
79 v.fType = Bytes;
80 v.fBytes = SkRef(data);
81 return v;
82 }
83
84 SkValue SkValue::Object(SkValue::Type t) {
85 SkValue v;
86 v.fType = t;
87 SkASSERT(v.isObject());
88 v.fObject = new Obj;
89 return v;
90 }
91
92 SkValue::Type SkValue::type() const { return fType; }
93
94 SkData* SkValue::bytes() const {
95 return Bytes == fType ? fBytes : nullptr;
mtklein 2016/01/15 20:42:20 Assert type?
96 }
97
98 void SkValue::set(SkValue::Key k, SkValue v) {
99 if (this->isObject()) {
mtklein 2016/01/15 20:42:20 Assert type?
100 fObject->fMap[k] = std::move(v);
101 }
102 }
103
104 const SkValue* SkValue::get(SkValue::Key k) const {
mtklein 2016/01/15 20:42:20 Assert type?
105 if (!this->isObject()) {
106 return nullptr;
107 }
108 auto it = fObject->fMap.find(k);
109 return it != fObject->fMap.end() ? &it->second : nullptr;
110 }
111
112 void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
mtklein 2016/01/15 20:42:20 Assert type?
113 if (!this->isObject()) {
114 return;
115 }
116 for (const auto& pair : fObject->fMap) {
117 fn(pair.first, pair.second);
118 }
119 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698