Branch data Line data Source code
1 : : /*
2 : : * Copyright 2025 Jinwoo Sung
3 : : *
4 : : * Licensed under the Apache License, Version 2.0 (the "License");
5 : : * you may not use this file except in compliance with the License.
6 : : * You may obtain a copy of the License at
7 : : *
8 : : * http://www.apache.org/licenses/LICENSE-2.0
9 : : *
10 : : * Unless required by applicable law or agreed to in writing, software
11 : : * distributed under the License is distributed on an "AS IS" BASIS,
12 : : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 : : * See the License for the specific language governing permissions and
14 : : * limitations under the License.
15 : : */
16 : :
17 : : #pragma once
18 : :
19 : : #include <any>
20 : : #include <functional>
21 : : #include <memory>
22 : : #include <string>
23 : : #include <unordered_map>
24 : : #include <vector>
25 : :
26 : : #include "wirestead/base/visibility.hpp"
27 : :
28 : : namespace wirestead {
29 : : namespace config {
30 : :
31 : : /**
32 : : * Configuration value types supported by the system
33 : : */
34 : : enum class ConfigType { String, Integer, Boolean, Double, Array, Object };
35 : :
36 : : /**
37 : : * Configuration validation result
38 : : */
39 : : struct ValidationResult {
40 : : bool is_valid;
41 : : std::string error_message;
42 : :
43 : 2548 : explicit ValidationResult(bool valid = true, const std::string& error = "") : is_valid(valid), error_message(error) {}
44 : :
45 : 7614 : static ValidationResult success() { return ValidationResult(true); }
46 : 10 : static ValidationResult error(const std::string& msg) { return ValidationResult(false, msg); }
47 : : };
48 : :
49 : : /**
50 : : * Configuration item definition
51 : : */
52 : : struct ConfigItem {
53 : : std::string key;
54 : : std::any value;
55 : : ConfigType type;
56 : : bool required;
57 : : std::string description;
58 : : std::function<ValidationResult(const std::any&)> validator;
59 : :
60 : : // Default constructor
61 : 6425 : ConfigItem() : key(""), value(std::any()), type(ConfigType::String), required(false), description("") {}
62 : :
63 : 1285 : ConfigItem(const std::string& k, const std::any& v, ConfigType t, bool req = false, const std::string& desc = "")
64 : 1285 : : key(k), value(v), type(t), required(req), description(desc) {}
65 : : };
66 : :
67 : : /**
68 : : * Configuration change callback
69 : : */
70 : : using ConfigChangeCallback =
71 : : std::function<void(const std::string& key, const std::any& old_value, const std::any& new_value)>;
72 : :
73 : : /**
74 : : * Abstract interface for configuration management
75 : : */
76 : : class WIRESTEAD_API ConfigManagerInterface {
77 : : public:
78 : 44 : virtual ~ConfigManagerInterface() = default;
79 : :
80 : : // Configuration access
81 : : virtual std::any get(const std::string& key) const = 0;
82 : : virtual std::any get(const std::string& key, const std::any& default_value) const = 0;
83 : : virtual bool has(const std::string& key) const = 0;
84 : :
85 : : // Configuration modification
86 : : virtual ValidationResult set(const std::string& key, const std::any& value) = 0;
87 : : virtual bool remove(const std::string& key) = 0;
88 : : virtual void clear() = 0;
89 : :
90 : : // Configuration validation
91 : : virtual ValidationResult validate() const = 0;
92 : : virtual ValidationResult validate(const std::string& key) const = 0;
93 : :
94 : : // Configuration registration
95 : : virtual void register_item(const ConfigItem& item) = 0;
96 : : virtual void register_validator(const std::string& key,
97 : : std::function<ValidationResult(const std::any&)> validator) = 0;
98 : :
99 : : // Change notifications
100 : : virtual void on_change(const std::string& key, ConfigChangeCallback callback) = 0;
101 : : virtual void remove_change_callback(const std::string& key) = 0;
102 : :
103 : : // Configuration persistence
104 : : virtual bool save_to_file(const std::string& filepath) const = 0;
105 : : virtual bool load_from_file(const std::string& filepath) = 0;
106 : :
107 : : // Configuration introspection
108 : : virtual std::vector<std::string> get_keys() const = 0;
109 : : virtual ConfigType get_type(const std::string& key) const = 0;
110 : : virtual std::string get_description(const std::string& key) const = 0;
111 : : virtual bool is_required(const std::string& key) const = 0;
112 : : };
113 : :
114 : : } // namespace config
115 : : } // namespace wirestead
|