|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <filesystem> |
| 5 | +#include <optional> |
| 6 | +#include <stdexcept> |
| 7 | +#include <string> |
| 8 | +#include <variant> |
| 9 | + |
| 10 | +#include "GenericConfiguration.h" |
| 11 | +#include "JsonSerializer.h" |
| 12 | +#include "Setting.h" |
| 13 | + |
| 14 | +namespace example { |
| 15 | + |
| 16 | +// Define our configuration enum |
| 17 | +enum class AppConfigName : uint8_t { |
| 18 | + DatabaseUrl, |
| 19 | + MaxConnections, |
| 20 | + EnableLogging, |
| 21 | + RetryCount, |
| 22 | + LogLevel |
| 23 | +}; |
| 24 | + |
| 25 | +// Implement the toString and fromString functions for the enum |
| 26 | +// (These are required by JsonSerializer) |
| 27 | +inline std::string ToString(AppConfigName name) |
| 28 | +{ |
| 29 | + switch (name) { |
| 30 | + case AppConfigName::DatabaseUrl: |
| 31 | + return "database_url"; |
| 32 | + case AppConfigName::MaxConnections: |
| 33 | + return "max_connections"; |
| 34 | + case AppConfigName::EnableLogging: |
| 35 | + return "enable_logging"; |
| 36 | + case AppConfigName::RetryCount: |
| 37 | + return "retry_count"; |
| 38 | + case AppConfigName::LogLevel: |
| 39 | + return "log_level"; |
| 40 | + default: |
| 41 | + return "unknown"; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +inline AppConfigName FromString(const std::string& str) |
| 46 | +{ |
| 47 | + if (str == "database_url") { |
| 48 | + return AppConfigName::DatabaseUrl; |
| 49 | + } |
| 50 | + if (str == "max_connections") { |
| 51 | + return AppConfigName::MaxConnections; |
| 52 | + } |
| 53 | + if (str == "enable_logging") { |
| 54 | + return AppConfigName::EnableLogging; |
| 55 | + } |
| 56 | + if (str == "retry_count") { |
| 57 | + return AppConfigName::RetryCount; |
| 58 | + } |
| 59 | + if (str == "log_level") { |
| 60 | + return AppConfigName::LogLevel; |
| 61 | + } |
| 62 | + throw std::runtime_error("Invalid configuration name: " + str); |
| 63 | +} |
| 64 | + |
| 65 | +} // namespace example |
| 66 | + |
| 67 | +// Provide template specializations for the JsonSerializer |
| 68 | +namespace config { |
| 69 | +template <> |
| 70 | +inline std::string JsonSerializer<example::AppConfigName>::ToString(example::AppConfigName enumValue) |
| 71 | +{ |
| 72 | + return example::ToString(enumValue); |
| 73 | +} |
| 74 | + |
| 75 | +template <> |
| 76 | +inline example::AppConfigName JsonSerializer<example::AppConfigName>::FromString(const std::string& str) |
| 77 | +{ |
| 78 | + return example::FromString(str); |
| 79 | +} |
| 80 | +} // namespace config |
| 81 | + |
| 82 | +namespace example { |
| 83 | + |
| 84 | +// Define the configuration type |
| 85 | +using AppConfig = config::GenericConfiguration<AppConfigName, config::JsonSerializer<AppConfigName>>; |
| 86 | + |
| 87 | +// Define the variant type for AppConfig settings |
| 88 | +using AppSettingVariant = std::variant< |
| 89 | + config::Setting<AppConfigName, int>, |
| 90 | + config::Setting<AppConfigName, float>, |
| 91 | + config::Setting<AppConfigName, double>, |
| 92 | + config::Setting<AppConfigName, std::string>, |
| 93 | + config::Setting<AppConfigName, bool>>; |
| 94 | + |
| 95 | +// Define default configuration values |
| 96 | +inline const AppConfig::DefaultConfigMap DefaultAppConfig = { |
| 97 | + { AppConfigName::DatabaseUrl, |
| 98 | + config::Setting<AppConfigName, std::string>( |
| 99 | + AppConfigName::DatabaseUrl, |
| 100 | + "mongodb://localhost:27017", |
| 101 | + std::nullopt, |
| 102 | + std::nullopt, |
| 103 | + std::nullopt, |
| 104 | + "URL for database connection") }, |
| 105 | + { AppConfigName::MaxConnections, |
| 106 | + config::Setting<AppConfigName, int>( |
| 107 | + AppConfigName::MaxConnections, |
| 108 | + 100, |
| 109 | + 1000, |
| 110 | + 1, |
| 111 | + "connections", |
| 112 | + "Maximum number of database connections") }, |
| 113 | + { AppConfigName::EnableLogging, |
| 114 | + config::Setting<AppConfigName, bool>( |
| 115 | + AppConfigName::EnableLogging, |
| 116 | + true, |
| 117 | + std::nullopt, |
| 118 | + std::nullopt, |
| 119 | + std::nullopt, |
| 120 | + "Enable application logging") }, |
| 121 | + { AppConfigName::RetryCount, |
| 122 | + config::Setting<AppConfigName, int>( |
| 123 | + AppConfigName::RetryCount, |
| 124 | + 3, |
| 125 | + 10, |
| 126 | + 0, |
| 127 | + "retries", |
| 128 | + "Number of retry attempts for operations") }, |
| 129 | + { AppConfigName::LogLevel, |
| 130 | + config::Setting<AppConfigName, std::string>( |
| 131 | + AppConfigName::LogLevel, |
| 132 | + "info", |
| 133 | + std::nullopt, |
| 134 | + std::nullopt, |
| 135 | + std::nullopt, |
| 136 | + "Logging level (debug, info, warning, error)") } |
| 137 | +}; |
| 138 | + |
| 139 | +// Example of how to use the configuration |
| 140 | +class Application { |
| 141 | +public: |
| 142 | + explicit Application(const std::filesystem::path& config_path) |
| 143 | + : config_(config_path, DefaultAppConfig) |
| 144 | + { |
| 145 | + // Access configuration values in a type-safe way with the new API |
| 146 | + auto db_url = config_.GetSetting(AppConfigName::DatabaseUrl).Value<std::string>(); |
| 147 | + auto max_conn = config_.GetSetting(AppConfigName::MaxConnections).Value<int>(); |
| 148 | + auto logging_enabled = config_.GetSetting(AppConfigName::EnableLogging).Value<bool>(); |
| 149 | + |
| 150 | + // Example of updating a setting |
| 151 | + config_.UpdateSetting(AppConfigName::RetryCount, 5); |
| 152 | + config_.Save(); |
| 153 | + } |
| 154 | + |
| 155 | + // Get the underlying config object |
| 156 | + AppConfig& GetConfig() { return config_; } |
| 157 | + |
| 158 | +private: |
| 159 | + AppConfig config_; |
| 160 | +}; |
| 161 | + |
| 162 | +} // namespace example |
0 commit comments