AutoAPMS
Streamlining behaviors in ROS 2
Loading...
Searching...
No Matches
yaml.hpp
1
// Copyright 2024 Robin Müller
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// https://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#pragma once
16
17
#include <algorithm>
18
#include <map>
19
#include <string>
20
21
#include "auto_apms_util/exceptions.hpp"
22
#include "auto_apms_util/filesystem.hpp"
23
#include "yaml-cpp/yaml.h"
24
27
32
#define AUTO_APMS_UTIL_DEFINE_YAML_CONVERSION_METHODS(ClassType) \
33
static ClassType fromFile(const std::string & path) \
34
{ \
35
if (auto_apms_util::isFileEmpty(path)) return ClassType(); \
36
try { \
37
return YAML::LoadFile(path).as<ClassType>(); \
38
} catch (const YAML::ParserException & e) { \
39
throw auto_apms_util::exceptions::YAMLFormatError( \
40
"Format error when loading YAML file " + path + ": " + std::string(e.what())); \
41
} \
42
} \
43
static ClassType decode(const std::string & str) \
44
{ \
45
const bool empty = std::all_of(str.begin(), str.end(), [](unsigned char c) { return std::isspace(c); }); \
46
return empty ? ClassType() : YAML::Load(str).as<ClassType>(); \
47
} \
48
std::string encode() const \
49
{ \
50
YAML::Node root; \
51
root = *this; \
52
YAML::Emitter out; \
53
out << root; \
54
if (!out.good()) { \
55
throw std::runtime_error("Error trying to encode to YAML: " + out.GetLastError()); \
56
} \
57
return out.c_str(); \
58
}
59
auto_apms_util
include
auto_apms_util
yaml.hpp
Generated by
1.13.2