SOLA
Loading...
Searching...
No Matches
scenariofile_component.h
1// Copyright 2023 The SOLA authors
2//
3// This file is part of DAISI.
4//
5// DAISI is free software: you can redistribute it and/or modify it under the terms of the GNU
6// General Public License as published by the Free Software Foundation; version 2.
7//
8// DAISI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
9// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
10// Public License for more details.
11//
12// You should have received a copy of the GNU General Public License along with DAISI. If not, see
13// <https://www.gnu.org/licenses/>.
14//
15// SPDX-License-Identifier: GPL-2.0-only
16
17#ifndef DAISI_CPPS_MANAGER_SCENARIOFILE_COMPONENT_H_
18#define DAISI_CPPS_MANAGER_SCENARIOFILE_COMPONENT_H_
19
20#include <yaml-cpp/yaml.h>
21
22#include <optional>
23#include <variant>
24
25#include "ns3/core-module.h"
26
27#define STRING_NAME(VAR_NAME) #VAR_NAME
28#define SERIALIZE_VAR(VAR) serializeType(VAR, STRING_NAME(VAR), node)
29#define SERIALIZE_NS3_TIME(VAR) \
30 { \
31 std::string internal; \
32 serializeType(internal, STRING_NAME(VAR), node); \
33 VAR = ns3::Time(internal); \
34 } \
35 static_assert(true)
36
37#define SERIALIZE_NS3_TIME_OPTIONAL(VAR) \
38 { \
39 std::optional<std::string> internal; \
40 serializeType(internal, STRING_NAME(VAR), node); \
41 if (internal) VAR = ns3::Time(internal.value()); \
42 } \
43 static_assert(true)
44
45namespace daisi {
46
47template <typename T> void serializeType(T &t, const std::string &key, const YAML::Node &node) {
48 try {
49 if constexpr (std::is_fundamental_v<T> || std::is_same_v<T, std::string>) {
50 // Can directly fetch this
51 t = node[key].as<T>();
52 } else {
53 // Require to call our own parse method
54 // For the future: If we could use reflections or recursive macros we could write
55 // YAML::convert<>::decode methods more or less automatically and always
56 // use the if-case
57 t.parse(node[key]);
58 }
59 } catch (YAML::TypedBadConversion<T> &e) {
60 throw std::runtime_error("Trouble reading key '" + key + "' from YAML.");
61 }
62}
63
64template <typename T>
65void serializeType(std::vector<T> &t, const std::string &key, YAML::Node node) {
66 for (const auto &n : node[key]) {
67 T inner;
68 inner.parse(n);
69 t.push_back(inner);
70 }
71}
72
73template <typename T>
74void serializeType(std::optional<T> &t, const std::string &key, const YAML::Node &node) {
75 if (!node[key].IsDefined()) {
76 t = std::nullopt;
77 return;
78 }
79
80 try {
81 if constexpr (std::is_fundamental_v<T> || std::is_same_v<T, std::string>) {
82 t = node[key].as<T>();
83 } else {
84 t.emplace();
85 t->parse(node[key]);
86 }
87 } catch (YAML::TypedBadConversion<T> &e) {
88 throw std::runtime_error("Wrong data type!");
89 }
90}
91
99template <typename T, typename... U>
100bool deserializeIf(YAML::Node node, std::variant<U...> &var, const std::string &type) {
101 if (type == T::typeName()) {
102 var.template emplace<T>();
103
104 // Do parsing of the actual object
105 std::get<T>(var).parse(node);
106
107 return true;
108 }
109 return false;
110}
111
112} // namespace daisi
113
114#endif