ObserverData
ObserverData
Manages observer callbacks for implementing the observer pattern. More…
#include <Observer_Data.hpp>
Inherited by RaggedTimeSeries< Line2D >, RaggedTimeSeries< Mask2D >, RaggedTimeSeries< Point2D< float > >, AnalogTimeSeries, DigitalEventSeries, DigitalIntervalSeries, MediaData, RaggedAnalogTimeSeries, RaggedTimeSeries< TData >, TensorData
Public Classes
| Name | |
|---|---|
| struct | ObserverEntry Entry in the observer map. |
Public Types
| Name | |
|---|---|
| using std::function< void()> | ObserverCallback Type alias for observer callback functions. |
| using int | CallbackID Type alias for callback identifiers. |
Public Functions
| Name | |
|---|---|
| ObserverData(std::string name =“UnnamedObservable”) | |
| CallbackID | addObserver(ObserverCallback callback, std::string name =“Anonymous”) Register a new observer callback. |
| void | notifyObservers() Notify all registered observers. |
| void | removeObserver(CallbackID id) Remove a previously registered observer. |
Detailed Description
class ObserverData;Manages observer callbacks for implementing the observer pattern.
Warning:
- This class is NOT thread-safe. Concurrent calls to addObserver(), removeObserver(), or notifyObservers() from multiple threads will result in undefined behavior. Callers must provide external synchronization if thread safety is required.
- Modifying observers (adding or removing) from within an observer callback during notifyObservers() is safe. The current notification round iterates a snapshot; additions or removals take effect on the next call to notifyObservers().
ObserverData provides a simple observer pattern implementation where callbacks can be registered, removed, and notified of changes. Each registered callback receives a unique ID that can be used for removal.
Example usage:
ObserverData observable;
// Register an observer
auto id = observable.addObserver([]() {
std::cout << "Data changed!" << std::endl;
});
// Trigger notifications
observable.notifyObservers();
// Unregister when done
observable.removeObserver(id);Public Types Documentation
using ObserverCallback
using ObserverData::ObserverCallback = std::function<void()>;Type alias for observer callback functions.
using CallbackID
using ObserverData::CallbackID = int;Type alias for callback identifiers.
Public Functions Documentation
function ObserverData
inline explicit ObserverData(
std::string name ="UnnamedObservable"
)function addObserver
CallbackID addObserver(
ObserverCallback callback,
std::string name ="Anonymous"
)Register a new observer callback.
Parameters:
- callback The callback function to invoke on notifications. Must be a valid callable (non-empty std::function).
- name The name of the observer. This is used for debugging and logging.
Return: A unique identifier for this observer registration. Use this ID to remove the observer later via removeObserver().
Note: The returned ID should be stored if you intend to remove the observer later. Discarding the ID makes removal impossible.
Adds the given callback to the list of observers that will be notified when notifyObservers() is called. The callback is stored by value.
function notifyObservers
void notifyObservers()Notify all registered observers.
Note: If any callback throws an exception, remaining callbacks will NOT be notified and the exception will propagate to the caller.
Invokes all registered observer callbacks. The order in which observers are called is unspecified (depends on unordered_map iteration order).
function removeObserver
void removeObserver(
CallbackID id
)Remove a previously registered observer.
Parameters:
- id The callback ID returned by addObserver(). If the ID is invalid or was already removed, this function does nothing.
Removes the observer associated with the given ID. After removal, the callback will no longer be invoked during notifyObservers().
Updated on 2026-11-13 at 20:25:40 +0000