rapidyaml  0.16.0
parseandemitYAML,anddoitfast
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_
10 #include "c4/yml/emit_options.hpp"
11 #endif
12 #ifndef C4_YML_NODE_HPP_
13 #include "c4/yml/node.hpp"
14 #endif
15 
16 
17 namespace c4 {
18 namespace 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) */
34 template<class CharOwningContainer>
35 substr 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 */
50 template<class CharOwningContainer>
51 substr 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) */
62 template<class CharOwningContainer>
63 substr 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 */
78 template<class CharOwningContainer>
79 substr 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. */
86 template<class CharOwningContainer>
87 CharOwningContainer 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. */
94 template<class CharOwningContainer>
95 CharOwningContainer 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. */
116 template<class CharOwningContainer>
117 substr 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 */
122 template<class CharOwningContainer>
123 substr 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. */
132 template<class CharOwningContainer>
133 substr 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 */
138 template<class CharOwningContainer>
139 substr 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. */
146 template<class CharOwningContainer>
147 CharOwningContainer 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. */
154 template<class CharOwningContainer>
155 CharOwningContainer 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 */
175 template<class CharOwningContainer>
176 substr 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 */
183 template<class CharOwningContainer>
184 substr 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 */
195 template<class CharOwningContainer>
196 substr 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 */
203 template<class CharOwningContainer>
204 substr 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. */
213 template<class CharOwningContainer>
214 CharOwningContainer 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. */
222 template<class CharOwningContainer>
223 CharOwningContainer 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_ */
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-like container, resizing it as needed to fit the emitted YAML.
id_type id() const noexcept
Definition: node.hpp:822
C * str
a restricted pointer to the first character of the substring
Definition: substr.hpp:216
id_type root_id_maybe() const
Get the id of the root node, or NONE if the tree is empty.
Definition: tree.hpp:388
substr to_substr(char(&s)[N]) noexcept
Definition: substr.hpp:2376
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-like container, resizing it as needed to fit the emitted JSON.
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
size_t size() const noexcept
Definition: substr.hpp:358
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition: substr.hpp:502
bool readable() const noexcept
because a ConstNodeRef cannot be used to write to the tree, readable() has the same meaning as !inval...
Definition: node.hpp:793
Utilities to emit YAML and JSON to buffers or containers.
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:736
Tree const * tree() const noexcept
Definition: node.hpp:821
size_t len
the length of the substring
Definition: substr.hpp:218
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
A lightweight object containing options to be used when emitting.
Node classes.