MAIA bb96820c
Multiphysics at AIA
Loading...
Searching...
No Matches
maia::io::toml Namespace Reference

Classes

struct  Enum2Type
 Type traits for enum type. More...
 
struct  Enum2Type< MBOOL >
 
struct  Enum2Type< MFLOAT >
 
struct  Enum2Type< MINT >
 
struct  Enum2Type< MSTRING >
 
class  Property
 Class that represents a single key-value pair for TOML properties. More...
 
struct  TypeTraits
 
struct  TypeTraits< MBool >
 
struct  TypeTraits< MFloat >
 
struct  TypeTraits< MInt >
 
struct  TypeTraits< MString >
 

Functions

VariableType value2type (const std::shared_ptr< cpptoml::base > &value)
 Obtain type information from TOML value. More...
 
Property makeProperty (const MString &name, const std::shared_ptr< cpptoml::base > &item)
 Create property from cpptoml item. More...
 
void collectSolverAliases (const std::shared_ptr< cpptoml::table > &tab, std::map< std::string, int > &solverAliases)
 Non-recursive search for solver aliases. More...
 
void collectProperties (const std::shared_ptr< cpptoml::table > &tab, std::vector< std::string > &names, std::vector< Property > &properties, std::map< std::string, int > &solverAliases)
 Recursively traverse TOML table and collect all properties with name, type, and count. More...
 
void collectProperties (const std::shared_ptr< cpptoml::table > &tab, std::vector< Property > &properties, std::map< std::string, int > &solverAliases)
 Helper function to not have to provide a names array oneself. More...
 
void collectProperties (const std::shared_ptr< cpptoml::table > &tab, std::vector< Property > &properties)
 Helper function to not have to provide a names array oneself. More...
 

Function Documentation

◆ collectProperties() [1/3]

void maia::io::toml::collectProperties ( const std::shared_ptr< cpptoml::table > &  tab,
std::vector< Property > &  properties 
)
inline

Definition at line 359 of file tomlutils.h.

359 {
360 std::vector<std::string> names;
361 std::map<std::string, int> aliasesdummy;
362 collectProperties(tab, names, properties, aliasesdummy);
363}
void collectProperties(const std::shared_ptr< cpptoml::table > &tab, std::vector< std::string > &names, std::vector< Property > &properties, std::map< std::string, int > &solverAliases)
Recursively traverse TOML table and collect all properties with name, type, and count.
Definition: tomlutils.h:256

◆ collectProperties() [2/3]

void maia::io::toml::collectProperties ( const std::shared_ptr< cpptoml::table > &  tab,
std::vector< Property > &  properties,
std::map< std::string, int > &  solverAliases 
)
inline

Definition at line 352 of file tomlutils.h.

353 {
354 std::vector<std::string> names;
355 collectProperties(tab, names, properties, solverAliases);
356}

◆ collectProperties() [3/3]

void maia::io::toml::collectProperties ( const std::shared_ptr< cpptoml::table > &  tab,
std::vector< std::string > &  names,
std::vector< Property > &  properties,
std::map< std::string, int > &  solverAliases 
)
inline

Definition at line 256 of file tomlutils.h.

259 {
260 // // TODO labels:IO,toremove remove
261 // for (auto&& item : *tab) {
262 // // Store key, value for convenience
263 // auto&& key = item.first;
264 // auto&& value = item.second;
265 // std::cerr << "key " << key << " value " << value << std::endl;
266 // }
267
268 // Iterate over table
269 for(auto&& item : *tab) {
270 // Store key, value for convenience
271 auto&& key = item.first;
272 auto&& value = item.second;
273 std::stringstream key_ss;
274 key_ss << key;
275
276 if(key_ss.str() == "default") {
277 // if default is the key we just add name to properties
278 properties.emplace_back(makeProperty(names[0], value));
279 } else if(cpptoml::is_number(key_ss.str().c_str()[0]) && !value->is_table()) {
280 std::stringstream k;
281 k << names[0] << "." << (std::atoi(key.c_str()));
282 properties.emplace_back(makeProperty(k.str(), value));
283 } else if(std::find_if(solverAliases.begin(), solverAliases.end(),
284 [&key_ss, &solverAliases](std::pair<const std::string, int>& entry) {
285 return !solverAliases.empty() && (entry.first == key_ss.str());
286 })
287 != solverAliases.end()
288 && !names.empty()) {
289 std::stringstream k;
290 k << names[0] << "." << solverAliases[key_ss.str()];
291 properties.emplace_back(makeProperty(k.str(), value));
292 } else if(value->is_value() || value->is_array()) {
293 // Determine fully qualified name
294 std::stringstream solverId;
295 std::stringstream k;
296 MInt counter = 0;
297 for(auto&& name : names) {
298 solverId << name << ".";
299 counter++;
300 }
301
302 if(solverId.str().empty()) {
303 k << key;
304 } else {
305 MString subString1 = solverId.str().substr(0, solverId.str().size() - 1);
306 MString subString2 = solverId.str().substr(0, solverId.str().find('.'));
307 if(subString2 == "solver" || subString1 == "solver") {
308 if(strstr(key.c_str(), ".") != nullptr) {
309 std::stringstream error;
310 error << "Setting a solverId inside the solver table environment '" << subString1 << "' is prohibited!!! ";
311 TERMM(1, error.str());
312 }
313 switch(counter) {
314 case 1: {
315 k << key;
316 break;
317 }
318 case 2: {
319 const char* du = std::strrchr(subString1.c_str(), '.') + 1;
320 const MInt singleSolverId = std::atoi(du);
321 k << key << "." << singleSolverId;
322 break;
323 }
324 default: {
325 std::stringstream error;
326 error << "Too many nested tables: " << subString1 << " Please use 'solver.solverId' instead!!!";
327 TERMM(1, error.str());
328 }
329 }
330 } else {
331 std::stringstream error;
332 error << "Unknown table type: " << subString2 << " Please use 'solver' instead!!!";
333 TERMM(1, error.str());
334 }
335 }
336
337 // Add info to collected properties
338 properties.emplace_back(makeProperty(k.str(), value));
339 } else if(value->is_table()) {
340 // If value is a table, add key to list of names and descend one recursion level
341 names.push_back(key);
342 collectProperties(value->as_table(), names, properties, solverAliases);
343 names.pop_back();
344 } else {
345 TERMM(1, "only values, arrays, and tables are supported");
346 }
347 }
348}
int32_t MInt
Definition: maiatypes.h:62
std::basic_string< char > MString
Definition: maiatypes.h:55
MBool is_number(char c)
Definition: cpptoml.h:1471
Property makeProperty(const MString &name, const std::shared_ptr< cpptoml::base > &item)
Create property from cpptoml item.
Definition: tomlutils.h:158

◆ collectSolverAliases()

void maia::io::toml::collectSolverAliases ( const std::shared_ptr< cpptoml::table > &  tab,
std::map< std::string, int > &  solverAliases 
)
inline

Definition at line 230 of file tomlutils.h.

231 {
232 // search for key "solverAlias"
233 for(auto&& item : *tab) {
234 auto&& key = item.first;
235 if(key == "solverAlias") {
236 auto&& solverAliasesTable = item.second;
237 // iterate over all items in solverAlias table
238 if(solverAliasesTable->is_table()) {
239 for(auto&& alias : *solverAliasesTable->as_table()) {
240 // check if solverAlias is given as string
241 if(value2type(alias.second) == MSTRING) {
242 // store solver alias in map (solverAlias <string> -> solverid <int> )
243 solverAliases[alias.second->as<std::string>()->get()] = std::stoi(alias.first);
244 } else {
245 TERMM(1, "solverAlias need to be defined as string!");
246 }
247 }
248 } else {
249 TERMM(1, "solverAlias needs to be in table format: solverAlias.solverId = 'alias'!");
250 }
251 }
252 }
253}
@ MSTRING
Definition: enums.h:269
VariableType value2type(const std::shared_ptr< cpptoml::base > &value)
Obtain type information from TOML value.
Definition: tomlutils.h:142

◆ makeProperty()

Property maia::io::toml::makeProperty ( const MString name,
const std::shared_ptr< cpptoml::base > &  item 
)
inline

Definition at line 158 of file tomlutils.h.

158 {
159 if(item->is_value()) {
160 switch(value2type(item)) {
161 case MSTRING: {
162 std::vector<MString> data(1);
163 data[0] = item->as<std::string>()->get();
164 return Property(name, data);
165 }
166 case MINT: {
167 std::vector<MInt> data(1);
168#if defined(MAIA_GCC_COMPILER)
169#pragma GCC diagnostic push
170#pragma GCC diagnostic ignored "-Wnull-dereference"
171#endif
172 data[0] = static_cast<MInt>(item->as<int64_t>()->get());
173#if defined(MAIA_GCC_COMPILER)
174#pragma GCC diagnostic pop
175#endif
176 return Property(name, data);
177 }
178 case MFLOAT: {
179 std::vector<MFloat> data(1);
180 data[0] = item->as<double>()->get();
181 return Property(name, data);
182 }
183 case MBOOL: {
184 std::vector<MBool> data(1);
185 data[0] = item->as<bool>()->get();
186 return Property(name, data);
187 }
188 default:
189 TERMM(1, "bad value type");
190 }
191 } else if(item->is_array() || item->is_table()) {
192 if(item->as_array()->get().empty()) {
193 TERMM(1, "array of size 0 found, cannot detect type");
194 }
195
196 switch(value2type(item->as_array()->at(0))) {
197 case MSTRING: {
198 auto v = *item->as_array()->get_array_of<std::string>();
199 std::vector<MString> data(v.size());
200 copy(v.begin(), v.end(), data.begin());
201 return Property(name, data);
202 }
203 case MINT: {
204 auto v = *item->as_array()->get_array_of<MLong>();
205 std::vector<MInt> data(v.size());
206 copy(v.begin(), v.end(), data.begin());
207 return Property(name, data);
208 }
209 case MFLOAT: {
210 auto v = *item->as_array()->get_array_of<double>();
211 std::vector<MFloat> data(v.size());
212 copy(v.begin(), v.end(), data.begin());
213 return Property(name, data);
214 }
215 case MBOOL: {
216 auto v = *item->as_array()->get_array_of<bool>();
217 std::vector<MBool> data(v.size());
218 copy(v.begin(), v.end(), data.begin());
219 return Property(name, data);
220 }
221 default:
222 TERMM(1, "bad value type");
223 }
224 } else {
225 TERMM(1, "item is not a value or array");
226 }
227}
Class that represents a single key-value pair for TOML properties.
Definition: tomlutils.h:69
@ MINT
Definition: enums.h:269
@ MFLOAT
Definition: enums.h:269
@ MBOOL
Definition: enums.h:269
int64_t MLong
Definition: maiatypes.h:64

◆ value2type()

VariableType maia::io::toml::value2type ( const std::shared_ptr< cpptoml::base > &  value)
inline

Definition at line 142 of file tomlutils.h.

142 {
143 if(value->as<std::string>()) {
144 return MSTRING;
145 } else if(value->as<int64_t>()) {
146 return MINT;
147 } else if(value->as<double>()) {
148 return MFLOAT;
149 } else if(value->as<bool>()) {
150 return MBOOL;
151 } else {
152 TERMM(1, "bad value type");
153 }
154}