ADTF
Loading...
Searching...
No Matches
access_observer.h
Go to the documentation of this file.
1
14
15#ifndef DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
16#define DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
17
18#include <string>
19#include <vector>
20
21namespace ddl {
22
23namespace dd {
24
25namespace utility {
26
33template <typename MODEL_SUBJECT_T, typename EVENT_CODE_T>
35public:
37 typedef EVENT_CODE_T event_code_type;
39 typedef MODEL_SUBJECT_T subject_type;
40
41protected:
47
48public:
56 virtual void modelChanged(event_code_type event_code,
57 subject_type& changed_subject,
58 const std::string& additional_info) = 0;
59};
60
67template <typename MODEL_SUBJECT_T, typename EVENT_CODE_T>
69public:
71 typedef EVENT_CODE_T event_code_type;
73 typedef MODEL_SUBJECT_T subject_type;
86 {
87 // nothing to do
88 // copy means we erase the current observers so we do not copy it
89 }
90
96 {
97 // nothing to do
98 // copy means we erase the current observers so we do not copy it
99 return *this;
100 }
101
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 }
121
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
136protected:
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
155private:
156 std::vector<observer_type*> _observers;
157};
158
159} // namespace utility
160} // namespace dd
161namespace utility {
162 using dd::utility::ModelSubjectUtility;
163 using dd::utility::ModelObserverUtility;
164}
165} // namespace ddl
166
167#endif // DD_ACCESS_OBSERVER_INTERFACE_H_INCLUDED
The ModelObserver utility template.
Definition access_observer.h:34
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
Definition access_observer.h:46
ModelEventCode event_code_type
Definition access_observer.h:37
void attachObserver(observer_type *observer)
adda a observer to notify.
Definition access_observer.h:112
ModelObserverUtility< TYPE, ModelEventCode > observer_type
Definition access_observer.h:75
void notifyChanged(event_code_type event_code, subject_type &changed_subject, const std::string &additional_info)
helper utility function to notify the current observers
Definition access_observer.h:144
ModelSubjectUtility(const ModelSubjectUtility &)
copy CTOR which removes the observers in case of!
Definition access_observer.h:85
void detachObserver(observer_type *observer)
remove a observer if in list.
Definition access_observer.h:126
ModelEventCode event_code_type
Definition access_observer.h:71
ModelSubjectUtility & operator=(const ModelSubjectUtility &)
copy assignment operator which removes the observers in case of!
Definition access_observer.h:95
virtual ~ModelSubjectUtility()=default
DTOR.
definition of the old compatibility utility namespace
Definition datamodel_keyvalue.h:160
definition of the dd namespace
Definition datamodel_base.h:26
definition of the ddl namespace
Definition codec.h:21