rapidyaml 0.15.2
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
emit_container.hpp
Go to the documentation of this file.
1#ifndef _C4_YML_EMIT_CONTAINER_HPP_
2#define _C4_YML_EMIT_CONTAINER_HPP_
3
4/** @file emit.hpp Utilities to emit YAML and JSON to resizeable containers. */
5
6#ifndef _C4_YML_EMIT_BUF_HPP_
7#include "c4/yml/emit_buf.hpp"
8#endif
9#ifndef _C4_YML_EMIT_OPTIONS_HPP_
11#endif
12#ifndef _C4_YML_NODE_HPP_
13#include "c4/yml/node.hpp"
14#endif
15
16
17namespace c4 {
18namespace yml {
19
20
21// emit from tree and node id ---------------------------
22
23/** @addtogroup doc_emit_to_container_from_node_id
24 *
25 * @{
26 */
27
28
29/** (1) emit+resize: emit YAML to the given `std::string`/`std::vector<char>`-like
30 * container, resizing it as needed to fit the emitted YAML. If @p append is
31 * set to true, the emitted YAML is appended at the end of the container.
32 *
33 * @return a substr trimmed to the emitted YAML (excluding the initial contents, when appending) */
34template<class CharOwningContainer>
35substr emitrs_yaml(Tree const& t, id_type id, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
36{
37 const size_t startpos = append ? cont->size() : 0u;
38 cont->resize(cont->capacity()); // otherwise the first emit would be certain to fail
39 substr buf = to_substr(*cont).sub(startpos);
40 substr ret = emit_yaml(t, id, opts, buf, /*error_on_excess*/false);
41 cont->resize(startpos + ret.len);
42 if(ret.len && !ret.str)
43 {
44 buf = to_substr(*cont).sub(startpos);
45 ret = emit_yaml(t, id, opts, buf, /*error_on_excess*/true);
46 }
47 return ret;
48}
49/** (2) like (1), but use default emit options */
50template<class CharOwningContainer>
51substr emitrs_yaml(Tree const& t, id_type id, CharOwningContainer * cont, bool append=false)
52{
53 return emitrs_yaml(t, id, EmitOptions{}, cont, append);
54}
55
56
57/** (1) emit+resize: emit JSON to the given `std::string`/`std::vector<char>`-like
58 * container, resizing it as needed to fit the emitted JSON. If @p append is
59 * set to true, the emitted YAML is appended at the end of the container.
60 *
61 * @return a substr trimmed to the emitted JSON (excluding the initial contents, when appending) */
62template<class CharOwningContainer>
63substr emitrs_json(Tree const& t, id_type id, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
64{
65 const size_t startpos = append ? cont->size() : 0u;
66 cont->resize(cont->capacity()); // otherwise the first emit would be certain to fail
67 substr buf = to_substr(*cont).sub(startpos);
68 substr ret = emit_json(t, id, opts, buf, /*error_on_excess*/false);
69 cont->resize(startpos + ret.len);
70 if(ret.len && !ret.str)
71 {
72 buf = to_substr(*cont).sub(startpos);
73 ret = emit_json(t, id, opts, buf, /*error_on_excess*/true);
74 }
75 return ret;
76}
77/** (2) like (1), but use default emit options */
78template<class CharOwningContainer>
79substr emitrs_json(Tree const& t, id_type id, CharOwningContainer * cont, bool append=false)
80{
81 return emitrs_json(t, id, EmitOptions{}, cont, append);
82}
83
84
85/** (3) emit+resize: YAML to a newly-created `std::string`/`std::vector<char>`-like container. */
86template<class CharOwningContainer>
87CharOwningContainer emitrs_yaml(Tree const& t, id_type id, EmitOptions const& opts={})
88{
89 CharOwningContainer c;
90 emitrs_yaml(t, id, opts, &c);
91 return c;
92}
93/** (3) emit+resize: JSON to a newly-created `std::string`/`std::vector<char>`-like container. */
94template<class CharOwningContainer>
95CharOwningContainer emitrs_json(Tree const& t, id_type id, EmitOptions const& opts={})
96{
97 CharOwningContainer c;
98 emitrs_json(t, id, opts, &c);
99 return c;
100}
101
102/** @} */
103
104
105// emit from root -------------------------
106
107/** @addtogroup doc_emit_to_container_from_root
108 *
109 * @{
110 */
111
112
113/** (1) emit+resize: YAML to the given `std::string`/`std::vector<char>`-like
114 * container, resizing it as needed to fit the emitted YAML.
115 * @return a substr trimmed to the new emitted contents. */
116template<class CharOwningContainer>
117substr emitrs_yaml(Tree const& t, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
118{
119 return emitrs_yaml(t, t.root_id_maybe(), opts, cont, append);
120}
121/** (2) like (1), but use default emit options */
122template<class CharOwningContainer>
123substr emitrs_yaml(Tree const& t, CharOwningContainer * cont, bool append=false)
124{
125 return emitrs_yaml(t, t.root_id_maybe(), EmitOptions{}, cont, append);
126}
127
128
129/** (1) emit+resize: JSON to the given `std::string`/`std::vector<char>`-like
130 * container, resizing it as needed to fit the emitted JSON.
131 * @return a substr trimmed to the new emitted contents. */
132template<class CharOwningContainer>
133substr emitrs_json(Tree const& t, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
134{
135 return emitrs_json(t, t.root_id_maybe(), opts, cont, append);
136}
137/** (2) like (1), but use default emit options */
138template<class CharOwningContainer>
139substr emitrs_json(Tree const& t, CharOwningContainer * cont, bool append=false)
140{
141 return emitrs_json(t, t.root_id_maybe(), EmitOptions{}, cont, append);
142}
143
144
145/** (3) emit+resize: YAML to a newly-created `std::string`/`std::vector<char>`-like container. */
146template<class CharOwningContainer>
147CharOwningContainer emitrs_yaml(Tree const& t, EmitOptions const& opts={})
148{
149 CharOwningContainer c;
150 emitrs_yaml(t, t.root_id_maybe(), opts, &c);
151 return c;
152}
153/** (3) emit+resize: JSON to a newly-created `std::string`/`std::vector<char>`-like container. */
154template<class CharOwningContainer>
155CharOwningContainer emitrs_json(Tree const& t, EmitOptions const& opts={})
156{
157 CharOwningContainer c;
158 emitrs_json(t, t.root_id_maybe(), opts, &c);
159 return c;
160}
161
162/** @} */
163
164
165// emit from ConstNodeRef ------------------------
166
167/** @addtogroup doc_emit_to_container_from_noderef
168 *
169 * @{
170 */
171
172/** (1) emit+resize: YAML to the given `std::string`/`std::vector<char>`-like container,
173 * resizing it as needed to fit the emitted YAML.
174 * @return a substr trimmed to the new emitted contents */
175template<class CharOwningContainer>
176substr emitrs_yaml(ConstNodeRef const& n, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
177{
178 if(!n.readable())
179 return {};
180 return emitrs_yaml(*n.tree(), n.id(), opts, cont, append);
181}
182/** (2) like (1), but use default emit options */
183template<class CharOwningContainer>
184substr emitrs_yaml(ConstNodeRef const& n, CharOwningContainer * cont, bool append=false)
185{
186 if(!n.readable())
187 return {};
188 return emitrs_yaml(*n.tree(), n.id(), EmitOptions{}, cont, append);
189}
190
191
192/** (1) emit+resize: JSON to the given `std::string`/`std::vector<char>`-like container,
193 * resizing it as needed to fit the emitted JSON.
194 * @return a substr trimmed to the new emitted contents */
195template<class CharOwningContainer>
196substr emitrs_json(ConstNodeRef const& n, EmitOptions const& opts, CharOwningContainer * cont, bool append=false)
197{
198 if(!n.readable())
199 return {};
200 return emitrs_json(*n.tree(), n.id(), opts, cont, append);
201}
202/** (2) like (1), but use default emit options */
203template<class CharOwningContainer>
204substr emitrs_json(ConstNodeRef const& n, CharOwningContainer * cont, bool append=false)
205{
206 if(!n.readable())
207 return {};
208 return emitrs_json(*n.tree(), n.id(), EmitOptions{}, cont, append);
209}
210
211
212/** (3) emit+resize: YAML to a newly-created `std::string`/`std::vector<char>`-like container. */
213template<class CharOwningContainer>
214CharOwningContainer emitrs_yaml(ConstNodeRef const& n, EmitOptions const& opts={})
215{
216 CharOwningContainer c;
217 if(n.readable())
218 emitrs_yaml(*n.tree(), n.id(), opts, &c);
219 return c;
220}
221/** (3) emit+resize: JSON to a newly-created `std::string`/`std::vector<char>`-like container. */
222template<class CharOwningContainer>
223CharOwningContainer emitrs_json(ConstNodeRef const& n, EmitOptions const& opts={})
224{
225 CharOwningContainer c;
226 if(n.readable())
227 emitrs_json(*n.tree(), n.id(), opts, &c);
228 return c;
229}
230
231/** @} */
232
233} // namespace yml
234} // namespace c4
235
236#endif /* _C4_YML_EMIT_CONTAINER_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
bool readable() const noexcept
because a ConstNodeRef cannot be used to write to the tree, readable() has the same meaning as !...
Definition node.hpp:535
id_type root_id_maybe() const
Get the id of the root node, or NONE if the tree is empty.
Definition tree.hpp:335
Utilities to emit YAML and JSON to buffers or containers.
substr emit_yaml(Tree const &t, EmitOptions const &opts, substr buf, bool error_on_excess)
(1) emit YAML to the given buffer.
Definition emit_buf.cpp:28
substr emit_json(Tree const &t, EmitOptions const &opts, substr buf, bool error_on_excess)
(1) emit JSON to the given buffer.
Definition emit_buf.cpp:42
substr emitrs_json(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit JSON to the given std::string/std::vector<char>-like container,...
substr emitrs_yaml(Tree const &t, id_type id, EmitOptions const &opts, CharOwningContainer *cont, bool append=false)
(1) emit+resize: emit YAML to the given std::string/std::vector<char>-like container,...
substr to_substr(char(&s)[N]) noexcept
Definition substr.hpp:2377
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2356
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
(Undefined by default) Use shorter error message from checks/asserts: do not show the check condition...
Definition common.cpp:14
Node classes.
size_t len
the length of the substring
Definition substr.hpp:218
size_t size() const noexcept
Definition substr.hpp:358
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:503
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216
A lightweight object containing options to be used when emitting.