helics  3.3.0
TomlProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2022,
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);
34 
35 bool hasTomlExtension(std::string_view tomlString);
39 toml::value loadTomlStr(const std::string& tomlString);
40 
42 std::string tomlAsString(const toml::value& element);
43 
45 helics::Time loadTomlTime(const toml::value& timeElement,
46  time_units defaultUnits = time_units::sec);
47 
49 std::string getName(const toml::value& element);
50 
52 inline std::string
53  getOrDefault(const toml::value& element, const std::string& key, std::string_view defVal)
54 {
55  if (element.contains(key)) {
56  return tomlAsString(element.at(key));
57  }
58  return std::string(defVal);
59 }
61 inline double getOrDefault(const toml::value& element, const std::string& key, double defVal)
62 {
63  return toml::find_or<double>(element, key, defVal);
64 }
65 
67 inline bool getOrDefault(const toml::value& element, const std::string& key, bool defVal)
68 {
69  return toml::find_or<bool>(element, key, defVal);
70 }
71 
73 inline int64_t getOrDefault(const toml::value& element, const std::string& key, int64_t defVal)
74 {
75  return toml::find_or<int64_t>(element, key, defVal);
76 }
77 
79 inline bool callIfMember(const toml::value& element,
80  const std::string& key,
81  const std::function<void(const std::string&)>& call)
82 {
83  const std::string empty;
84  auto& val = toml::find_or<std::string>(element, key, empty);
85  if (!val.empty()) {
86  call(val);
87  return true;
88  }
89  return false;
90 }
91 
93 inline bool callIfMember(const toml::value& element,
94  const std::string& key,
95  const std::function<void(const std::string&, helics::Time)>& call)
96 {
97  toml::value uval;
98  auto val = toml::find_or(element, key, uval);
99 
100  if (!val.is_uninitialized()) {
101  call(key, loadTomlTime(val));
102  return true;
103  }
104  return false;
105 }
106 
108 template<class X>
109 inline bool callIfMember(const toml::value& element,
110  const std::string& key,
111  const std::function<void(const std::string&, X)>& call)
112 {
113  toml::value uval;
114  auto val = toml::find_or(element, key, uval);
115  if (!val.is_uninitialized()) {
116  call(key, toml::get<X>(val));
117  return true;
118  }
119  return false;
120 }
121 
122 inline void
123  replaceIfMember(const toml::value& element, const std::string& key, helics::Time& timeVal)
124 {
125  toml::value uval;
126  auto val = toml::find_or(element, key, uval);
127 
128  if (!val.is_uninitialized()) {
129  timeVal = loadTomlTime(val);
130  }
131 }
132 
133 inline void replaceIfMember(const toml::value& element, const std::string& key, std::string& loc)
134 {
135  toml::value uval;
136  auto val = toml::find_or(element, key, uval);
137 
138  if (!val.is_uninitialized()) {
139  loc = tomlAsString(val);
140  }
141 }
142 
143 template<class X>
144 inline void replaceIfMember(const toml::value& element, const std::string& key, X& loc)
145 {
146  toml::value uval;
147  auto val = toml::find_or(element, key, uval);
148 
149  if (!val.is_uninitialized()) {
150  loc = toml::get<X>(val);
151  }
152 }
153 
155 inline bool isMember(const toml::value& element, const std::string& key)
156 {
157  toml::value uval;
158  auto val = toml::find_or(element, key, uval);
159 
160  return (!val.is_uninitialized());
161 }
162 
163 } // namespace helics::fileops
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helicsTime.hpp:27
TomlProcessingFunctions.hpp
helics::fileops::isMember
bool isMember(const toml::value &element, const std::string &key)
Definition: TomlProcessingFunctions.hpp:155