helics  3.5.2
TomlProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2024,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-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 
29 namespace helics::fileops {
33 toml::value loadToml(const std::string& tomlString);
35 bool hasTomlExtension(std::string_view tomlString);
37 bool looksLikeConfigToml(std::string_view tomlString);
41 toml::value loadTomlStr(const std::string& tomlString);
42 
44 std::string tomlAsString(const toml::value& element);
45 
47 helics::Time loadTomlTime(const toml::value& timeElement,
48  time_units defaultUnits = time_units::sec);
49 
51 std::string getName(const toml::value& element);
52 
54 inline 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 }
63 inline double getOrDefault(const toml::value& element, const std::string& key, double defVal)
64 {
65  return toml::find_or<double>(element, key, defVal);
66 }
67 
69 inline bool getOrDefault(const toml::value& element, const std::string& key, bool defVal)
70 {
71  return toml::find_or<bool>(element, key, defVal);
72 }
73 
75 inline 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 
81 inline 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 
95 inline 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_uninitialized()) {
103  call(key, loadTomlTime(val));
104  return true;
105  }
106  return false;
107 }
108 
110 template<class X>
111 inline 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_uninitialized()) {
118  call(key, toml::get<X>(val));
119  return true;
120  }
121  return false;
122 }
123 
124 inline 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_uninitialized()) {
131  timeVal = loadTomlTime(val);
132  }
133 }
134 
135 inline 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_uninitialized()) {
141  loc = tomlAsString(val);
142  }
143 }
144 
145 template<class X>
146 inline 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_uninitialized()) {
152  loc = toml::get<X>(val);
153  }
154 }
155 
157 inline 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_uninitialized());
163 }
164 
165 } // namespace helics::fileops
toml::value loadTomlStr(const std::string &tomlString)
toml::value loadToml(const std::string &tomlString)
helics::Time loadTomlTime(const toml::value &timeElement, time_units defaultUnits=time_units::sec)
std::string tomlAsString(const toml::value &element)
bool isMember(const toml::value &element, const std::string &key)
Definition: TomlProcessingFunctions.hpp:157
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27