24 template <
typename MessageType>
35 checkMessageType(message);
44 void update(MessageType message,
double val) {
45 checkMessageType(message);
54 void update(MessageType message,
int val) {
55 checkMessageType(message);
64 void update(MessageType message,
const std::string& val) {
65 checkMessageType(message);
75 void update(MessageType message,
int val1,
double val2) {
76 checkMessageType(message);
86 void update(MessageType message,
int val1,
const std::string& val2) {
87 checkMessageType(message);
96 void update(MessageType message,
const Eigen::VectorXd& vec) {
97 checkMessageType(message);
102 virtual void updateImpl([[maybe_unused]] MessageType message){};
103 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
double val){};
104 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
int val){};
105 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
const std::string& val){};
106 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
int val1,
const std::string& val2){};
107 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
int val1,
double val2){};
108 virtual void updateImpl([[maybe_unused]] MessageType message, [[maybe_unused]]
const Eigen::VectorXd& vec) {}
111 void checkMessageType(MessageType message) {
112 if (MessageType::END == message) DUNE_THROW(Dune::InvalidStateException,
"The END enum type should not be used");
113 if (MessageType::BEGIN == message)
114 DUNE_THROW(Dune::InvalidStateException,
"The BEGIN enum type should not be used");
124 template <
typename MessageType>
128 for (MessageType msg = MessageType::BEGIN; msg != MessageType::END;
Ikarus::increment(msg))
129 messages_.push_back(msg);
171 template <std::
floating_po
int ScalarType>
172 void notify(MessageType message, ScalarType val);
179 void notify(MessageType message,
int val);
186 void notify(MessageType message,
const std::string& val);
194 void notify(MessageType message,
int val1,
double val2);
202 void notify(MessageType message,
int val1,
const std::string& val2);
210 template <std::
floating_po
int ScalarType>
211 void notify(MessageType message, Eigen::VectorX<ScalarType> vec);
214 using ObserverVector = std::vector<std::shared_ptr<IObserver<MessageType>>>;
215 using ObserverMap = std::map<MessageType, ObserverVector>;
216 ObserverMap observers_;
217 std::vector<MessageType> messages_;
220 template <
typename MessageType>
223 auto&& vectorOfObserversOfASpecificMessage = observers_[message];
224 vectorOfObserversOfASpecificMessage.push_back(observer);
227 template <
typename MessageType>
229 for (
auto& msg : messages_)
230 subscribe(msg, observer);
233 template <
typename MessageType>
236 for (
auto& observer : observers)
237 for (
auto& msg : messages_)
238 subscribe(msg, observer);
241 template <
typename MessageType>
243 auto vectorOfObserversOfASpecificMessage = observers_[message];
244 std::ranges::remove_if(vectorOfObserversOfASpecificMessage, [&observer](
auto&& obs) {
return obs == observer; });
247 template <
typename MessageType>
249 for (
auto& msg : messages_)
250 unSubscribe(msg, observer);
253 template <
typename MessageType>
255 auto vectorOfObserversOfASpecificMessage = observers_[message];
256 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
257 obs->update(message);
260 template <
typename MessageType>
261 template <std::
floating_po
int ScalarType>
263 auto vectorOfObserversOfASpecificMessage = observers_[message];
264 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
265 obs->update(message, val);
268 template <
typename MessageType>
270 auto vectorOfObserversOfASpecificMessage = observers_[message];
271 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
272 obs->update(message, val);
275 template <
typename MessageType>
277 auto vectorOfObserversOfASpecificMessage = observers_[message];
278 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
279 obs->update(message, val);
282 template <
typename MessageType>
284 auto vectorOfObserversOfASpecificMessage = observers_[message];
285 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
286 obs->update(message, val1, val2);
289 template <
typename MessageType>
291 auto vectorOfObserversOfASpecificMessage = observers_[message];
292 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
293 obs->update(message, val1, val2);
296 template <
typename MessageType>
297 template <std::
floating_po
int ScalarType>
299 auto vectorOfObserversOfASpecificMessage = observers_[message];
300 for (
auto&& obs : vectorOfObserversOfASpecificMessage)
301 obs->update(message, vec);
Implementation of the make enum macro.
MessageType & increment(MessageType &e)
Increments the given enum value.
Definition: makeenum.hh:63
Definition: simpleassemblers.hh:21
Generic observer interface for the Observer design pattern. See for a description of the design patt...
Definition: observer.hh:25
void update(MessageType message, const std::string &val)
Update method for receiving notifications with a message and a string value.
Definition: observer.hh:64
void update(MessageType message, double val)
Update method for receiving notifications with a message and a double value.
Definition: observer.hh:44
void update(MessageType message, const Eigen::VectorXd &vec)
Update method for receiving notifications with a message and an Eigen::VectorXd.
Definition: observer.hh:96
virtual void updateImpl(MessageType message)
Definition: observer.hh:102
virtual void updateImpl(MessageType message, const Eigen::VectorXd &vec)
Definition: observer.hh:108
virtual void updateImpl(MessageType message, int val1, double val2)
Definition: observer.hh:107
void update(MessageType message, int val1, const std::string &val2)
Update method for receiving notifications with a message, an integer value, and a string value.
Definition: observer.hh:86
virtual void updateImpl(MessageType message, double val)
Definition: observer.hh:103
void update(MessageType message)
Update method for receiving notifications with a message.
Definition: observer.hh:34
void update(MessageType message, int val)
Update method for receiving notifications with a message and an integer value.
Definition: observer.hh:54
void update(MessageType message, int val1, double val2)
Update method for receiving notifications with a message and two values (integer and double).
Definition: observer.hh:75
virtual void updateImpl(MessageType message, int val)
Definition: observer.hh:104
virtual void updateImpl(MessageType message, const std::string &val)
Definition: observer.hh:105
virtual void updateImpl(MessageType message, int val1, const std::string &val2)
Definition: observer.hh:106
virtual ~IObserver()=default
Virtual destructor for the observer interface.
Generic observable interface for the Observer design pattern. See for a description of the design pa...
Definition: observer.hh:125
void subscribeAll(std::initializer_list< std::shared_ptr< IObserver< MessageType > > > observers)
Subscribe multiple observers to receive notifications for all message types.
Definition: observer.hh:234
void subscribeAll(std::shared_ptr< IObserver< MessageType > > observer)
Subscribe an observer to receive notifications for all message types.
Definition: observer.hh:228
void notify(MessageType message, const std::string &val)
Notify observers about a specific message type with a string value.
Definition: observer.hh:276
void notify(MessageType message, int val)
Notify observers about a specific message type with an integer value.
Definition: observer.hh:269
void unSubscribe(MessageType message, std::shared_ptr< IObserver< MessageType > > observer)
Unsubscribe an observer from receiving notifications for a specific message type.
Definition: observer.hh:242
void notify(MessageType message)
Notify observers about a specific message type.
Definition: observer.hh:254
void unSubscribeAll(std::shared_ptr< IObserver< MessageType > > observer)
Unsubscribe an observer from receiving notifications for all message types.
Definition: observer.hh:248
void notify(MessageType message, int val1, const std::string &val2)
Notify observers about a specific message type with an integer value and a string value.
Definition: observer.hh:290
void notify(MessageType message, ScalarType val)
Notify observers about a specific message type with a floating-point value.
Definition: observer.hh:262
void notify(MessageType message, Eigen::VectorX< ScalarType > vec)
Notify observers about a specific message type with an Eigen::VectorX.
Definition: observer.hh:298
IObservable()
Definition: observer.hh:127
virtual ~IObservable()=default
void notify(MessageType message, int val1, double val2)
Notify observers about a specific message type with an integer and a double value.
Definition: observer.hh:283
void subscribe(MessageType message, std::shared_ptr< IObserver< MessageType > > observer)
Subscribe an observer to receive notifications for a specific message type.
Definition: observer.hh:221