17#ifndef DAISI_CPPS_MANAGER_SCENARIOFILE_COMPONENT_H_
18#define DAISI_CPPS_MANAGER_SCENARIOFILE_COMPONENT_H_
20#include <yaml-cpp/yaml.h>
25#include "ns3/core-module.h"
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) \
31 std::string internal; \
32 serializeType(internal, STRING_NAME(VAR), node); \
33 VAR = ns3::Time(internal); \
37#define SERIALIZE_NS3_TIME_OPTIONAL(VAR) \
39 std::optional<std::string> internal; \
40 serializeType(internal, STRING_NAME(VAR), node); \
41 if (internal) VAR = ns3::Time(internal.value()); \
47template <
typename T>
void serializeType(T &t,
const std::string &key,
const YAML::Node &node) {
49 if constexpr (std::is_fundamental_v<T> || std::is_same_v<T, std::string>) {
51 t = node[key].as<T>();
59 }
catch (YAML::TypedBadConversion<T> &e) {
60 throw std::runtime_error(
"Trouble reading key '" + key +
"' from YAML.");
65void serializeType(std::vector<T> &t,
const std::string &key, YAML::Node node) {
66 for (
const auto &n : node[key]) {
74void serializeType(std::optional<T> &t,
const std::string &key,
const YAML::Node &node) {
75 if (!node[key].IsDefined()) {
81 if constexpr (std::is_fundamental_v<T> || std::is_same_v<T, std::string>) {
82 t = node[key].as<T>();
87 }
catch (YAML::TypedBadConversion<T> &e) {
88 throw std::runtime_error(
"Wrong data type!");
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>();
105 std::get<T>(var).parse(node);