config.hpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #pragma once
  2. #include <string>
  3. #include <unordered_map>
  4. #include <variant>
  5. #include <vector>
  6. namespace lumacs {
  7. /// Configuration value type
  8. using ConfigValue = std::variant<bool, int, std::string>;
  9. /// Global configuration manager
  10. class Config {
  11. public:
  12. Config();
  13. /// Set a configuration value
  14. void set(const std::string& key, const ConfigValue& value);
  15. /// Get a configuration value
  16. template<typename T>
  17. T get(const std::string& key, const T& default_value = T{}) const {
  18. auto it = values_.find(key);
  19. if (it == values_.end()) {
  20. return default_value;
  21. }
  22. try {
  23. return std::get<T>(it->second);
  24. } catch (const std::bad_variant_access&) {
  25. return default_value;
  26. }
  27. }
  28. /// Check if a key exists
  29. bool has(const std::string& key) const;
  30. /// Get all keys
  31. std::vector<std::string> keys() const;
  32. private:
  33. std::unordered_map<std::string, ConfigValue> values_;
  34. void set_defaults();
  35. };
  36. } // namespace lumacs