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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « no previous file | fpdfsdk/cpdfsdk_annot.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifndef CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
6 #define CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
7
8 #include <set>
9
10 #include "core/fxcrt/include/fx_system.h"
11 #include "third_party/base/stl_util.h"
12
13 template <class T>
14 class CFX_Observable {
15 public:
16 class Observer {
Tom Sepez 2016/09/07 17:10:16 An Observable<>::Observer that observes observable
17 public:
18 Observer() : m_pWatchedPtr(nullptr) {}
19 Observer(T** pWatchedPtr) : m_pWatchedPtr(pWatchedPtr) {}
20 Observer(const Observer& that) = delete;
21 ~Observer() {
22 if (m_pWatchedPtr)
23 (*m_pWatchedPtr)->RemoveObserver(this);
24 }
25 void SetWatchedPtr(T** pWatchedPtr) {
26 if (m_pWatchedPtr)
27 (*m_pWatchedPtr)->RemoveObserver(this);
28 m_pWatchedPtr = pWatchedPtr;
29 if (m_pWatchedPtr)
30 (*m_pWatchedPtr)->AddObserver(this);
31 }
32 void OnDestroy() {
33 ASSERT(m_pWatchedPtr);
34 *m_pWatchedPtr = nullptr;
35 m_pWatchedPtr = nullptr;
36 }
37 Observer& operator=(const Observer& that) = delete;
38
39 private:
40 T** m_pWatchedPtr;
41 };
42
43 CFX_Observable() {}
44 CFX_Observable(const CFX_Observable& that) = delete;
45 ~CFX_Observable() {
46 for (auto* pObserver : m_Observers)
47 pObserver->OnDestroy();
48 }
49 void AddObserver(Observer* pObserver) {
50 ASSERT(!pdfium::ContainsKey(m_Observers, pObserver));
51 m_Observers.insert(pObserver);
52 }
53 void RemoveObserver(Observer* pObserver) {
54 ASSERT(pdfium::ContainsKey(m_Observers, pObserver));
55 m_Observers.erase(pObserver);
56 }
57 CFX_Observable& operator=(const CFX_Observable& that) = delete;
58
59 private:
60 std::set<Observer*> m_Observers;
61 };
62
63 #endif // CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
OLDNEW
« 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