On this page
article
peary/config/configuration.hpp
peary/config/configuration.hpp
Caribou configuration class used by the Caribou libraries. More…
Namespaces
| Name |
|---|
| peary |
| peary::config |
Classes
| Name | |
|---|---|
| class | peary::config::ConfigParser Simple parser class for TOML-like configurations consisting of sections indicated by […] and key-value pairs. |
| class | peary::config::Configuration Configuration class holding key-value pairs of configuration parameters. |
Detailed Description
Caribou configuration class used by the Caribou libraries.
Copyright: Copyright (c) 2016-2025 CERN and the Peary Caribou authors. This software is distributed under the terms of the LGPL-3.0-only License, copied verbatim in the file “LICENSE.md”. SPDX-License-Identifier: LGPL-3.0-only
Source code
#pragma once
#include <iomanip>
#include <map>
#include <string>
#include <vector>
#include "peary/config/exceptions.hpp"
#include "peary/utils/utils.hpp"
namespace peary::config {
class Configuration;
class ConfigParser {
public:
ConfigParser() = default;
explicit ConfigParser(const std::string& path);
explicit ConfigParser(std::istream& file);
bool HasSection(const std::string& section) const;
Configuration GetConfig(const std::string& section) const;
Configuration GetMainConfig() const;
std::map<std::string, Configuration> GetAllConfigs() const { return configs_; };
void Save(std::ostream& file) const;
std::vector<std::string> GetSections() const;
private:
void Load(std::istream& file);
// Map of section names to Configuration objects
std::map<std::string, Configuration> configs_;
};
class Configuration {
public:
Configuration() = default;
Configuration(const std::string& section_name) : section_name_(std::move(section_name)) {}
Configuration(const Configuration& other);
void Add(const std::string& key, const std::string& value);
std::string operator[](const std::string& key) const { return GetKeyString(key); }
bool Has(const std::string& key) const;
std::string Get(const std::string& key, const std::string& def) const;
double Get(const std::string& key, double def) const;
int64_t Get(const std::string& key, int64_t def) const;
uint64_t Get(const std::string& key, uint64_t def) const;
template <typename T> T Get(const std::string& key) const { return utils::from_string<T>(GetKeyString(key)); }
template <typename T> T Get(const std::string& key, T def) const {
return utils::from_string<T>(Get(key, utils::to_string(def)));
}
template <typename T> std::vector<T> Get(const std::string& key, std::vector<T> def) const {
return split(Get(key, std::string()), def, ',');
}
int Get(const std::string& key, int def) const;
template <typename T> T Get(const std::string& key, const std::string fallback, const T& def) const {
return Get(key, Get(fallback, def));
}
std::string Get(const std::string& key, const char* def) const {
std::string ret(Get(key, std::string(def)));
return ret;
}
std::string Get(const std::string& key, const std::string& fallback, const std::string& def) const {
return Get(key, Get(fallback, def));
}
template <typename T> void Set(const std::string& key, const T& val);
bool empty() const { return dictionary_.empty(); }
std::string Section() const { return section_name_; }
Configuration& operator=(const Configuration& other);
void Print(std::ostream& out) const;
void Print() const;
private:
friend std::ostream& operator<<(std::ostream& os, const Configuration& c);
std::string GetKeyString(const std::string& key) const;
void SetKeyString(const std::string& key, const std::string& val);
// Map of key-value pairs
std::map<std::string, std::string> dictionary_;
// Name of the section this configuration belongs to
std::string section_name_;
};
inline std::ostream& operator<<(std::ostream& os, const Configuration& c) {
for(const auto& j : c.dictionary_) {
os << j.first << " = " << j.second << "\n";
}
return os;
}
inline std::ostream& operator<<(std::ostream& os, const ConfigParser& c) {
c.Save(os);
return os;
}
template <typename T> inline void Configuration::Set(const std::string& key, const T& val) {
SetKeyString(key, utils::to_string(val));
}
} // namespace peary::config
Updated on 2025-11-14 at 11:31:23 +0100