src/core/config/Configuration.hpp
Core object of the configuration system. More…
Namespaces
Name |
---|
allpix Helper class to hold support layers for a detector model. |
Classes
Name | |
---|---|
class | allpix::Configuration Generic configuration object storing keys. |
Types
Name | |
---|---|
template <typename T > using std::vector< std::vector< T > > |
Matrix |
Detailed Description
Core object of the configuration system.
Copyright: Copyright (c) 2017-2024 CERN and the Allpix Squared authors. This software is distributed under the terms of the MIT License, copied verbatim in the file “LICENSE.md”. In applying this license, CERN does not waive the privileges and immunities granted to it by virtue of its status as an Intergovernmental Organization or submit itself to any jurisdiction. SPDX-License-Identifier: MIT
Types Documentation
using Matrix
template <typename T >
using allpix::Matrix = typedef std::vector<std::vector<T> >;
Source code
#ifndef ALLPIX_CONFIGURATION_H
#define ALLPIX_CONFIGURATION_H
#include <atomic>
#include <filesystem>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <unordered_set>
#include <vector>
#include "core/config/exceptions.h"
#include "core/utils/text.h"
namespace allpix {
template <typename T> using Matrix = std::vector<std::vector<T>>;
class Configuration {
class AccessMarker {
public:
AccessMarker() = default;
AccessMarker(const AccessMarker& rhs);
AccessMarker& operator=(const AccessMarker& rhs);
void registerMarker(const std::string& key);
void markUsed(const std::string& key) { markers_.at(key).store(true); };
bool isUsed(const std::string& key) { return markers_.at(key).load(); }
private:
std::map<std::string, std::atomic<bool>> markers_;
};
public:
explicit Configuration(std::string name = "", std::filesystem::path path = "");
Configuration(const Configuration&) = default;
Configuration& operator=(const Configuration&) = default;
Configuration(Configuration&&) noexcept = default; // NOLINT
Configuration& operator=(Configuration&&) = default;
bool has(const std::string& key) const;
unsigned int count(std::initializer_list<std::string> keys) const;
template <typename T> T get(const std::string& key) const;
template <typename T> T get(const std::string& key, const T& def) const;
// TODO [doc] Provide second template parameter to specify the vector type to return it in
template <typename T> std::vector<T> getArray(const std::string& key) const;
// TODO [doc] Provide second template parameter to specify the vector type to return it in
template <typename T> std::vector<T> getArray(const std::string& key, const std::vector<T> def) const;
template <typename T> Matrix<T> getMatrix(const std::string& key) const;
template <typename T> Matrix<T> getMatrix(const std::string& key, const Matrix<T> def) const;
std::string getText(const std::string& key) const;
std::string getText(const std::string& key, const std::string& def) const;
std::filesystem::path getPath(const std::string& key, bool check_exists = false) const;
std::filesystem::path
getPathWithExtension(const std::string& key, const std::string& extension, bool check_exists) const;
// TODO [doc] Provide second template parameter to specify the vector type to return it in
std::vector<std::filesystem::path> getPathArray(const std::string& key, bool check_exists = false) const;
template <typename T> void set(const std::string& key, const T& val, bool mark_used = false);
template <typename T> void set(const std::string& key, const T& val, std::initializer_list<std::string> units);
// TODO [doc] Provide second template parameter to specify the vector type to return it in
template <typename T> void setArray(const std::string& key, const std::vector<T>& val, bool mark_used = false);
template <typename T> void setMatrix(const std::string& key, const Matrix<T>& val);
template <typename T> void setDefault(const std::string& key, const T& val);
template <typename T> void setDefaultArray(const std::string& key, const std::vector<T>& val);
void setText(const std::string& key, const std::string& val);
void setAlias(const std::string& new_key, const std::string& old_key, bool warn = false);
unsigned int countSettings() const { return static_cast<unsigned int>(config_.size()); }
const std::string& getName() const { return name_; }
std::filesystem::path getFilePath() const { return path_; }
void merge(const Configuration& other);
// FIXME Better name for this function
std::vector<std::pair<std::string, std::string>> getAll() const;
std::vector<std::string> getUnusedKeys() const;
private:
std::filesystem::path path_to_absolute(std::filesystem::path path, bool canonicalize_path) const;
struct parse_node {
std::string value;
std::vector<std::unique_ptr<parse_node>> children;
};
static std::unique_ptr<parse_node> parse_value(std::string str, int depth = 0);
std::string name_;
std::filesystem::path path_;
using ConfigMap = std::map<std::string, std::string>;
ConfigMap config_;
mutable AccessMarker used_keys_;
};
} // namespace allpix
// Include template members
#include "Configuration.tpp"
#endif /* ALLPIX_CONFIGURATION_H */
Updated on 2024-12-13 at 08:31:37 +0000