Program Listing for File yaml_parser.hpp
↰ Return to documentation for file (include/holoscan/utils/yaml_parser.hpp
)
/*
* SPDX-FileCopyrightText: Copyright (c) 2022-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef HOLOSCAN_UTILS_YAML_PARSER_HPP
#define HOLOSCAN_UTILS_YAML_PARSER_HPP
#include <yaml-cpp/yaml.h>
#include <iostream>
#include <memory>
#include <regex>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "../core/common.hpp"
namespace {
bool is_space(unsigned char c) {
return c == ' ';
}
} // namespace
template <typename typeT>
struct YAML::convert<std::complex<typeT>> {
static Node encode(const std::complex<typeT>& data) {
Node node;
node =
std::string{fmt::format("{}{}{}j", data.real(), (data.imag() < 0) ? "" : "+", data.imag())};
return node;
}
static bool decode(const Node& node, std::complex<typeT>& data) {
if (!node.IsScalar()) {
HOLOSCAN_LOG_ERROR("complex<T> decode: expected a scalar");
return false;
}
std::string value = node.as<std::string>();
std::regex complex_reg("\\s*([+-]?\\s?\\d*\\.?\\d+)\\s?([+-]{1}\\s?\\d*\\.?\\d+)[ij]{1}\\s*$");
std::smatch m;
if (std::regex_search(value, m, complex_reg)) {
if (m.size() != 3) {
HOLOSCAN_LOG_ERROR("unexpected match size: {}, matched: {}", m.size(), m.str(0));
}
// extract the real and imaginary components of the number
std::string real_str = m.str(1);
std::string imag_str = m.str(2);
// remove any white space around + or - (necessary for std::stod to work)
real_str.erase(std::remove_if(real_str.begin(), real_str.end(), is_space), real_str.end());
imag_str.erase(std::remove_if(imag_str.begin(), imag_str.end(), is_space), imag_str.end());
// format real and imaginary strings as floating point
double real = std::stod(real_str);
double imag = std::stod(imag_str);
data = std::complex<typeT>(real, imag);
} else {
HOLOSCAN_LOG_ERROR("failed to match expected regex for complex<T>");
return false;
}
return true;
}
};
namespace holoscan {
template <typename typeT, typename valueT = void>
struct YAMLNodeParser;
template <typename typeT>
struct YAMLNodeParser<typeT> {
static typeT parse(const YAML::Node& node) {
try {
return node.as<typeT>();
} catch (...) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'", ss.str());
return typeT();
}
}
};
template <>
struct YAMLNodeParser<int8_t> {
static uint8_t parse(const YAML::Node& node) {
try {
return static_cast<int8_t>(node.as<int32_t>());
} catch (...) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'", ss.str());
return 0;
}
}
};
template <>
struct YAMLNodeParser<uint8_t> {
static uint8_t parse(const YAML::Node& node) {
try {
return static_cast<uint8_t>(node.as<uint32_t>());
} catch (...) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'", ss.str());
return 0;
}
}
};
template <typename typeT>
struct YAMLNodeParser<std::complex<typeT>> {
static std::complex<typeT> parse(const YAML::Node& node) {
try {
return static_cast<std::complex<typeT>>(node.as<std::complex<typeT>>());
} catch (...) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'", ss.str());
return 0;
}
}
};
template <typename typeT>
struct YAMLNodeParser<std::vector<typeT>> {
static std::vector<typeT> parse(const YAML::Node& node) {
if (!node.IsSequence()) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'. It is not a sequence.", ss.str());
return std::vector<typeT>();
}
std::vector<typeT> result(node.size());
for (size_t i = 0; i < node.size(); i++) {
const auto value = YAMLNodeParser<typeT>::parse(node[i]);
// TODO: check if value is valid
result[i] = std::move(value);
}
return result;
}
};
template <typename typeT, std::size_t N>
struct YAMLNodeParser<std::array<typeT, N>> {
static std::array<typeT, N> parse(const YAML::Node& node) {
if (!node.IsSequence()) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR("Unable to parse YAML node: '{}'. It is not a sequence.", ss.str());
return std::array<typeT, N>();
}
if (node.size() != N) {
std::stringstream ss;
ss << node;
HOLOSCAN_LOG_ERROR(
"Unable to parse YAML node: '{}'. It is not a sequence of size {}.", ss.str(), N);
return std::array<typeT, N>();
}
std::array<typeT, N> result;
for (size_t i = 0; i < node.size(); i++) {
const auto value = YAMLNodeParser<typeT>::parse(node[i]);
// TODO: check if value is valid
result[i] = std::move(value);
}
return result;
}
};
// Skip std::shared_ptr<Resource> and std::shared_ptr<Condition>
template <typename typeT>
struct YAMLNodeParser<std::shared_ptr<typeT>> {
static std::shared_ptr<typeT> parse(const YAML::Node&) { return {}; }
};
// Skip IOSpec*
template <>
struct YAMLNodeParser<IOSpec*> {
static IOSpec* parse(const YAML::Node&) { return nullptr; }
};
// Skip std::vector<IOSpec*>
template <>
struct YAMLNodeParser<std::vector<IOSpec*>> {
static std::vector<IOSpec*> parse(const YAML::Node&) { return {}; }
};
} // namespace holoscan
#endif/* HOLOSCAN_UTILS_YAML_PARSER_HPP */