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

Unified Diff: core/fxcrt/include/cfx_observable.h

Issue 2311343003: Make Observers into a templated class (Closed)
Patch Set: Add back ASSERTS Created 4 years, 3 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 | « no previous file | fpdfsdk/cpdfsdk_annot.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: core/fxcrt/include/cfx_observable.h
diff --git a/core/fxcrt/include/cfx_observable.h b/core/fxcrt/include/cfx_observable.h
new file mode 100644
index 0000000000000000000000000000000000000000..1f447bcaf19de44800cb855da5f6a22933e01249
--- /dev/null
+++ b/core/fxcrt/include/cfx_observable.h
@@ -0,0 +1,63 @@
+// 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_INCLUDE_CFX_OBSERVABLE_H_
+#define CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
+
+#include <set>
+
+#include "core/fxcrt/include/fx_system.h"
+#include "third_party/base/stl_util.h"
+
+template <class T>
+class CFX_Observable {
+ public:
+ class Observer {
Tom Sepez 2016/09/07 17:10:16 An Observable<>::Observer that observes observable
+ public:
+ Observer() : m_pWatchedPtr(nullptr) {}
+ Observer(T** pWatchedPtr) : m_pWatchedPtr(pWatchedPtr) {}
+ Observer(const Observer& that) = delete;
+ ~Observer() {
+ if (m_pWatchedPtr)
+ (*m_pWatchedPtr)->RemoveObserver(this);
+ }
+ void SetWatchedPtr(T** pWatchedPtr) {
+ if (m_pWatchedPtr)
+ (*m_pWatchedPtr)->RemoveObserver(this);
+ m_pWatchedPtr = pWatchedPtr;
+ if (m_pWatchedPtr)
+ (*m_pWatchedPtr)->AddObserver(this);
+ }
+ void OnDestroy() {
+ ASSERT(m_pWatchedPtr);
+ *m_pWatchedPtr = nullptr;
+ m_pWatchedPtr = nullptr;
+ }
+ Observer& operator=(const Observer& that) = delete;
+
+ private:
+ T** m_pWatchedPtr;
+ };
+
+ CFX_Observable() {}
+ CFX_Observable(const CFX_Observable& that) = delete;
+ ~CFX_Observable() {
+ for (auto* pObserver : m_Observers)
+ pObserver->OnDestroy();
+ }
+ void AddObserver(Observer* pObserver) {
+ ASSERT(!pdfium::ContainsKey(m_Observers, pObserver));
+ m_Observers.insert(pObserver);
+ }
+ void RemoveObserver(Observer* pObserver) {
+ ASSERT(pdfium::ContainsKey(m_Observers, pObserver));
+ m_Observers.erase(pObserver);
+ }
+ CFX_Observable& operator=(const CFX_Observable& that) = delete;
+
+ private:
+ std::set<Observer*> m_Observers;
+};
+
+#endif // CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
« no previous file with comments | « no previous file | fpdfsdk/cpdfsdk_annot.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698