ADTF  3.18.2
dd_access_observer.h
Go to the documentation of this file.
1 
15 #ifndef DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
16 #define DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
17 
18 #include <string>
19 #include <vector>
20 
21 namespace ddl {
22 
23 namespace dd {
24 
25 namespace utility {
26 
33 template <typename MODEL_SUBJECT_T, typename EVENT_CODE_T>
35 public:
37  typedef EVENT_CODE_T event_code_type;
39  typedef MODEL_SUBJECT_T subject_type;
40 
41 protected:
46  virtual ~ModelObserverUtility(){};
47 
48 public:
56  virtual void modelChanged(event_code_type event_code,
57  subject_type& changed_subject,
58  const std::string& additional_info) = 0;
59 };
60 
67 template <typename MODEL_SUBJECT_T, typename EVENT_CODE_T>
69 public:
71  typedef EVENT_CODE_T event_code_type;
73  typedef MODEL_SUBJECT_T subject_type;
80  ModelSubjectUtility() = default;
86  {
87  // nothing to do
88  // copy means we erase the current observers so we do not copy it
89  }
96  {
97  // nothing to do
98  // copy means we erase the current observers so we do not copy it
99  return *this;
100  }
105  virtual ~ModelSubjectUtility() = default;
106 
113  {
114  for (auto& current: _observers) {
115  if (current == observer) {
116  return;
117  }
118  }
119  _observers.push_back(observer);
120  }
127  {
128  for (auto current = _observers.begin(); current != _observers.end(); ++current) {
129  if (*current == observer) {
130  _observers.erase(current);
131  return;
132  }
133  }
134  }
135 
136 protected:
145  subject_type& changed_subject,
146  const std::string& additional_info)
147  {
148  // we take a copy, to prevent invalidation of the iterator
149  auto observers_copy = _observers;
150  for (auto& current: observers_copy) {
151  current->modelChanged(event_code, changed_subject, additional_info);
152  }
153  }
154 
155 private:
156  std::vector<observer_type*> _observers;
157 };
158 
159 } // namespace utility
160 } // namespace dd
161 } // namespace ddl
162 
163 #endif // DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
The ModelObserver utility template.
virtual void modelChanged(event_code_type event_code, subject_type &changed_subject, const std::string &additional_info)=0
interface function to override.
virtual ~ModelObserverUtility()
protected DTOR
MODEL_SUBJECT_T subject_type
local definition of the subject code type
EVENT_CODE_T event_code_type
local definition of the event code type
Model Subject utility to define a Model Subject that notifies one or more observers.
ModelSubjectUtility & operator=(const ModelSubjectUtility &)
copy assignment operator which removes the observers in case of!
void notifyChanged(event_code_type event_code, subject_type &changed_subject, const std::string &additional_info)
helper utility function to notify the current observers
void detachObserver(observer_type *observer)
remove a observer if in list.
virtual ~ModelSubjectUtility()=default
DTOR.
ModelObserverUtility< MODEL_SUBJECT_T, EVENT_CODE_T > observer_type
local definition of the observer type to notify
MODEL_SUBJECT_T subject_type
local definition of the subject type
EVENT_CODE_T event_code_type
local definition of the event code type
ModelSubjectUtility(const ModelSubjectUtility &)
copy CTOR which removes the observers in case of!
void attachObserver(observer_type *observer)
adda a observer to notify.