Files
faumaray 24075fbd52 Update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-07-15 19:50:07 +03:00

95 lines
2.8 KiB
C++

#pragma once
#include "ConfigTypes.hpp"
#include <atomic>
#include <filesystem>
#include <functional>
#include <mutex>
#include <optional>
#include <shared_mutex>
namespace Echoion::Api
{
class ConfigManager
{
public:
using ConfigChangedCallback = std::function<void(const AppConfig&)>;
// Owned by Core and provided through the service registry — no
// global singleton.
ConfigManager() = default;
// Delete copy/move
ConfigManager(const ConfigManager&) = delete;
ConfigManager& operator=(const ConfigManager&) = delete;
// Initialize with config file path (call once at startup)
[[nodiscard]] bool
initialize(const std::filesystem::path& configPath);
// Initialize with default path (~/.config/echoion/config.json)
[[nodiscard]] bool initialize();
// Get current config (thread-safe)
[[nodiscard]] AppConfig getConfig() const;
// Get config by reference (faster, but hold lock briefly)
template <typename Func>
auto withConfig(Func&& func) const
-> decltype(func(std::declval<const AppConfig&>()))
{
std::shared_lock lock(mutex_);
return func(config_);
}
// Direct accessors for config sections (convenience methods)
[[nodiscard]] const BotConfig& getBot() const;
[[nodiscard]] const YouTubeConfig& getYouTube() const;
[[nodiscard]] const NetworkConfig& getNetwork() const;
[[nodiscard]] const SourcesConfig& getSources() const;
[[nodiscard]] const TeamSpeakConfig& getTeamSpeak() const;
[[nodiscard]] const SecurityConfig& getSecurity() const;
[[nodiscard]] const FeaturesConfig& getFeatures() const;
[[nodiscard]] const AdvancedConfig& getAdvanced() const;
// Reload configuration from file
[[nodiscard]] bool reload();
// Save current configuration to file
[[nodiscard]] bool save();
// Update config (and optionally save)
void updateConfig(const AppConfig& config, bool saveToFile = true);
// Check if initialized
[[nodiscard]] bool isInitialized() const;
// Get config file path
[[nodiscard]] std::filesystem::path getConfigPath() const;
// Get default config path (~/.config/echoion/config.json)
[[nodiscard]] static std::filesystem::path getDefaultConfigPath();
// Subscribe to config changes
void onConfigChanged(ConfigChangedCallback callback);
// Create default config file if it doesn't exist
[[nodiscard]] bool createDefaultConfigIfNeeded();
private:
mutable std::shared_mutex mutex_;
AppConfig config_;
std::filesystem::path configPath_;
std::atomic<bool> initialized_{false};
std::vector<ConfigChangedCallback> callbacks_;
mutable std::shared_mutex callbackMutex_;
void notifyCallbacks();
[[nodiscard]] bool loadFromFileInternal();
[[nodiscard]] bool saveToFileInternal();
};
} // namespace Echoion::Api