diff options
author | Tom Sepez <tsepez@chromium.org> | 2018-07-02 23:08:53 +0000 |
---|---|---|
committer | Chromium commit bot <commit-bot@chromium.org> | 2018-07-02 23:08:53 +0000 |
commit | c9aa2f8bfdf74ffae633c7bb1a4d908dda9caed5 (patch) | |
tree | d7e5045ac8add9395b2a62242fbdee0eecfd8bfb /core/fxcrt/observable.h | |
parent | 555b41aebe002918c806a8239dcab9ec2c032252 (diff) | |
download | pdfium-c9aa2f8bfdf74ffae633c7bb1a4d908dda9caed5.tar.xz |
Virtualize Observable<T>::ObservedPtr::OnDestroy() for CPDF_Avail cleanup
This enables more complicated cleanup when an observed object
is destroyed. Use it to make documents observable and to allow
the CPDF_Avail to cleanup without the need for intermediate class.
Change-Id: I3a8e758b7ff542e0a58710eff1ac8017205cbd45
Reviewed-on: https://pdfium-review.googlesource.com/36373
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Tom Sepez <tsepez@chromium.org>
Diffstat (limited to 'core/fxcrt/observable.h')
-rw-r--r-- | core/fxcrt/observable.h | 46 |
1 files changed, 27 insertions, 19 deletions
diff --git a/core/fxcrt/observable.h b/core/fxcrt/observable.h index 2013b75be9..e99dadfc8e 100644 --- a/core/fxcrt/observable.h +++ b/core/fxcrt/observable.h @@ -15,26 +15,34 @@ namespace fxcrt { template <class T> class Observable { public: - class ObservedPtr { + // General-purpose interface for more complicated cleanup. + class Observer { + public: + virtual ~Observer() = default; + virtual void OnObservableDestroyed() = 0; + }; + + // Simple case of a self-nulling pointer. + class ObservedPtr final : public Observer { public: ObservedPtr() : m_pObservable(nullptr) {} explicit ObservedPtr(T* pObservable) : m_pObservable(pObservable) { if (m_pObservable) - m_pObservable->AddObservedPtr(this); + m_pObservable->AddObserver(this); } ObservedPtr(const ObservedPtr& that) : ObservedPtr(that.Get()) {} ~ObservedPtr() { if (m_pObservable) - m_pObservable->RemoveObservedPtr(this); + m_pObservable->RemoveObserver(this); } void Reset(T* pObservable = nullptr) { if (m_pObservable) - m_pObservable->RemoveObservedPtr(this); + m_pObservable->RemoveObserver(this); m_pObservable = pObservable; if (m_pObservable) - m_pObservable->AddObservedPtr(this); + m_pObservable->AddObserver(this); } - void OnDestroy() { + void OnObservableDestroyed() override { ASSERT(m_pObservable); m_pObservable = nullptr; } @@ -57,27 +65,27 @@ class Observable { Observable() = default; Observable(const Observable& that) = delete; - ~Observable() { NotifyObservedPtrs(); } - void AddObservedPtr(ObservedPtr* pObservedPtr) { - ASSERT(!pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr)); - m_ObservedPtrs.insert(pObservedPtr); + ~Observable() { NotifyObservers(); } + void AddObserver(Observer* pObserver) { + ASSERT(!pdfium::ContainsKey(m_Observers, pObserver)); + m_Observers.insert(pObserver); } - void RemoveObservedPtr(ObservedPtr* pObservedPtr) { - ASSERT(pdfium::ContainsKey(m_ObservedPtrs, pObservedPtr)); - m_ObservedPtrs.erase(pObservedPtr); + void RemoveObserver(Observer* pObserver) { + ASSERT(pdfium::ContainsKey(m_Observers, pObserver)); + m_Observers.erase(pObserver); } - void NotifyObservedPtrs() { - for (auto* pObservedPtr : m_ObservedPtrs) - pObservedPtr->OnDestroy(); - m_ObservedPtrs.clear(); + void NotifyObservers() { + for (auto* pObserver : m_Observers) + pObserver->OnObservableDestroyed(); + m_Observers.clear(); } Observable& operator=(const Observable& that) = delete; protected: - size_t ActiveObservedPtrsForTesting() const { return m_ObservedPtrs.size(); } + size_t ActiveObserversForTesting() const { return m_Observers.size(); } private: - std::set<ObservedPtr*> m_ObservedPtrs; + std::set<Observer*> m_Observers; }; } // namespace fxcrt |