summaryrefslogtreecommitdiff
path: root/core/fxcrt/observable.h
diff options
context:
space:
mode:
authorDan Sinclair <dsinclair@chromium.org>2017-09-21 15:25:32 -0400
committerChromium commit bot <commit-bot@chromium.org>2017-09-21 19:41:49 +0000
commit65ecca4bc9c8e043b355c459486870ac1518095d (patch)
tree2cca0a32a7873f2743e9d927010533019e4c85b9 /core/fxcrt/observable.h
parent3418f710f923785a0fd05f7c556d09bc9a8d3447 (diff)
downloadpdfium-65ecca4bc9c8e043b355c459486870ac1518095d.tar.xz
Move CFX_Observable to Observable
This CL renames CFX_Observable to Observable and moves into the fxcrt namespace. The test suite names were updated to ObservedPtr from fxcrt. Bug: pdfium:898 Change-Id: Ia507482bc0a1d6d8bbf12c0c55c4a88f6c8910e2 Reviewed-on: https://pdfium-review.googlesource.com/14615 Reviewed-by: Tom Sepez <tsepez@chromium.org> Commit-Queue: dsinclair <dsinclair@chromium.org>
Diffstat (limited to 'core/fxcrt/observable.h')
-rw-r--r--core/fxcrt/observable.h87
1 files changed, 87 insertions, 0 deletions
diff --git a/core/fxcrt/observable.h b/core/fxcrt/observable.h
new file mode 100644
index 0000000000..659f8ad5b9
--- /dev/null
+++ b/core/fxcrt/observable.h
@@ -0,0 +1,87 @@
+// Copyright 2016 PDFium 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 CORE_FXCRT_OBSERVABLE_H_
+#define CORE_FXCRT_OBSERVABLE_H_
+
+#include <set>
+
+#include "core/fxcrt/fx_system.h"
+#include "third_party/base/stl_util.h"
+
+namespace fxcrt {
+
+template <class T>
+class Observable {
+ public:
+ class ObservedPtr {
+ public:
+ ObservedPtr() : m_pObservable(nullptr) {}
+ explicit ObservedPtr(T* pObservable) : m_pObservable(pObservable) {
+ if (m_pObservable)
+ m_pObservable->AddObservedPtr(this);
+ }
+ ObservedPtr(const ObservedPtr& that) : ObservedPtr(that.Get()) {}
+ ~ObservedPtr() {
+ if (m_pObservable)
+ m_pObservable->RemoveObservedPtr(this);
+ }
+ void Reset(T* pObservable = nullptr) {
+ if (m_pObservable)
+ m_pObservable->RemoveObservedPtr(this);
+ m_pObservable = pObservable;
+ if (m_pObservable)
+ m_pObservable->AddObservedPtr(this);
+ }
+ void OnDestroy() {
+ ASSERT(m_pObservable);
+ m_pObservable = nullptr;
+ }
+ ObservedPtr& operator=(const ObservedPtr& that) {
+ Reset(that.Get());
+ return *this;
+ }
+ bool operator==(const ObservedPtr& that) const {
+ return m_pObservable == that.m_pObservable;
+ }
+ bool operator!=(const ObservedPtr& that) const { return !(*this == that); }
+ explicit operator bool() const { return !!m_pObservable; }
+ T* Get() const { return m_pObservable; }
+ T& operator*() const { return *m_pObservable; }
+ T* operator->() const { return m_pObservable; }
+
+ private:
+ T* m_pObservable;
+ };
+
+ Observable() {}
+ Observable(const Observable& that) = delete;
+ ~Observable() { NotifyObservedPtrs(); }
+ void AddObservedPtr(ObservedPtr* pObservedPtr) {
+ ASSERT(!pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr));
+ m_ObservedPtrs.insert(pObservedPtr);
+ }
+ void RemoveObservedPtr(ObservedPtr* pObservedPtr) {
+ ASSERT(pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr));
+ m_ObservedPtrs.erase(pObservedPtr);
+ }
+ void NotifyObservedPtrs() {
+ for (auto* pObservedPtr : m_ObservedPtrs)
+ pObservedPtr->OnDestroy();
+ m_ObservedPtrs.clear();
+ }
+ Observable& operator=(const Observable& that) = delete;
+
+ protected:
+ size_t ActiveObservedPtrsForTesting() const { return m_ObservedPtrs.size(); }
+
+ private:
+ std::set<ObservedPtr*> m_ObservedPtrs;
+};
+
+} // namespace fxcrt
+
+using fxcrt::Observable;
+
+#endif // CORE_FXCRT_OBSERVABLE_H_