helics  2.8.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/helics-time.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 
30 toml::value loadToml(const std::string& tomlString);
31 
32 bool hasTomlExtension(const std::string& tomlString);
35 toml::value loadTomlStr(const std::string& tomlString);
36 
38 std::string tomlAsString(const toml::value& element);
39 
41 helics::Time loadTomlTime(const toml::value& timeElement,
42  time_units defaultUnits = time_units::sec);
43 
45 std::string getKey(const toml::value& element);
46 
48 inline std::string
49  getOrDefault(const toml::value& element, const std::string& key, const std::string& defVal)
50 {
51  return toml::find_or<std::string>(element, key, defVal);
52 }
54 inline double getOrDefault(const toml::value& element, const std::string& key, double defVal)
55 {
56  return toml::find_or<double>(element, key, defVal);
57 }
58 
60 inline bool getOrDefault(const toml::value& element, const std::string& key, bool defVal)
61 {
62  return toml::find_or<bool>(element, key, defVal);
63 }
64 
66 inline int64_t getOrDefault(const toml::value& element, const std::string& key, int64_t defVal)
67 {
68  return toml::find_or<int64_t>(element, key, defVal);
69 }
70 
72 inline bool callIfMember(const toml::value& element,
73  const std::string& key,
74  const std::function<void(const std::string&)>& call)
75 {
76  const std::string empty;
77  auto& val = toml::find_or<std::string>(element, key, empty);
78  if (!val.empty()) {
79  call(val);
80  return true;
81  }
82  return false;
83 }
84 
86 inline bool callIfMember(const toml::value& element,
87  const std::string& key,
88  const std::function<void(const std::string&, helics::Time)>& call)
89 {
90  toml::value uval;
91  auto val = toml::find_or(element, key, uval);
92 
93  if (!val.is_uninitialized()) {
94  call(key, loadTomlTime(val));
95  return true;
96  }
97  return false;
98 }
99 
101 template<class X>
102 inline bool callIfMember(const toml::value& element,
103  const std::string& key,
104  const std::function<void(const std::string&, X)>& call)
105 {
106  toml::value uval;
107  auto val = toml::find_or(element, key, uval);
108  if (!val.is_uninitialized()) {
109  call(key, toml::get<X>(val));
110  return true;
111  }
112  return false;
113 }
114 
115 inline void
116  replaceIfMember(const toml::value& element, const std::string& key, helics::Time& timeVal)
117 {
118  toml::value uval;
119  auto val = toml::find_or(element, key, uval);
120 
121  if (!val.is_uninitialized()) {
122  timeVal = loadTomlTime(val);
123  }
124 }
125 
126 inline void replaceIfMember(const toml::value& element, const std::string& key, std::string& loc)
127 {
128  toml::value uval;
129  auto val = toml::find_or(element, key, uval);
130 
131  if (!val.is_uninitialized()) {
132  loc = tomlAsString(val);
133  }
134 }
135 
136 template<class X>
137 inline void replaceIfMember(const toml::value& element, const std::string& key, X& loc)
138 {
139  toml::value uval;
140  auto val = toml::find_or(element, key, uval);
141 
142  if (!val.is_uninitialized()) {
143  loc = toml::get<X>(val);
144  }
145 }
146 
148 inline bool isMember(const toml::value& element, const std::string& key)
149 {
150  toml::value uval;
151  auto val = toml::find_or(element, key, uval);
152 
153  return (!val.is_uninitialized());
154 }
isMember
bool isMember(const toml::value &element, const std::string &key)
Definition: TomlProcessingFunctions.hpp:148
tomlAsString
std::string tomlAsString(const toml::value &element)
Definition: TomlProcessingFunctions.cpp:104
loadTomlTime
helics::Time loadTomlTime(const toml::value &timeElement, time_units defaultUnits=time_units::sec)
Definition: TomlProcessingFunctions.cpp:60
helics::Time
TimeRepresentation< count_time< 9 > > Time
Definition: helics-time.hpp:27
loadToml
toml::value loadToml(const std::string &tomlString)
Definition: TomlProcessingFunctions.cpp:22
getKey
std::string getKey(const Json::Value &element)
Definition: JsonProcessingFunctions.cpp:90
TomlProcessingFunctions.hpp
loadTomlStr
toml::value loadTomlStr(const std::string &tomlString)
Definition: TomlProcessingFunctions.cpp:45
callIfMember
bool callIfMember(const toml::value &element, const std::string &key, const std::function< void(const std::string &)> &call)
Definition: TomlProcessingFunctions.hpp:72
getKey
std::string getKey(const toml::value &element)
Definition: TomlProcessingFunctions.cpp:95
getOrDefault
std::string getOrDefault(const toml::value &element, const std::string &key, const std::string &defVal)
Definition: TomlProcessingFunctions.hpp:49