1 #ifndef ABLATELIBRARY_PARAMETERS_HPP
2 #define ABLATELIBRARY_PARAMETERS_HPP
9 #include <unordered_set>
11 #include "parameterException.hpp"
13 namespace ablate::parameters {
18 static void toValue(
const std::string& inputString, T& outputValue) {
19 std::istringstream ss(inputString);
24 static void toValue(
const std::string& inputString, std::vector<T>& outputValue) {
25 std::istringstream ss(inputString);
27 while (ss >> tempValue) {
28 outputValue.push_back(tempValue);
32 template <
typename T, std::
size_t N>
33 static void toValue(
const std::string& inputString, std::array<T, N>& outputValue) {
34 std::istringstream ss(inputString);
36 std::size_t index = 0;
39 outputValue.fill(defaultValue);
41 while (ss >> tempValue && index < N) {
42 outputValue[index] = tempValue;
48 static void toValue(
const std::string& inputString,
bool& outputValue);
53 virtual std::optional<std::string> GetString(std::string paramName)
const = 0;
54 virtual std::unordered_set<std::string> GetKeys()
const = 0;
57 std::optional<T> Get(std::string paramName)
const {
58 auto value = GetString(paramName);
59 if (value.has_value()) {
61 toValue(value.value(), num);
69 T Get(std::string paramName, T defaultValue)
const {
70 auto value = GetString(paramName);
71 if (value.has_value()) {
73 toValue(value.value(), num);
81 T GetExpect(std::string paramName)
const {
82 auto value = GetString(paramName);
83 if (value.has_value()) {
85 toValue(value.value(), num);
99 std::map<std::string, T>
ToMap()
const {
100 std::map<std::string, T> map;
101 for (
const auto& key : GetKeys()) {
102 map[key] = GetExpect<T>(key);
107 void Fill(PetscOptions options)
const;
109 template <
typename T>
110 void Fill(
int numberValues,
const char*
const* valueNames, T* constantArray)
const {
112 for (
int n = 0; n < numberValues; n++) {
114 auto stringName = std::string(valueNames[n]);
117 constantArray[n] = GetExpect<T>(stringName);
121 template <
typename T>
122 void Fill(
int numberValues,
const char*
const* valueNames, T* constantArray, std::map<std::string, T> defaultValues)
const {
124 for (
int n = 0; n < numberValues; n++) {
126 auto stringName = std::string(valueNames[n]);
129 if (defaultValues.count(stringName)) {
130 constantArray[n] = Get<T>(stringName, defaultValues[stringName]);
133 constantArray[n] = GetExpect<T>(stringName);
Definition: parameters.hpp:15
std::map< std::string, T > ToMap() const
Definition: parameters.hpp:99
Definition: parameterException.hpp:6