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

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: another unit test 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
« no previous file with comments | « src/core/SkValue.h ('k') | tests/ValueTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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[index];
39 }
40
41 private:
42 std::vector<SkValue> fVec;
43 };
44
45 SkValue::SkValue() : fType(Null) {}
46
47 SkValue::SkValue(Type type) : fType(type) {}
48
49 SkValue::SkValue(const SkValue& o) {
50 memcpy(this, &o, sizeof(o));
51 if (this->isData()) {
52 fBytes->ref();
53 } else if (this->isObject()) {
54 fObject = new Obj(*fObject);
55 } else if (Array == fType) {
56 fArray = new Arr(*fArray);
57 }
58 }
59
60 SkValue::SkValue(SkValue&& o) {
61 memcpy(this, &o, sizeof(o));
62 new (&o) SkValue();
63 }
64
65 SkValue& SkValue::operator=(const SkValue& o) {
66 if (this != &o) {
67 this->~SkValue();
68 new (this) SkValue(o);
69 }
70 return *this;
71 }
72
73 SkValue& SkValue::operator=(SkValue&& o) {
74 if (this != &o) {
75 this->~SkValue();
76 new (this) SkValue(std::move(o));
77 }
78 return *this;
79 }
80
81 SkValue::~SkValue() {
82 if (this->isData()) {
83 fBytes->unref();
84 } else if (this->isObject()) {
85 delete fObject;
86 } else if (Array == fType) {
87 delete fArray;
88 }
89 }
90
91 template <typename T>
92 SkValue SkValue::FromT(SkValue::Type type, T SkValue::*mp, T t) {
93 SkValue v(type);
94 v.*mp = t;
95 return v;
96 }
97
98 SkValue SkValue::FromS32(int32_t x) { return FromT(S32, &SkValue::fS32, x); }
99 SkValue SkValue::FromU32(uint32_t x) { return FromT(U32, &SkValue::fU32, x); }
100 SkValue SkValue::FromF32(float x) { return FromT(F32, &SkValue::fF32, x); }
101
102 int32_t SkValue::s32() const { SkASSERT(S32 == fType); return fS32; }
103 uint32_t SkValue::u32() const { SkASSERT(U32 == fType); return fU32; }
104 float SkValue::f32() const { SkASSERT(F32 == fType); return fF32; }
105
106 SkValue SkValue::FromBytes(SkData* data) {
107 if (!data) {
108 return SkValue();
109 }
110 SkValue v(Bytes);
111 v.fBytes = SkRef(data);
112 return v;
113 }
114
115 SkValue SkValue::Object(SkValue::Type t) {
116 SkValue v(t);
117 SkASSERT(v.isObject());
118 v.fObject = new Obj;
119 return v;
120 }
121
122 SkValue SkValue::ValueArray() {
123 SkValue v(Array);
124 v.fArray = new Arr;
125 return v;
126 }
127
128 SkData* SkValue::bytes() const {
129 SkASSERT(this->isData());
130 return fBytes;
131 }
132
133 void SkValue::set(SkValue::Key k, SkValue v) {
134 SkASSERT(this->isObject());
135 fObject->set(k, std::move(v));
136 }
137
138 void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
139 SkASSERT(this->isObject());
140 fObject->foreach(fn);
141 }
142
143 size_t SkValue::length() const {
144 SkASSERT(Array == fType);
145 return fArray->length();
146 }
147
148 const SkValue& SkValue::at(size_t index) const {
149 SkASSERT(Array == fType);
150 return fArray->at(index);
151 }
152
153 void SkValue::append(SkValue val) {
154 SkASSERT(Array == fType);
155 fArray->append(std::move(val));
156 }
157
158 template <typename T>
159 const T* SkValue::asTs(SkValue::Type t, int* count) const {
160 SkASSERT(t == fType && this->isData());
161 SkASSERT(count);
162 *count = fBytes->size() / sizeof(T);
163 return static_cast<const T*>(fBytes->data());
164 }
165
166 const uint16_t* SkValue::u16s(int* c) const { return this->asTs<uint16_t>(U16s, c); }
167 const uint32_t* SkValue::u32s(int* c) const { return this->asTs<uint32_t>(U32s, c); }
168 const float* SkValue::f32s(int* c) const { return this->asTs<float >(F32s, c); }
169
170 template <typename T>
171 SkValue SkValue::FromTs(SkValue::Type type, SkData* data) {
172 SkValue val(type);
173 val.fBytes = SkRef(data);
174 SkASSERT(val.isData());
175 SkASSERT(0 == (reinterpret_cast<uintptr_t>(data->bytes()) & (sizeof(T)-1)));
176 return val;
177 }
178
179 SkValue SkValue::FromU16s(SkData* d) { return FromTs<uint16_t>(U16s, d); }
180 SkValue SkValue::FromU32s(SkData* d) { return FromTs<uint32_t>(U32s, d); }
181 SkValue SkValue::FromF32s(SkData* d) { return FromTs< float>(F32s, d); }
182
183 ////////////////////////////////////////////////////////////////////////////////
184
185 template<> SkValue SkToValue<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(i, SkValue::FromF32(mat[i]));
190 }
191 }
192 return val;
193 }
194
195 template<> bool SkFromValue<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 auto fn = [&](SkValue::Key key, const SkValue& v) {
203 if (key < 9) {
204 if (v.type() != SkValue::F32) {
205 SkASSERT(false);
206 good = false;
207 } else {
208 (*m)[key] = v.f32();
209 }
210 } else {
211 SkASSERT(false);
212 good = false;
213 }
214 };
215 val.foreach(fn);
216 return good;
217 }
OLDNEW
« no previous file with comments | « src/core/SkValue.h ('k') | tests/ValueTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698