rapidyaml 0.16.0
parse and emit YAML, and do it fast
Loading...
Searching...
No Matches
tree.hpp
Go to the documentation of this file.
1#ifndef C4_YML_TREE_HPP_
2#define C4_YML_TREE_HPP_
3
4/** @file tree.hpp */
5
6#ifndef C4_ERROR_HPP_
7#include "c4/error.hpp"
8#endif
9#ifndef C4_LANGUAGE_HPP_
10#include "c4/language.hpp"
11#endif
12#ifndef C4_YML_FWD_HPP_
13#include "c4/yml/fwd.hpp"
14#endif
15#ifndef C4_YML_COMMON_HPP_
16#include "c4/yml/common.hpp"
17#endif
18#ifndef C4_YML_NODE_TYPE_HPP_
19#include "c4/yml/node_type.hpp"
20#endif
21#ifndef C4_YML_TAG_HPP_
22#include "c4/yml/tag.hpp"
23#endif
24#ifndef C4_YML_ERROR_HPP_
25#include "c4/yml/error.hpp"
26#endif
27#ifndef C4_YML_SCALAR_CHARCONV_HPP_
29#endif
30#ifndef C4_YML_RESOLVE_OPTIONS_HPP_
32#endif
33
34#include <math.h>
35#include <limits.h>
36
37
38C4_SUPPRESS_WARNING_PUSH
39C4_SUPPRESS_WARNING_GCC("-Wtype-limits")
40C4_SUPPRESS_WARNING_GCC("-Wuseless-cast")
41C4_SUPPRESS_WARNING_GCC_CLANG("-Wold-style-cast")
42#if defined(__GNUC__) && (__GNUC__ >= 6)
43C4_SUPPRESS_WARNING_GCC("-Wnull-dereference")
44#endif
45C4_SUPPRESS_WARNING_CLANG("-Wnull-dereference")
46C4_SUPPRESS_WARNING_CLANG("-Wtautological-compare")
47C4_SUPPRESS_WARNING_MSVC(4127) // conditional expression is constant
48C4_SUPPRESS_WARNING_MSVC(4296) // expression is always 'boolean_value'
49C4_SUPPRESS_WARNING_MSVC(4251) // needs to have dll-interface
50// NOLINTBEGIN(modernize-avoid-c-style-cast)
51
52
53namespace c4 {
54namespace yml {
55
56
57/** @cond dev */
58template<class T>
59using _is_string_nocvref = is_string<typename detail::_remove_cvref<T>::type>;
60/** @endcond */
61
62
63/** @addtogroup doc_serialization_tree_write_arena
64 *
65 * @{
66 */
67
68
69/** Serialize a scalar to the tree's arena. This is an implementation
70 * helper for @ref serialize_to_arena(), serializing through
71 * @ref scalar_serialize(). */
72template<class T>
73csubstr serialize_to_arena_scalar(Tree * tree, T const& scalar);
74
75
76/** Serialize a string type (as specified by @ref c4::is_string) to a
77 * tree's arena, ensuring that there is an entry for the string in the
78 * arena even if the string is empty. This is an implementation helper
79 * for @ref serialize_to_arena(), serializing through @ref
80 * scalar_serialize() and then ensuring that the serialized string
81 * will be placed in the arena, even if the string is zero-length. */
82RYML_EXPORT csubstr serialize_to_arena_str(Tree * tree, csubstr scalar);
83
84
85
86#if (C4_CPP >= 17) || defined(__DOXYGEN__)
87/** Serialize a scalar to a tree's arena, dispatching to either @ref
88 * serialize_to_arena_scalar() or @ref serialize_to_arena_str() when
89 * the type is a string according to @ref c4::is_string. This is the
90 * entry point for customizing how a scalar is serialized to a tree's
91 * arena. It is never needed for the user to call this function, and
92 * generally there is no reason for overriding this function for user
93 * types, unless it has specific requirements for the tree's arena, as
94 * happens for example with string types. For user string types,
95 * defining @ref c4::is_string is enough. For example:
96 *
97 * @note When using a standard older than C++17, `if constexpr` is not
98 * available, and the implementation reverts to SFINAE to achieve the
99 * compile-time dispatch.
100 *
101 * @code{c++}
102 * namespace foo {
103 * class MyStringType {...}; // an example of a user-defined string type
104 * // define conversion to/from substrings
105 * c4::yml::csubstr to_csubstr(MyStringType const& s) { return ...; }
106 * c4::yml::substr to_substr(MyStringType & s) { return ...; }
107 * } // namespace foo
108 * // tell ryml to treat this type as a string
109 * template<> struct c4::is_string<foo::MyStringType> : std::true_type {};
110 * @endcode */
111template<class T>
112C4_ALWAYS_INLINE csubstr serialize_to_arena(Tree * tree, T const& scalar)
113{
114 if constexpr (_is_string_nocvref<T>::value)
115 return serialize_to_arena_str(tree, to_csubstr(scalar));
116 else
117 return serialize_to_arena_scalar<T>(tree, scalar);
118}
119
120#else // pre-C++17: need to use SFINAE
121
122template<class T>
123C4_ALWAYS_INLINE auto serialize_to_arena(Tree * tree, T const& scalar)
124 -> typename std::enable_if<_is_string_nocvref<T>::value, csubstr>::type
125{
126 return serialize_to_arena_str(tree, to_csubstr(scalar));
127}
128
129template<class T>
130C4_ALWAYS_INLINE auto serialize_to_arena(Tree * tree, T const& scalar)
131 -> typename std::enable_if< ! _is_string_nocvref<T>::value, csubstr>::type
132{
133 return serialize_to_arena_scalar<T>(tree, scalar);
134}
135
136#endif // pre-C++17: need to use SFINAE
137
138/** implementation for null values */
139C4_ALWAYS_INLINE csubstr serialize_to_arena(Tree * C4_RESTRICT, std::nullptr_t /*scalar*/) noexcept
140{
141 return csubstr{};
142}
143
144/** @} */
145
146
147//-----------------------------------------------------------------------------
148// fwd decl: write/read, write_key/read_key
149
150/** @cond dev */
151
152template<class T> void write(Tree * tree, id_type id, T const& v);
153template<class T> void write_key(Tree *, id_type id, T const& v);
154
155template<class T> C4_NODISCARD C4_ALWAYS_INLINE ReadResult read(Tree const* tree, id_type id, T * v);
156template<class T> C4_NODISCARD C4_ALWAYS_INLINE ReadResult read_key(Tree const* tree, id_type id, T * v);
157template<class Wrapper> C4_NODISCARD C4_ALWAYS_INLINE ReadResult read(Tree const* tree, id_type id, Wrapper const& w);
158template<class Wrapper> C4_NODISCARD C4_ALWAYS_INLINE ReadResult read_key(Tree const* tree, id_type id, Wrapper const& w);
159/** @endcond */
160
161
162//-----------------------------------------------------------------------------
163//-----------------------------------------------------------------------------
164//-----------------------------------------------------------------------------
165
166/** @addtogroup doc_tree
167 *
168 * @{
169 */
170
171/** a node scalar is a csubstr, which may be tagged and anchored. */
173{
177
178public:
179
180 /// initialize as an empty scalar
181 NodeScalar() noexcept : tag(), scalar(), anchor() {} // NOLINT
182
183 /// initialize as an untagged scalar
184 template<size_t N>
185 NodeScalar(const char (&s)[N]) noexcept : tag(), scalar(s), anchor() {} // NOLINT
186 NodeScalar(csubstr s ) noexcept : tag(), scalar(s), anchor() {} // NOLINT
187
188 /// initialize as a tagged scalar
189 template<size_t N, size_t M>
190 NodeScalar(const char (&t)[N], const char (&s)[N]) noexcept : tag(t), scalar(s), anchor() {}
191 NodeScalar(csubstr t , csubstr s ) noexcept : tag(t), scalar(s), anchor() {}
192
193public:
194
195 ~NodeScalar() noexcept = default;
196 NodeScalar(NodeScalar &&) noexcept = default;
197 NodeScalar(NodeScalar const&) noexcept = default;
198 NodeScalar& operator= (NodeScalar &&) noexcept = default;
199 NodeScalar& operator= (NodeScalar const&) noexcept = default;
200
201public:
202
203 bool empty() const noexcept { return tag.empty() && scalar.empty() && anchor.empty(); }
204
205 void clear() noexcept { tag.clear(); scalar.clear(); anchor.clear(); }
206
208 {
209 csubstr trimmed = ref.begins_with('*') ? ref.sub(1) : ref;
210 anchor = trimmed;
211 if((!has_scalar) || !scalar.ends_with(trimmed))
212 scalar = ref;
213 }
214};
215static_assert(std::is_trivially_copyable<NodeScalar>::value, "must be trivially copyable");
216
217
218//-----------------------------------------------------------------------------
219//-----------------------------------------------------------------------------
220//-----------------------------------------------------------------------------
221
222/** @cond dev */ // LCOV_EXCL_START
223struct RYML_DEPRECATED("") NodeInit
224{
225
226 NodeType type;
227 NodeScalar key;
228 NodeScalar val;
229
230public:
231
232 /// initialize as an empty node
233 NodeInit() : type(NOTYPE), key(), val() {}
234 /// initialize as a typed node
235 NodeInit(type_bits t) : type(t), key(), val() {} // NOLINT
236 /// initialize as a sequence member
237 NodeInit(NodeScalar const& v) : type(VAL), key(), val(v) { _add_flags(); } // NOLINT
238 /// initialize as a sequence member with explicit type
239 NodeInit(NodeScalar const& v, type_bits t) : type(t|VAL), key(), val(v) { _add_flags(); }
240 /// initialize as a mapping member
241 NodeInit( NodeScalar const& k, NodeScalar const& v) : type(KEYVAL), key(k), val(v) { _add_flags(); }
242 /// initialize as a mapping member with explicit type
243 NodeInit(type_bits t, NodeScalar const& k, NodeScalar const& v) : type(t), key(k), val(v) { _add_flags(); }
244 /// initialize as a mapping member with explicit type (eg for SEQ or MAP)
245 NodeInit(type_bits t, NodeScalar const& k ) : type(t), key(k), val( ) { _add_flags(KEY); }
246
247public:
248
249 void clear()
250 {
251 type.clear();
252 key.clear();
253 val.clear();
254 }
255
256 void _add_flags(type_bits more_flags=0)
257 {
258 type = (type.m_bits|more_flags);
259 if( ! key.tag.empty())
260 type = (type.m_bits|KEYTAG);
261 if( ! val.tag.empty())
262 type = (type.m_bits|VALTAG);
263 if( ! key.anchor.empty())
264 type = (type.m_bits|KEYANCH);
265 if( ! val.anchor.empty())
266 type = (type.m_bits|VALANCH);
267 }
268
269 bool _check() const
270 {
271 // key cannot be empty
272 RYML_ASSERT_BASIC_(key.scalar.empty() == ((type & KEY) == 0));
273 // key tag cannot be empty
274 RYML_ASSERT_BASIC_(key.tag.empty() == ((type & KEYTAG) == 0));
275 // val may be empty even though VAL is set. But when VAL is not set, val must be empty
276 RYML_ASSERT_BASIC_(((type & VAL) != 0) || val.scalar.empty());
277 // val tag cannot be empty
278 RYML_ASSERT_BASIC_(val.tag.empty() == ((type & VALTAG) == 0));
279 return true;
280 }
281};
282/** @endcond */ // LCOV_EXCL_STOP
283
284
285//-----------------------------------------------------------------------------
286//-----------------------------------------------------------------------------
287//-----------------------------------------------------------------------------
288
289/** contains the data for each YAML node. */
303static_assert(std::is_trivially_copyable<NodeData>::value, "must be trivially copyable");
304
305
306//-----------------------------------------------------------------------------
307//-----------------------------------------------------------------------------
308//-----------------------------------------------------------------------------
309
311{
312public:
313
314 /** @name construction and assignment */
315 /** @{ */
316
318 Tree(Callbacks const& cb); // NOLINT
319 Tree(id_type node_capacity, size_t arena_capacity=RYML_DEFAULT_TREE_ARENA_CAPACITY) : Tree(node_capacity, arena_capacity, get_callbacks()) {} // NOLINT
320 Tree(id_type node_capacity, size_t arena_capacity, Callbacks const& cb);
321
322 ~Tree() noexcept;
323
324 Tree(Tree const& that);
325 Tree(Tree && that) noexcept;
326
327 Tree& operator= (Tree const& that);
328 Tree& operator= (Tree && that) noexcept;
329
330 /** @} */
331
332public:
333
334 /** @name memory and sizing */
335 /** @{ */
336
337 void reserve(id_type node_capacity=RYML_DEFAULT_TREE_CAPACITY);
338
339 /** clear the tree
340 * @note does NOT clear the arena
341 * @see clear_arena() */
342 void clear();
343 void clear_arena() { m_arena_pos = 0; }
344
345 /** Query for zero size. The tree can be empty only when constructed with explicitly zero-capacity. */
346 bool empty() const { return m_size == 0; }
347
348 /** Get the number of existing nodes. */
349 id_type size() const { return m_size; }
350 id_type capacity() const { return m_cap; }
351 id_type slack() const { RYML_ASSERT_BASIC_(m_cap >= m_size); return m_cap - m_size; }
352
353 Callbacks const& callbacks() const { return m_callbacks; }
354 void callbacks(Callbacks const& cb) { m_callbacks = cb; }
355
356 /** @} */
357
358public:
359
360 /** @name node getters */
361 /** @{ */
362
363 //! get a pointer to a node's NodeData.
364 //! node can be NONE, in which case a nullptr is returned
365 NodeData *get(id_type node) // NOLINT(readability-make-member-function-const)
366 {
367 if(node == NONE)
368 return nullptr;
369 RYML_ASSERT_VISIT_CB_(m_callbacks, node >= 0 && node < m_cap, this, node);
370 return m_buf + node;
371 }
372 //! get a pointer to a node's NodeData.
373 //! node can be NONE, in which case a nullptr is returned.
374 NodeData const *get(id_type node) const
375 {
376 if(node == NONE)
377 return nullptr;
378 RYML_ASSERT_VISIT_CB_(m_callbacks, node >= 0 && node < m_cap, this, node);
379 return m_buf + node;
380 }
381
382 //! An if-less form of get() that demands a valid node index.
383 //! This function is implementation only; use at your own risk.
384 NodeData * _p(id_type node) { RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_first_unused, this, node); return m_buf + node; } // NOLINT(readability-make-member-function-const)
385 //! An if-less form of get() that demands a valid node index.
386 //! This function is implementation only; use at your own risk.
387 NodeData const * _p(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_first_unused, this, node); return m_buf + node; }
388
389 //! Get the id of the root node. The tree must not be empty. The tree can be empty only when constructed with explicitly zero-capacity.
390 id_type root_id() const { RYML_ASSERT_VISIT_CB_(m_callbacks, m_size > 0, this, id_type(0)); return 0; }
391 //! Get the id of the root node, or NONE if the tree is empty.
392 id_type root_id_maybe() const { return m_size ? 0 : id_type(NONE); }
393 //! get the id of a node belonging to this tree.
394 //! @p n can be nullptr, in which case NONE is returned
395 //! @p n must belong to this tree
396 id_type id(NodeData const* n) const
397 {
398 if( ! n)
399 return NONE;
400 RYML_ASSERT_VISIT_CB_(m_callbacks, n >= m_buf && n < m_buf + m_cap, this, NONE);
401 return static_cast<id_type>(n - m_buf);
402 }
403
404 /** @} */
405
406public:
407
408 /** @name NodeRef helpers */
409 /** @{ */
410
411 //! Get a NodeRef of a node by id
412 NodeRef ref(id_type node);
413 //! Get a NodeRef of a node by id
414 ConstNodeRef ref(id_type node) const;
415 //! Get a NodeRef of a node by id
416 ConstNodeRef cref(id_type node) const;
417
418 //! Get the root as a @ref NodeRef . Note that a non-const Tree implicitly converts to @ref NodeRef.
419 NodeRef rootref();
420 //! Get the root as a @ref ConstNodeRef . Note that Tree implicitly converts to @ref ConstNodeRef.
421 ConstNodeRef rootref() const;
422 //! Get the root as a @ref ConstNodeRef . Note that Tree implicitly converts to @ref ConstNodeRef.
423 ConstNodeRef crootref() const;
424
425 //! get the i-th document of the stream
426 //! @note @p i is NOT the node id, but the doc position within the stream
427 NodeRef docref(id_type i);
428 //! get the i-th document of the stream
429 //! @note @p i is NOT the node id, but the doc position within the stream
430 ConstNodeRef docref(id_type i) const;
431 //! get the i-th document of the stream
432 //! @note @p i is NOT the node id, but the doc position within the stream
433 ConstNodeRef cdocref(id_type i) const;
434
435 //! find a root child (ie child of root) by name, return it as a NodeRef
436 //! @note requires the root to be a map.
437 NodeRef operator[] (csubstr key);
438 //! find a root child (ie child of root) by name, return it as a NodeRef
439 //! @note requires the root to be a map.
440 ConstNodeRef operator[] (csubstr key) const;
441
442 //! find a root child (ie child of root) by index: return the root node's @p i-th child as a NodeRef
443 //! @note @p i is NOT the node id, but the child's position within the parent
444 NodeRef operator[] (id_type i);
445 //! find a root child (ie child of root) by index: return the root node's @p i-th child as a NodeRef
446 //! @note @p i is NOT the node id, but the child's position within the parent
447 ConstNodeRef operator[] (id_type i) const;
448
449 /** @} */
450
451public:
452
453 /** @name node property getters */
454 /** @{ */
455
456 NodeType type(id_type node) const { return _p(node)->m_type; }
457
458 csubstr const& key (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); return _p(node)->m_key.scalar; }
459 csubstr const& key_tag (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key_tag(node), this, node); return _p(node)->m_key.tag; }
460 csubstr const& key_ref (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, is_key_ref(node), this, node); return _p(node)->m_key.anchor; }
461 csubstr const& key_anchor(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key_anchor(node), this, node); return _p(node)->m_key.anchor; }
462 NodeScalar const& keysc (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); return _p(node)->m_key; }
463
464 csubstr const& val (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node), this, node); return _p(node)->m_val.scalar; }
465 csubstr const& val_tag (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val_tag(node), this, node); return _p(node)->m_val.tag; }
466 csubstr const& val_ref (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, is_val_ref(node), this, node); return _p(node)->m_val.anchor; }
467 csubstr const& val_anchor(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val_anchor(node), this, node); return _p(node)->m_val.anchor; }
468 NodeScalar const& valsc (id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node), this, node); return _p(node)->m_val; }
469
470 /** @} */
471
472public:
473
474 /** @name node type predicates */
475 /** @{ */
476
477 C4_ALWAYS_INLINE bool type_has_any(id_type node, type_bits bits) const { return _p(node)->m_type.has_any(bits); }
478 C4_ALWAYS_INLINE bool type_has_all(id_type node, type_bits bits) const { return _p(node)->m_type.has_all(bits); }
479 C4_ALWAYS_INLINE bool type_has_none(id_type node, type_bits bits) const { return _p(node)->m_type.has_none(bits); }
480
481 C4_ALWAYS_INLINE bool is_stream(id_type node) const { return _p(node)->m_type.is_stream(); }
482 C4_ALWAYS_INLINE bool is_doc(id_type node) const { return _p(node)->m_type.is_doc(); }
483 C4_ALWAYS_INLINE bool is_container(id_type node) const { return _p(node)->m_type.is_container(); }
484 C4_ALWAYS_INLINE bool is_map(id_type node) const { return _p(node)->m_type.is_map(); }
485 C4_ALWAYS_INLINE bool is_seq(id_type node) const { return _p(node)->m_type.is_seq(); }
486 C4_ALWAYS_INLINE bool has_key(id_type node) const { return _p(node)->m_type.has_key(); }
487 C4_ALWAYS_INLINE bool has_val(id_type node) const { return _p(node)->m_type.has_val(); }
488 C4_ALWAYS_INLINE bool is_val(id_type node) const { return _p(node)->m_type.is_val(); }
489 C4_ALWAYS_INLINE bool is_keyval(id_type node) const { return _p(node)->m_type.is_keyval(); }
490 C4_ALWAYS_INLINE bool has_key_tag(id_type node) const { return _p(node)->m_type.has_key_tag(); }
491 C4_ALWAYS_INLINE bool has_val_tag(id_type node) const { return _p(node)->m_type.has_val_tag(); }
492 C4_ALWAYS_INLINE bool has_key_anchor(id_type node) const { return _p(node)->m_type.has_key_anchor(); }
493 C4_ALWAYS_INLINE bool has_val_anchor(id_type node) const { return _p(node)->m_type.has_val_anchor(); }
494 C4_ALWAYS_INLINE bool has_anchor(id_type node) const { return _p(node)->m_type.has_anchor(); }
495 C4_ALWAYS_INLINE bool is_key_ref(id_type node) const { return _p(node)->m_type.is_key_ref(); }
496 C4_ALWAYS_INLINE bool is_val_ref(id_type node) const { return _p(node)->m_type.is_val_ref(); }
497 C4_ALWAYS_INLINE bool is_ref(id_type node) const { return _p(node)->m_type.is_ref(); }
498
499 C4_ALWAYS_INLINE bool parent_is_seq(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_parent(node), this, node); return is_seq(_p(node)->m_parent); }
500 C4_ALWAYS_INLINE bool parent_is_map(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_parent(node), this, node); return is_map(_p(node)->m_parent); }
501
502 /** true when the node has an anchor named a */
503 C4_ALWAYS_INLINE bool has_anchor(id_type node, csubstr a) const { return _p(node)->m_key.anchor == a || _p(node)->m_val.anchor == a; }
504
505 /** true if the node key is empty, or its scalar verifies @ref scalar_is_null().
506 * @warning the node must verify @ref Tree::has_key() (asserted) (ie must be a member of a map)
507 * @see https://github.com/biojppm/rapidyaml/issues/413 */
508 C4_ALWAYS_INLINE bool key_is_null(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_key_quoted() && (n->m_type.key_is_null() || scalar_is_null(n->m_key.scalar)); }
509 /** true if the node val is empty, or its scalar verifies @ref scalar_is_null().
510 * @warning the node must verify @ref Tree::has_val() (asserted) (ie must be a scalar / must not be a container)
511 * @see https://github.com/biojppm/rapidyaml/issues/413 */
512 C4_ALWAYS_INLINE bool val_is_null(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node), this, node); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_val_quoted() && (n->m_type.val_is_null() || scalar_is_null(n->m_val.scalar)); }
513
514 /// true if the key was a scalar requiring filtering and was left
515 /// unfiltered during the parsing (see ParserOptions)
516 C4_ALWAYS_INLINE bool is_key_unfiltered(id_type node) const { return _p(node)->m_type.is_key_unfiltered(); }
517 /// true if the val was a scalar requiring filtering and was left
518 /// unfiltered during the parsing (see ParserOptions)
519 C4_ALWAYS_INLINE bool is_val_unfiltered(id_type node) const { return _p(node)->m_type.is_val_unfiltered(); }
520
521 RYML_DEPRECATED("use has_key_anchor()") bool is_key_anchor(id_type node) const { return _p(node)->m_type.has_key_anchor(); }
522 RYML_DEPRECATED("use has_val_anchor()") bool is_val_anchor(id_type node) const { return _p(node)->m_type.has_val_anchor(); }
523 RYML_DEPRECATED("use has_anchor()") bool is_anchor(id_type node) const { return _p(node)->m_type.has_anchor(); }
524 RYML_DEPRECATED("use has_anchor_or_ref()") bool is_anchor_or_ref(id_type node) const { return _p(node)->m_type.has_anchor() || _p(node)->m_type.is_ref(); }
525
526 /** @} */
527
528public:
529
530 /** @name hierarchy predicates */
531 /** @{ */
532
533 bool is_root(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, _p(node)->m_parent != NONE || node == 0, this, node); return _p(node)->m_parent == NONE; }
534
535 bool has_parent(id_type node) const { return _p(node)->m_parent != NONE; }
536
537 /** true when ancestor is parent or parent of a parent of node */
538 bool is_ancestor(id_type node, id_type ancestor) const;
539
540 /** true when key and val are empty, and has no children */
541 bool empty(id_type node) const { return ! has_children(node) && _p(node)->m_key.empty() && (( ! (_p(node)->m_type & VAL)) || _p(node)->m_val.empty()); }
542
543 /** true if @p node has a child with id @p ch */
544 bool has_child(id_type node, id_type ch) const { return _p(ch)->m_parent == node; }
545 /** true if @p node has a child with key @p key */
546 bool has_child(id_type node, csubstr key) const { return find_child(node, key) != NONE; }
547 /** true if @p node has any children */
548 bool has_children(id_type node) const { return _p(node)->m_first_child != NONE; }
549
550 /** true if @p node has a sibling with id @p sib */
551 bool has_sibling(id_type node, id_type sib) const { return _p(node)->m_parent == _p(sib)->m_parent; }
552 /** true if one of the node's siblings has the given key */
553 bool has_sibling(id_type node, csubstr key) const { return find_sibling(node, key) != NONE; }
554 /** true if node is not a single child */
556 {
557 NodeData const *n = _p(node);
558 if C4_LIKELY(n->m_parent != NONE)
559 {
560 n = _p(n->m_parent);
561 return n->m_first_child != n->m_last_child;
562 }
563 return false;
564 }
565
566 /** @} */
567
568public:
569
570 /** @name hierarchy getters */
571 /** @{ */
572
573 id_type parent(id_type node) const { return _p(node)->m_parent; }
574
575 id_type prev_sibling(id_type node) const { return _p(node)->m_prev_sibling; }
576 id_type next_sibling(id_type node) const { return _p(node)->m_next_sibling; }
577
578 /** O(#num_children) */
579 id_type num_children(id_type node) const;
580 id_type child_pos(id_type node, id_type ch) const;
581 id_type first_child(id_type node) const { return _p(node)->m_first_child; }
582 id_type last_child(id_type node) const { return _p(node)->m_last_child; }
583 id_type child(id_type node, id_type pos) const; ///< find child by position, or NONE if there are less than pos children posi
584 id_type find_child(id_type node, csubstr const& key) const; ///< find child by name, or NONE if no child is found with this key
585 /// like @ref Tree::child(), but return a @ref ReadResult with the status
586 C4_NODISCARD ReadResult child_r(id_type node, id_type pos, id_type *child_id) const { return ReadResult(node, *child_id = child(node, pos)); }
587 /// like @ref Tree::find_child(), but return a @ref ReadResult with the status
588 C4_NODISCARD ReadResult find_child_r(id_type node, csubstr const& key, id_type *child_id) const { return ReadResult(node, *child_id = find_child(node, key)); }
589
590 /** O(#num_siblings) */
591 /** counts with this */
592 id_type num_siblings(id_type node) const { return is_root(node) ? 1 : num_children(_p(node)->m_parent); }
593 /** does not count with this */
594 id_type num_other_siblings(id_type node) const { id_type ns = num_siblings(node); RYML_ASSERT_VISIT_CB_(m_callbacks, ns > 0, this, node); return ns-1; }
595 id_type sibling_pos(id_type node, id_type sib) const { RYML_ASSERT_VISIT_CB_(m_callbacks, ! is_root(node) || node == root_id(), this, node); return child_pos(_p(node)->m_parent, sib); }
596 id_type first_sibling(id_type node) const { return is_root(node) ? node : _p(_p(node)->m_parent)->m_first_child; }
597 id_type last_sibling(id_type node) const { return is_root(node) ? node : _p(_p(node)->m_parent)->m_last_child; }
598 id_type sibling(id_type node, id_type pos) const { return child(_p(node)->m_parent, pos); }
599 id_type find_sibling(id_type node, csubstr const& key) const { return find_child(_p(node)->m_parent, key); }
600 /// like @ref Tree::sibling(), but return a @ref ReadResult with the status
601 C4_NODISCARD ReadResult sibling_r(id_type node, id_type pos, id_type *sibling_id) const { return child_r(_p(node)->m_parent, pos, sibling_id); }
602 /// like @ref Tree::find_sibling(), but return a @ref ReadResult if with the status
603 C4_NODISCARD ReadResult find_sibling_r(id_type node, csubstr const& key, id_type *sibling_id) const { return find_child_r(_p(node)->m_parent, key, sibling_id); }
604
605 id_type depth_asc(id_type node) const; /**< O(log(num_tree_nodes)) get the ascending depth of the node: number of levels between root and node */
606 id_type depth_desc(id_type node) const; /**< O(num_tree_nodes) get the descending depth of the node: number of levels between node and deepest child */
607
608 /** gets the @p i document node index. requires that the root node is a stream. */
609 id_type doc(id_type i) const { id_type rid = root_id(); RYML_ASSERT_VISIT_CB_(m_callbacks, is_stream(rid), this, rid); return child(rid, i); }
610
611 /** get the document which is a parent document of node i, or the root if the tree is not a stream */
613 {
614 NodeData const *nd;
615 do
616 {
617 nd = _p(node);
618 if(nd->m_type.is_doc() || nd->m_parent == NONE)
619 break;
620 node = nd->m_parent;
621 } while(nd->m_parent != NONE);
622 return node;
623 }
624
625 /** @} */
626
627public:
628
629 /** @name node style predicates and modifiers. see the corresponding predicate in NodeType */
630 /** @{ */
631
632 C4_ALWAYS_INLINE bool is_container_styled(id_type node) const { return _p(node)->m_type.is_container_styled(); }
633 C4_ALWAYS_INLINE bool is_block(id_type node) const { return _p(node)->m_type.is_block(); }
634 C4_ALWAYS_INLINE bool is_flow_sl(id_type node) const { return _p(node)->m_type.is_flow_sl(); }
635 RYML_DEPRECATED("use one of .is_flow_ml{1,n,x}()")
636 C4_ALWAYS_INLINE bool is_flow_ml(id_type node) const { return _p(node)->m_type.is_flow_ml1(); }
637 C4_ALWAYS_INLINE bool is_flow_ml1(id_type node) const { return _p(node)->m_type.is_flow_ml1(); }
638 C4_ALWAYS_INLINE bool is_flow_mln(id_type node) const { return _p(node)->m_type.is_flow_mln(); }
639 C4_ALWAYS_INLINE bool is_flow_mlx(id_type node) const { return _p(node)->m_type.is_flow_mlx(); }
640 C4_ALWAYS_INLINE bool is_flow(id_type node) const { return _p(node)->m_type.is_flow(); }
641 C4_ALWAYS_INLINE bool has_flow_space(id_type node) const { return _p(node)->m_type.has_flow_space(); }
642
643 C4_ALWAYS_INLINE bool is_key_styled(id_type node) const { return _p(node)->m_type.is_key_styled(); }
644 C4_ALWAYS_INLINE bool is_val_styled(id_type node) const { return _p(node)->m_type.is_val_styled(); }
645 C4_ALWAYS_INLINE bool is_key_literal(id_type node) const { return _p(node)->m_type.is_key_literal(); }
646 C4_ALWAYS_INLINE bool is_val_literal(id_type node) const { return _p(node)->m_type.is_val_literal(); }
647 C4_ALWAYS_INLINE bool is_key_folded(id_type node) const { return _p(node)->m_type.is_key_folded(); }
648 C4_ALWAYS_INLINE bool is_val_folded(id_type node) const { return _p(node)->m_type.is_val_folded(); }
649 C4_ALWAYS_INLINE bool is_key_squo(id_type node) const { return _p(node)->m_type.is_key_squo(); }
650 C4_ALWAYS_INLINE bool is_val_squo(id_type node) const { return _p(node)->m_type.is_val_squo(); }
651 C4_ALWAYS_INLINE bool is_key_dquo(id_type node) const { return _p(node)->m_type.is_key_dquo(); }
652 C4_ALWAYS_INLINE bool is_val_dquo(id_type node) const { return _p(node)->m_type.is_val_dquo(); }
653 C4_ALWAYS_INLINE bool is_key_plain(id_type node) const { return _p(node)->m_type.is_key_plain(); }
654 C4_ALWAYS_INLINE bool is_val_plain(id_type node) const { return _p(node)->m_type.is_val_plain(); }
655 C4_ALWAYS_INLINE bool is_key_quoted(id_type node) const { return _p(node)->m_type.is_key_quoted(); }
656 C4_ALWAYS_INLINE bool is_val_quoted(id_type node) const { return _p(node)->m_type.is_val_quoted(); }
657 C4_ALWAYS_INLINE bool is_quoted(id_type node) const { return _p(node)->m_type.is_quoted(); }
658
659 C4_ALWAYS_INLINE NodeType key_style(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); return _p(node)->m_type.key_style(); }
660 C4_ALWAYS_INLINE NodeType val_style(id_type node) const { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node) || is_root(node), this, node); return _p(node)->m_type.val_style(); }
661
662 C4_ALWAYS_INLINE void set_container_style(id_type node, type_bits style) { RYML_ASSERT_VISIT_CB_(m_callbacks, is_container(node), this, node); _p(node)->m_type.set_container_style(style); }
663 C4_ALWAYS_INLINE void set_key_style(id_type node, type_bits style) { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); _p(node)->m_type.set_key_style(style); }
664 C4_ALWAYS_INLINE void set_val_style(id_type node, type_bits style) { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node), this, node); _p(node)->m_type.set_val_style(style); }
665
666 void clear_style(id_type node, bool recurse=false);
667 void set_style_conditionally(id_type node,
668 NodeType type_mask,
669 NodeType rem_style_flags,
670 NodeType add_style_flags,
671 bool recurse=false);
672 /** @} */
673
674public:
675
676 /** @name node type modifiers */
677 /** @{ */
678
680 {
681 RYML_ASSERT_VISIT_CB_(m_callbacks, is_root(node), this, node);
682 RYML_ASSERT_VISIT_CB_(m_callbacks, (_p(node)->m_type & (DOC|MAP|VAL)) == 0, this, node);
683 _p(node)->m_type |= STREAM;
684 }
685
686 void set_doc(id_type node)
687 {
688 RYML_ASSERT_VISIT_CB_(m_callbacks, is_root(node) || (is_root(parent(node)) && is_stream(parent(node))), this, node);
689 _p(node)->m_type |= DOC;
690 }
691
693 {
694 RYML_ASSERT_VISIT_CB_(m_callbacks, (_p(node)->m_type & (SEQ|MAP)) == 0, this, node);
695 RYML_ASSERT_VISIT_CB_(m_callbacks, (parent(node) == NONE || (_p(parent(node))->m_type & (SEQ|MAP))), this, node);
696 NodeData *C4_RESTRICT nd = _p(node);
697 nd->m_type |= VAL;
698 nd->m_val.scalar = val;
699 }
701 {
702 RYML_ASSERT_VISIT_CB_(m_callbacks, (_p(node)->m_type & (SEQ|MAP)) == 0, this, node);
703 RYML_ASSERT_VISIT_CB_(m_callbacks, (parent(node) == NONE || (_p(parent(node))->m_type & (SEQ|MAP))), this, node);
704 NodeData *C4_RESTRICT nd = _p(node);
705 nd->m_type |= VAL|more_flags;
706 nd->m_val.scalar = val;
707 }
708
710 {
711 RYML_ASSERT_VISIT_CB_(m_callbacks, (parent(node) != NONE && (_p(parent(node))->m_type & MAP)), this, node);
712 NodeData *C4_RESTRICT nd = _p(node);
713 nd->m_type |= KEY;
714 nd->m_key.scalar = key;
715 }
717 {
718 RYML_ASSERT_VISIT_CB_(m_callbacks, (parent(node) != NONE && (_p(parent(node))->m_type & MAP)), this, node);
719 NodeData *C4_RESTRICT nd = _p(node);
720 nd->m_type |= KEY|more_flags;
721 nd->m_key.scalar = key;
722 }
723
725 {
726 RYML_ASSERT_VISIT_CB_(m_callbacks, (_p(node)->m_type & (VAL|MAP)) == 0, this, node);
727 _p(node)->m_type |= SEQ;
728 }
729 void set_seq(id_type node, NodeType more_flags) RYML_NOEXCEPT
730 {
731 RYML_ASSERT_VISIT_CB_(m_callbacks, ((_p(node)->m_type|more_flags) & (VAL|MAP)) == 0, this, node);
732 _p(node)->m_type |= SEQ|more_flags;
733 }
734
736 {
737 RYML_ASSERT_VISIT_CB_(m_callbacks, (_p(node)->m_type & (VAL|SEQ)) == 0, this, node);
738 _p(node)->m_type |= MAP;
739 }
740 void set_map(id_type node, NodeType more_flags) RYML_NOEXCEPT
741 {
742 RYML_ASSERT_VISIT_CB_(m_callbacks, ((_p(node)->m_type|more_flags) & (VAL|SEQ)) == 0, this, node);
743 _p(node)->m_type |= MAP|more_flags;
744 }
745
746 void set_key_tag(id_type node, csubstr tag) { RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node); _p(node)->m_key.tag = tag; _add_flags(node, KEYTAG); }
747 void set_val_tag(id_type node, csubstr tag) { RYML_ASSERT_VISIT_CB_(m_callbacks, has_val(node) || is_container(node), this, node); _p(node)->m_val.tag = tag; _add_flags(node, VALTAG); }
748
749 void set_key_anchor(id_type node, csubstr anchor) { RYML_ASSERT_VISIT_CB_(m_callbacks, ! is_key_ref(node), this, node); _p(node)->m_key.anchor = anchor.triml('&'); _add_flags(node, KEYANCH); }
750 void set_val_anchor(id_type node, csubstr anchor) { RYML_ASSERT_VISIT_CB_(m_callbacks, ! is_val_ref(node), this, node); _p(node)->m_val.anchor = anchor.triml('&'); _add_flags(node, VALANCH); }
751 void set_key_ref (id_type node, csubstr ref ) { RYML_ASSERT_VISIT_CB_(m_callbacks, ! has_key_anchor(node), this, node); NodeData* C4_RESTRICT n = _p(node); n->m_key.set_ref_maybe_replacing_scalar(ref, n->m_type.has_key()); _add_flags(node, KEY|KEYREF); }
752 void set_val_ref (id_type node, csubstr ref ) { RYML_ASSERT_VISIT_CB_(m_callbacks, ! has_val_anchor(node), this, node); NodeData* C4_RESTRICT n = _p(node); n->m_val.set_ref_maybe_replacing_scalar(ref, n->m_type.has_val()); _add_flags(node, VAL|VALREF); }
753
754 void rem_key_anchor(id_type node) { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYANCH); }
755 void rem_val_anchor(id_type node) { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALANCH); }
756 void rem_key_ref (id_type node) { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYREF); }
757 void rem_val_ref (id_type node) { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALREF); }
758 void rem_anchor_ref(id_type node) { _p(node)->m_key.anchor.clear(); _p(node)->m_val.anchor.clear(); _rem_flags(node, KEYANCH|VALANCH|KEYREF|VALREF); }
759
760 /** @} */
761
762private:
763
764 C4_NORETURN C4_NO_INLINE C4_COLD
765 void err_visit_(id_type node) const
766 {
767 RYML_ERR_VISIT_CB_(m_callbacks, this, node, "invalid node");
768 }
769
770public:
771
772 /** @name serialization - checked */
773 /** @{ */
774
775 template<class T>
776 C4_ALWAYS_INLINE void save(id_type node, T const& val)
777 {
778 if C4_LIKELY(node != NONE && node < m_first_unused && node >= 0)
779 {
780 write(this, node, val);
781 return;
782 }
783 err_visit_(node);
784 }
785 template<class T>
786 C4_ALWAYS_INLINE void save(id_type node, T const& val, NodeType more_flags)
787 {
788 if C4_LIKELY(node != NONE && node < m_first_unused && node >= 0)
789 {
790 write(this, node, val);
791 _p(node)->m_type |= more_flags;
792 return;
793 }
794 err_visit_(node);
795 }
796
797 template<class T>
798 C4_ALWAYS_INLINE void save_key(id_type node, T const& key)
799 {
800 if C4_LIKELY(node != NONE && node < m_first_unused && node >= 0)
801 {
802 write_key(this, node, key);
803 return;
804 }
805 err_visit_(node);
806 }
807 template<class T>
808 C4_ALWAYS_INLINE void save_key(id_type node, T const& key, NodeType more_flags)
809 {
810 if C4_LIKELY(node != NONE && node < m_first_unused && node >= 0)
811 {
812 write_key(this, node, key);
813 _p(node)->m_type |= more_flags;
814 return;
815 }
816 err_visit_(node);
817 }
818
819 /** @} */
820
821public:
822
823 /** @name serialization - asserted */
824 /** @{ */
825
826 template<class T>
827 C4_ALWAYS_INLINE void set_serialized(id_type node, T const& val) RYML_NOEXCEPT
828 {
829 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
830 write(this, node, val);
831 }
832 template<class T>
833 C4_ALWAYS_INLINE void set_serialized(id_type node, T const& val, NodeType more_flags) RYML_NOEXCEPT
834 {
835 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
836 write(this, node, val);
837 _p(node)->m_type |= more_flags;
838 }
839
840 template<class T>
841 C4_ALWAYS_INLINE void set_key_serialized(id_type node, T const& key) RYML_NOEXCEPT
842 {
843 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
844 write_key(this, node, key);
845 }
846 template<class T>
847 C4_ALWAYS_INLINE void set_key_serialized(id_type node, T const& key, NodeType more_flags) RYML_NOEXCEPT
848 {
849 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
850 write_key(this, node, key);
851 _p(node)->m_type |= more_flags;
852 }
853
854 /** @} */
855
856public:
857
858 /** @name deserialization - checked
859 *
860 * These methods raise an error if the deserialization failed or
861 * optionally if the node is not readable.
862 */
863 /** @{ */
864
865 static C4_ALWAYS_INLINE ReadResult to_result_(bool, id_type node) noexcept { return ReadResult(node); }
866 static C4_ALWAYS_INLINE ReadResult to_result_(ReadResult notlegacy, id_type) noexcept { return notlegacy; }
867
868 /** (1) deserialize the node's contents (val or container) to the
869 * given variable, forwarding to the user-overrideable @ref read()
870 * function (see @ref doc_serialization_tree_read). This function
871 * differs from @ref Tree::deserialize() in that here the error
872 * callback is called if the deserialization failed, or
873 * (optionally) the node is not readable. */
874 template<class T>
875 C4_ALWAYS_INLINE void load(id_type node, T * v, bool check_readable=true) const
876 {
877 const bool can_read_val = (node != NONE && node < m_first_unused && node >= 0 && (_p(node)->m_type & (VAL|MAP|SEQ)));
878 RYML_ASSERT_VISIT_CB_(m_callbacks, can_read_val, this, node);
879 if C4_LIKELY(!check_readable || can_read_val)
880 {
881 const ReadResult result(read(this, node, v), node);
882 if C4_LIKELY(result)
883 return;
884 else
885 node = result.node;
886 }
887 err_visit_(node);
888 }
889 /** (2) like (1), but for wrapper tag types such as @ref
890 * c4::fmt::base64() */
891 template<class Wrapper>
892 C4_ALWAYS_INLINE void load(id_type node, Wrapper const& w, bool check_readable=true) const
893 {
894 RYML_CHECK_TYPE_IS_WRAPPER_LIKE_(Wrapper);
895 const bool can_read_val = (node != NONE && node < m_first_unused && node >= 0 && (_p(node)->m_type & (VAL|MAP|SEQ)));
896 RYML_ASSERT_VISIT_CB_(m_callbacks, can_read_val, this, node);
897 if C4_LIKELY(!check_readable || can_read_val)
898 {
899 const ReadResult result(read(this, node, w), node);
900 if C4_LIKELY(result)
901 return;
902 else
903 node = result.node;
904 }
905 err_visit_(node);
906 }
907
908 /** (1) deserialize the node's key (necessarily a scalar) to the
909 * given variable, forwarding to the user-overrideable @ref
910 * read_key() function (see @ref
911 * doc_serialization_node_read). This function differs from @ref
912 * Tree::deserialize_key() in that here the error callback is
913 * called if the deserialization failed, or (optionally) the node
914 * is not readable. */
915 template<class T>
916 C4_ALWAYS_INLINE void load_key(id_type node, T * k, bool check_readable=true) const
917 {
918 const bool can_read_key = (node != NONE && node < m_first_unused && node >= 0 && (_p(node)->m_type & KEY));
919 RYML_ASSERT_VISIT_CB_(m_callbacks, can_read_key, this, node);
920 if C4_LIKELY(!check_readable || can_read_key)
921 {
922 const ReadResult result(read_key(this, node, k), node);
923 if C4_LIKELY(result)
924 return;
925 else
926 node = result.node;
927 }
928 err_visit_(node);
929 }
930 /** (2) like (1), but for wrapper tag types such as @ref
931 * c4::fmt::base64() */
932 template<class Wrapper>
933 C4_ALWAYS_INLINE void load_key(id_type node, Wrapper const& w, bool check_readable=true) const
934 {
935 RYML_CHECK_TYPE_IS_WRAPPER_LIKE_(Wrapper);
936 bool can_read_key = (node != NONE && node < m_first_unused && node >= 0 && (_p(node)->m_type & KEY));
937 RYML_ASSERT_VISIT_CB_(m_callbacks, can_read_key, this, node);
938 if C4_LIKELY(!check_readable || can_read_key)
939 {
940 const ReadResult result(read_key(this, node, w), node);
941 if C4_LIKELY(result)
942 return;
943 else
944 node = result.node;
945 }
946 err_visit_(node);
947 }
948
949 /** @} */
950
951public:
952
953 /** @name deserialization - asserted preconditions */
954 /** @{ */
955
956 /** (1) deserialize a node's contents to a variable */
957 template<class T>
958 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize(id_type node, T * v) const
959 {
960 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
961 RYML_ASSERT_VISIT_CB_(m_callbacks, _p(node)->m_type & (VAL|MAP|SEQ), this, node);
962 // use the adapter ctor to accomodate legacy read() implementations
963 return ReadResult(read(this, node, v), node);
964 }
965 /** (2) like (1), but for a wrapper type like those used in tag
966 * functions */
967 template<class Wrapper>
968 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize(id_type node, Wrapper const& w) const
969 {
970 RYML_CHECK_TYPE_IS_WRAPPER_LIKE_(Wrapper);
971 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
972 RYML_ASSERT_VISIT_CB_(m_callbacks, _p(node)->m_type & (VAL|MAP|SEQ), this, node);
973 // use the adapter ctor to accomodate legacy read() implementations
974 return ReadResult(read(this, node, w), node);
975 }
976
977 /** (1) deserialize a node's key to a variable */
978 template<class T>
979 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_key(id_type node, T * v) const
980 {
981 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
982 RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node);
983 // use the adapter ctor to accomodate legacy read_key() implementations
984 return ReadResult(read_key(this, node, v), node);
985 }
986 /** (2) like (1), but for a wrapper type like those used in tag
987 * functions */
988 template<class Wrapper>
989 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_key(id_type node, Wrapper const& w) const
990 {
991 RYML_CHECK_TYPE_IS_WRAPPER_LIKE_(Wrapper);
992 RYML_ASSERT_VISIT_CB_(m_callbacks, node != NONE && node >= 0 && node < m_cap, this, node);
993 RYML_ASSERT_VISIT_CB_(m_callbacks, has_key(node), this, node);
994 // use the adapter ctor to accomodate legacy read_key() implementations
995 return ReadResult(read_key(this, node, w), node);
996 }
997
998 /** @} */
999
1000public:
1001
1002 /** @name lookup and deserialize */
1003 /** @{ */
1004
1005 /** (1) find a child by name and deserialize its contents to the
1006 * given variable (ie call .deserialize() on the child if it
1007 * exists). Otherwise, the variable is kept unchanged.
1008 *
1009 * @return a @ref ReadResult set with this node's id if no child
1010 * exists, or the ReadResult from the deserialization.
1011 *
1012 * @see see also @ref Tree::find_child_r()
1013 */
1014 template<class T>
1015 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, csubstr child_key, T * v) const
1016 {
1017 id_type ch;
1018 ReadResult r = find_child_r(node, child_key, &ch);
1019 if(r)
1020 r = deserialize(ch, v);
1021 return r;
1022 }
1023 /** (2) like (1), but assign from fallback if no such child exists. */
1024 template<class T>
1025 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, csubstr child_key, T * v, T const& fallback) const
1026 {
1027 id_type ch;
1028 if(find_child_r(node, child_key, &ch))
1029 return deserialize(ch, v);
1030 *v = fallback;
1031 return ReadResult();
1032 }
1033 /** (3) like (1), but for wrapper tag types such as @ref c4::fmt::base64() */
1034 template<class Wrapper>
1035 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, csubstr child_key, Wrapper const& wrapper) const
1036 {
1037 id_type ch;
1038 ReadResult r = find_child_r(node, child_key, &ch);
1039 if(r)
1040 r = deserialize(ch, wrapper);
1041 return r;
1042 }
1043
1044 /** (1) find a child by position and deserialize its contents to
1045 * the given variable (ie call .deserialize() on the child if it
1046 * exists). Otherwise, the variable is kept unchanged.
1047 *
1048 * @return a @ref ReadResult set with this node's id if no child
1049 * exists, or the ReadResult from the deserialization.
1050 *
1051 * @see see also @ref Tree::child_r()
1052 */
1053 template<class T>
1054 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, id_type child_pos, T * v) const
1055 {
1056 RYML_ASSERT_VISIT_CB_(m_callbacks, is_container(node), this, node); // this assertion is needed because child_r() does not assert, contrary to find_child_r()
1057 id_type ch;
1058 ReadResult r = child_r(node, child_pos, &ch);
1059 if(r)
1060 r = deserialize(ch, v);
1061 return r;
1062 }
1063 /** (2) like (1), but assign from fallback if no such child exists */
1064 template<class T>
1065 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, id_type child_pos, T * v, T const& fallback) const
1066 {
1067 RYML_ASSERT_VISIT_CB_(m_callbacks, is_container(node), this, node); // this assertion is needed because child_r() does not assert, contrary to find_child_r()
1068 id_type ch;
1069 if(child_r(node, child_pos, &ch))
1070 return deserialize(ch, v);
1071 *v = fallback;
1072 return ReadResult();
1073 }
1074 /** (3) like (1), but for wrapper tag types such as @ref
1075 * c4::fmt::base64() */
1076 template<class Wrapper>
1077 C4_NODISCARD C4_ALWAYS_INLINE ReadResult deserialize_child(id_type node, id_type child_pos, Wrapper const& wrapper) const
1078 {
1079 RYML_ASSERT_VISIT_CB_(m_callbacks, is_container(node), this, node); // this assertion is needed because child_r() does not assert, contrary to find_child_r()
1080 id_type ch;
1081 ReadResult r = child_r(node, child_pos, &ch);
1082 if(r)
1083 r = deserialize(ch, wrapper);
1084 return r;
1085 }
1086
1087 /** @} */
1088
1089public:
1090
1091 /** @name tree modifiers */
1092 /** @{ */
1093
1094 /** reorder the tree in memory so that all the nodes are stored
1095 * in a linear sequence when visited in depth-first order.
1096 * This will invalidate existing ids, since the node id is its
1097 * position in the tree's node array. */
1098 void reorder();
1099
1100 /** @} */
1101
1102public:
1103
1104 /** @name anchors and references/aliases */
1105 /** @{ */
1106
1107 /** Resolve references (aliases <- anchors), by forwarding to @ref
1108 * ReferenceResolver::resolve(). */
1109 void resolve(ReferenceResolver *C4_RESTRICT rr, ResolveOptions const& opts={});
1110
1111 /** Resolve references (aliases <- anchors), by forwarding to @ref
1112 * ReferenceResolver::resolve(). This overload
1113 * uses a throwaway resolver object. */
1114 void resolve(ResolveOptions const& opts={});
1115
1116 /** @} */
1117 /** @cond dev */
1118 RYML_DEPRECATED("use the overload receiving ResolveOptions")
1119 void resolve(ReferenceResolver *C4_RESTRICT rr, bool clear_anchors)
1120 {
1121 resolve(rr, ResolveOptions{}.clear_anchors(clear_anchors));
1122 }
1123 RYML_DEPRECATED("use the overload receiving ResolveOptions")
1124 void resolve(bool clear_anchors)
1125 {
1126 resolve(ResolveOptions{}.clear_anchors(clear_anchors));
1127 }
1128 /** @endcond */
1129
1130public:
1131
1132 /** @name tags and tag directives */
1133 /** @{ */
1134
1135 /** Resolve tags in the tree such as \c "!!str" ->
1136 * \c "<tag:yaml.org,2002:str>", \c "!foo" -> \c "<!foo>" and custom tags
1137 * as well, ie tags of the form \c "!handle!tag" for which there is a
1138 * corresponding \c "%TAG" directive
1139 *
1140 * @param cache an object of type @ref TagCache to minimize memory
1141 * usage by avoiding repeated instantiation of the resolved
1142 * tags in the tree's arena.
1143 *
1144 * @param all if true, resolve all tags; if false resolve only
1145 * custom tags, ie those that have a prefix such as
1146 * \c "!m!tag" with a matching \c "\%TAG" directive */
1147 void resolve_tags(TagCache &cache, bool all=true);
1148 void normalize_tags();
1149 void normalize_tags_long();
1150
1151 id_type num_tag_directives() const;
1152 void add_tag_directive(csubstr handle, csubstr prefix, id_type id);
1153 void clear_tag_directives();
1154
1155 /** resolve the given tag, appearing at node_id. Write the result into output.
1156 * @return the number of characters required for the resolved tag */
1157 size_t resolve_tag(substr output, csubstr tag, id_type node_id) const;
1158 /** Wrapper for @ref Tree::resolve_tag(), returning a substring */
1159 csubstr resolve_tag_sub(substr output, csubstr tag, id_type node_id) const
1160 {
1161 size_t needed = resolve_tag(output, tag, node_id);
1162 return needed <= output.len ? output.first(needed) : output;
1163 }
1164
1166
1167 /** @cond dev */
1168 RYML_DEPRECATED("use c4::yml::tag_directive_const_iterator") typedef TagDirective const* tag_directive_const_iterator;
1169 RYML_DEPRECATED("use c4::yml::TagDirectiveRange") typedef c4::yml::TagDirectiveRange TagDirectiveProxy;
1170 /** @endcond */
1171
1172 /** @} */
1173
1174public:
1175
1176 /** @name modifying hierarchy */
1177 /** @{ */
1178
1179 /** create and insert a new child of @p parent. insert after the (to-be)
1180 * sibling @p after, which must be a child of @p parent. To insert as the
1181 * first child, set after to NONE */
1182 C4_ALWAYS_INLINE id_type insert_child(id_type parent, id_type after)
1183 {
1184 RYML_ASSERT_VISIT_CB_(m_callbacks, parent != NONE, this, parent);
1185 RYML_ASSERT_VISIT_CB_(m_callbacks, is_container(parent) || is_root(parent), this, parent);
1186 RYML_ASSERT_VISIT_CB_(m_callbacks, after == NONE || (_p(after)->m_parent == parent), this, parent);
1187 id_type child = _claim();
1188 _set_hierarchy(child, parent, after);
1189 return child;
1190 }
1191 /** create and insert a node as the first child of @p parent */
1193 /** create and insert a node as the last child of @p parent */
1194 C4_ALWAYS_INLINE id_type append_child(id_type parent) { return insert_child(parent, _p(parent)->m_last_child); }
1196 {
1197 id_type child = _claim();
1198 _set_hierarchy(child, parent, _p(parent)->m_last_child);
1199 return child;
1200 }
1201
1202public:
1203
1204 //! create and insert a new sibling of n. insert after "after"
1205 C4_ALWAYS_INLINE id_type insert_sibling(id_type node, id_type after)
1206 {
1207 return insert_child(_p(node)->m_parent, after);
1208 }
1209 /** create and insert a node as the first node of @p parent */
1210 C4_ALWAYS_INLINE id_type prepend_sibling(id_type node) { return prepend_child(_p(node)->m_parent); }
1211 C4_ALWAYS_INLINE id_type append_sibling(id_type node) { return append_child(_p(node)->m_parent); }
1212
1213public:
1214
1215 /** remove an entire branch at once: ie remove the children and the node itself */
1216 void remove(id_type node)
1217 {
1218 remove_children(node);
1219 _release(node);
1220 }
1221
1222 /** remove all the node's children, but keep the node itself */
1223 void remove_children(id_type node);
1224
1225public:
1226
1227 /** change the node's position in the parent */
1228 void move(id_type node, id_type after);
1229
1230 /** change the node's parent and position */
1231 void move(id_type node, id_type new_parent, id_type after);
1232
1233 /** change the node's parent and position to a different tree
1234 * @return the index of the new node in the destination tree */
1235 id_type move(Tree * src, id_type node, id_type new_parent, id_type after);
1236
1237 /** ensure the first node is a stream. Eg, change this tree
1238 *
1239 * DOCMAP
1240 * MAP
1241 * KEYVAL
1242 * KEYVAL
1243 * SEQ
1244 * VAL
1245 *
1246 * to
1247 *
1248 * STREAM
1249 * DOCMAP
1250 * MAP
1251 * KEYVAL
1252 * KEYVAL
1253 * SEQ
1254 * VAL
1255 *
1256 * If the root is already a stream, this is a no-op.
1257 */
1258 void set_root_as_stream();
1259
1260 bool change_type(id_type node, NodeType type);
1262 {
1263 return change_type(node, (NodeType)type);
1264 }
1265
1266public:
1267
1268 /** recursively duplicate a node from this tree into a new parent,
1269 * placing it after one of its children
1270 * @return the index of the copy */
1271 id_type duplicate(id_type node, id_type new_parent, id_type after);
1272 /** recursively duplicate a node from a different tree into a new parent,
1273 * placing it after one of its children
1274 * @return the index of the copy */
1275 id_type duplicate(Tree const* src, id_type node, id_type new_parent, id_type after);
1276
1277 /** recursively duplicate the node's children (but not the node)
1278 * @return the index of the last duplicated child */
1279 id_type duplicate_children(id_type node, id_type parent, id_type after);
1280 /** recursively duplicate the node's children (but not the node), where
1281 * the node is from a different tree
1282 * @return the index of the last duplicated child */
1283 id_type duplicate_children(Tree const* src, id_type node, id_type parent, id_type after);
1284
1285 /** duplicate the node's children (but not the node) in a new parent, but
1286 * omit repetitions where a duplicated node has the same key (in maps) or
1287 * value (in seqs). If one of the duplicated children has the same key
1288 * (in maps) or value (in seqs) as one of the parent's children, the one
1289 * that is placed closest to the end will prevail. */
1290 id_type duplicate_children_no_rep(id_type node, id_type parent, id_type after);
1291 id_type duplicate_children_no_rep(Tree const* src, id_type node, id_type parent, id_type after);
1292
1293 void duplicate_contents(id_type node, id_type where);
1294 void duplicate_contents(Tree const* src, id_type node, id_type where);
1295
1296public:
1297
1298 void merge_with(Tree const* src, id_type src_node=NONE, id_type dst_root=NONE);
1299
1300 /** @} */
1301
1302public:
1303
1304 /** @name locations */
1305 /** @{ */
1306
1307 /** Get the location of a node from the parse used to parse this tree. */
1308 Location location(Parser const& p, id_type node) const;
1309
1310private:
1311
1312 bool _location_from_node(Parser const& p, id_type node, Location *C4_RESTRICT loc, id_type level) const;
1313 bool _location_from_cont(Parser const& p, id_type node, Location *C4_RESTRICT loc) const;
1314
1315 /** @} */
1316
1317public:
1318
1319 /** @name internal string arena */
1320 /** @{ */
1321
1322 /** get the current size of the tree's internal arena */
1323 size_t arena_size() const { return m_arena_pos; }
1324 /** get the current capacity of the tree's internal arena */
1325 size_t arena_capacity() const { return m_arena.len; }
1326 /** get the current slack of the tree's internal arena */
1327 size_t arena_slack() const { RYML_ASSERT_VISIT_CB_(m_callbacks, m_arena.len >= m_arena_pos, this, NONE); return m_arena.len - m_arena_pos; }
1328 RYML_DEPRECATED("use arena_size() instead") size_t arena_pos() const { return m_arena_pos; }
1329
1330 /** get the current arena */
1331 csubstr arena() const { return m_arena.first(m_arena_pos); }
1332 /** get the current arena */
1333 substr arena() { return m_arena.first(m_arena_pos); } // NOLINT(readability-make-member-function-const)
1334
1335 /** get the free space at the end of the arena */
1336 csubstr arena_rem() const { return m_arena.sub(m_arena_pos); }
1337 /** get the free space at the end of the arena */
1338 substr arena_rem() { return m_arena.sub(m_arena_pos); } // NOLINT(readability-make-member-function-const)
1339
1340 /** return true if the given substring is part of the tree's string arena */
1341 C4_ALWAYS_INLINE bool in_arena(csubstr s) const
1342 {
1343 return m_arena.is_super(s);
1344 }
1345
1346 /** serialize the given variable to the tree's
1347 * arena, growing it as needed to accomodate the serialization.
1348 *
1349 * @note To customize how the type gets serialized to a string,
1350 * you can overload c4::to_chars(substr, T const&)
1351 *
1352 * @note To customize how the type gets serialized to the arena,
1353 * you can overload @ref scalar_serialize()
1354 *
1355 * @note Growing the arena may cause relocation of the entire
1356 * existing arena, and thus change the contents of individual
1357 * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
1358 * cost, ensure that the arena is reserved to an appropriate size
1359 * using @ref Tree::reserve_arena().
1360 *
1361 * @see alloc_arena() */
1362 template<class T>
1363 C4_ALWAYS_INLINE csubstr to_arena(T const& C4_RESTRICT a)
1364 {
1365 return serialize_to_arena(this, a);
1366 }
1367
1368 /** copy the given string to the tree's arena, growing the arena
1369 * by the required size.
1370 *
1371 * @note this method differs from @ref to_arena() in that it
1372 * returns a mutable substr, and further it does not deal
1373 * with some corner cases for null/empty strings
1374 *
1375 * @note Growing the arena may cause relocation of the entire
1376 * existing arena, and thus change the contents of individual
1377 * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
1378 * cost, ensure that the arena is reserved to an appropriate size
1379 * before using @ref Tree::reserve_arena()
1380 *
1381 * @see reserve_arena()
1382 * @see alloc_arena()
1383 */
1385 {
1386 RYML_ASSERT_VISIT_CB_(m_callbacks, !s.overlaps(m_arena), this, NONE);
1387 substr cp = alloc_arena(s.len);
1388 RYML_ASSERT_VISIT_CB_(m_callbacks, cp.len == s.len, this, NONE);
1389 RYML_ASSERT_VISIT_CB_(m_callbacks, !s.overlaps(cp), this, NONE);
1390 C4_SUPPRESS_WARNING_GCC_PUSH
1391 #if (!defined(__clang__)) && (defined(__GNUC__) && __GNUC__ >= 10)
1392 C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow=") // no need for terminating \0
1393 C4_SUPPRESS_WARNING_GCC("-Wrestrict") // there's an assert to ensure no violation of restrict behavior
1394 #endif
1395 if(s.len)
1396 memcpy(cp.str, s.str, s.len);
1397 C4_SUPPRESS_WARNING_GCC_POP
1398 return cp;
1399 }
1400
1401 /** grow the tree's string arena by the given size and return a substr
1402 * of the added portion
1403 *
1404 * @note Growing the arena may cause relocation of the entire
1405 * existing arena, and thus change the contents of individual
1406 * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
1407 * cost, ensure that the arena is reserved to an appropriate size
1408 * using @ref Tree::reserve_arena().
1409 *
1410 * @see reserve_arena() */
1412 {
1413 if(sz > arena_slack())
1414 _grow_arena(sz - arena_slack());
1415 substr s = _request_span(sz);
1416 return s;
1417 }
1418
1419 /** ensure the tree's internal string arena is at least the given capacity
1420 * @warning This operation may be expensive, with a potential complexity of O(numNodes)+O(arenasize).
1421 * @warning Growing the arena may cause relocation of the entire
1422 * existing arena, and thus change the contents of individual nodes. */
1424 {
1425 if(arena_cap > m_arena.len)
1426 {
1427 substr buf;
1428 buf.str = RYML_CB_ALLOC_(m_callbacks, char, arena_cap);
1429 buf.len = arena_cap;
1430 if(m_arena.str)
1431 {
1432 RYML_ASSERT_VISIT_CB_(m_callbacks, m_arena.len >= 0, this, NONE);
1433 _relocate(buf); // does a memcpy and changes nodes using the arena
1434 RYML_CB_FREE_(m_callbacks, m_arena.str, char, m_arena.len);
1435 }
1436 m_arena = buf;
1437 }
1438 }
1439
1440 /** @} */
1441
1442public:
1443
1444 /** @cond dev */
1445
1446 substr _grow_arena(size_t more)
1447 {
1448 size_t cap = m_arena.len + more;
1450 cap = cap < 2 * m_arena.len ? 2 * m_arena.len : cap;
1451 reserve_arena(cap);
1452 return m_arena.sub(m_arena_pos);
1453 }
1454
1455 substr _request_span(size_t sz)
1456 {
1457 RYML_ASSERT_VISIT_CB_(m_callbacks, m_arena_pos + sz <= m_arena.len, this, NONE);
1458 substr s;
1459 s = m_arena.sub(m_arena_pos, sz);
1460 m_arena_pos += sz;
1461 return s;
1462 }
1463
1464 substr _relocated(csubstr s, substr next_arena) const
1465 {
1466 RYML_ASSERT_VISIT_CB_(m_callbacks, m_arena.is_super(s) || s.len == 0, this, NONE);
1467 RYML_ASSERT_VISIT_CB_(m_callbacks, m_arena.sub(0, m_arena_pos).is_super(s) || s.len == 0, this, NONE);
1468 auto pos = (s.str - m_arena.str); // this is larger than 0 based on the assertions above
1469 substr r(next_arena.str + pos, s.len);
1470 RYML_ASSERT_VISIT_CB_(m_callbacks, r.str - next_arena.str == pos, this, NONE);
1471 RYML_ASSERT_VISIT_CB_(m_callbacks, next_arena.sub(0, m_arena_pos).is_super(r) || r.len == 0, this, NONE);
1472 return r;
1473 }
1474
1475 /** @endcond */
1476
1477public:
1478
1479 /** @name lookup */
1480 /** @{ */
1481
1483 {
1486 size_t path_pos;
1488
1489 operator bool() const { return target != NONE; } // NOLINT
1490
1492 lookup_result(csubstr path_, id_type start) : target(NONE), closest(start), path_pos(0), path(path_) {}
1493
1494 /** get the part ot the input path that was resolved */
1495 csubstr resolved() const;
1496 /** get the part ot the input path that was unresolved */
1497 csubstr unresolved() const;
1498 };
1499
1500 /** for example foo.bar[0].baz */
1501 lookup_result lookup_path(csubstr path, id_type start=NONE) const;
1502
1503 /** defaulted lookup: lookup @p path; if the lookup fails, recursively modify
1504 * the tree so that the corresponding lookup_path() would return the
1505 * default value.
1506 * @see lookup_path() */
1507 id_type lookup_path_or_modify(csubstr default_value, csubstr path, id_type start=NONE);
1508
1509 /** defaulted lookup: lookup @p path; if the lookup fails, recursively modify
1510 * the tree so that the corresponding lookup_path() would return the
1511 * branch @p src_node (from the tree @p src).
1512 * @see lookup_path() */
1513 id_type lookup_path_or_modify(Tree const *src, id_type src_node, csubstr path, id_type start=NONE);
1514
1515 /** @} */
1516
1517private:
1518
1519 struct _lookup_path_token
1520 {
1521 csubstr value;
1522 NodeType type;
1523 _lookup_path_token() : value(), type() {} // LCOV_EXCL_LINE
1524 _lookup_path_token(csubstr v, NodeType t) : value(v), type(t) {}
1525 operator bool() const { return type != NOTYPE; } // NOLINT
1526 bool is_index() const { return value.begins_with('[') && value.ends_with(']'); }
1527 };
1528
1529 id_type _lookup_path_or_create(csubstr path, id_type start);
1530
1531 void _lookup_path (lookup_result *r) const;
1532 void _lookup_path_modify(lookup_result *r);
1533
1534 id_type _next_node (lookup_result *r, _lookup_path_token *parent) const;
1535 id_type _next_node_modify(lookup_result *r, _lookup_path_token *parent);
1536
1537 static void _advance(lookup_result *r, size_t more);
1538
1539 _lookup_path_token _next_token(lookup_result *r, _lookup_path_token const& parent) const;
1540
1541private:
1542
1543 void _clear();
1544 void _free();
1545 void _copy(Tree const& that);
1546 void _move(Tree & that) noexcept;
1547
1548 void _relocate(substr next_arena);
1549
1550public:
1551
1552 /** @cond dev*/
1553
1554 #if ! RYML_USE_ASSERT
1555 C4_ALWAYS_INLINE void _check_next_flags(id_type, type_bits) {}
1556 #else
1557 void _check_next_flags(id_type node, type_bits f)
1558 {
1559 NodeData *n = _p(node);
1560 type_bits o = n->m_type; // old
1561 C4_UNUSED(o);
1562 if(f & MAP)
1563 {
1564 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (f & SEQ) == 0, this, node, "cannot mark simultaneously as map and seq");
1565 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (f & VAL) == 0, this, node, "cannot mark simultaneously as map and val");
1566 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (o & SEQ) == 0, this, node, "cannot turn a seq into a map; clear first");
1567 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (o & VAL) == 0, this, node, "cannot turn a val into a map; clear first");
1568 }
1569 else if(f & SEQ)
1570 {
1571 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (f & MAP) == 0, this, node, "cannot mark simultaneously as seq and map");
1572 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (f & VAL) == 0, this, node, "cannot mark simultaneously as seq and val");
1573 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (o & MAP) == 0, this, node, "cannot turn a map into a seq; clear first");
1574 RYML_ASSERT_VISIT_MSG_CB_(m_callbacks, (o & VAL) == 0, this, node, "cannot turn a val into a seq; clear first");
1575 }
1576 if(f & KEY)
1577 {
1578 RYML_ASSERT_VISIT_CB_(m_callbacks, !is_root(node), this, node);
1579 RYML_ASSERT_VISIT_CB_(m_callbacks, is_map(parent(node)), this, node);
1580 }
1581 if((f & VAL) && !is_root(node))
1582 {
1583 auto pid = parent(node); C4_UNUSED(pid);
1584 RYML_ASSERT_VISIT_CB_(m_callbacks, is_map(pid) || is_seq(pid), this, node);
1585 }
1586 }
1587 #endif
1588
1589 void _add_flags(id_type node, type_bits f) { NodeData *d = _p(node); type_bits fb = f | d->m_type; _check_next_flags(node, fb); d->m_type = fb; }
1590 void _rem_flags(id_type node, type_bits f) { NodeData *d = _p(node); type_bits fb = d->m_type & ~f; _check_next_flags(node, fb); d->m_type = fb; }
1591
1592 id_type _do_reorder(id_type *node, id_type count);
1593
1594 void _swap(id_type n_, id_type m_);
1595 void _swap_props(id_type n_, id_type m_);
1596 void _swap_hierarchy(id_type n_, id_type m_);
1597 void _copy_hierarchy(id_type dst_, id_type src_);
1598
1599 void _copy_props(id_type dst_, id_type src_)
1600 {
1601 _copy_props(dst_, this, src_);
1602 }
1603
1604 void _copy_props_wo_key(id_type dst_, id_type src_)
1605 {
1606 _copy_props_wo_key(dst_, this, src_);
1607 }
1608
1609 void _copy_props(id_type dst_, Tree const* that_tree, id_type src_)
1610 {
1611 NodeData & C4_RESTRICT dst = *_p(dst_);
1612 NodeData const& C4_RESTRICT src = *that_tree->_p(src_);
1613 dst.m_type = src.m_type;
1614 dst.m_key = src.m_key;
1615 dst.m_val = src.m_val;
1616 }
1617
1618 void _copy_props(id_type dst_, Tree const* that_tree, id_type src_, type_bits src_mask)
1619 {
1620 NodeData & C4_RESTRICT dst = *_p(dst_);
1621 NodeData const& C4_RESTRICT src = *that_tree->_p(src_);
1622 dst.m_type = (src.m_type & src_mask) | (dst.m_type & ~src_mask);
1623 dst.m_key = src.m_key;
1624 dst.m_val = src.m_val;
1625 }
1626
1627 void _copy_props_wo_key(id_type dst_, Tree const* that_tree, id_type src_)
1628 {
1629 auto & C4_RESTRICT dst = *_p(dst_);
1630 auto const& C4_RESTRICT src = *that_tree->_p(src_);
1631 dst.m_type = (src.m_type & ~KEYMASK_) | (dst.m_type & KEYMASK_);
1632 dst.m_val = src.m_val;
1633 }
1634
1635 void _copy_props_wo_key(id_type dst_, Tree const* that_tree, id_type src_, type_bits src_mask)
1636 {
1637 auto & C4_RESTRICT dst = *_p(dst_);
1638 auto const& C4_RESTRICT src = *that_tree->_p(src_);
1639 dst.m_type = (src.m_type & ((~KEYMASK_)|src_mask)) | (dst.m_type & (KEYMASK_|~src_mask));
1640 dst.m_val = src.m_val;
1641 }
1642
1643 void _clear_type(id_type node)
1644 {
1645 _p(node)->m_type = NOTYPE;
1646 }
1647
1648 void _clear(id_type node)
1649 {
1650 auto *C4_RESTRICT n = _p(node);
1651 n->m_type = NOTYPE;
1652 n->m_key.clear();
1653 n->m_val.clear();
1654 n->m_parent = NONE;
1655 n->m_first_child = NONE;
1656 n->m_last_child = NONE;
1657 }
1658
1659 void _clear_key(id_type node)
1660 {
1661 _p(node)->m_key.clear();
1662 _rem_flags(node, KEY);
1663 }
1664
1665 void _clear_val(id_type node)
1666 {
1667 _p(node)->m_val.clear();
1668 _rem_flags(node, VAL);
1669 }
1670
1671 /** @endcond */
1672
1673private:
1674
1675 id_type _claim();
1676 void _claim_root();
1677 void _release(id_type node);
1678 void _free_list_add(id_type node);
1679 void _free_list_rem(id_type node);
1680
1681 void _set_hierarchy(id_type node, id_type parent, id_type after_sibling);
1682 void _rem_hierarchy(id_type node);
1683
1684public:
1685
1686 // members are exposed, but you should NOT access them directly
1687
1691
1695
1698
1700
1702
1703public:
1704
1705 /** @cond dev */ // LCOV_EXCL_START
1706 C4_SUPPRESS_WARNING_GCC_CLANG_PUSH
1707 C4_SUPPRESS_WARNING_MSVC_PUSH
1708 C4_SUPPRESS_WARNING_GCC_CLANG("-Wdeprecated")
1709 C4_SUPPRESS_WARNING_GCC_CLANG("-Wdeprecated-declarations")
1710 C4_SUPPRESS_WARNING_MSVC(4996) // deprecated
1711 RYML_DEPRECATED("use .type().type_str(buf)") const char* type_str(id_type node) const { return NodeType::type_str(_p(node)->m_type); }
1712 RYML_DEPRECATED("use has_other_siblings()") static bool has_siblings(id_type /*node*/) { return true; }
1713 RYML_DEPRECATED("use set_key()+set_val()") void to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags=0);
1714 RYML_DEPRECATED("use set_key()+set_map()") void to_map(id_type node, csubstr key, type_bits more_flags=0);
1715 RYML_DEPRECATED("use set_key()+set_seq()") void to_seq(id_type node, csubstr key, type_bits more_flags=0);
1716 RYML_DEPRECATED("use set_val()") void to_val(id_type node, csubstr val, type_bits more_flags=0);
1717 RYML_DEPRECATED("use set_map()") void to_map(id_type node, type_bits more_flags=0);
1718 RYML_DEPRECATED("use set_seq()") void to_seq(id_type node, type_bits more_flags=0);
1719 RYML_DEPRECATED("use set_doc()") void to_doc(id_type node, type_bits more_flags=0);
1720 RYML_DEPRECATED("use set_stream()") void to_stream(id_type node, type_bits more_flags=0);
1721 RYML_DEPRECATED("use resolve_tags(TagCache&)") void resolve_tags() { TagCache cache; resolve_tags(cache); }
1722 RYML_DEPRECATED("") void _set(id_type node, NodeInit const& i)
1723 {
1724 RYML_ASSERT_VISIT_CB_(m_callbacks, i._check(), this, node);
1725 NodeData *n = _p(node);
1726 RYML_ASSERT_VISIT_CB_(m_callbacks, n->m_key.scalar.empty() || i.key.scalar.empty() || i.key.scalar == n->m_key.scalar, this, node);
1727 _add_flags(node, i.type);
1728 if(n->m_key.scalar.empty() && ( ! i.key.scalar.empty()))
1729 {
1730 set_key(node, i.key.scalar);
1731 }
1732 n->m_key.tag = i.key.tag;
1733 n->m_val = i.val;
1734 }
1735 RYML_DEPRECATED("") void _set_key(id_type node, NodeScalar const& key, NodeType more_flags=0)
1736 {
1737 _p(node)->m_key = key;
1738 _add_flags(node, KEY|more_flags);
1739 }
1740 RYML_DEPRECATED("") void _set_val(id_type node, NodeScalar const& val, NodeType more_flags=0)
1741 {
1742 _p(node)->m_val = val;
1743 _add_flags(node, VAL|more_flags);
1744 }
1745 C4_SUPPRESS_WARNING_MSVC_POP
1746 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1747 /** @endcond */ // LCOV_EXCL_STOP
1748};
1749
1750
1751//-----------------------------------------------------------------------------
1752//-----------------------------------------------------------------------------
1753//-----------------------------------------------------------------------------
1754
1755
1756/** @addtogroup doc_serialization_tree_write_arena
1757 *
1758 * @{
1759 */
1760
1761/** Forward to @ref scalar_serialize(), giving it the tree's arena and
1762 * resizing the arena as needed to fit the result. */
1763template<class T>
1765{
1766 substr buf = tree->arena_rem(); // buffer: the free part of the tree's arenra.
1767 again:
1768 size_t num = scalar_serialize(buf, a); // try to write into it
1769 if C4_LIKELY(num <= buf.len) // was it enough?
1770 {
1771 buf = buf.first(num); // fit the payload
1772 }
1773 else
1774 {
1775 buf = tree->_grow_arena(num); // does not advance pos
1776 goto again; // NOLINT
1777 }
1778 tree->m_arena_pos += num;
1779 return buf;
1780}
1781
1782/** @} */
1783
1784
1785/** @addtogroup doc_serialization_tree_write
1786 *
1787 * @{
1788 */
1789
1790#if (C4_CPP >= 17) || defined(__DOXYGEN__)
1791
1792/** Return extra style flags to use when setting a scalar as val.
1793 * Defaults to VAL_PLAIN for arithmetic types, or NOTYPE otherwise */
1794template<class T>
1795C4_ALWAYS_INLINE type_bits scalar_flags_val(T const&) noexcept
1796{
1797 if constexpr (std::is_arithmetic_v<T>)
1798 return VAL_PLAIN;
1799 else
1800 return NOTYPE;
1801}
1802/** Return extra style flags to use when setting a scalar as key.
1803 * Defaults to KEY_PLAIN for arithmetic types, or NOTYPE otherwise */
1804template<class T>
1805C4_ALWAYS_INLINE type_bits scalar_flags_key(T const&) noexcept
1806{
1807 if constexpr (std::is_arithmetic_v<T>)
1808 return KEY_PLAIN;
1809 else
1810 return NOTYPE;
1811}
1812
1813#else // pre-C++17 implementation: need to use SFINAE
1814
1815template<class T>
1816C4_ALWAYS_INLINE auto scalar_flags_val(T const&) noexcept
1817 -> typename std::enable_if<std::is_arithmetic<T>::value, type_bits>::type
1818{
1819 return VAL_PLAIN;
1820}
1821template<class T>
1822C4_ALWAYS_INLINE auto scalar_flags_key(T const&) noexcept
1823 -> typename std::enable_if<std::is_arithmetic<T>::value, type_bits>::type
1824{
1825 return KEY_PLAIN;
1826}
1827
1828template<class T>
1829C4_ALWAYS_INLINE auto scalar_flags_val(T const&) noexcept
1830 -> typename std::enable_if< ! std::is_arithmetic<T>::value, type_bits>::type
1831{
1832 return NOTYPE;
1833}
1834template<class T>
1835C4_ALWAYS_INLINE auto scalar_flags_key(T const&) noexcept
1836 -> typename std::enable_if< ! std::is_arithmetic<T>::value, type_bits>::type
1837{
1838 return NOTYPE;
1839}
1840
1841#endif // pre-C++17 implementation
1842
1843
1844//-----------------------------------------------------------------------------
1845
1846/** Serialize a variable to the tree's arena, and set it as the node's val. */
1847template<class T>
1848C4_ALWAYS_INLINE void write(Tree * tree, id_type id, T const& v)
1849{
1850 tree->set_val(id, serialize_to_arena(tree, v), scalar_flags_val(v));
1851}
1852
1853/** Serialize a variable to the tree's arena, and set it as the node's key.
1854 *
1855 * @warning The key MUST be a scalar value. The tree cannot handle
1856 * container keys. */
1857template<class T>
1858C4_ALWAYS_INLINE void write_key(Tree * tree, id_type id, T const& v)
1859{
1860 tree->set_key(id, serialize_to_arena(tree, v), scalar_flags_key(v));
1861}
1862
1863/** @} */
1864
1865
1866//-----------------------------------------------------------------------------
1867//-----------------------------------------------------------------------------
1868//-----------------------------------------------------------------------------
1869
1870/** @addtogroup doc_serialization_tree_read
1871 * @{
1872 */
1873
1874/** Deserialize a scalar node's val from a tree object, returning
1875 * false if the conversion failed.
1876 *
1877 * @return false if the conversion failed
1878 * @see @ref scalar_deserialize()
1879 * @see the counterpart function @ref read_key() */
1880template<class T>
1881C4_NODISCARD C4_ALWAYS_INLINE ReadResult read(Tree const* tree, id_type id, T * v)
1882{
1883 // caller only checks that type is one of VAL|MAP|SEQ (because it
1884 // can't be more concrete than that). Here, we expect a VAL so now
1885 // we can check for that:
1886 NodeData const* C4_RESTRICT nd = tree->_p(id);
1887 return ReadResult((nd->m_type & VAL) && scalar_deserialize(nd->m_val.scalar, v), id);
1888}
1889/** overload to enable use of wrapper tag-types like eg @ref
1890 * c4::fmt::base64() */
1891template<class Wrapper>
1892C4_NODISCARD C4_ALWAYS_INLINE ReadResult read(Tree const* tree, id_type id, Wrapper const& w)
1893{
1894 // caller only checks that type is one of VAL|MAP|SEQ (because it
1895 // can't be more concrete than that). Here, we expect a VAL so now
1896 // we can check for that:
1897 NodeData const* C4_RESTRICT nd = tree->_p(id);
1898 return ReadResult((nd->m_type & VAL) && from_chars(nd->m_val.scalar, w), id);
1899}
1900
1901
1902/** Deserialize a node's key from a tree object, returning false if
1903 * the conversion failed
1904 *
1905 * @return false if the conversion failed
1906 * @see the counterpart function @ref read(Tree const*, id_type, T*) */
1907template<class T>
1908C4_NODISCARD C4_ALWAYS_INLINE ReadResult read_key(Tree const* tree, id_type id, T * v)
1909{
1910 // caller already checks availability of key
1911 return ReadResult(scalar_deserialize(tree->_p(id)->m_key.scalar, v), id);
1912}
1913/** overload to enable use of wrapper tag-types like eg @ref
1914 * c4::fmt::base64() */
1915template<class Wrapper>
1916C4_NODISCARD C4_ALWAYS_INLINE ReadResult read_key(Tree const* tree, id_type id, Wrapper const& w)
1917{
1918 // caller already checks availability of key
1919 return ReadResult(from_chars(tree->_p(id)->m_key.scalar, w), id);
1920}
1921
1922/** @} */
1923
1924/** @} */
1925
1926
1927} // namespace yml
1928} // namespace c4
1929
1930// NOLINTEND(modernize-avoid-c-style-cast)
1931C4_SUPPRESS_WARNING_POP
1932
1933#endif /* C4_YML_TREE_HPP_ */
Holds a pointer to an existing tree, and a node id.
Definition node.hpp:737
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition node.hpp:1063
id_type num_other_siblings(id_type node) const
does not count with this
Definition tree.hpp:594
NodeData * m_buf
Definition tree.hpp:1688
NodeData * _p(id_type node)
An if-less form of get() that demands a valid node index. This function is implementation only; use a...
Definition tree.hpp:384
id_type m_free_head
Definition tree.hpp:1692
void set_val_style(id_type node, type_bits style)
Definition tree.hpp:664
bool has_sibling(id_type node, csubstr key) const
true if one of the node's siblings has the given key
Definition tree.hpp:553
csubstr resolve_tag_sub(substr output, csubstr tag, id_type node_id) const
Wrapper for Tree::resolve_tag(), returning a substring.
Definition tree.hpp:1159
csubstr const & key_ref(id_type node) const
Definition tree.hpp:460
csubstr const & key(id_type node) const
Definition tree.hpp:458
bool is_key_plain(id_type node) const
Definition tree.hpp:653
id_type num_siblings(id_type node) const
O(num_siblings).
Definition tree.hpp:592
id_type first_child(id_type node) const
Definition tree.hpp:581
bool is_stream(id_type node) const
Definition tree.hpp:481
void save_key(id_type node, T const &key, NodeType more_flags)
Definition tree.hpp:808
NodeType type(id_type node) const
Definition tree.hpp:456
id_type append_sibling(id_type node)
Definition tree.hpp:1211
void set_val_ref(id_type node, csubstr ref)
Definition tree.hpp:752
csubstr const & val_ref(id_type node) const
Definition tree.hpp:466
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:390
size_t arena_slack() const
get the current slack of the tree's internal arena
Definition tree.hpp:1327
bool has_key_tag(id_type node) const
Definition tree.hpp:490
void save(id_type node, T const &val)
Definition tree.hpp:776
id_type prev_sibling(id_type node) const
Definition tree.hpp:575
void reserve_arena(size_t arena_cap=RYML_DEFAULT_TREE_ARENA_CAPACITY)
ensure the tree's internal string arena is at least the given capacity
Definition tree.hpp:1423
id_type prepend_sibling(id_type node)
create and insert a node as the first node of parent
Definition tree.hpp:1210
size_t m_arena_pos
Definition tree.hpp:1697
void set_val(id_type node, csubstr val) RYML_NOEXCEPT
Definition tree.hpp:692
ReadResult deserialize_key(id_type node, Wrapper const &w) const
(2) like (1), but for a wrapper type like those used in tag functions
Definition tree.hpp:989
bool is_map(id_type node) const
Definition tree.hpp:484
NodeData const * _p(id_type node) const
An if-less form of get() that demands a valid node index. This function is implementation only; use a...
Definition tree.hpp:387
void set_serialized(id_type node, T const &val) RYML_NOEXCEPT
Definition tree.hpp:827
void callbacks(Callbacks const &cb)
Definition tree.hpp:354
bool is_val_anchor(id_type node) const
Definition tree.hpp:522
void set_key_serialized(id_type node, T const &key) RYML_NOEXCEPT
Definition tree.hpp:841
ReadResult deserialize_child(id_type node, csubstr child_key, T *v, T const &fallback) const
(2) like (1), but assign from fallback if no such child exists.
Definition tree.hpp:1025
id_type sibling(id_type node, id_type pos) const
Definition tree.hpp:598
void set_key_serialized(id_type node, T const &key, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:847
void load(id_type node, T *v, bool check_readable=true) const
(1) deserialize the node's contents (val or container) to the given variable, forwarding to the user-...
Definition tree.hpp:875
NodeScalar const & keysc(id_type node) const
Definition tree.hpp:462
c4::yml::TagDirectiveRange tag_directives() const
Definition tree.hpp:1165
bool is_key_quoted(id_type node) const
Definition tree.hpp:655
csubstr const & val(id_type node) const
Definition tree.hpp:464
bool has_val_anchor(id_type node) const
Definition tree.hpp:493
void clear_arena()
Definition tree.hpp:343
id_type insert_child(id_type parent, id_type after)
create and insert a new child of parent.
Definition tree.hpp:1182
TagDirectives m_tag_directives
Definition tree.hpp:1701
bool is_root(id_type node) const
Definition tree.hpp:533
bool key_is_null(id_type node) const
true if the node key is empty, or its scalar verifies scalar_is_null().
Definition tree.hpp:508
NodeRef ref(id_type node)
Get a NodeRef of a node by id.
Definition tree.cpp:70
substr alloc_arena(size_t sz)
grow the tree's string arena by the given size and return a substr of the added portion
Definition tree.hpp:1411
bool is_keyval(id_type node) const
Definition tree.hpp:489
bool is_val_folded(id_type node) const
Definition tree.hpp:648
void set_key(id_type node, csubstr key, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:716
void set_serialized(id_type node, T const &val, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:833
id_type m_size
Definition tree.hpp:1690
id_type last_sibling(id_type node) const
Definition tree.hpp:597
static ReadResult to_result_(ReadResult notlegacy, id_type) noexcept
Definition tree.hpp:866
bool has_key(id_type node) const
Definition tree.hpp:486
bool has_other_siblings(id_type node) const
true if node is not a single child
Definition tree.hpp:555
csubstr const & val_tag(id_type node) const
Definition tree.hpp:465
bool in_arena(csubstr s) const
return true if the given substring is part of the tree's string arena
Definition tree.hpp:1341
ReadResult deserialize_child(id_type node, id_type child_pos, Wrapper const &wrapper) const
(3) like (1), but for wrapper tag types such as c4::fmt::base64()
Definition tree.hpp:1077
id_type parent(id_type node) const
Definition tree.hpp:573
bool is_key_unfiltered(id_type node) const
true if the key was a scalar requiring filtering and was left unfiltered during the parsing (see Pars...
Definition tree.hpp:516
void rem_key_anchor(id_type node)
Definition tree.hpp:754
size_t resolve_tag(substr output, csubstr tag, id_type node_id) const
resolve the given tag, appearing at node_id.
Definition tree.cpp:1425
bool is_key_literal(id_type node) const
Definition tree.hpp:645
NodeType key_style(id_type node) const
Definition tree.hpp:659
bool is_block(id_type node) const
Definition tree.hpp:633
id_type prepend_child(id_type parent)
create and insert a node as the first child of parent
Definition tree.hpp:1192
bool is_val(id_type node) const
Definition tree.hpp:488
bool is_container_styled(id_type node) const
Definition tree.hpp:632
bool empty() const
Query for zero size.
Definition tree.hpp:346
ReadResult find_sibling_r(id_type node, csubstr const &key, id_type *sibling_id) const
like Tree::find_sibling(), but return a ReadResult if with the status
Definition tree.hpp:603
void load_key(id_type node, T *k, bool check_readable=true) const
(1) deserialize the node's key (necessarily a scalar) to the given variable, forwarding to the user-o...
Definition tree.hpp:916
void set_seq(id_type node) RYML_NOEXCEPT
Definition tree.hpp:724
void set_stream(id_type node)
Definition tree.hpp:679
id_type m_first_unused
Definition tree.hpp:1694
id_type sibling_pos(id_type node, id_type sib) const
Definition tree.hpp:595
id_type child_pos(id_type node, id_type ch) const
Definition tree.cpp:1193
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition tree.hpp:1194
ReadResult find_child_r(id_type node, csubstr const &key, id_type *child_id) const
like Tree::find_child(), but return a ReadResult with the status
Definition tree.hpp:588
bool has_sibling(id_type node, id_type sib) const
true if node has a sibling with id sib
Definition tree.hpp:551
ReadResult deserialize(id_type node, T *v) const
(1) deserialize a node's contents to a variable
Definition tree.hpp:958
id_type next_sibling(id_type node) const
Definition tree.hpp:576
bool is_flow_mln(id_type node) const
Definition tree.hpp:638
bool is_key_squo(id_type node) const
Definition tree.hpp:649
bool parent_is_seq(id_type node) const
Definition tree.hpp:499
void save_key(id_type node, T const &key)
Definition tree.hpp:798
bool has_anchor(id_type node) const
Definition tree.hpp:494
id_type insert_sibling(id_type node, id_type after)
create and insert a new sibling of n. insert after "after"
Definition tree.hpp:1205
bool has_val_tag(id_type node) const
Definition tree.hpp:491
NodeData const * get(id_type node) const
get a pointer to a node's NodeData. node can be NONE, in which case a nullptr is returned.
Definition tree.hpp:374
ReadResult sibling_r(id_type node, id_type pos, id_type *sibling_id) const
like Tree::sibling(), but return a ReadResult with the status
Definition tree.hpp:601
bool is_flow_ml1(id_type node) const
Definition tree.hpp:637
bool is_key_dquo(id_type node) const
Definition tree.hpp:651
void set_key_tag(id_type node, csubstr tag)
Definition tree.hpp:746
void set_container_style(id_type node, type_bits style)
Definition tree.hpp:662
id_type m_cap
Definition tree.hpp:1689
void rem_anchor_ref(id_type node)
Definition tree.hpp:758
id_type last_child(id_type node) const
Definition tree.hpp:582
csubstr const & key_tag(id_type node) const
Definition tree.hpp:459
id_type id(NodeData const *n) const
get the id of a node belonging to this tree. n can be nullptr, in which case NONE is returned n must ...
Definition tree.hpp:396
bool type_has_any(id_type node, type_bits bits) const
Definition tree.hpp:477
substr arena_rem()
get the free space at the end of the arena
Definition tree.hpp:1338
substr m_arena
Definition tree.hpp:1696
void set_map(id_type node, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:740
csubstr to_arena(T const &a)
serialize the given variable to the tree's arena, growing it as needed to accomodate the serializatio...
Definition tree.hpp:1363
void set_val_anchor(id_type node, csubstr anchor)
Definition tree.hpp:750
bool is_val_plain(id_type node) const
Definition tree.hpp:654
bool is_doc(id_type node) const
Definition tree.hpp:482
bool has_child(id_type node, csubstr key) const
true if node has a child with key key
Definition tree.hpp:546
void set_val(id_type node, csubstr val, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:700
ReadResult deserialize(id_type node, Wrapper const &w) const
(2) like (1), but for a wrapper type like those used in tag functions
Definition tree.hpp:968
ReadResult deserialize_child(id_type node, id_type child_pos, T *v, T const &fallback) const
(2) like (1), but assign from fallback if no such child exists
Definition tree.hpp:1065
void set_seq(id_type node, NodeType more_flags) RYML_NOEXCEPT
Definition tree.hpp:729
id_type root_id_maybe() const
Get the id of the root node, or NONE if the tree is empty.
Definition tree.hpp:392
bool is_key_ref(id_type node) const
Definition tree.hpp:495
bool is_key_anchor(id_type node) const
Definition tree.hpp:521
void remove_children(id_type node)
remove all the node's children, but keep the node itself
Definition tree.cpp:886
bool type_has_all(id_type node, type_bits bits) const
Definition tree.hpp:478
bool has_val(id_type node) const
Definition tree.hpp:487
bool is_val_dquo(id_type node) const
Definition tree.hpp:652
Callbacks const & callbacks() const
Definition tree.hpp:353
size_t arena_capacity() const
get the current capacity of the tree's internal arena
Definition tree.hpp:1325
id_type doc(id_type i) const
gets the i document node index.
Definition tree.hpp:609
bool change_type(id_type node, NodeType type)
Definition tree.cpp:909
bool is_flow_ml(id_type node) const
Definition tree.hpp:636
NodeType val_style(id_type node) const
Definition tree.hpp:660
bool is_key_folded(id_type node) const
Definition tree.hpp:647
size_t arena_pos() const
Definition tree.hpp:1328
csubstr arena_rem() const
get the free space at the end of the arena
Definition tree.hpp:1336
Tree(id_type node_capacity, size_t arena_capacity=RYML_DEFAULT_TREE_ARENA_CAPACITY)
Definition tree.hpp:319
id_type m_free_tail
Definition tree.hpp:1693
bool is_anchor_or_ref(id_type node) const
Definition tree.hpp:524
id_type slack() const
Definition tree.hpp:351
bool is_anchor(id_type node) const
Definition tree.hpp:523
id_type find_sibling(id_type node, csubstr const &key) const
Definition tree.hpp:599
bool is_seq(id_type node) const
Definition tree.hpp:485
bool is_val_styled(id_type node) const
Definition tree.hpp:644
bool parent_is_map(id_type node) const
Definition tree.hpp:500
void set_map(id_type node) RYML_NOEXCEPT
Definition tree.hpp:735
bool has_key_anchor(id_type node) const
Definition tree.hpp:492
void remove(id_type node)
remove an entire branch at once: ie remove the children and the node itself
Definition tree.hpp:1216
ReadResult deserialize_key(id_type node, T *v) const
(1) deserialize a node's key to a variable
Definition tree.hpp:979
id_type find_child(id_type node, csubstr const &key) const
find child by name, or NONE if no child is found with this key like Tree::child(),...
Definition tree.cpp:1218
void save(id_type node, T const &val, NodeType more_flags)
Definition tree.hpp:786
bool type_has_none(id_type node, type_bits bits) const
Definition tree.hpp:479
bool is_quoted(id_type node) const
Definition tree.hpp:657
csubstr const & val_anchor(id_type node) const
Definition tree.hpp:467
bool change_type(id_type node, type_bits type)
Definition tree.hpp:1261
NodeData * get(id_type node)
get a pointer to a node's NodeData. node can be NONE, in which case a nullptr is returned
Definition tree.hpp:365
substr arena()
get the current arena
Definition tree.hpp:1333
void rem_val_ref(id_type node)
Definition tree.hpp:757
bool has_parent(id_type node) const
Definition tree.hpp:535
bool val_is_null(id_type node) const
true if the node val is empty, or its scalar verifies scalar_is_null().
Definition tree.hpp:512
id_type first_sibling(id_type node) const
Definition tree.hpp:596
void set_key_style(id_type node, type_bits style)
Definition tree.hpp:663
bool is_flow(id_type node) const
Definition tree.hpp:640
bool is_val_quoted(id_type node) const
Definition tree.hpp:656
bool empty(id_type node) const
true when key and val are empty, and has no children
Definition tree.hpp:541
csubstr const & key_anchor(id_type node) const
Definition tree.hpp:461
bool is_val_squo(id_type node) const
Definition tree.hpp:650
substr copy_to_arena(csubstr s)
copy the given string to the tree's arena, growing the arena by the required size.
Definition tree.hpp:1384
ReadResult deserialize_child(id_type node, csubstr child_key, Wrapper const &wrapper) const
(3) like (1), but for wrapper tag types such as c4::fmt::base64()
Definition tree.hpp:1035
void set_key(id_type node, csubstr key) RYML_NOEXCEPT
Definition tree.hpp:709
bool has_flow_space(id_type node) const
Definition tree.hpp:641
id_type num_children(id_type node) const
O(num_children).
Definition tree.cpp:1185
csubstr arena() const
get the current arena
Definition tree.hpp:1331
id_type size() const
Get the number of existing nodes.
Definition tree.hpp:349
bool is_container(id_type node) const
Definition tree.hpp:483
void set_doc(id_type node)
Definition tree.hpp:686
bool is_flow_mlx(id_type node) const
Definition tree.hpp:639
ReadResult deserialize_child(id_type node, id_type child_pos, T *v) const
(1) find a child by position and deserialize its contents to the given variable (ie call ....
Definition tree.hpp:1054
size_t arena_size() const
get the current size of the tree's internal arena
Definition tree.hpp:1323
id_type child(id_type node, id_type pos) const
find child by position, or NONE if there are less than pos children posi
Definition tree.cpp:1206
bool is_ref(id_type node) const
Definition tree.hpp:497
void set_val_tag(id_type node, csubstr tag)
Definition tree.hpp:747
bool is_key_styled(id_type node) const
Definition tree.hpp:643
void set_key_anchor(id_type node, csubstr anchor)
Definition tree.hpp:749
bool is_val_ref(id_type node) const
Definition tree.hpp:496
void set_key_ref(id_type node, csubstr ref)
Definition tree.hpp:751
void load(id_type node, Wrapper const &w, bool check_readable=true) const
(2) like (1), but for wrapper tag types such as c4::fmt::base64()
Definition tree.hpp:892
id_type _append_child__unprotected(id_type parent)
Definition tree.hpp:1195
bool has_child(id_type node, id_type ch) const
true if node has a child with id ch
Definition tree.hpp:544
bool is_val_literal(id_type node) const
Definition tree.hpp:646
void rem_val_anchor(id_type node)
Definition tree.hpp:755
NodeScalar const & valsc(id_type node) const
Definition tree.hpp:468
Callbacks m_callbacks
Definition tree.hpp:1699
bool is_val_unfiltered(id_type node) const
true if the val was a scalar requiring filtering and was left unfiltered during the parsing (see Pars...
Definition tree.hpp:519
ReadResult deserialize_child(id_type node, csubstr child_key, T *v) const
(1) find a child by name and deserialize its contents to the given variable (ie call ....
Definition tree.hpp:1015
void load_key(id_type node, Wrapper const &w, bool check_readable=true) const
(2) like (1), but for wrapper tag types such as c4::fmt::base64()
Definition tree.hpp:933
void rem_key_ref(id_type node)
Definition tree.hpp:756
ReadResult child_r(id_type node, id_type pos, id_type *child_id) const
Definition tree.hpp:586
id_type capacity() const
Definition tree.hpp:350
bool is_flow_sl(id_type node) const
Definition tree.hpp:634
id_type ancestor_doc(id_type node) const
get the document which is a parent document of node i, or the root if the tree is not a stream
Definition tree.hpp:612
bool has_children(id_type node) const
true if node has any children
Definition tree.hpp:548
static ReadResult to_result_(bool, id_type node) noexcept
Definition tree.hpp:865
bool has_anchor(id_type node, csubstr a) const
true when the node has an anchor named a
Definition tree.hpp:503
Common utilities and infrastructure used by ryml.
#define RYML_DEFAULT_TREE_CAPACITY
default capacity for the tree when not set explicitly
Definition common.hpp:21
#define RYML_DEFAULT_TREE_ARENA_CAPACITY
default capacity for the tree's arena when not set explicitly
Definition common.hpp:26
#define RYML_DEFAULT_TREE_ARENA_CAPACITY_START
default starting capacity for the tree's arena when it is first allocated. should be larger than RYML...
Definition common.hpp:32
#define RYML_NOEXCEPT
Conditionally expands to noexcept when RYML_USE_ASSERT is 0 and is empty otherwise.
Error utilities used by ryml.
#define RYML_EXPORT
Definition export.hpp:18
forward declarations
Callbacks const & get_callbacks()
get the global callbacks
Definition common.cpp:94
bool from_chars(csubstr buf, uint8_t *v) noexcept
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
Definition node_type.hpp:26
@ VALANCH
the val has an &anchor
Definition node_type.hpp:42
@ NOTYPE
no node type or style is set
Definition node_type.hpp:32
@ VALREF
a *reference: the val references an &anchor
Definition node_type.hpp:40
@ MAP
a map: a parent of KEYVAL/KEYSEQ/KEYMAP nodes
Definition node_type.hpp:35
@ STREAM
a stream: a seq of docs
Definition node_type.hpp:38
@ KEY
the scalar to the left of : in a map's member
Definition node_type.hpp:33
@ KEYTAG
the key has a tag
Definition node_type.hpp:43
@ VAL
a scalar: has a scalar (ie string) value, possibly empty. must be a leaf node, and cannot be MAP or S...
Definition node_type.hpp:34
@ VALTAG
the val has a tag
Definition node_type.hpp:44
@ SEQ
a seq: a parent of VAL/SEQ/MAP nodes
Definition node_type.hpp:36
@ VAL_PLAIN
mark val scalar as plain scalar (unquoted, even when multiline)
@ KEYREF
a *reference: the key references an &anchor
Definition node_type.hpp:39
@ KEYVAL
mask of KEY|VAL
@ KEYANCH
the key has an &anchor
Definition node_type.hpp:41
@ KEY_PLAIN
mark key scalar as plain scalar (unquoted, even when multiline)
@ DOC
a document
Definition node_type.hpp:37
ParseEngine< EventHandlerTree > Parser
This is the main ryml parser, where the parser events are handled to create a ryml tree (see Event Ha...
Definition fwd.hpp:19
void write(ryml::NodeRef &n, Inner const &inner)
serializes as a seq
ryml::ReadResult read(ryml::ConstNodeRef const &n, Inner *inner)
bool from_chars(ryml::csubstr buf, vec2< T > *v)
size_t scalar_serialize(substr buf, T const &a)
Serialize a scalar to the buffer, dispatching to to_chars() or to_chars_float() as appropriate.
bool scalar_is_null(csubstr s) noexcept
YAML-sense query of nullity.
bool scalar_deserialize(csubstr str, T *val)
Deserialize a scalar from its string representation, dispatching to one of from_chars(),...
ReadResult read_key(ConstNodeRef const &n, T *v)
Definition node.hpp:2088
void write_key(NodeRef *n, T const &v)
Definition node.hpp:2119
csubstr serialize_to_arena(Tree *tree, T const &scalar)
Serialize a scalar to a tree's arena, dispatching to either serialize_to_arena_scalar() or serialize_...
Definition tree.hpp:112
csubstr serialize_to_arena_str(Tree *tree, csubstr scalar)
Serialize a string type (as specified by c4::is_string) to a tree's arena, ensuring that there is an ...
Definition tree.cpp:27
csubstr serialize_to_arena_scalar(Tree *tree, T const &scalar)
Serialize a scalar to the tree's arena.
Definition tree.hpp:1764
type_bits scalar_flags_val(T const &) noexcept
Return extra style flags to use when setting a scalar as val.
Definition tree.hpp:1795
type_bits scalar_flags_key(T const &) noexcept
Return extra style flags to use when setting a scalar as key.
Definition tree.hpp:1805
csubstr to_csubstr(const char(&s)[N]) noexcept
Definition substr.hpp:2380
basic_substring< char > substr
a mutable string view
Definition substr.hpp:2355
basic_substring< const char > csubstr
an immutable 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:124
@ NONE
an index to none
Definition common.hpp:131
bool begins_with(const C c) const noexcept
true if the first character of the string is c
Definition substr.hpp:850
basic_substring triml(const C c) const
trim left
Definition substr.hpp:629
size_t len
the length of the substring
Definition substr.hpp:218
bool ends_with(const C c) const noexcept
true if the last character of the string is c
Definition substr.hpp:894
bool overlaps(ro_substr const that) const noexcept
true if there is overlap of at least one element between that and *this
Definition substr.hpp:493
basic_substring first(size_t num) const noexcept
return the first num elements: [0,num[
Definition substr.hpp:529
basic_substring sub(size_t first) const noexcept
return [first,len[
Definition substr.hpp:502
C * str
a restricted pointer to the first character of the substring
Definition substr.hpp:216
A c-style callbacks class to customize behavior on errors or allocation.
Definition common.hpp:374
holds a source or yaml file position, for example when an error is detected; See also location_format...
Definition common.hpp:229
contains the data for each YAML node.
Definition tree.hpp:291
NodeType m_type
Definition tree.hpp:292
id_type m_next_sibling
Definition tree.hpp:300
id_type m_parent
Definition tree.hpp:297
NodeScalar m_key
Definition tree.hpp:294
id_type m_prev_sibling
Definition tree.hpp:301
id_type m_first_child
Definition tree.hpp:298
NodeScalar m_val
Definition tree.hpp:295
id_type m_last_child
Definition tree.hpp:299
a node scalar is a csubstr, which may be tagged and anchored.
Definition tree.hpp:173
NodeScalar(const char(&t)[N], const char(&s)[N]) noexcept
initialize as a tagged scalar
Definition tree.hpp:190
bool empty() const noexcept
Definition tree.hpp:203
NodeScalar() noexcept
initialize as an empty scalar
Definition tree.hpp:181
NodeScalar(csubstr s) noexcept
Definition tree.hpp:186
NodeScalar(csubstr t, csubstr s) noexcept
Definition tree.hpp:191
void clear() noexcept
Definition tree.hpp:205
NodeScalar(const char(&s)[N]) noexcept
initialize as an untagged scalar
Definition tree.hpp:185
~NodeScalar() noexcept=default
void set_ref_maybe_replacing_scalar(csubstr ref, bool has_scalar) RYML_NOEXCEPT
Definition tree.hpp:207
Wraps a type_bits mask of NodeTypeBits flags with some syntactic sugar and predicates.
bool is_doc() const noexcept
const char * type_str() const noexcept
return a preset string based on the node type
A lightweight truthy type, used to enable reporting the offending node when a deserializing error hap...
Definition common.hpp:162
Reusable object to resolve references/aliases in a Tree.
Options to control reference resolving.
lookup_result(csubstr path_, id_type start)
Definition tree.hpp:1492