aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--gn/tests.gni1
-rw-r--r--include/core/SkTLazy.h21
-rw-r--r--tests/TLazyTest.cpp77
3 files changed, 97 insertions, 2 deletions
diff --git a/gn/tests.gni b/gn/tests.gni
index 8fc7210aa7..0bbb6da936 100644
--- a/gn/tests.gni
+++ b/gn/tests.gni
@@ -265,6 +265,7 @@ tests_sources = [
"$_tests/TextureProxyTest.cpp",
"$_tests/TextureStripAtlasManagerTest.cpp",
"$_tests/Time.cpp",
+ "$_tests/TLazyTest.cpp",
"$_tests/TopoSortTest.cpp",
"$_tests/ToSRGBColorFilter.cpp",
"$_tests/TraceMemoryDumpTest.cpp",
diff --git a/include/core/SkTLazy.h b/include/core/SkTLazy.h
index 39ebdbef4a..a166f05a74 100644
--- a/include/core/SkTLazy.h
+++ b/include/core/SkTLazy.h
@@ -148,13 +148,28 @@ private:
template <typename T>
class SkTCopyOnFirstWrite {
public:
- SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
+ explicit SkTCopyOnFirstWrite(const T& initial) : fObj(&initial) {}
- SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {}
+ explicit SkTCopyOnFirstWrite(const T* initial) : fObj(initial) {}
// Constructor for delayed initialization.
SkTCopyOnFirstWrite() : fObj(nullptr) {}
+ SkTCopyOnFirstWrite(const SkTCopyOnFirstWrite& that) { *this = that; }
+ SkTCopyOnFirstWrite( SkTCopyOnFirstWrite&& that) { *this = std::move(that); }
+
+ SkTCopyOnFirstWrite& operator=(const SkTCopyOnFirstWrite& that) {
+ fLazy = that.fLazy;
+ fObj = fLazy.isValid() ? fLazy.get() : that.fObj;
+ return *this;
+ }
+
+ SkTCopyOnFirstWrite& operator=(SkTCopyOnFirstWrite&& that) {
+ fLazy = std::move(that.fLazy);
+ fObj = fLazy.isValid() ? fLazy.get() : that.fObj;
+ return *this;
+ }
+
// Should only be called once, and only if the default constructor was used.
void init(const T& initial) {
SkASSERT(nullptr == fObj);
@@ -174,6 +189,8 @@ public:
return const_cast<T*>(fObj);
}
+ const T* get() const { return fObj; }
+
/**
* Operators for treating this as though it were a const pointer.
*/
diff --git a/tests/TLazyTest.cpp b/tests/TLazyTest.cpp
new file mode 100644
index 0000000000..7be514fac6
--- /dev/null
+++ b/tests/TLazyTest.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2018 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkTLazy.h"
+#include "Test.h"
+
+DEF_TEST(TLazy_copy, r) {
+ SkTLazy<int> lazy;
+
+ REPORTER_ASSERT(r, !lazy.isValid());
+ REPORTER_ASSERT(r, lazy.getMaybeNull() == nullptr);
+
+ {
+ SkTLazy<int> lazy_copy(lazy);
+ REPORTER_ASSERT(r, !lazy_copy.isValid());
+ REPORTER_ASSERT(r, lazy_copy.getMaybeNull() == nullptr);
+ }
+
+ lazy.init(42);
+
+ REPORTER_ASSERT(r, lazy.isValid());
+ REPORTER_ASSERT(r, 42 == *lazy.get());
+
+ {
+ SkTLazy<int> lazy_copy(lazy);
+ REPORTER_ASSERT(r, lazy_copy.isValid());
+ REPORTER_ASSERT(r, 42 == *lazy_copy.get());
+ REPORTER_ASSERT(r, lazy.get() != lazy_copy.get());
+ }
+}
+
+DEF_TEST(TCopyOnFirstWrite_copy, r) {
+ const int v = 42;
+
+ SkTCopyOnFirstWrite<int> cow(v);
+
+ REPORTER_ASSERT(r, 42 == *cow);
+ REPORTER_ASSERT(r, &v == cow.get());
+
+ {
+ SkTCopyOnFirstWrite<int> cow_copy(cow);
+ REPORTER_ASSERT(r, 42 == *cow_copy);
+ REPORTER_ASSERT(r, &v == cow_copy.get());
+ REPORTER_ASSERT(r, cow.get() == cow_copy.get());
+
+ *cow_copy.writable() = 43;
+ REPORTER_ASSERT(r, 42 == *cow);
+ REPORTER_ASSERT(r, &v == cow.get());
+ REPORTER_ASSERT(r, 43 == *cow_copy);
+ REPORTER_ASSERT(r, &v != cow_copy.get());
+ REPORTER_ASSERT(r, cow.get() != cow_copy.get());
+ }
+
+ *cow.writable() = 43;
+
+ REPORTER_ASSERT(r, 43 == *cow);
+ REPORTER_ASSERT(r, &v != cow.get());
+
+ {
+ SkTCopyOnFirstWrite<int> cow_copy(cow);
+ REPORTER_ASSERT(r, 43 == *cow_copy);
+ REPORTER_ASSERT(r, &v != cow_copy.get());
+ REPORTER_ASSERT(r, cow.get() != cow_copy.get());
+
+ *cow_copy.writable() = 44;
+
+ REPORTER_ASSERT(r, 43 == *cow);
+ REPORTER_ASSERT(r, &v != cow.get());
+ REPORTER_ASSERT(r, 44 == *cow_copy);
+ REPORTER_ASSERT(r, &v != cow_copy.get());
+ REPORTER_ASSERT(r, cow.get() != cow_copy.get());
+ }
+}