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

Unified Diff: src/core/SkValue.cpp

Issue 1605093003: SkValue: improve SkFromValue<T> implementation (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: hal 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/core/SkValue.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/core/SkValue.cpp
diff --git a/src/core/SkValue.cpp b/src/core/SkValue.cpp
index 8f58dd6ef9a0da4b12a0afa5a6464c54a4fb77ee..787cdf58df8f01c89b06bd47105a7aec01e80fa2 100644
--- a/src/core/SkValue.cpp
+++ b/src/core/SkValue.cpp
@@ -135,6 +135,11 @@ void SkValue::set(SkValue::Key k, SkValue v) {
fObject->set(k, std::move(v));
}
+const SkValue* SkValue::get(Key k) const {
+ SkASSERT(this->isObject());
+ return fObject->get(k);
+}
+
void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
SkASSERT(this->isObject());
fObject->foreach(fn);
@@ -182,6 +187,8 @@ SkValue SkValue::FromF32s(SkData* d) { return FromTs< float>(F32s, d); }
////////////////////////////////////////////////////////////////////////////////
+#define REQUIRE(cond) do { if (!(cond)) { SkASSERT(false); return false; } } while (false)
+
template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) {
auto val = SkValue::Object(SkValue::Matrix);
for (int i = 0; i < 9; ++i) {
@@ -192,26 +199,22 @@ template<> SkValue SkToValue<SkMatrix>(const SkMatrix& mat) {
return val;
}
+template<> bool SkFromValue<float>(const SkValue& val, float* f) {
+ REQUIRE(val.type() == SkValue::F32);
+ *f = val.f32();
+ return true;
+}
+
template<> bool SkFromValue<SkMatrix>(const SkValue& val, SkMatrix* m){
- SkASSERT(val.type() == SkValue::Matrix);
- if (val.type() != SkValue::Matrix) {
- return false;
- }
+ REQUIRE(val.type() == SkValue::Matrix);
+
*m = SkMatrix::I();
- bool good = true;
- auto fn = [&](SkValue::Key key, const SkValue& v) {
- if (key < 9) {
- if (v.type() != SkValue::F32) {
- SkASSERT(false);
- good = false;
- } else {
- (*m)[key] = v.f32();
- }
- } else {
- SkASSERT(false);
- good = false;
+ for (int i = 0; i < 9; i++) {
+ if (auto v = val.get(i)) {
+ REQUIRE(SkFromValue(*v, &(*m)[i]));
}
- };
- val.foreach(fn);
- return good;
+ }
+ return true;
}
+
+#undef REQUIRE
« no previous file with comments | « src/core/SkValue.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698