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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: src/core/SkValue.cpp
diff --git a/src/core/SkValue.cpp b/src/core/SkValue.cpp
new file mode 100644
index 0000000000000000000000000000000000000000..6934481f704416d49a5af27da6793b7ed12bfe82
--- /dev/null
+++ b/src/core/SkValue.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include <unordered_map>
+
+#include "SkData.h"
+#include "SkValue.h"
+
+class SkValue::Obj {
+public:
+ std::unordered_map<SkValue::Key, SkValue> fMap;
+};
+
+SkValue::SkValue() : fType(Null) {}
+
+SkValue::SkValue(const SkValue& o) {
+ memcpy(this, &o, sizeof(o));
+ if (Bytes == fType) {
+ fBytes->ref();
+ } else if (this->isObject()) {
+ fObject = new Obj(*fObject);
+ }
+}
+
+SkValue::SkValue(SkValue&& o) {
+ memcpy(this, &o, sizeof(o));
+ new (&o) SkValue();
+}
+
+SkValue& SkValue::operator=(const SkValue& o) {
+ if (this != &o) {
+ this->~SkValue();
+ new (this) SkValue(o);
+ }
+ return *this;
+}
+
+SkValue& SkValue::operator=(SkValue&& o) {
+ if (this != &o) {
+ this->~SkValue();
+ new (this) SkValue(std::move(o));
+ }
+ return *this;
+}
+
+SkValue::~SkValue() {
+ if (Bytes == fType) {
+ fBytes->unref();
+ } else if (this->isObject()) {
+ delete fObject;
+ }
+}
+
+#define FN(NAME, LNAME, TYPE) \
+ SkValue SkValue::From##NAME(TYPE x) { \
+ SkValue v; \
+ v.fType = NAME; \
+ v.f##NAME = x; \
+ return v; \
+ } \
+ TYPE SkValue::LNAME() const { \
+ SkASSERT(NAME == fType); \
+ return f##NAME; \
+ }
+FN(S32, s32, int32_t)
+FN(U32, u32, uint32_t)
+FN(F32, f32, float)
+#undef FN
+
+SkValue SkValue::FromBytes(SkData* data) {
+ if (!data) {
+ return SkValue();
+ }
+ SkValue v;
+ v.fType = Bytes;
+ v.fBytes = SkRef(data);
+ return v;
+}
+
+SkValue SkValue::Object(SkValue::Type t) {
+ SkValue v;
+ v.fType = t;
+ SkASSERT(v.isObject());
+ v.fObject = new Obj;
+ return v;
+}
+
+SkValue::Type SkValue::type() const { return fType; }
+
+SkData* SkValue::bytes() const {
+ return Bytes == fType ? fBytes : nullptr;
mtklein 2016/01/15 20:42:20 Assert type?
+}
+
+void SkValue::set(SkValue::Key k, SkValue v) {
+ if (this->isObject()) {
mtklein 2016/01/15 20:42:20 Assert type?
+ fObject->fMap[k] = std::move(v);
+ }
+}
+
+const SkValue* SkValue::get(SkValue::Key k) const {
mtklein 2016/01/15 20:42:20 Assert type?
+ if (!this->isObject()) {
+ return nullptr;
+ }
+ auto it = fObject->fMap.find(k);
+ return it != fObject->fMap.end() ? &it->second : nullptr;
+}
+
+void SkValue::foreach(std::function<void(Key, const SkValue&)> fn) const {
mtklein 2016/01/15 20:42:20 Assert type?
+ if (!this->isObject()) {
+ return;
+ }
+ for (const auto& pair : fObject->fMap) {
+ fn(pair.first, pair.second);
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698