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

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

Issue 1604253002: SkValue: implementation, unit test (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: 2016-01-20 (Wednesday) 07:22:07 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 #include <vector>
10
11 #include "SkData.h"
12 #include "SkMatrix.h"
13 #include "SkValue.h"
14
15 class SkValue::Obj {
16 public:
17 void set(SkValue::Key k, SkValue&& v) { fMap[k] = std::move(v); }
18 const SkValue* get(SkValue::Key k) const {
19 auto it = fMap.find(k);
20 return it != fMap.end() ? &it->second : nullptr;
21 }
22 void foreach(std::function<void(Key, const SkValue&)> fn) const {
23 for (const auto& pair : fMap) {
24 fn(pair.first, pair.second);
25 }
26 }
27
28 private:
29 std::unordered_map<SkValue::Key, SkValue> fMap;
30 };
31
32 class SkValue::Arr {
33 public:
34 size_t length() const { return fVec.size(); }
35 void append(SkValue&& val) { fVec.emplace_back(std::move(val)); }
36 const SkValue& at(size_t index) const {
37 SkASSERT(index < fVec.size());
38 return fVec.at(index);
mtklein 2016/01/20 14:20:58 vector::at does bounds checks. I think you want f
hal.canary 2016/01/20 15:14:35 done
39 }
40
41 private:
42 std::vector<SkValue> fVec;
43 };
44
45 SkValue::SkValue() : fType(Null) {}
46
47 SkValue::SkValue(const SkValue& o) {
48 memcpy(this, &o, sizeof(o));
49 if (this->isData()) {
50 fBytes->ref();
51 } else if (this->isObject()) {
52 fObject = new Obj(*fObject);
53 } else if (Array == fType) {
54 fArray = new Arr(*fArray);
55 }
56 }
57
58 SkValue::SkValue(SkValue&& o) {
59 memcpy(this, &o, sizeof(o));
60 new (&o) SkValue();
61 }
62
63 SkValue& SkValue::operator=(const SkValue& o) {
64 if (this != &o) {
65 this->~SkValue();
66 new (this) SkValue(o);
67 }
68 return *this;
69 }
70
71 SkValue& SkValue::operator=(SkValue&& o) {
72 if (this != &o) {
73 this->~SkValue();
74 new (this) SkValue(std::move(o));
75 }
76 return *this;
77 }
78
79 SkValue::~SkValue() {
80 if (this->isData()) {
81 fBytes->unref();
82 } else if (this->isObject()) {
83 delete fObject;
84 } else if (Array == fType) {
85 delete fArray;
86 }
87 }
88
89 #define FN(NAME, LNAME, TYPE) \
90 SkValue SkValue::From##NAME(TYPE x) { \
91 SkValue v; \
92 v.fType = NAME; \
93 v.f##NAME = x; \
94 return v; \
95 } \
96 TYPE SkValue::LNAME() const { \
97 SkASSERT(NAME == fType); \
98 return f##NAME; \
99 }
100 FN(S32, s32, int32_t)
101 FN(U32, u32, uint32_t)
102 FN(F32, f32, float)
103 #undef FN
104
105 SkValue SkValue::FromBytes(SkData* data) {
106 if (!data) {
107 return SkValue();
108 }
109 SkValue v;
110 v.fType = Bytes;
111 v.fBytes = SkRef(data);
112 return v;
113 }
114
115 SkValue SkValue::Object(SkValue::Type t) {
116 SkValue v;
117 v.fType = t;
118 SkASSERT(v.isObject());
119 v.fObject = new Obj;
120 return v;
121 }
122
123 SkValue SkValue::ValueArray() {
124 SkValue v;
125 v.fType = Array;
126 v.fArray = new Arr;
127 return v;
128 }
129
130 SkData* SkValue::bytes() const {
131 SkASSERT(this->isData());
132 return fBytes;
133 }
134
135 void SkValue::set(SkValue::Key k, SkValue v) {
136 SkASSERT(this->isObject());
137 fObject->set(k, std::move(v));
138 }
139
140 void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
141 SkASSERT(this->isObject());
142 fObject->foreach(fn);
143 }
144
145 size_t SkValue::length() const {
146 SkASSERT(Array == fType);
147 return fArray->length();
148 }
149
150 const SkValue& SkValue::at(size_t index) const {
151 SkASSERT(Array == fType);
152 return fArray->at(index);
153 }
154
155 void SkValue::append(SkValue val) {
156 SkASSERT(Array == fType);
157 fArray->append(std::move(val));
158 }
159
160 #define PTR_IS_ALIGNED_TO(PTR, TYPE) \
161 (0 == (reinterpret_cast<uintptr_t>(PTR) & (sizeof(TYPE) - 1)))
162
163 #define FN(NAME, MNAME, TYPE) \
mtklein 2016/01/20 14:20:58 Seems like we can get most of the concision by usi
hal.canary 2016/01/20 15:14:34 DONE. awesome.
164 SkValue SkValue::From##NAME(SkData* data) { \
165 SkValue val; \
166 val.fType = NAME; \
167 SkASSERT(val.isData()); \
168 val.fBytes = SkRef(data); \
169 SkASSERT(PTR_IS_ALIGNED_TO(data->bytes(), TYPE)); \
170 return val; \
171 } \
172 const TYPE* SkValue::MNAME(int* count) const { \
173 SkASSERT(NAME == fType); \
174 SkASSERT(count); \
175 *count = fBytes->size() / sizeof(TYPE); \
176 return static_cast<const TYPE*>(fBytes->data()); \
177 }
178 FN(U16s, u16s, uint16_t)
179 FN(U32s, u32s, uint32_t)
180 FN(F32s, f32s, float)
181 #undef FN
mtklein 2016/01/20 14:20:58 #undef PTR_IS_ALIGNED_TO also, or inline it into F
hal.canary 2016/01/20 15:14:35 done
182
183 ////////////////////////////////////////////////////////////////////////////////
184
185 template<> SkValue SkValue::Encode<SkMatrix>(const SkMatrix& mat) {
186 auto val = SkValue::Object(SkValue::Matrix);
187 for (int i = 0; i < 9; ++i) {
188 if (mat[i] != SkMatrix::I()[i]) {
189 val.set((SkValue::Key)i, SkValue::FromF32(mat[i]));
mtklein 2016/01/20 14:20:59 val.set(i, ...) ? Isn't this cast implicit?
hal.canary 2016/01/20 15:14:34 done.
190 }
191 }
192 return val;
193 }
194
195 template<> bool SkValue::Decode<SkMatrix>(const SkValue& val, SkMatrix* m){
196 SkASSERT(val.type() == SkValue::Matrix);
197 if (val.type() != SkValue::Matrix) {
198 return false;
199 }
200 *m = SkMatrix::I();
201 bool good = true;
202 val.foreach([&](SkValue::Key key, const SkValue& v) {
203 if (key < 9) {
mtklein 2016/01/20 14:20:59 the indentation from 203-211 is pretty weird
hal.canary 2016/01/20 15:14:34 let me write that simpler.
204 if (v.type() != SkValue::F32) {
205 SkASSERT(false);
206 good = false;
207 } else {
208 (*m)[(int)key] = v.f32();
mtklein 2016/01/20 14:20:58 (*m)[key] ? Isn't this cast implicit?
hal.canary 2016/01/20 15:14:35 done.
209 }
210 }
mtklein 2016/01/20 14:20:59 I guess, } else { good = false; } here too?
hal.canary 2016/01/20 15:14:35 right.
211 });
212 return good;
213 }
OLDNEW
« src/core/SkValue.h ('K') | « src/core/SkValue.h ('k') | tests/ValueTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698