helics 3.7.0
Loading...
Searching...
No Matches
addTargets.hpp
1/*
2Copyright (c) 2017-2026,
3Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Energy
4Innovation LLC. See the top-level NOTICE for additional details. All rights reserved.
5SPDX-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
15namespace helics {
16
17template<typename Callable>
18bool 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_empty()) {
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
47template<typename Callable>
48bool addTargets(const nlohmann::json& 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.contains(targetName)) {
53 auto targets = section[targetName];
54 if (targets.is_array()) {
55 for (const auto& target : targets) {
56 callback(target.get<std::string>());
57 }
58 } else {
59 callback(targets.get<std::string>());
60 }
61 found = true;
62 }
63 if (targetName.back() == 's') {
64 targetName.pop_back();
65 if (section.contains(targetName)) {
66 callback(section[targetName].get<std::string>());
67 found = true;
68 }
69 }
70 return found;
71}
72
73template<typename Block, typename Callable>
74void 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
89void 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
94void processOptions(const nlohmann::json& 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
99void loadTags(const nlohmann::json& section,
100 const std::function<void(std::string_view, std::string_view)>& tagAction);
101
102void 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