Open
Graph Drawing
Framework

 v. 2023.09 (Elderberry)
 

Observer.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/List.h>
35 #include <ogdf/basic/basic.h>
37 
38 #ifndef OGDF_MEMORY_POOL_NTS
39 # include <mutex>
40 #endif
41 
42 namespace ogdf {
43 
52 template<typename TObserved, typename TObserver>
53 class Observer {
54 public:
56  Observer() = default;
57 
59  virtual ~Observer() { reregister(nullptr); }
60 
71 
72 
73  void reregister(const TObserved* obs) {
74  const TObserved* old = m_pObserved;
75  if (m_pObserved) {
76  m_pObserved->unregisterObserver(m_itObsList);
77  }
78  m_pObserved = obs;
79  if (m_pObserved != nullptr) {
80  m_itObsList = obs->registerObserver(dynamic_cast<TObserver*>(this));
81  }
83  }
84 
86  virtual void registrationChanged(const TObserved* old) { }
87 
88  const TObserved* getObserved() const { return m_pObserved; }
89 
90 private:
91  const TObserved* m_pObserved = nullptr;
93 };
94 
103 template<typename TObserver, typename TObserved>
104 class Observable {
106 
107 #ifndef OGDF_MEMORY_POOL_NTS
108  mutable std::mutex m_mutexRegArrays;
109 #endif
111 
112 public:
113  Observable() = default;
114 
115  virtual ~Observable() {
116  // clearObservers must be called by child class, calling it here would
117  // notify Observers with an already partially destructed Observable
119  }
120 
132 
133 private:
135 
140  typename ListPure<TObserver*>::iterator registerObserver(TObserver* obs) const {
141 #ifndef OGDF_MEMORY_POOL_NTS
142  std::lock_guard<std::mutex> guard(m_mutexRegArrays);
143 #endif
144  OGDF_ASSERT(obs != nullptr);
145  return m_regObservers.pushBack(obs);
146  }
147 
149 
154 #ifndef OGDF_MEMORY_POOL_NTS
155  std::lock_guard<std::mutex> guard(m_mutexRegArrays);
156 #endif
157  m_regObservers.del(it);
158  }
159 
160 protected:
162 
163  void clearObservers() {
164  while (!m_regObservers.empty()) {
165  TObserver* obs = m_regObservers.front();
166  obs->reregister(nullptr);
168  || (m_regObservers.front() != obs && m_regObservers.back() != obs));
169  }
170  }
171 };
172 }
ogdf
The namespace for all OGDF objects.
Definition: multilevelmixer.cpp:39
ogdf::Observer::getObserved
const TObserved * getObserved() const
Definition: Observer.h:88
OGDF_ASSERT
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:66
copy_move.h
Utility macros for declaring copy and move constructors and assignment operations.
ogdf::ListPure::back
const_reference back() const
Returns a const reference to the last element.
Definition: List.h:323
ogdf::Observable::registerObserver
ListPure< TObserver * >::iterator registerObserver(TObserver *obs) const
Registers an observer.
Definition: Observer.h:140
ogdf::Observer::reregister
void reregister(const TObserved *obs)
Associates observer instance with instance obs.
Definition: Observer.h:73
OGDF_NO_MOVE
#define OGDF_NO_MOVE(cls)
Explicitly disables (deletes) move construction and assignment for class cls.
Definition: copy_move.h:42
ogdf::Observer::m_pObserved
const TObserved * m_pObserved
Definition: Observer.h:91
ogdf::Observable::unregisterObserver
void unregisterObserver(typename ListPure< TObserver * >::iterator it) const
Unregisters an observer.
Definition: Observer.h:153
ogdf::Observable
Base class for an observable object that can be tracked by multiple Observer objects.
Definition: Observer.h:104
ogdf::Observable::getObservers
const ListPure< TObserver * > & getObservers() const
Definition: Observer.h:161
ogdf::Observer::Observer
Observer()=default
Constructs instance of Observer class.
OGDF_NO_COPY
#define OGDF_NO_COPY(cls)
Explicitly disables (deletes) copy construction and assignment for class cls.
Definition: copy_move.h:37
ogdf::Observer::m_itObsList
ListPure< TObserver * >::iterator m_itObsList
watched instance
Definition: Observer.h:92
ogdf::Observable::~Observable
virtual ~Observable()
Definition: Observer.h:115
ogdf::ListPure
Doubly linked lists.
Definition: List.h:55
ogdf::Observable::Observable
Observable()=default
ogdf::Observer
Base class for an observer for a single Observable object.
Definition: Observer.h:53
basic.h
Basic declarations, included by all source files.
ogdf::Observable::clearObservers
void clearObservers()
Definition: Observer.h:163
ogdf::Observer::~Observer
virtual ~Observer()
Destroys the instance, unregisters it from watched instance.
Definition: Observer.h:59
List.h
Declaration of doubly linked lists and iterators.
ogdf::ListPure::front
const_reference front() const
Returns a const reference to the first element.
Definition: List.h:305
ogdf::Observer::registrationChanged
virtual void registrationChanged(const TObserved *old)
Called after reregister() changed the observed instance.
Definition: Observer.h:86
ogdf::Observable::m_mutexRegArrays
std::mutex m_mutexRegArrays
The critical section for protecting shared access to register/unregister methods.
Definition: Observer.h:108
ogdf::Observable::m_regObservers
ListPure< TObserver * > m_regObservers
The registered observers.
Definition: Observer.h:110
ogdf::ListPure::del
void del(iterator it)
Removes it from the list.
Definition: List.h:765
ogdf::ListPure::empty
bool empty() const
Returns true iff the list is empty.
Definition: List.h:286
ogdf::ListPure::pushBack
iterator pushBack(const E &x)
Adds element x at the end of the list.
Definition: List.h:612