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

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

Issue 2311343003: Make Observers into a templated class (Closed)
Patch Set: rebase 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
12 template <class T>
13 class CFX_Observable {
14 public:
15 class Observer {
16 public:
17 Observer() : m_pWatchedPtr(nullptr) {}
18 Observer(T** pWatchedPtr) : m_pWatchedPtr(pWatchedPtr) {}
19 Observer(const Observer& that) = delete;
20 ~Observer() {
21 if (m_pWatchedPtr)
22 (*m_pWatchedPtr)->RemoveObserver(this);
23 }
24 void SetWatchedPtr(T** pWatchedPtr) {
25 if (m_pWatchedPtr)
26 (*m_pWatchedPtr)->RemoveObserver(this);
27 m_pWatchedPtr = pWatchedPtr;
28 if (m_pWatchedPtr)
29 (*m_pWatchedPtr)->AddObserver(this);
30 }
31 void OnDestroy() {
32 if (m_pWatchedPtr) {
33 *m_pWatchedPtr = nullptr;
34 m_pWatchedPtr = nullptr;
35 }
36 }
37
38 private:
39 T** m_pWatchedPtr;
40 };
41
42 CFX_Observable() {}
43 CFX_Observable(const CFX_Observable& that) = delete;
44 ~CFX_Observable() {
45 for (auto* pObserver : m_Observers)
46 pObserver->OnDestroy();
47 }
48 void AddObserver(Observer* pObserver) { m_Observers.insert(pObserver); }
49 void RemoveObserver(Observer* pObserver) { m_Observers.erase(pObserver); }
50
51 private:
52 std::set<Observer*> m_Observers;
53 };
54
55 #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