Goby3 3.7.0
2026.11.12
Loading...
Searching...
No Matches
yaml_raii.h
Go to the documentation of this file.
1// Copyright 2020-2026:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_TOOL_INTERFACE_YAML_RAII_H
25#define GOBY_MIDDLEWARE_TOOL_INTERFACE_YAML_RAII_H
26
27#include <yaml-cpp/yaml.h>
28
29namespace goby
30{
31namespace yaml
32{
33class YSeq
34{
35 public:
36 YSeq(YAML::Emitter& out, bool flow = false) : out_(out)
37 {
38 if (flow)
39 out << YAML::Flow;
40 else
41 out << YAML::Block;
42
43 out << YAML::BeginSeq;
44 }
45 ~YSeq() { out_ << YAML::EndSeq; }
46
47 template <typename A> void add(A a) { out_ << a; }
48
49 private:
50 YAML::Emitter& out_;
51};
52
53class YMap
54{
55 public:
56 YMap(YAML::Emitter& out, bool flow = false) : out_(out)
57 {
58 if (flow)
59 out << YAML::Flow;
60 else
61 out << YAML::Block;
62
63 out << YAML::BeginMap;
64 }
65
66 template <typename A, typename B> YMap(YAML::Emitter& out, A key, B value) : YMap(out)
67 {
68 add(key, value);
69 }
70
71 template <typename A> YMap(YAML::Emitter& out, A key) : YMap(out) { add_key(key); }
72
73 ~YMap() { out_ << YAML::EndMap; }
74
75 template <typename A, typename B> void add(A key, B value)
76 {
77 out_ << YAML::Key << key << YAML::Value << value;
78 }
79
80 template <typename A> void add(A key, std::string value)
81 {
82 bool double_quote = value.find(' ') != std::string::npos;
83 if (double_quote)
84 out_ << YAML::Key << key << YAML::Value << YAML::DoubleQuoted << value;
85 else
86 add<A, std::string>(key, value);
87 }
88
89 template <typename A> void add_key(A key) { out_ << YAML::Key << key << YAML::Value; }
90
91 private:
92 YAML::Emitter& out_;
93};
94} // namespace yaml
95} // namespace goby
96
97#endif
void add_key(A key)
Definition yaml_raii.h:89
YMap(YAML::Emitter &out, bool flow=false)
Definition yaml_raii.h:56
void add(A key, B value)
Definition yaml_raii.h:75
YMap(YAML::Emitter &out, A key)
Definition yaml_raii.h:71
void add(A key, std::string value)
Definition yaml_raii.h:80
YMap(YAML::Emitter &out, A key, B value)
Definition yaml_raii.h:66
void add(A a)
Definition yaml_raii.h:47
YSeq(YAML::Emitter &out, bool flow=false)
Definition yaml_raii.h:36
The global namespace for the Goby project.