PolyDiM
C++ library for POLYtopal DIscretization Methods
Loading...
Searching...
No Matches
Configurations.hpp
Go to the documentation of this file.
1// _LICENSE_HEADER_
2//
3// Copyright (C) 2019 - 2025.
4// Terms register on the GPL-3.0 license.
5//
6// This file can be redistributed and/or modified under the license terms.
7//
8// See top level LICENSE file for more details.
9//
10// This file can be used citing references in CITATION.cff file.
11
12#ifndef __CONFIGURATIONS_H
13#define __CONFIGURATIONS_H
14
15#include <fstream>
16#include <string>
17#include <unordered_map>
18
20
21namespace Gedim
22{
24{
25 private:
26 template <class T> struct ConfigurationProperty
27 {
28 std::string Id;
29 std::string Description = "";
30 T Value = T();
31 };
32
33 struct ExportProperty
34 {
35 std::string Description = "";
36 std::string Value;
37 };
38
39 static unsigned int numberProperties;
40 static std::unordered_map<std::string, ConfigurationPropertySupportedTypes::SupportedTypes> propertyTypes;
41 static std::unordered_map<std::string, void *> properties;
42
44 static void RemoveProperty(const std::string &id, const ConfigurationPropertySupportedTypes::SupportedTypes &type);
45
47 static ConfigurationPropertySupportedTypes::SupportedTypes &GetPropertyType(const std::string &id)
48 {
49 return propertyTypes[id];
50 }
51
53 template <class T> static ConfigurationProperty<T> &GetProperty(const std::string &id)
54 {
55 return *static_cast<ConfigurationProperty<T> *>(properties[id]);
56 }
57
59 static ExportProperty GetPropertyForExport(const std::string &id,
61
62 public:
64 {
65 Reset();
66 }
67
68 static const unsigned int &NumberProperties()
69 {
70 return numberProperties;
71 }
72
74 static void InitializeFromCsv(const std::string &ConfigurationsFile, const bool &hasHeader = true, const char &separator = ';');
75
77 static void InitializeFromIni(const std::string &ConfigurationsFile);
78
81 static void Initialize(const int &argc, char **argv, const std::string &format = "id:type=value");
82
84 static void ExportToCsv(const std::string &filePath, const bool &append = false, const char &separator = ';');
85
87 static void ExportToIni(const std::string &filePath, const bool &append = false, const std::string &section = "");
88
90 static void Reset();
91
93 static void RemoveProperty(const std::string &id);
94
96 static bool ExistsProperty(const std::string &id)
97 {
98 return !(propertyTypes.find(id) == propertyTypes.end());
99 }
100
101 template <class T> static bool CheckPropertyType(const std::string &id)
102 {
103 return GetPropertyType(id) == ConfigurationPropertySupportedTypes::GetSupportedType<T>();
104 }
105
107 static void ConvertPropertyFromString(const std::string &id,
108 const std::string &type,
109 const std::string &value,
110 const std::string &description = "");
111
113 template <class T>
114 static void AddProperty(const std::string &id, const T &value = T(), const std::string &description = "")
115 {
116 if (!ConfigurationPropertySupportedTypes::IsSupported<T>())
117 throw std::invalid_argument("Property '" + id + "': type not supported");
118
119 RemoveProperty(id);
120
121 properties.insert(std::pair<std::string, void *>(id, new ConfigurationProperty<T>()));
122 propertyTypes.insert(std::pair<std::string, ConfigurationPropertySupportedTypes::SupportedTypes>(
123 id,
124 ConfigurationPropertySupportedTypes::GetSupportedType<T>()));
125 numberProperties++;
126
128
129 property.Id = id;
130 property.Value = value;
131 property.Description = description;
132 }
133
135 static void AddProperty(const std::string &id, const char *value = "", const std::string &description = "");
136
138 template <class T> static void SetPropertyValue(const std::string &id, const T value)
139 {
140 if (!ExistsProperty(id))
141 throw std::invalid_argument("");
142
143 if (!CheckPropertyType<T>(id))
144 {
145 std::string propertyType = ConfigurationPropertySupportedTypes::TypeToString(GetPropertyType(id));
146 std::string valueType = ConfigurationPropertySupportedTypes::TypeToString<T>();
147 throw std::invalid_argument("Property '" + id + "' has type " + propertyType + " and not " + valueType);
148 }
149
151 property.Value = value;
152 }
153
155 static void SetPropertyValue(const std::string &id, const char *value);
156
158 template <class T> static const T &GetPropertyValue(const std::string &id)
159 {
160 if (!ExistsProperty(id))
161 throw std::invalid_argument("");
162
163 if (!CheckPropertyType<T>(id))
164 {
165 std::string propertyType = ConfigurationPropertySupportedTypes::TypeToString(GetPropertyType(id));
166 std::string valueType = ConfigurationPropertySupportedTypes::TypeToString<T>();
167 throw std::invalid_argument("Property '" + id + "' has type " + propertyType + " and not " + valueType);
168 }
169
170 const ConfigurationProperty<T> &property = GetProperty<T>(id);
171 return property.Value;
172 }
173};
174} // namespace Gedim
175
176#endif // __CONFIGURATIONS_H
SupportedTypes
Definition ConfigurationPropertySupportedTypes.hpp:27
static std::string TypeToString()
Definition ConfigurationPropertySupportedTypes.hpp:83
Definition Configurations.hpp:24
static void Reset()
Reset the Configurations class.
Definition Configurations.cpp:499
static const unsigned int & NumberProperties()
Definition Configurations.hpp:68
~Configurations()
Definition Configurations.hpp:63
static void SetPropertyValue(const std::string &id, const T value)
Set Property Value.
Definition Configurations.hpp:138
static bool ExistsProperty(const std::string &id)
Check if Property exists.
Definition Configurations.hpp:96
static void ConvertPropertyFromString(const std::string &id, const std::string &type, const std::string &value, const std::string &description="")
Convert Property by string Type and string value.
Definition Configurations.cpp:149
static const T & GetPropertyValue(const std::string &id)
Get Property Value.
Definition Configurations.hpp:158
static void Initialize(const int &argc, char **argv, const std::string &format="id:type=value")
Definition Configurations.cpp:384
static void ExportToIni(const std::string &filePath, const bool &append=false, const std::string &section="")
Export the configuration to ini file.
Definition Configurations.cpp:458
static void ExportToCsv(const std::string &filePath, const bool &append=false, const char &separator=';')
Export the configuration to csv file.
Definition Configurations.cpp:417
static void InitializeFromCsv(const std::string &ConfigurationsFile, const bool &hasHeader=true, const char &separator=';')
Read the configuration from csv file.
Definition Configurations.cpp:264
static void AddProperty(const std::string &id, const T &value=T(), const std::string &description="")
Add or Overwrite Property.
Definition Configurations.hpp:114
static void InitializeFromIni(const std::string &ConfigurationsFile)
Read the configuration from ini file.
Definition Configurations.cpp:328
static bool CheckPropertyType(const std::string &id)
Definition Configurations.hpp:101
Eigen column vector.
Definition Eigen_Array.hpp:23
Definition Eigen_Array.cpp:22