rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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_
13 #include "c4/yml/emitter.def.hpp"
14 #endif
15 #ifndef C4_YML_WRITER_OSTREAM_HPP_
17 #endif
18 
19 
20 namespace c4 {
21 namespace yml {
22 
23 
24 /** @addtogroup doc_emit_to_ostream
25  *
26  * @{
27  */
28 
29 template<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  */
43 struct 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  */
65 struct 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 */
77 template<class OStream>
78 OStream& operator<< (OStream& stream, Tree const& tree)
79 {
80  EmitterOStream<OStream> em(EmitOptions{}, &stream);
81  em.emit_as(EMIT_YAML, &tree);
82  return stream;
83 }
84 
85 
86 /** emit YAML to an STL-like ostream
87  * @overload */
88 template<class OStream>
89 OStream& operator<< (OStream& stream, ConstNodeRef const& node)
90 {
91  EmitterOStream<OStream> em(EmitOptions{}, &stream);
92  em.emit_as(EMIT_YAML, node.tree(), node.id());
93  return stream;
94 }
95 
96 
97 /** emit JSON to an STL-like ostream */
98 template<class OStream>
99 OStream& 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 */
112 template<class OStream>
113 OStream& 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_ */
an index to none
Definition: common.hpp:131
A YAML/JSON emitter, templated on a writer class such as WriterBuf, WriterFile, or WriterOStream...
Definition: emitter.hpp:34
bool empty() const
Query for zero size.
Definition: tree.hpp:343
id_type id() const noexcept
Definition: node.hpp:822
EmitOptions options
id_type root_id_maybe() const
Get the id of the root node, or NONE if the tree is empty.
Definition: tree.hpp:388
as_yaml(Tree const &t, id_type id, EmitOptions const &opts={}) noexcept
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:386
Tree const * tree
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:124
Tree const * tree
void emit_as(EmitType_e type, Tree const *tree, id_type id=NONE)
emit!
Definition: emitter.def.hpp:48
as_json(ConstNodeRef const &n, EmitOptions const &opts={}) noexcept
as_yaml(Tree const &t, EmitOptions const &opts={}) noexcept
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:736
Tree const * tree() const noexcept
Definition: node.hpp:821
A lightweight object containing options to be used when emitting.
EmitOptions options
as_yaml(ConstNodeRef const &n, EmitOptions const &opts={}) noexcept
Node classes.
OStream & operator<<(OStream &stream, Tree const &tree)
emit YAML to an STL-like ostream
tag type to mark a tree or node to be emitted as yaml when using operator<< .
as_json(Tree const &t, id_type id, 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<<, with options...