helics  3.0.1
TomlProcessingFunctions.hpp
Go to the documentation of this file.
1 /*
2 Copyright (c) 2017-2021,
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 "toml.hpp"
20 #ifdef __GNUC__
21 # pragma GCC diagnostic pop
22 #endif
23 
24 #include <functional>
25 #include <string>
26 
27 namespace helics::fileops {
31 toml::value loadToml(const std::string& tomlString);
32 
33 bool hasTomlExtension(const std::string& tomlString);
36 toml::value loadTomlStr(const std::string& tomlString);
37 
39 std::string tomlAsString(const toml::value& element);
40 
42 helics::Time loadTomlTime(const toml::value& timeElement,
43  time_units defaultUnits = time_units::sec);
44 
46 std::string getName(const toml::value& element);
47 
49 inline std::string
50  getOrDefault(const toml::value& element, const std::string& key, const std::string& defVal)
51 {
52  if (element.contains(key)) {
53  return tomlAsString(element.at(key));
54  }
55  return defVal;
56 }
58 inline double getOrDefault(const toml::value& element, const std::string& key, double defVal)
59 {
60  return toml::find_or<double>(element, key, defVal);
61 }
62 
64 inline bool getOrDefault(const toml::value& element, const std::string& key, bool defVal)
65 {
66  return toml::find_or<bool>(element, key, defVal);
67 }
68 
70 inline int64_t getOrDefault(const toml::value& element, const std::string& key, int64_t defVal)
71 {
72  return toml::find_or<int64_t>(element, key, defVal);
73 }
74 
76 inline bool callIfMember(const toml::value& element,
77  const std::string& key,
78  const std::function<void(const std::string&)>& call)
79 {
80  const std::string empty;
81  auto& val = toml::find_or<std::string>(element, key, empty);
82  if (!val.empty()) {
83  call(val);
84  return true;
85  }
86  return false;
87 }
88 
90 inline bool callIfMember(const toml::value& element,
91  const std::string& key,
92  const std::function<void(const std::string&, helics::Time)>& call)
93 {
94  toml::value uval;
95  auto val = toml::find_or(element, key, uval);
96 
97  if (!val.is_uninitialized()) {
98  call(key, loadTomlTime(val));
99  return true;
100  }
101  return false;
102 }
103 
105 template<class X>
106 inline bool callIfMember(const toml::value& element,
107  const std::string& key,
108  const std::function<void(const std::string&, X)>& call)
109 {
110  toml::value uval;
111  auto val = toml::find_or(element, key, uval);
112  if (!val.is_uninitialized()) {
113  call(key, toml::get<X>(val));
114  return true;
115  }
116  return false;
117 }
118 
119 inline void
120  replaceIfMember(const toml::value& element, const std::string& key, helics::Time& timeVal)
121 {
122  toml::value uval;
123  auto val = toml::find_or(element, key, uval);
124 
125  if (!val.is_uninitialized()) {
126  timeVal = loadTomlTime(val);
127  }
128 }
129 
130 inline void replaceIfMember(const toml::value& element, const std::string& key, std::string& loc)
131 {
132  toml::value uval;
133  auto val = toml::find_or(element, key, uval);
134 
135  if (!val.is_uninitialized()) {
136  loc = tomlAsString(val);
137  }
138 }
139 
140 template<class X>
141 inline void replaceIfMember(const toml::value& element, const std::string& key, X& loc)
142 {
143  toml::value uval;
144  auto val = toml::find_or(element, key, uval);
145 
146  if (!val.is_uninitialized()) {
147  loc = toml::get<X>(val);
148  }
149 }
150 
152 inline bool isMember(const toml::value& element, const std::string& key)
153 {
154  toml::value uval;
155  auto val = toml::find_or(element, key, uval);
156 
157  return (!val.is_uninitialized());
158 }
159 
160 } // 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:152