Skip to main content

src/Observer/Observer_Data.hpp

src/Observer/Observer_Data.hpp

Observer pattern implementation for data change notifications. More…

Classes

Name
class ObserverData
Manages observer callbacks for implementing the observer pattern.
struct ObserverData::ObserverEntry
Entry in the observer map.
class ModificationHandle
A RAII handle for modifying an object and notifying on completion.

Types

Name
enum class NotifyObservers { Yes, No}
Strong type for specifying observer notification behavior.

Detailed Description

Observer pattern implementation for data change notifications.

Note: This library is NOT thread-safe. All observer operations (add, remove, notify) must be performed from the same thread, or external synchronization must be provided by the caller.

This file provides the core observer pattern infrastructure used throughout Neuralyzer for propagating data changes to dependent components.

Types Documentation

enum NotifyObservers

Enumerator Value Description
Yes Notify observers after the operation.
No Do not notify observers after the operation.

Strong type for specifying observer notification behavior.

See: ObserverData for the observer registration and notification mechanism

This enum class makes the intent explicit when calling methods that modify observer data. Users must explicitly choose whether to notify observers.

This enum is designed for use by consuming libraries (e.g., DataManager) to provide a clear API for methods that optionally notify observers:

void setData(const Data& data, NotifyObservers notify = NotifyObservers::Yes);

Source code

#ifndef OBSERVER_DATA_HPP
#define OBSERVER_DATA_HPP


#include <functional>
#include <string>
#include <unordered_map>

enum class NotifyObservers {
    Yes,
    No  
};


class ObserverData {

public:
    explicit ObserverData(std::string name = "UnnamedObservable")
        : _name(std::move(name)) {}

    using ObserverCallback = std::function<void()>;

    using CallbackID = int;

    struct ObserverEntry {
        ObserverCallback callback;
        std::string name;
    };

    [[nodiscard]] CallbackID addObserver(ObserverCallback callback, std::string name = "Anonymous");

    void notifyObservers();

    void removeObserver(CallbackID id);

private:
    std::string _name;
    std::unordered_map<CallbackID, ObserverEntry> _observers;
    CallbackID _next_id = 1;
};


template<typename T>
class ModificationHandle {
public:
    // Constructor: Takes the data to modify and the function to call on destruction
    ModificationHandle(T & data_ref, std::function<void()> on_destroy_callback)
        : _data_ref(data_ref),
          _on_destroy_cb(std::move(on_destroy_callback)) {}

    // Destructor: The core RAII magic. This triggers the notification.
    ~ModificationHandle() {
        if (_on_destroy_cb) {
            _on_destroy_cb();
        }
    }

    // --- Accessors ---
    // Provide pointer-like access
    T * operator->() { return &_data_ref.get(); }
    T const * operator->() const { return &_data_ref.get(); }

    // Provide reference-like access
    T & operator*() { return _data_ref.get(); }
    T const & operator*() const { return _data_ref.get(); }

    // Explicit getter
    T & get() { return _data_ref.get(); }
    T const & get() const { return _data_ref.get(); }

    // --- Move Semantics ---
    // Enable moving: transfer ownership of the notification
    ModificationHandle(ModificationHandle && other) noexcept
        : _data_ref(other._data_ref),
          _on_destroy_cb(std::move(other._on_destroy_cb)) {
        // Null out the other's callback so it doesn't fire
        other._on_destroy_cb = nullptr;
    }

    ModificationHandle & operator=(ModificationHandle && other) noexcept {
        if (this != &other) {
            // If this handle is about to be overwritten, fire its own callback first
            // if it's still valid (or just let it be destroyed, depends on design)
            if (_on_destroy_cb) {
                _on_destroy_cb();
            }

            _data_ref = other._data_ref;// Note: std::reference_wrapper assignment re-seats the reference
            _on_destroy_cb = std::move(other._on_destroy_cb);
            other._on_destroy_cb = nullptr;
        }
        return *this;
    }

    // --- Disable Copying ---
    ModificationHandle(ModificationHandle const &) = delete;
    ModificationHandle & operator=(ModificationHandle const &) = delete;

private:
    std::reference_wrapper<T> _data_ref;
    std::function<void()> _on_destroy_cb;
};

#endif// OBSERVER_DATA_HPP

Updated on 2026-11-13 at 20:25:44 +0000