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

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

Issue 2382723003: Move core/fxcrt/include to core/fxcrt (Closed)
Patch Set: Rebase to master Created 4 years, 2 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 | « core/fxcrt/include/cfx_count_ref.h ('k') | core/fxcrt/include/cfx_retain_ptr.h » ('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 ObservedPtr {
17 public:
18 ObservedPtr() : m_pObservedPtr(nullptr) {}
19 explicit ObservedPtr(T* pObservedPtr) : m_pObservedPtr(pObservedPtr) {
20 if (m_pObservedPtr)
21 m_pObservedPtr->AddObservedPtr(this);
22 }
23 ObservedPtr(const ObservedPtr& that) = delete;
24 ~ObservedPtr() {
25 if (m_pObservedPtr)
26 m_pObservedPtr->RemoveObservedPtr(this);
27 }
28 void Reset(T* pObservedPtr = nullptr) {
29 if (m_pObservedPtr)
30 m_pObservedPtr->RemoveObservedPtr(this);
31 m_pObservedPtr = pObservedPtr;
32 if (m_pObservedPtr)
33 m_pObservedPtr->AddObservedPtr(this);
34 }
35 void OnDestroy() {
36 ASSERT(m_pObservedPtr);
37 m_pObservedPtr = nullptr;
38 }
39 ObservedPtr& operator=(const ObservedPtr& that) = delete;
40 bool operator==(const ObservedPtr& that) const {
41 return m_pObservedPtr == that.m_pObservedPtr;
42 }
43 bool operator!=(const ObservedPtr& that) const { return !(*this == that); }
44 explicit operator bool() const { return !!m_pObservedPtr; }
45 T* Get() const { return m_pObservedPtr; }
46 T& operator*() const { return *m_pObservedPtr; }
47 T* operator->() const { return m_pObservedPtr; }
48
49 private:
50 T* m_pObservedPtr;
51 };
52
53 CFX_Observable() {}
54 CFX_Observable(const CFX_Observable& that) = delete;
55 ~CFX_Observable() { NotifyObservedPtrs(); }
56 void AddObservedPtr(ObservedPtr* pObservedPtr) {
57 ASSERT(!pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr));
58 m_ObservedPtrs.insert(pObservedPtr);
59 }
60 void RemoveObservedPtr(ObservedPtr* pObservedPtr) {
61 ASSERT(pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr));
62 m_ObservedPtrs.erase(pObservedPtr);
63 }
64 void NotifyObservedPtrs() {
65 for (auto* pObservedPtr : m_ObservedPtrs)
66 pObservedPtr->OnDestroy();
67 m_ObservedPtrs.clear();
68 }
69 CFX_Observable& operator=(const CFX_Observable& that) = delete;
70
71 protected:
72 size_t ActiveObservedPtrsForTesting() const { return m_ObservedPtrs.size(); }
73
74 private:
75 std::set<ObservedPtr*> m_ObservedPtrs;
76 };
77
78 #endif // CORE_FXCRT_INCLUDE_CFX_OBSERVABLE_H_
OLDNEW
« no previous file with comments | « core/fxcrt/include/cfx_count_ref.h ('k') | core/fxcrt/include/cfx_retain_ptr.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698