3#include "util/Assert.hpp"
4#include "util/config/Array.hpp"
5#include "util/config/ConfigConstraints.hpp"
6#include "util/config/ConfigFileInterface.hpp"
7#include "util/config/ConfigValue.hpp"
8#include "util/config/Error.hpp"
9#include "util/config/ObjectView.hpp"
10#include "util/config/ValueView.hpp"
15#include <initializer_list>
19#include <unordered_map>
24namespace util::config {
33 using KeyValuePair = std::pair<std::string_view, std::variant<ConfigValue, Array>>;
54 [[nodiscard]] std::optional<std::vector<Error>>
65 getObject(std::string_view prefix, std::optional<std::size_t> idx = std::nullopt)
const;
85 get(std::string_view fullKey)
const
87 ASSERT(map_.contains(fullKey),
"key {} does not exist in config", fullKey);
88 auto const val = map_.at(fullKey);
89 if (std::holds_alternative<ConfigValue>(val)) {
112 getArray(std::string_view prefix)
const;
121 contains(std::string_view key)
const;
138 [[nodiscard]]
Array const&
139 asArray(std::string_view key)
const;
147 [[nodiscard]] std::size_t
148 arraySize(std::string_view prefix)
const;
156 static std::chrono::milliseconds
166 template <
typename T>
203 getArrayIterator(std::string_view key)
const
205 auto const fullKey = addBracketsForArrayKey(key);
207 std::ranges::find_if(map_, [&fullKey](
auto pair) {
return pair.first == fullKey; });
209 ASSERT(it != map_.end(),
"key {} does not exist in config", fullKey);
210 ASSERT(std::holds_alternative<Array>(it->second),
"Value of {} is not an array", fullKey);
221 [[nodiscard]]
static std::string
222 addBracketsForArrayKey(std::string_view key)
224 std::string fullKey = std::string(key);
225 if (!key.contains(
".[]"))
230 std::unordered_map<std::string_view, std::variant<ConfigValue, Array>> map_;
View for array structure for config.
Definition ArrayView.hpp:23
Array definition to store multiple values provided by the user from Json/Yaml.
Definition Array.hpp:22
All the config data will be stored and extracted from this class.
Definition ConfigDefinition.hpp:31
auto begin() const
Returns an iterator to the beginning of the configuration map.
Definition ConfigDefinition.hpp:179
std::size_t arraySize(std::string_view prefix) const
Returns the size of an Array.
Definition ConfigDefinition.cpp:132
ArrayView getArray(std::string_view prefix) const
Returns the specified Array object from ClioConfigDefinition.
Definition ConfigDefinition.cpp:66
static std::chrono::milliseconds toMilliseconds(float value)
Method to convert a float seconds value to milliseconds.
Definition ConfigDefinition.cpp:109
auto end() const
Returns an iterator to the end of the configuration map.
Definition ConfigDefinition.hpp:190
std::optional< std::vector< Error > > parse(ConfigFileInterface const &config)
Parses the configuration file.
Definition ConfigDefinition.cpp:146
bool contains(std::string_view key) const
Checks if a key is present in the configuration map.
Definition ConfigDefinition.cpp:84
ObjectView getObject(std::string_view prefix, std::optional< std::size_t > idx=std::nullopt) const
Returns the ObjectView specified with the prefix.
Definition ConfigDefinition.cpp:44
bool hasItemsWithPrefix(std::string_view key) const
Checks if any key in config starts with "key".
Definition ConfigDefinition.cpp:90
ClioConfigDefinition(std::initializer_list< KeyValuePair > pair)
Constructs a new ClioConfigDefinition.
Definition ConfigDefinition.cpp:34
Array const & asArray(std::string_view key) const
Returns the Array object associated with the specified key.
Definition ConfigDefinition.cpp:125
ValueView getValueView(std::string_view fullKey) const
Returns the specified ValueView object associated with the key.
Definition ConfigDefinition.cpp:98
std::optional< T > maybeValue(std::string_view fullKey) const
Returns the specified value of given string of type T if type and value exists.
Definition ConfigDefinition.hpp:168
T get(std::string_view fullKey) const
Returns the specified value of given string if value exists.
Definition ConfigDefinition.hpp:85
ValueView getValueInArray(std::string_view fullKey, std::size_t index) const
Returns the specified ValueView object in an array with a given index.
Definition ConfigDefinition.cpp:118
The interface for configuration files.
Definition ConfigFileInterface.hpp:18
Provides a view into a subset of configuration data defined by a prefix.
Definition ObjectView.hpp:21
Provides view into ConfigValues that represents values in Clio Config.
Definition ValueView.hpp:27
T getValueImpl() const
Retrieves the stored value as the specified type T.
Definition ValueView.hpp:146
std::optional< T > asOptional() const
Returns an optional value of the specified type T if valid.
Definition ValueView.hpp:177