#pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Configuration { /// Watches a configuration file and reloads it on change (debounced, /// see FileSystem::FileWatcher). RAII: destroying the watch stops it. /// /// The callback receives the freshly loaded Config plus the sorted /// list of keys whose values changed (added, removed, or modified) /// relative to the previous load. A load failure keeps the previous /// configuration and logs a warning — the callback is not invoked. /// /// Callbacks run on the watcher thread. class ConfigWatch { public: using Loader = std::function( const std::filesystem::path&)>; using OnReload = std::function& changed)>; ConfigWatch(std::filesystem::path path, Loader loader, OnReload on_reload, FileSystem::FileWatcherOptions options = {}) : m_path(std::move(path)), m_loader(std::move(loader)), m_on_reload(std::move(on_reload)), m_watcher(options) { if (auto initial = m_loader(m_path); initial.has_value()) { m_current = std::move(*initial); } m_watcher.watch( m_path, [this](const FileSystem::FileWatcher::Event& e) { on_event(e); }); } /// Convenience factories for the built-in formats. [[nodiscard]] static ConfigWatch ini(std::filesystem::path path, OnReload on_reload, FileSystem::FileWatcherOptions options = {}) { return ConfigWatch{std::move(path), [](const std::filesystem::path& p) { return Config::from_ini(p); }, std::move(on_reload), options}; } [[nodiscard]] static ConfigWatch json(std::filesystem::path path, OnReload on_reload, FileSystem::FileWatcherOptions options = {}) { return ConfigWatch{std::move(path), [](const std::filesystem::path& p) { return Config::from_json(p); }, std::move(on_reload), options}; } [[nodiscard]] static ConfigWatch key_value(std::filesystem::path path, OnReload on_reload, FileSystem::FileWatcherOptions options = {}) { return ConfigWatch{std::move(path), [](const std::filesystem::path& p) { return Config::from_file(p); }, std::move(on_reload), options}; } /// The most recently loaded configuration (thread-safe copy). [[nodiscard]] Config current() const { std::lock_guard lock(m_mutex); return m_current; } /// Force a poll round (mainly for tests). void poll_now() { m_watcher.poll_now(); } private: void on_event(const FileSystem::FileWatcher::Event& event) { if (!event.exists) { Void::Log::warn("Configuration.Watch", "'{}' disappeared; keeping last config", m_path.string()); return; } auto loaded = m_loader(m_path); if (!loaded.has_value()) { Void::Log::warn( "Configuration.Watch", "reload of '{}' failed: {}; keeping last config", m_path.string(), loaded.error().message()); return; } std::vector changed; Config snapshot; { std::lock_guard lock(m_mutex); changed = diff(m_current, *loaded); m_current = std::move(*loaded); snapshot = m_current; } Void::Log::info("Configuration.Watch", "reloaded '{}' ({} keys changed)", m_path.string(), changed.size()); if (m_on_reload) { m_on_reload(snapshot, changed); } } [[nodiscard]] static std::vector diff(const Config& before, const Config& after) { std::vector changed; for (const auto& key : after.keys()) { const auto old_val = before.get(key); if (!old_val.has_value() || *old_val != after.get_or(key)) { changed.push_back(key); } } for (const auto& key : before.keys()) { if (!after.contains(key)) { changed.push_back(key); } } std::sort(changed.begin(), changed.end()); return changed; } std::filesystem::path m_path; Loader m_loader; OnReload m_on_reload; mutable std::mutex m_mutex; Config m_current; FileSystem::FileWatcher m_watcher; // last: stops first on dtor }; } // namespace Void::Configuration