helics 3.7.0
Loading...
Searching...
No Matches
TomlProcessingFunctions.hpp
Go to the documentation of this file.
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-License-Identifier: BSD-3-Clause
6*/
7#pragma once
8
14#include "../core/helicsTime.hpp"
15#ifdef __GNUC__
16# pragma GCC diagnostic push
17# pragma GCC diagnostic ignored "-Wshadow"
18#endif
19#include <string_view>
20#define TOML11_USING_STRING_VIEW 1
21#include "toml.hpp"
22#ifdef __GNUC__
23# pragma GCC diagnostic pop
24#endif
25
26#include <functional>
27#include <string>
28
29namespace helics::fileops {
33toml::value loadToml(const std::string& tomlString);
35bool hasTomlExtension(std::string_view tomlString);
37bool looksLikeConfigToml(std::string_view tomlString);
41toml::value loadTomlStr(const std::string& tomlString);
42
44std::string tomlAsString(const toml::value& element);
45
47helics::Time loadTomlTime(const toml::value& timeElement,
48 time_units defaultUnits = time_units::sec);
49
51std::string getName(const toml::value& element);
52
54inline std::string
55 getOrDefault(const toml::value& element, const std::string& key, std::string_view defVal)
56{
57 if (element.contains(key)) {
58 return tomlAsString(element.at(key));
59 }
60 return std::string(defVal);
61}
63inline double getOrDefault(const toml::value& element, const std::string& key, double defVal)
64{
65 return toml::find_or<double>(element, key, defVal);
66}
67
69inline bool getOrDefault(const toml::value& element, const std::string& key, bool defVal)
70{
71 return toml::find_or<bool>(element, key, defVal);
72}
73
75inline int64_t getOrDefault(const toml::value& element, const std::string& key, int64_t defVal)
76{
77 return toml::find_or<int64_t>(element, key, defVal);
78}
79
81inline bool callIfMember(const toml::value& element,
82 const std::string& key,
83 const std::function<void(const std::string&)>& call)
84{
85 const std::string empty;
86 auto& val = toml::find_or<std::string>(element, key, empty);
87 if (!val.empty()) {
88 call(val);
89 return true;
90 }
91 return false;
92}
93
95inline bool callIfMember(const toml::value& element,
96 const std::string& key,
97 const std::function<void(const std::string&, helics::Time)>& call)
98{
99 toml::value uval;
100 auto val = toml::find_or(element, key, uval);
101
102 if (!val.is_empty()) {
103 call(key, loadTomlTime(val));
104 return true;
105 }
106 return false;
107}
108
110template<class X>
111inline bool callIfMember(const toml::value& element,
112 const std::string& key,
113 const std::function<void(const std::string&, X)>& call)
114{
115 toml::value uval;
116 auto val = toml::find_or(element, key, uval);
117 if (!val.is_empty()) {
118 call(key, toml::get<X>(val));
119 return true;
120 }
121 return false;
122}
123
124inline void
125 replaceIfMember(const toml::value& element, const std::string& key, helics::Time& timeVal)
126{
127 toml::value uval;
128 auto val = toml::find_or(element, key, uval);
129
130 if (!val.is_empty()) {
131 timeVal = loadTomlTime(val);
132 }
133}
134
135inline void replaceIfMember(const toml::value& element, const std::string& key, std::string& loc)
136{
137 toml::value uval;
138 auto val = toml::find_or(element, key, uval);
139
140 if (!val.is_empty()) {
141 loc = tomlAsString(val);
142 }
143}
144
145template<class X>
146inline void replaceIfMember(const toml::value& element, const std::string& key, X& loc)
147{
148 toml::value uval;
149 auto val = toml::find_or(element, key, uval);
150
151 if (!val.is_empty()) {
152 loc = toml::get<X>(val);
153 }
154}
155
157inline bool isMember(const toml::value& element, const std::string& key)
158{
159 toml::value uval;
160 auto val = toml::find_or(element, key, uval);
161
162 return (!val.is_empty());
163}
164
165} // namespace helics::fileops
bool isMember(const toml::value &element, const std::string &key)
Definition TomlProcessingFunctions.hpp:157
TimeRepresentation< count_time< 9 > > Time
Definition helicsTime.hpp:27