rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
emit_ostream.hpp
Go to the documentation of this file.
1#ifndef _C4_YML_EMIT_OSTREAM_HPP_
2#define _C4_YML_EMIT_OSTREAM_HPP_
3
4/** @file emit_ostream.hpp emit to STL-like ostreams */
5
6#ifndef _C4_YML_NODE_HPP_
7#include "c4/yml/node.hpp"
8#endif
9#ifndef _C4_YML_EMITTER_HPP_
10#include "c4/yml/emitter.hpp"
11#endif
12#ifndef _C4_YML_EMITTER_DEF_HPP_
14#endif
15#ifndef _C4_YML_WRITER_OSTREAM_HPP_
17#endif
18
19
20namespace c4 {
21namespace yml {
22
23
24/** @addtogroup doc_emit_to_ostream
25 *
26 * @{
27 */
28
29template<class OStream>
31
32
33/** tag type to mark a tree or node to be emitted as yaml when using
34 * @ref operator<<, with options. For example:
35 *
36 * ```cpp
37 * Tree t = parse_in_arena("{foo: bar}");
38 * std::cout << t; // emits YAML
39 * std::cout << as_yaml(t, id); // emits YAML from nested node
40 * std::cout << as_yaml(t, EmitOptions{}.flow_spc(true)); // emits YAML forcing spaces after flow commas
41 * ```
42 */
43struct as_json
44{
45 Tree const* tree;
48 as_json(Tree const& t, EmitOptions const& opts={}) noexcept : tree(&t), node(t.root_id_maybe()), options(opts) {}
49 as_json(Tree const& t, id_type id, EmitOptions const& opts={}) noexcept : tree(&t), node(id), options(opts) {}
50 as_json(ConstNodeRef const& n, EmitOptions const& opts={}) noexcept : tree(n.tree()), node(n.id()), options(opts) {}
51};
52
53
54/** tag type to mark a tree or node to be emitted as yaml when using
55 * @ref operator<< . For example:
56 *
57 * ```cpp
58 * Tree t = parse_in_arena("{foo: bar}");
59 * std::cout << t; // emits YAML
60 * std::cout << as_yaml(t); // emits JSON
61 * std::cout << as_yaml(t, id); // emits JSON from nested node
62 * std::cout << as_json(t, EmitOptions{}.flow_spc(true)); // emits JSON forcing spaces after commas
63 * ```
64 */
65struct as_yaml
66{
67 Tree const* tree;
70 as_yaml(Tree const& t, EmitOptions const& opts={}) noexcept : tree(&t), node(t.root_id_maybe()), options(opts) {}
71 as_yaml(Tree const& t, id_type id, EmitOptions const& opts={}) noexcept : tree(&t), node(id), options(opts) {}
72 as_yaml(ConstNodeRef const& n, EmitOptions const& opts={}) noexcept : tree(n.tree()), node(n.id()), options(opts) {}
73};
74
75
76/** emit YAML to an STL-like ostream */
77template<class OStream>
78OStream& operator<< (OStream& stream, Tree const& tree)
79{
81 em.emit_as(EMIT_YAML, &tree);
82 return stream;
83}
84
85
86/** emit YAML to an STL-like ostream
87 * @overload */
88template<class OStream>
89OStream& operator<< (OStream& stream, ConstNodeRef const& node)
90{
92 em.emit_as(EMIT_YAML, node.tree(), node.id());
93 return stream;
94}
95
96
97/** emit JSON to an STL-like ostream */
98template<class OStream>
99OStream& operator<< (OStream& stream, as_json const& json_spec)
100{
101 if(json_spec.tree && !json_spec.tree->empty())
102 {
103 EmitterOStream<OStream> em(json_spec.options, &stream);
104 id_type node_id = json_spec.node != NONE ? json_spec.node : json_spec.tree->root_id();
105 em.emit_as(EMIT_JSON, json_spec.tree, node_id);
106 }
107 return stream;
108}
109
110
111/** emit YAML to an STL-like ostream */
112template<class OStream>
113OStream& operator<< (OStream& stream, as_yaml const& yaml_spec)
114{
115 if(yaml_spec.tree && !yaml_spec.tree->empty())
116 {
117 EmitterOStream<OStream> em(yaml_spec.options, &stream);
118 id_type node_id = yaml_spec.node != NONE ? yaml_spec.node : yaml_spec.tree->root_id();
119 em.emit_as(EMIT_YAML, yaml_spec.tree, node_id);
120 }
121 return stream;
122}
123
124/** @} */
125
126
127} // namespace yml
128} // namespace c4
129
130#endif /* _C4_YML_EMIT_OSTREAM_HPP_ */
Holds a pointer to an existing tree, and a node id.
Definition node.hpp:478
id_type id() const noexcept
Definition node.hpp:564
Tree const * tree() const noexcept
Definition node.hpp:563
A YAML/JSON emitter, templated on a writer class such as WriterBuf, WriterFile, or WriterOStream.
Definition emitter.hpp:35
void emit_as(EmitType_e type, Tree const *tree, id_type id=NONE)
emit!
id_type root_id() const
Get the id of the root node. The tree must not be empty. The tree can be empty only when constructed ...
Definition tree.hpp:333
bool empty() const
Query for zero size.
Definition tree.hpp:290
OStream & operator<<(OStream &stream, Tree const &tree)
emit YAML to an STL-like ostream
Emitter< WriterOStream< OStream > > EmitterOStream
RYML_ID_TYPE id_type
The type of a node id in the YAML tree; to override the default type, define the macro RYML_ID_TYPE t...
Definition common.hpp:305
@ NONE
an index to none
Definition common.hpp:312
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
Node classes.
A lightweight object containing options to be used when emitting.
tag type to mark a tree or node to be emitted as yaml when using operator<<, with options.
Tree const * tree
EmitOptions options
as_json(Tree const &t, id_type id, EmitOptions const &opts={}) noexcept
as_json(ConstNodeRef const &n, EmitOptions const &opts={}) noexcept
as_json(Tree const &t, EmitOptions const &opts={}) noexcept
tag type to mark a tree or node to be emitted as yaml when using operator<< .
Tree const * tree
as_yaml(ConstNodeRef const &n, EmitOptions const &opts={}) noexcept
EmitOptions options
as_yaml(Tree const &t, EmitOptions const &opts={}) noexcept
as_yaml(Tree const &t, id_type id, EmitOptions const &opts={}) noexcept