helics  3.5.2
addTargets.hpp
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 
9 #include "configFileHelpers.hpp"
10 
11 #include <cctype>
12 #include <string>
13 #include <type_traits>
14 
15 namespace helics {
16 
17 template<typename Callable>
18 bool addTargets(const toml::value& section, std::string targetName, Callable callback)
19 {
20  bool found{false};
21  toml::value uval;
22  // There should probably be a static_assert here but there isn't a nice type trait to check that
23  auto targets = toml::find_or(section, targetName, uval);
24  if (!targets.is_uninitialized()) {
25  if (targets.is_array()) {
26  auto& targetArray = targets.as_array();
27  for (const auto& target : targetArray) {
28  callback(static_cast<const std::string&>(target.as_string()));
29  }
30  } else {
31  callback(static_cast<const std::string&>(targets.as_string()));
32  }
33  found = true;
34  }
35  if (targetName.back() == 's') {
36  targetName.pop_back();
37  std::string target;
38  target = toml::find_or(section, targetName, target);
39  if (!target.empty()) {
40  found = true;
41  callback(target);
42  }
43  }
44  return found;
45 }
46 
47 template<typename Callable>
48 bool addTargets(const Json::Value& section, std::string targetName, Callable callback)
49 {
50  bool found{false};
51  // There should probably be a static_assert here but there isn't a nice type trait to check that
52  if (section.isMember(targetName)) {
53  auto targets = section[targetName];
54  if (targets.isArray()) {
55  for (const auto& target : targets) {
56  callback(target.asString());
57  }
58  } else {
59  callback(targets.asString());
60  }
61  found = true;
62  }
63  if (targetName.back() == 's') {
64  targetName.pop_back();
65  if (section.isMember(targetName)) {
66  callback(section[targetName].asString());
67  found = true;
68  }
69  }
70  return found;
71 }
72 
73 template<typename Block, typename Callable>
74 void addTargetVariations(const Block& section,
75  const std::string& name1,
76  std::string name2,
77  Callable callback)
78 {
79  bool found = addTargets(section, name1 + "_" + name2, callback);
80  if (!found) {
81  found = addTargets(section, name1 + name2, callback);
82  }
83  if (!found) {
84  name2.front() = std::toupper(name2.front());
85  addTargets(section, name1 + name2, callback);
86  }
87 }
88 
89 void processOptions(const toml::value& section,
90  const std::function<int(const std::string&)>& optionConversion,
91  const std::function<int(const std::string&)>& valueConversion,
92  const std::function<void(int, int)>& optionAction);
93 
94 void processOptions(const Json::Value& section,
95  const std::function<int(const std::string&)>& optionConversion,
96  const std::function<int(const std::string&)>& valueConversion,
97  const std::function<void(int, int)>& optionAction);
98 
99 void loadTags(const Json::Value& section,
100  const std::function<void(std::string_view, std::string_view)>& tagAction);
101 
102 void loadTags(const toml::value& section,
103  const std::function<void(std::string_view, std::string_view)>& tagAction);
104 
105 } // namespace helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14