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

Side by Side Diff: core/fxcrt/include/cfx_observable.h

Issue 2311343003: Make Observers into a templated class (Closed)
Patch Set: Make Annot is-a CPDFSDK_Annot::Observer 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') | fpdfsdk/javascript/Field.cpp » ('J')
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 {
17 public:
18 Observer() : m_pWatchedPtr(nullptr) {}
19 Observer(T** pWatchedPtr) : m_pWatchedPtr(pWatchedPtr) {
20 if (m_pWatchedPtr)
21 (*m_pWatchedPtr)->AddObserver(this);
22 }
23 Observer(const Observer& that) = delete;
24 ~Observer() {
25 if (m_pWatchedPtr)
26 (*m_pWatchedPtr)->RemoveObserver(this);
27 }
28 void SetWatchedPtr(T** pWatchedPtr) {
29 if (m_pWatchedPtr)
30 (*m_pWatchedPtr)->RemoveObserver(this);
31 m_pWatchedPtr = pWatchedPtr;
32 if (m_pWatchedPtr)
33 (*m_pWatchedPtr)->AddObserver(this);
34 }
35 void OnDestroy() {
36 ASSERT(m_pWatchedPtr);
37 *m_pWatchedPtr = nullptr;
38 m_pWatchedPtr = nullptr;
39 }
40 Observer& operator=(const Observer& that) = delete;
41
42 private:
43 T** m_pWatchedPtr;
44 };
45
46 CFX_Observable() {}
47 CFX_Observable(const CFX_Observable& that) = delete;
48 ~CFX_Observable() {
49 for (auto* pObserver : m_Observers)
50 pObserver->OnDestroy();
51 }
52 void AddObserver(Observer* pObserver) {
53 ASSERT(!pdfium::ContainsKey(m_Observers, pObserver));
54 m_Observers.insert(pObserver);
55 }
56 void RemoveObserver(Observer* pObserver) {
57 ASSERT(pdfium::ContainsKey(m_Observers, pObserver));
58 m_Observers.erase(pObserver);
59 }
60 CFX_Observable& operator=(const CFX_Observable& that) = delete;
61
62 private:
63 std::set<Observer*> m_Observers;
64 };
65
66 #endif // CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
OLDNEW
« no previous file with comments | « no previous file | fpdfsdk/cpdfsdk_annot.cpp » ('j') | fpdfsdk/javascript/Field.cpp » ('J')

Powered by Google App Engine
This is Rietveld 408576698