Bring in a reduced/simplified version of Chromium's base::WeakPtr to //mojo/edk/util.
Mostly, we just don't support SupportsWeakPtr.
R=jamesr@chromium.org
Review URL: https://codereview.chromium.org/1646113002 .
diff --git a/mojo/edk/util/BUILD.gn b/mojo/edk/util/BUILD.gn
index 371c292..2024194 100644
--- a/mojo/edk/util/BUILD.gn
+++ b/mojo/edk/util/BUILD.gn
@@ -32,6 +32,9 @@
"thread_checker.h",
"waitable_event.cc",
"waitable_event.h",
+ "weak_ptr.h",
+ "weak_ptr_internal.cc",
+ "weak_ptr_internal.h",
]
mojo_sdk_public_deps = [ "mojo/public/cpp/system" ]
@@ -54,6 +57,7 @@
"thread_annotations_unittest.cc",
"thread_checker_unittest.cc",
"waitable_event_unittest.cc",
+ "weak_ptr_unittest.cc",
]
deps = [
diff --git a/mojo/edk/util/weak_ptr.h b/mojo/edk/util/weak_ptr.h
new file mode 100644
index 0000000..e5ac4b1
--- /dev/null
+++ b/mojo/edk/util/weak_ptr.h
@@ -0,0 +1,193 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// This file provides weak pointers and weak pointer factories that work like
+// Chromium's |base::WeakPtr<T>| and |base::WeakPtrFactory<T>|. Note that we do
+// not provide anything analogous to |base::SupportsWeakPtr<T>|.
+
+#ifndef MOJO_UTIL_WEAK_PTR_H_
+#define MOJO_UTIL_WEAK_PTR_H_
+
+#include <assert.h>
+
+#include <utility>
+
+#include "mojo/edk/util/ref_ptr.h"
+#include "mojo/edk/util/weak_ptr_internal.h"
+
+namespace mojo {
+namespace util {
+
+// Forward declaration, so |WeakPtr<T>| can friend it.
+template <typename T>
+class WeakPtrFactory;
+
+// Class for "weak pointers" that can be invalidated. Valid weak pointers can
+// only originate from a |WeakPtrFactory| (see below), though weak pointers are
+// copyable and movable.
+//
+// Weak pointers are not in general thread-safe. They may only be *used* on a
+// single thread, namely the same thread as the "originating" |WeakPtrFactory|
+// (which can invalidate the weak pointers that it generates).
+//
+// However, weak pointers may be passed to other threads, reset on other
+// threads, or destroyed on other threads. They may also be reassigned on other
+// threads (in which case they should then only be used on the thread
+// corresponding to the new "originating" |WeakPtrFactory|).
+template <typename T>
+class WeakPtr {
+ public:
+ WeakPtr() : ptr_(nullptr) {}
+
+ // Copy constructor.
+ WeakPtr(const WeakPtr<T>& r) = default;
+
+ template <typename U>
+ WeakPtr(const WeakPtr<U>& r) : ptr_(r.ptr_), flag_(r.flag_) {}
+
+ // Move constructor.
+ WeakPtr(WeakPtr<T>&& r) = default;
+
+ template <typename U>
+ WeakPtr(WeakPtr<U>&& r) : ptr_(r.ptr_), flag_(std::move(r.flag_)) {}
+
+ ~WeakPtr() = default;
+
+ // The following methods are thread-friendly, in the sense that they may be
+ // called subject to additional synchronization.
+
+ // Copy assignment.
+ WeakPtr<T>& operator=(const WeakPtr<T>& r) = default;
+
+ template <typename U>
+ WeakPtr<T>& operator=(const WeakPtr<T>& r) {
+ ptr_ = r.ptr_;
+ flag_ = r.flag_;
+ }
+
+ // Move assignment.
+ WeakPtr<T>& operator=(WeakPtr<T>&& r) = default;
+
+ template <typename U>
+ WeakPtr<T>& operator=(WeakPtr<T>&& r) {
+ ptr_ = r.ptr_;
+ flag_ = std::move(r.flag_);
+ }
+
+ void reset() { flag_ = nullptr; }
+
+ // The following methods should only be called on the same thread as the
+ // "originating" |WeakPtrFactory|.
+
+ explicit operator bool() const { return flag_ && flag_->is_valid(); }
+
+ T* get() const { return *this ? ptr_ : nullptr; }
+
+ T& operator*() const {
+ assert(*this);
+ return *get();
+ }
+
+ T* operator->() const {
+ assert(*this);
+ return get();
+ }
+
+ private:
+ template <typename U>
+ friend class WeakPtr;
+
+ friend class WeakPtrFactory<T>;
+
+ explicit WeakPtr(T* ptr, RefPtr<internal::WeakPtrFlag>&& flag)
+ : ptr_(ptr), flag_(std::move(flag)) {}
+
+ T* ptr_;
+ RefPtr<internal::WeakPtrFlag> flag_;
+
+ // Copy/move construction/assignment supported.
+};
+
+// Class that produces (valid) |WeakPtr<T>|s. Typically, this is used as a
+// member variable of |T| (preferably the last one -- see below), and |T|'s
+// methods control how weak pointers to it are vended. This class is not
+// thread-safe, and should only be used on a single thread.
+//
+// Example:
+//
+// class Controller {
+// public:
+// Controller() : ..., weak_factory_(this) {}
+// ...
+//
+// void SpawnWorker() { Worker::StartNew(weak_factory_.GetWeakPtr()); }
+// void WorkComplete(const Result& result) { ... }
+//
+// private:
+// ...
+//
+// // Member variables should appear before the |WeakPtrFactory|, to ensure
+// // that any |WeakPtr|s to |Controller| are invalidated before its member
+// // variables' destructors are executed.
+// WeakPtrFactory<Controller> weak_factory_;
+// };
+//
+// class Worker {
+// public:
+// static void StartNew(const WeakPtr<Controller>& controller) {
+// Worker* worker = new Worker(controller);
+// // Kick off asynchronous processing....
+// }
+//
+// private:
+// Worker(const WeakPtr<Controller>& controller) : controller_(controller) {}
+//
+// void DidCompleteAsynchronousProcessing(const Result& result) {
+// if (controller_)
+// controller_->WorkComplete(result);
+// }
+//
+// WeakPtr<Controller> controller_;
+// };
+template <typename T>
+class WeakPtrFactory {
+ public:
+ explicit WeakPtrFactory(T* ptr) : ptr_(ptr) { assert(ptr_); }
+ ~WeakPtrFactory() { InvalidateWeakPtrs(); }
+
+ // Gets a new weak pointer, which will be valid until either
+ // |InvalidateWeakPtrs()| is called or this object is destroyed.
+ WeakPtr<T> GetWeakPtr() {
+ if (!flag_)
+ flag_ = MakeRefCounted<internal::WeakPtrFlag>();
+ return WeakPtr<T>(ptr_, flag_.Clone());
+ }
+
+ // Call this method to invalidate all existing weak pointers. (Note that
+ // additional weak pointers can be produced even after this is called.)
+ void InvalidateWeakPtrs() {
+ if (!flag_)
+ return;
+ flag_->Invalidate();
+ flag_ = nullptr;
+ }
+
+ // Call this method to determine if any weak pointers exist. (Note that a
+ // "false" result is definitive, but a "true" result may not be if weak
+ // pointers are held/reset/destroyed/reassigned on other threads.)
+ bool HasWeakPtrs() const { return flag_ && !flag_->HasOneRef(); }
+
+ private:
+ // Note: See weak_ptr_internal.h for an explanation of why we store the
+ // pointer here, instead of in the "flag".
+ T* const ptr_;
+ RefPtr<internal::WeakPtrFlag> flag_;
+
+ MOJO_DISALLOW_COPY_AND_ASSIGN(WeakPtrFactory);
+};
+
+} // namespace util
+} // namespace mojo
+
+#endif // MOJO_UTIL_WEAK_PTR_H_
diff --git a/mojo/edk/util/weak_ptr_internal.cc b/mojo/edk/util/weak_ptr_internal.cc
new file mode 100644
index 0000000..f6047ae
--- /dev/null
+++ b/mojo/edk/util/weak_ptr_internal.cc
@@ -0,0 +1,28 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/edk/util/weak_ptr_internal.h"
+
+#include <assert.h>
+
+namespace mojo {
+namespace util {
+namespace internal {
+
+WeakPtrFlag::WeakPtrFlag() : is_valid_(true) {}
+
+WeakPtrFlag::~WeakPtrFlag() {
+ // Should be invalidated before destruction.
+ assert(!is_valid_);
+}
+
+void WeakPtrFlag::Invalidate() {
+ // Invalidation should happen exactly once.
+ assert(is_valid_);
+ is_valid_ = false;
+}
+
+} // namespace internal
+} // namespace util
+} // namespace mojo
diff --git a/mojo/edk/util/weak_ptr_internal.h b/mojo/edk/util/weak_ptr_internal.h
new file mode 100644
index 0000000..ed90d76
--- /dev/null
+++ b/mojo/edk/util/weak_ptr_internal.h
@@ -0,0 +1,44 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#ifndef MOJO_UTIL_WEAK_PTR_INTERNAL_H_
+#define MOJO_UTIL_WEAK_PTR_INTERNAL_H_
+
+#include <assert.h>
+
+#include "mojo/edk/util/ref_counted.h"
+#include "mojo/public/cpp/system/macros.h"
+
+namespace mojo {
+namespace util {
+namespace internal {
+
+// |WeakPtr<T>|s have a reference to a |WeakPtrFlag| to determine whether they
+// are valid (non-null) or not. We do not store a |T*| in this object since
+// there may also be |WeakPtr<U>|s to the same object, where |U| is a superclass
+// of |T|.
+//
+// This class in not thread-safe, though references may be released on any
+// thread (allowing weak pointers to be destroyed/reset/reassigned on any
+// thread).
+class WeakPtrFlag : public RefCountedThreadSafe<WeakPtrFlag> {
+ public:
+ WeakPtrFlag();
+ ~WeakPtrFlag();
+
+ bool is_valid() const { return is_valid_; }
+
+ void Invalidate();
+
+ private:
+ bool is_valid_;
+
+ MOJO_DISALLOW_COPY_AND_ASSIGN(WeakPtrFlag);
+};
+
+} // namespace internal
+} // namespace util
+} // namespace mojo
+
+#endif // MOJO_UTIL_WEAK_PTR_INTERNAL_H_
diff --git a/mojo/edk/util/weak_ptr_unittest.cc b/mojo/edk/util/weak_ptr_unittest.cc
new file mode 100644
index 0000000..7419eb4
--- /dev/null
+++ b/mojo/edk/util/weak_ptr_unittest.cc
@@ -0,0 +1,202 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "mojo/edk/util/weak_ptr.h"
+
+#include <utility>
+
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace mojo {
+namespace util {
+namespace {
+
+TEST(WeakPtrTest, Basic) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ EXPECT_EQ(&data, ptr.get());
+}
+
+TEST(WeakPtrTest, CopyConstruction) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ WeakPtr<int> ptr2(ptr);
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, MoveConstruction) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ WeakPtr<int> ptr2(std::move(ptr));
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, CopyAssignment) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ WeakPtr<int> ptr2;
+ EXPECT_EQ(nullptr, ptr2.get());
+ ptr2 = ptr;
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, MoveAssignment) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ WeakPtr<int> ptr2;
+ EXPECT_EQ(nullptr, ptr2.get());
+ ptr2 = std::move(ptr);
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, Testable) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr;
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_FALSE(ptr);
+ ptr = factory.GetWeakPtr();
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_TRUE(ptr);
+}
+
+TEST(WeakPtrTest, OutOfScope) {
+ WeakPtr<int> ptr;
+ EXPECT_EQ(nullptr, ptr.get());
+ {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ ptr = factory.GetWeakPtr();
+ }
+ EXPECT_EQ(nullptr, ptr.get());
+}
+
+TEST(WeakPtrTest, Multiple) {
+ WeakPtr<int> a;
+ WeakPtr<int> b;
+ {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ a = factory.GetWeakPtr();
+ b = factory.GetWeakPtr();
+ EXPECT_EQ(&data, a.get());
+ EXPECT_EQ(&data, b.get());
+ }
+ EXPECT_EQ(nullptr, a.get());
+ EXPECT_EQ(nullptr, b.get());
+}
+
+TEST(WeakPtrTest, MultipleStaged) {
+ WeakPtr<int> a;
+ {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ a = factory.GetWeakPtr();
+ { WeakPtr<int> b = factory.GetWeakPtr(); }
+ EXPECT_NE(a.get(), nullptr);
+ }
+ EXPECT_EQ(nullptr, a.get());
+}
+
+struct Base {
+ double member = 0.;
+};
+struct Derived : public Base {};
+
+TEST(WeakPtrTest, Dereference) {
+ Base data;
+ data.member = 123456.;
+ WeakPtrFactory<Base> factory(&data);
+ WeakPtr<Base> ptr = factory.GetWeakPtr();
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_EQ(data.member, (*ptr).member);
+ EXPECT_EQ(data.member, ptr->member);
+}
+
+TEST(WeakPtrTest, UpcastCopyConstruction) {
+ Derived data;
+ WeakPtrFactory<Derived> factory(&data);
+ WeakPtr<Derived> ptr = factory.GetWeakPtr();
+ WeakPtr<Base> ptr2(ptr);
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, UpcastMoveConstruction) {
+ Derived data;
+ WeakPtrFactory<Derived> factory(&data);
+ WeakPtr<Derived> ptr = factory.GetWeakPtr();
+ WeakPtr<Base> ptr2(std::move(ptr));
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, UpcastCopyAssignment) {
+ Derived data;
+ WeakPtrFactory<Derived> factory(&data);
+ WeakPtr<Derived> ptr = factory.GetWeakPtr();
+ WeakPtr<Base> ptr2;
+ EXPECT_EQ(nullptr, ptr2.get());
+ ptr2 = ptr;
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, UpcastMoveAssignment) {
+ Derived data;
+ WeakPtrFactory<Derived> factory(&data);
+ WeakPtr<Derived> ptr = factory.GetWeakPtr();
+ WeakPtr<Base> ptr2;
+ EXPECT_EQ(nullptr, ptr2.get());
+ ptr2 = std::move(ptr);
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_EQ(&data, ptr2.get());
+}
+
+TEST(WeakPtrTest, InvalidateWeakPtrs) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ EXPECT_EQ(&data, ptr.get());
+ EXPECT_TRUE(factory.HasWeakPtrs());
+ factory.InvalidateWeakPtrs();
+ EXPECT_EQ(nullptr, ptr.get());
+ EXPECT_FALSE(factory.HasWeakPtrs());
+
+ // Test that the factory can create new weak pointers after a
+ // |InvalidateWeakPtrs()| call, and that they remain valid until the next
+ // |InvalidateWeakPtrs()| call.
+ WeakPtr<int> ptr2 = factory.GetWeakPtr();
+ EXPECT_EQ(&data, ptr2.get());
+ EXPECT_TRUE(factory.HasWeakPtrs());
+ factory.InvalidateWeakPtrs();
+ EXPECT_EQ(nullptr, ptr2.get());
+ EXPECT_FALSE(factory.HasWeakPtrs());
+}
+
+TEST(WeakPtrTest, HasWeakPtrs) {
+ int data = 0;
+ WeakPtrFactory<int> factory(&data);
+ {
+ WeakPtr<int> ptr = factory.GetWeakPtr();
+ EXPECT_TRUE(factory.HasWeakPtrs());
+ }
+ EXPECT_FALSE(factory.HasWeakPtrs());
+}
+
+// TODO(vtl): Copy/convert the various threaded tests from Chromium's
+// //base/memory/weak_ptr_unittest.cc.
+
+} // namespace
+} // namespace util
+} // namespace mojo