rapidyaml  0.7.0
parse and emit YAML, and do it fast
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 #include "c4/error.hpp"
7 #include "c4/types.hpp"
8 #ifndef _C4_YML_FWD_HPP_
9 #include "c4/yml/fwd.hpp"
10 #endif
11 #ifndef _C4_YML_COMMON_HPP_
12 #include "c4/yml/common.hpp"
13 #endif
14 #ifndef C4_YML_NODE_TYPE_HPP_
15 #include "c4/yml/node_type.hpp"
16 #endif
17 #ifndef _C4_YML_TAG_HPP_
18 #include "c4/yml/tag.hpp"
19 #endif
20 #ifndef _C4_CHARCONV_HPP_
21 #include <c4/charconv.hpp>
22 #endif
23 
24 #include <cmath>
25 #include <limits>
26 
27 
28 C4_SUPPRESS_WARNING_MSVC_PUSH
29 C4_SUPPRESS_WARNING_MSVC(4251) // needs to have dll-interface to be used by clients of struct
30 C4_SUPPRESS_WARNING_MSVC(4296) // expression is always 'boolean_value'
31 C4_SUPPRESS_WARNING_GCC_CLANG_PUSH
32 C4_SUPPRESS_WARNING_GCC_CLANG("-Wold-style-cast")
33 C4_SUPPRESS_WARNING_GCC("-Wuseless-cast")
34 C4_SUPPRESS_WARNING_GCC("-Wtype-limits")
35 
36 
37 namespace c4 {
38 namespace yml {
39 
40 /** encode a floating point value to a string. */
41 template<class T>
42 size_t to_chars_float(substr buf, T val)
43 {
44  C4_SUPPRESS_WARNING_GCC_CLANG_WITH_PUSH("-Wfloat-equal");
45  static_assert(std::is_floating_point<T>::value, "must be floating point");
46  if(C4_UNLIKELY(std::isnan(val)))
47  return to_chars(buf, csubstr(".nan"));
48  else if(C4_UNLIKELY(val == std::numeric_limits<T>::infinity()))
49  return to_chars(buf, csubstr(".inf"));
50  else if(C4_UNLIKELY(val == -std::numeric_limits<T>::infinity()))
51  return to_chars(buf, csubstr("-.inf"));
52  return to_chars(buf, val);
53  C4_SUPPRESS_WARNING_GCC_CLANG_POP
54 }
55 
56 
57 /** decode a floating point from string. Accepts special values: .nan,
58  * .inf, -.inf */
59 template<class T>
60 bool from_chars_float(csubstr buf, T *C4_RESTRICT val)
61 {
62  static_assert(std::is_floating_point<T>::value, "must be floating point");
63  if(C4_LIKELY(from_chars(buf, val)))
64  {
65  return true;
66  }
67  else if(C4_UNLIKELY(buf == ".nan" || buf == ".NaN" || buf == ".NAN"))
68  {
69  *val = std::numeric_limits<T>::quiet_NaN();
70  return true;
71  }
72  else if(C4_UNLIKELY(buf == ".inf" || buf == ".Inf" || buf == ".INF"))
73  {
74  *val = std::numeric_limits<T>::infinity();
75  return true;
76  }
77  else if(C4_UNLIKELY(buf == "-.inf" || buf == "-.Inf" || buf == "-.INF"))
78  {
79  *val = -std::numeric_limits<T>::infinity();
80  return true;
81  }
82  else
83  {
84  return false;
85  }
86 }
87 
88 
89 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
91 //-----------------------------------------------------------------------------
92 
93 
94 /** @addtogroup doc_tree
95  *
96  * @{
97  */
98 
99 /** a node scalar is a csubstr, which may be tagged and anchored. */
101 {
102  csubstr tag;
103  csubstr scalar;
104  csubstr anchor;
105 
106 public:
107 
108  /// initialize as an empty scalar
109  inline NodeScalar() noexcept : tag(), scalar(), anchor() {}
110 
111  /// initialize as an untagged scalar
112  template<size_t N>
113  inline NodeScalar(const char (&s)[N]) noexcept : tag(), scalar(s), anchor() {}
114  inline NodeScalar(csubstr s ) noexcept : tag(), scalar(s), anchor() {}
115 
116  /// initialize as a tagged scalar
117  template<size_t N, size_t M>
118  inline NodeScalar(const char (&t)[N], const char (&s)[N]) noexcept : tag(t), scalar(s), anchor() {}
119  inline NodeScalar(csubstr t , csubstr s ) noexcept : tag(t), scalar(s), anchor() {}
120 
121 public:
122 
123  ~NodeScalar() noexcept = default;
124  NodeScalar(NodeScalar &&) noexcept = default;
125  NodeScalar(NodeScalar const&) noexcept = default;
126  NodeScalar& operator= (NodeScalar &&) noexcept = default;
127  NodeScalar& operator= (NodeScalar const&) noexcept = default;
128 
129 public:
130 
131  bool empty() const noexcept { return tag.empty() && scalar.empty() && anchor.empty(); }
132 
133  void clear() noexcept { tag.clear(); scalar.clear(); anchor.clear(); }
134 
135  void set_ref_maybe_replacing_scalar(csubstr ref, bool has_scalar) RYML_NOEXCEPT
136  {
137  csubstr trimmed = ref.begins_with('*') ? ref.sub(1) : ref;
138  anchor = trimmed;
139  if((!has_scalar) || !scalar.ends_with(trimmed))
140  scalar = ref;
141  }
142 };
143 C4_MUST_BE_TRIVIAL_COPY(NodeScalar);
144 
145 
146 //-----------------------------------------------------------------------------
147 //-----------------------------------------------------------------------------
148 //-----------------------------------------------------------------------------
149 
150 /** convenience class to initialize nodes */
151 struct NodeInit
152 {
153 
157 
158 public:
159 
160  /// initialize as an empty node
161  NodeInit() : type(NOTYPE), key(), val() {}
162  /// initialize as a typed node
163  NodeInit(NodeType_e t) : type(t), key(), val() {}
164  /// initialize as a sequence member
165  NodeInit(NodeScalar const& v) : type(VAL), key(), val(v) { _add_flags(); }
166  /// initialize as a sequence member with explicit type
167  NodeInit(NodeScalar const& v, NodeType_e t) : type(t|VAL), key(), val(v) { _add_flags(); }
168  /// initialize as a mapping member
169  NodeInit( NodeScalar const& k, NodeScalar const& v) : type(KEYVAL), key(k), val(v) { _add_flags(); }
170  /// initialize as a mapping member with explicit type
171  NodeInit(NodeType_e t, NodeScalar const& k, NodeScalar const& v) : type(t), key(k), val(v) { _add_flags(); }
172  /// initialize as a mapping member with explicit type (eg for SEQ or MAP)
173  NodeInit(NodeType_e t, NodeScalar const& k ) : type(t), key(k), val( ) { _add_flags(KEY); }
174 
175 public:
176 
177  void clear()
178  {
179  type.clear();
180  key.clear();
181  val.clear();
182  }
183 
184  void _add_flags(type_bits more_flags=0)
185  {
186  type = (type|more_flags);
187  if( ! key.tag.empty())
188  type = (type|KEYTAG);
189  if( ! val.tag.empty())
190  type = (type|VALTAG);
191  if( ! key.anchor.empty())
192  type = (type|KEYANCH);
193  if( ! val.anchor.empty())
194  type = (type|VALANCH);
195  }
196 
197  bool _check() const
198  {
199  // key cannot be empty
200  RYML_ASSERT(key.scalar.empty() == ((type & KEY) == 0));
201  // key tag cannot be empty
202  RYML_ASSERT(key.tag.empty() == ((type & KEYTAG) == 0));
203  // val may be empty even though VAL is set. But when VAL is not set, val must be empty
204  RYML_ASSERT(((type & VAL) != 0) || val.scalar.empty());
205  // val tag cannot be empty
206  RYML_ASSERT(val.tag.empty() == ((type & VALTAG) == 0));
207  return true;
208  }
209 };
210 
211 
212 //-----------------------------------------------------------------------------
213 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
215 
216 /** contains the data for each YAML node. */
217 struct NodeData
218 {
220 
223 
229 };
230 C4_MUST_BE_TRIVIAL_COPY(NodeData);
231 
232 
233 //-----------------------------------------------------------------------------
234 //-----------------------------------------------------------------------------
235 //-----------------------------------------------------------------------------
236 
238 {
239 public:
240 
241  /** @name construction and assignment */
242  /** @{ */
243 
245  Tree(Callbacks const& cb);
246  Tree(id_type node_capacity, size_t arena_capacity=0) : Tree(node_capacity, arena_capacity, get_callbacks()) {}
247  Tree(id_type node_capacity, size_t arena_capacity, Callbacks const& cb);
248 
249  ~Tree();
250 
251  Tree(Tree const& that);
252  Tree(Tree && that) noexcept;
253 
254  Tree& operator= (Tree const& that);
255  Tree& operator= (Tree && that) RYML_NOEXCEPT;
256 
257  /** @} */
258 
259 public:
260 
261  /** @name memory and sizing */
262  /** @{ */
263 
264  void reserve(id_type node_capacity);
265 
266  /** clear the tree and zero every node
267  * @note does NOT clear the arena
268  * @see clear_arena() */
269  void clear();
270  inline void clear_arena() { m_arena_pos = 0; }
271 
272  inline bool empty() const { return m_size == 0; }
273 
274  inline id_type size() const { return m_size; }
275  inline id_type capacity() const { return m_cap; }
276  inline id_type slack() const { RYML_ASSERT(m_cap >= m_size); return m_cap - m_size; }
277 
278  Callbacks const& callbacks() const { return m_callbacks; }
279  void callbacks(Callbacks const& cb) { m_callbacks = cb; }
280 
281  /** @} */
282 
283 public:
284 
285  /** @name node getters */
286  /** @{ */
287 
288  //! get the index of a node belonging to this tree.
289  //! @p n can be nullptr, in which case NONE is returned
290  id_type id(NodeData const* n) const
291  {
292  if( ! n)
293  return NONE;
294  _RYML_CB_ASSERT(m_callbacks, n >= m_buf && n < m_buf + m_cap);
295  return static_cast<id_type>(n - m_buf);
296  }
297 
298  //! get a pointer to a node's NodeData.
299  //! i can be NONE, in which case a nullptr is returned
300  inline NodeData *get(id_type node)
301  {
302  if(node == NONE)
303  return nullptr;
304  _RYML_CB_ASSERT(m_callbacks, node >= 0 && node < m_cap);
305  return m_buf + node;
306  }
307  //! get a pointer to a node's NodeData.
308  //! i can be NONE, in which case a nullptr is returned.
309  inline NodeData const *get(id_type node) const
310  {
311  if(node == NONE)
312  return nullptr;
313  _RYML_CB_ASSERT(m_callbacks, node >= 0 && node < m_cap);
314  return m_buf + node;
315  }
316 
317  //! An if-less form of get() that demands a valid node index.
318  //! This function is implementation only; use at your own risk.
319  inline NodeData * _p(id_type node) { _RYML_CB_ASSERT(m_callbacks, node != NONE && node >= 0 && node < m_cap); return m_buf + node; }
320  //! An if-less form of get() that demands a valid node index.
321  //! This function is implementation only; use at your own risk.
322  inline NodeData const * _p(id_type node) const { _RYML_CB_ASSERT(m_callbacks, node != NONE && node >= 0 && node < m_cap); return m_buf + node; }
323 
324  //! Get the id of the root node
325  id_type root_id() { if(m_cap == 0) { reserve(16); } _RYML_CB_ASSERT(m_callbacks, m_cap > 0 && m_size > 0); return 0; }
326  //! Get the id of the root node
327  id_type root_id() const { _RYML_CB_ASSERT(m_callbacks, m_cap > 0 && m_size > 0); return 0; }
328 
329  //! Get a NodeRef of a node by id
330  NodeRef ref(id_type node);
331  //! Get a NodeRef of a node by id
332  ConstNodeRef ref(id_type node) const;
333  //! Get a NodeRef of a node by id
334  ConstNodeRef cref(id_type node) const;
335 
336  //! Get the root as a NodeRef
337  NodeRef rootref();
338  //! Get the root as a ConstNodeRef
339  ConstNodeRef rootref() const;
340  //! Get the root as a ConstNodeRef
341  ConstNodeRef crootref() const;
342 
343  //! get the i-th document of the stream
344  //! @note @p i is NOT the node id, but the doc position within the stream
345  NodeRef docref(id_type i);
346  //! get the i-th document of the stream
347  //! @note @p i is NOT the node id, but the doc position within the stream
348  ConstNodeRef docref(id_type i) const;
349  //! get the i-th document of the stream
350  //! @note @p i is NOT the node id, but the doc position within the stream
351  ConstNodeRef cdocref(id_type i) const;
352 
353  //! find a root child by name, return it as a NodeRef
354  //! @note requires the root to be a map.
355  NodeRef operator[] (csubstr key);
356  //! find a root child by name, return it as a NodeRef
357  //! @note requires the root to be a map.
358  ConstNodeRef operator[] (csubstr key) const;
359 
360  //! find a root child by index: return the root node's @p i-th child as a NodeRef
361  //! @note @p i is NOT the node id, but the child's position
362  NodeRef operator[] (id_type i);
363  //! find a root child by index: return the root node's @p i-th child as a NodeRef
364  //! @note @p i is NOT the node id, but the child's position
365  ConstNodeRef operator[] (id_type i) const;
366 
367  /** @} */
368 
369 public:
370 
371  /** @name node property getters */
372  /** @{ */
373 
374  NodeType type(id_type node) const { return _p(node)->m_type; }
375  const char* type_str(id_type node) const { return NodeType::type_str(_p(node)->m_type); }
376 
377  csubstr const& key (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_key(node)); return _p(node)->m_key.scalar; }
378  csubstr const& key_tag (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_key_tag(node)); return _p(node)->m_key.tag; }
379  csubstr const& key_ref (id_type node) const { _RYML_CB_ASSERT(m_callbacks, is_key_ref(node)); return _p(node)->m_key.anchor; }
380  csubstr const& key_anchor(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_key_anchor(node)); return _p(node)->m_key.anchor; }
381  NodeScalar const& keysc (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_key(node)); return _p(node)->m_key; }
382 
383  csubstr const& val (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_val(node)); return _p(node)->m_val.scalar; }
384  csubstr const& val_tag (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_val_tag(node)); return _p(node)->m_val.tag; }
385  csubstr const& val_ref (id_type node) const { _RYML_CB_ASSERT(m_callbacks, is_val_ref(node)); return _p(node)->m_val.anchor; }
386  csubstr const& val_anchor(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_val_anchor(node)); return _p(node)->m_val.anchor; }
387  NodeScalar const& valsc (id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_val(node)); return _p(node)->m_val; }
388 
389  /** @} */
390 
391 public:
392 
393  /** @name node type predicates */
394  /** @{ */
395 
396  C4_ALWAYS_INLINE bool type_has_any(id_type node, NodeType_e bits) const { return _p(node)->m_type.has_any(bits); }
397  C4_ALWAYS_INLINE bool type_has_all(id_type node, NodeType_e bits) const { return _p(node)->m_type.has_all(bits); }
398  C4_ALWAYS_INLINE bool type_has_none(id_type node, NodeType_e bits) const { return _p(node)->m_type.has_none(bits); }
399 
400  C4_ALWAYS_INLINE bool is_stream(id_type node) const { return _p(node)->m_type.is_stream(); }
401  C4_ALWAYS_INLINE bool is_doc(id_type node) const { return _p(node)->m_type.is_doc(); }
402  C4_ALWAYS_INLINE bool is_container(id_type node) const { return _p(node)->m_type.is_container(); }
403  C4_ALWAYS_INLINE bool is_map(id_type node) const { return _p(node)->m_type.is_map(); }
404  C4_ALWAYS_INLINE bool is_seq(id_type node) const { return _p(node)->m_type.is_seq(); }
405  C4_ALWAYS_INLINE bool has_key(id_type node) const { return _p(node)->m_type.has_key(); }
406  C4_ALWAYS_INLINE bool has_val(id_type node) const { return _p(node)->m_type.has_val(); }
407  C4_ALWAYS_INLINE bool is_val(id_type node) const { return _p(node)->m_type.is_val(); }
408  C4_ALWAYS_INLINE bool is_keyval(id_type node) const { return _p(node)->m_type.is_keyval(); }
409  C4_ALWAYS_INLINE bool has_key_tag(id_type node) const { return _p(node)->m_type.has_key_tag(); }
410  C4_ALWAYS_INLINE bool has_val_tag(id_type node) const { return _p(node)->m_type.has_val_tag(); }
411  C4_ALWAYS_INLINE bool has_key_anchor(id_type node) const { return _p(node)->m_type.has_key_anchor(); }
412  C4_ALWAYS_INLINE bool has_val_anchor(id_type node) const { return _p(node)->m_type.has_val_anchor(); }
413  C4_ALWAYS_INLINE bool has_anchor(id_type node) const { return _p(node)->m_type.has_anchor(); }
414  C4_ALWAYS_INLINE bool is_key_ref(id_type node) const { return _p(node)->m_type.is_key_ref(); }
415  C4_ALWAYS_INLINE bool is_val_ref(id_type node) const { return _p(node)->m_type.is_val_ref(); }
416  C4_ALWAYS_INLINE bool is_ref(id_type node) const { return _p(node)->m_type.is_ref(); }
417 
418  C4_ALWAYS_INLINE bool parent_is_seq(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_parent(node)); return is_seq(_p(node)->m_parent); }
419  C4_ALWAYS_INLINE bool parent_is_map(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_parent(node)); return is_map(_p(node)->m_parent); }
420 
421  /** true when the node has an anchor named a */
422  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; }
423 
424  /** true if the node key does not have any KEYQUO flags, and its scalar verifies scalar_is_null().
425  * @warning the node must verify .has_key() (asserted) (ie must be a member of a map)
426  * @see https://github.com/biojppm/rapidyaml/issues/413 */
427  C4_ALWAYS_INLINE bool key_is_null(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_key(node)); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_key_quoted() && scalar_is_null(n->m_key.scalar); }
428  /** true if the node key does not have any VALQUO flags, and its scalar verifies scalar_is_null().
429  * @warning the node must verify .has_val() (asserted) (ie must be a scalar / must not be a container)
430  * @see https://github.com/biojppm/rapidyaml/issues/413 */
431  C4_ALWAYS_INLINE bool val_is_null(id_type node) const { _RYML_CB_ASSERT(m_callbacks, has_val(node)); NodeData const* C4_RESTRICT n = _p(node); return !n->m_type.is_val_quoted() && scalar_is_null(n->m_val.scalar); }
432 
433  /// true if the key was a scalar requiring filtering and was left
434  /// unfiltered during the parsing (see ParserOptions)
435  C4_ALWAYS_INLINE bool is_key_unfiltered(id_type node) const { return _p(node)->m_type.is_key_unfiltered(); }
436  /// true if the val was a scalar requiring filtering and was left
437  /// unfiltered during the parsing (see ParserOptions)
438  C4_ALWAYS_INLINE bool is_val_unfiltered(id_type node) const { return _p(node)->m_type.is_val_unfiltered(); }
439 
440  RYML_DEPRECATED("use has_key_anchor()") bool is_key_anchor(id_type node) const { return _p(node)->m_type.has_key_anchor(); }
441  RYML_DEPRECATED("use has_val_anchor()") bool is_val_anchor(id_type node) const { return _p(node)->m_type.has_val_anchor(); }
442  RYML_DEPRECATED("use has_anchor()") bool is_anchor(id_type node) const { return _p(node)->m_type.has_anchor(); }
443  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(); }
444 
445  /** @} */
446 
447 public:
448 
449  /** @name hierarchy predicates */
450  /** @{ */
451 
452  bool is_root(id_type node) const { _RYML_CB_ASSERT(m_callbacks, _p(node)->m_parent != NONE || node == 0); return _p(node)->m_parent == NONE; }
453 
454  bool has_parent(id_type node) const { return _p(node)->m_parent != NONE; }
455 
456  /** true when key and val are empty, and has no children */
457  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()); }
458 
459  /** true if @p node has a child with id @p ch */
460  bool has_child(id_type node, id_type ch) const { return _p(ch)->m_parent == node; }
461  /** true if @p node has a child with key @p key */
462  bool has_child(id_type node, csubstr key) const { return find_child(node, key) != NONE; }
463  /** true if @p node has any children key */
464  bool has_children(id_type node) const { return _p(node)->m_first_child != NONE; }
465 
466  /** true if @p node has a sibling with id @p sib */
467  bool has_sibling(id_type node, id_type sib) const { return _p(node)->m_parent == _p(sib)->m_parent; }
468  /** true if one of the node's siblings has the given key */
469  bool has_sibling(id_type node, csubstr key) const { return find_sibling(node, key) != NONE; }
470  /** true if node is not a single child */
471  bool has_other_siblings(id_type node) const
472  {
473  NodeData const *n = _p(node);
474  if(C4_LIKELY(n->m_parent != NONE))
475  {
476  n = _p(n->m_parent);
477  return n->m_first_child != n->m_last_child;
478  }
479  return false;
480  }
481 
482  RYML_DEPRECATED("use has_other_siblings()") bool has_siblings(id_type /*node*/) const { return true; }
483 
484  /** @} */
485 
486 public:
487 
488  /** @name hierarchy getters */
489  /** @{ */
490 
491  id_type parent(id_type node) const { return _p(node)->m_parent; }
492 
493  id_type prev_sibling(id_type node) const { return _p(node)->m_prev_sibling; }
494  id_type next_sibling(id_type node) const { return _p(node)->m_next_sibling; }
495 
496  /** O(#num_children) */
497  id_type num_children(id_type node) const;
498  id_type child_pos(id_type node, id_type ch) const;
499  id_type first_child(id_type node) const { return _p(node)->m_first_child; }
500  id_type last_child(id_type node) const { return _p(node)->m_last_child; }
501  id_type child(id_type node, id_type pos) const;
502  id_type find_child(id_type node, csubstr const& key) const;
503 
504  /** O(#num_siblings) */
505  /** counts with this */
506  id_type num_siblings(id_type node) const { return is_root(node) ? 1 : num_children(_p(node)->m_parent); }
507  /** does not count with this */
508  id_type num_other_siblings(id_type node) const { id_type ns = num_siblings(node); _RYML_CB_ASSERT(m_callbacks, ns > 0); return ns-1; }
509  id_type sibling_pos(id_type node, id_type sib) const { _RYML_CB_ASSERT(m_callbacks, ! is_root(node) || node == root_id()); return child_pos(_p(node)->m_parent, sib); }
510  id_type first_sibling(id_type node) const { return is_root(node) ? node : _p(_p(node)->m_parent)->m_first_child; }
511  id_type last_sibling(id_type node) const { return is_root(node) ? node : _p(_p(node)->m_parent)->m_last_child; }
512  id_type sibling(id_type node, id_type pos) const { return child(_p(node)->m_parent, pos); }
513  id_type find_sibling(id_type node, csubstr const& key) const { return find_child(_p(node)->m_parent, key); }
514 
515  id_type doc(id_type i) const { id_type rid = root_id(); _RYML_CB_ASSERT(m_callbacks, is_stream(rid)); return child(rid, i); } //!< gets the @p i document node index. requires that the root node is a stream.
516 
517  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 */
518  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 */
519 
520  /** @} */
521 
522 public:
523 
524  /** @name node style predicates and modifiers. see the corresponding predicate in NodeType */
525  /** @{ */
526 
527  C4_ALWAYS_INLINE bool is_container_styled(id_type node) const { return _p(node)->m_type.is_container_styled(); }
528  C4_ALWAYS_INLINE bool is_block(id_type node) const { return _p(node)->m_type.is_block(); }
529  C4_ALWAYS_INLINE bool is_flow_sl(id_type node) const { return _p(node)->m_type.is_flow_sl(); }
530  C4_ALWAYS_INLINE bool is_flow_ml(id_type node) const { return _p(node)->m_type.is_flow_ml(); }
531  C4_ALWAYS_INLINE bool is_flow(id_type node) const { return _p(node)->m_type.is_flow(); }
532 
533  C4_ALWAYS_INLINE bool is_key_styled(id_type node) const { return _p(node)->m_type.is_key_styled(); }
534  C4_ALWAYS_INLINE bool is_val_styled(id_type node) const { return _p(node)->m_type.is_val_styled(); }
535  C4_ALWAYS_INLINE bool is_key_literal(id_type node) const { return _p(node)->m_type.is_key_literal(); }
536  C4_ALWAYS_INLINE bool is_val_literal(id_type node) const { return _p(node)->m_type.is_val_literal(); }
537  C4_ALWAYS_INLINE bool is_key_folded(id_type node) const { return _p(node)->m_type.is_key_folded(); }
538  C4_ALWAYS_INLINE bool is_val_folded(id_type node) const { return _p(node)->m_type.is_val_folded(); }
539  C4_ALWAYS_INLINE bool is_key_squo(id_type node) const { return _p(node)->m_type.is_key_squo(); }
540  C4_ALWAYS_INLINE bool is_val_squo(id_type node) const { return _p(node)->m_type.is_val_squo(); }
541  C4_ALWAYS_INLINE bool is_key_dquo(id_type node) const { return _p(node)->m_type.is_key_dquo(); }
542  C4_ALWAYS_INLINE bool is_val_dquo(id_type node) const { return _p(node)->m_type.is_val_dquo(); }
543  C4_ALWAYS_INLINE bool is_key_plain(id_type node) const { return _p(node)->m_type.is_key_plain(); }
544  C4_ALWAYS_INLINE bool is_val_plain(id_type node) const { return _p(node)->m_type.is_val_plain(); }
545  C4_ALWAYS_INLINE bool is_key_quoted(id_type node) const { return _p(node)->m_type.is_key_quoted(); }
546  C4_ALWAYS_INLINE bool is_val_quoted(id_type node) const { return _p(node)->m_type.is_val_quoted(); }
547  C4_ALWAYS_INLINE bool is_quoted(id_type node) const { return _p(node)->m_type.is_quoted(); }
548 
549  C4_ALWAYS_INLINE void set_container_style(id_type node, NodeType_e style) { _RYML_CB_ASSERT(m_callbacks, is_container(node)); _p(node)->m_type.set_container_style(style); }
550  C4_ALWAYS_INLINE void set_key_style(id_type node, NodeType_e style) { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_type.set_key_style(style); }
551  C4_ALWAYS_INLINE void set_val_style(id_type node, NodeType_e style) { _RYML_CB_ASSERT(m_callbacks, has_val(node)); _p(node)->m_type.set_val_style(style); }
552 
553  /** @} */
554 
555 public:
556 
557  /** @name node type modifiers */
558  /** @{ */
559 
560  void to_keyval(id_type node, csubstr key, csubstr val, type_bits more_flags=0);
561  void to_map(id_type node, csubstr key, type_bits more_flags=0);
562  void to_seq(id_type node, csubstr key, type_bits more_flags=0);
563  void to_val(id_type node, csubstr val, type_bits more_flags=0);
564  void to_map(id_type node, type_bits more_flags=0);
565  void to_seq(id_type node, type_bits more_flags=0);
566  void to_doc(id_type node, type_bits more_flags=0);
567  void to_stream(id_type node, type_bits more_flags=0);
568 
569  void set_key(id_type node, csubstr key) { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_key.scalar = key; }
570  void set_val(id_type node, csubstr val) { _RYML_CB_ASSERT(m_callbacks, has_val(node)); _p(node)->m_val.scalar = val; }
571 
572  void set_key_tag(id_type node, csubstr tag) { _RYML_CB_ASSERT(m_callbacks, has_key(node)); _p(node)->m_key.tag = tag; _add_flags(node, KEYTAG); }
573  void set_val_tag(id_type node, csubstr tag) { _RYML_CB_ASSERT(m_callbacks, has_val(node) || is_container(node)); _p(node)->m_val.tag = tag; _add_flags(node, VALTAG); }
574 
575  void set_key_anchor(id_type node, csubstr anchor) { _RYML_CB_ASSERT(m_callbacks, ! is_key_ref(node)); _p(node)->m_key.anchor = anchor.triml('&'); _add_flags(node, KEYANCH); }
576  void set_val_anchor(id_type node, csubstr anchor) { _RYML_CB_ASSERT(m_callbacks, ! is_val_ref(node)); _p(node)->m_val.anchor = anchor.triml('&'); _add_flags(node, VALANCH); }
577  void set_key_ref (id_type node, csubstr ref ) { _RYML_CB_ASSERT(m_callbacks, ! has_key_anchor(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); }
578  void set_val_ref (id_type node, csubstr ref ) { _RYML_CB_ASSERT(m_callbacks, ! has_val_anchor(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); }
579 
580  void rem_key_anchor(id_type node) { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYANCH); }
581  void rem_val_anchor(id_type node) { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALANCH); }
582  void rem_key_ref (id_type node) { _p(node)->m_key.anchor.clear(); _rem_flags(node, KEYREF); }
583  void rem_val_ref (id_type node) { _p(node)->m_val.anchor.clear(); _rem_flags(node, VALREF); }
584  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); }
585 
586  /** @} */
587 
588 public:
589 
590  /** @name tree modifiers */
591  /** @{ */
592 
593  /** reorder the tree in memory so that all the nodes are stored
594  * in a linear sequence when visited in depth-first order.
595  * This will invalidate existing ids, since the node id is its
596  * position in the tree's node array. */
597  void reorder();
598 
599  /** Resolve references (aliases <- anchors) in the tree.
600  *
601  * Dereferencing is opt-in; after parsing, Tree::resolve() has to
602  * be called explicitly for obtaining resolved references in the
603  * tree. This method will @ref ReferenceResolver::resolve()
604  * to resolve all references and substitute the anchored values in
605  * place of the reference.
606  *
607  * This method first does a full traversal of the tree to gather all
608  * anchors and references in a separate collection, then it goes through
609  * that collection to locate the names, which it does by obeying the YAML
610  * standard diktat that "an alias node refers to the most recent node in
611  * the serialization having the specified anchor"
612  *
613  * So, depending on the number of anchor/alias nodes, this is a
614  * potentially expensive operation, with a best-case linear complexity
615  * (from the initial traversal). This potential cost is the reason for
616  * requiring an explicit call.
617  *
618  * @see ReferenceResolver::resolve()
619  */
620  void resolve(ReferenceResolver *C4_RESTRICT rr);
621 
622  /** Resolve references using a throw-away resolver. */
623  void resolve();
624 
625  /** @} */
626 
627 public:
628 
629  /** @name tag directives */
630  /** @{ */
631 
632  void resolve_tags();
633  void normalize_tags();
634  void normalize_tags_long();
635 
636  id_type num_tag_directives() const;
637  bool add_tag_directive(csubstr directive);
638  id_type add_tag_directive(TagDirective const& td);
639  void clear_tag_directives();
640 
641  /** resolve the given tag, appearing at node_id. Write the result into output.
642  * @return the number of characters required for the resolved tag */
643  size_t resolve_tag(substr output, csubstr tag, id_type node_id) const;
644  csubstr resolve_tag_sub(substr output, csubstr tag, id_type node_id) const
645  {
646  size_t needed = resolve_tag(output, tag, node_id);
647  return needed <= output.len ? output.first(needed) : output;
648  }
649 
650  TagDirective const* begin_tag_directives() const { return m_tag_directives; }
651  TagDirective const* end_tag_directives() const { return m_tag_directives + num_tag_directives(); }
652  c4::yml::TagDirectiveRange tag_directives() const { return c4::yml::TagDirectiveRange{begin_tag_directives(), end_tag_directives()}; }
653 
654  RYML_DEPRECATED("use c4::yml::tag_directive_const_iterator") typedef TagDirective const* tag_directive_const_iterator;
655  RYML_DEPRECATED("use c4::yml::TagDirectiveRange") typedef c4::yml::TagDirectiveRange TagDirectiveProxy;
656 
657  /** @} */
658 
659 public:
660 
661  /** @name modifying hierarchy */
662  /** @{ */
663 
664  /** create and insert a new child of @p parent. insert after the (to-be)
665  * sibling @p after, which must be a child of @p parent. To insert as the
666  * first child, set after to NONE */
667  C4_ALWAYS_INLINE id_type insert_child(id_type parent, id_type after)
668  {
669  _RYML_CB_ASSERT(m_callbacks, parent != NONE);
670  _RYML_CB_ASSERT(m_callbacks, is_container(parent) || is_root(parent));
671  _RYML_CB_ASSERT(m_callbacks, after == NONE || (_p(after)->m_parent == parent));
672  id_type child = _claim();
673  _set_hierarchy(child, parent, after);
674  return child;
675  }
676  /** create and insert a node as the first child of @p parent */
677  C4_ALWAYS_INLINE id_type prepend_child(id_type parent) { return insert_child(parent, NONE); }
678  /** create and insert a node as the last child of @p parent */
679  C4_ALWAYS_INLINE id_type append_child(id_type parent) { return insert_child(parent, _p(parent)->m_last_child); }
680  C4_ALWAYS_INLINE id_type _append_child__unprotected(id_type parent)
681  {
682  id_type child = _claim();
683  _set_hierarchy(child, parent, _p(parent)->m_last_child);
684  return child;
685  }
686 
687 public:
688 
689  #if defined(__clang__)
690  # pragma clang diagnostic push
691  # pragma clang diagnostic ignored "-Wnull-dereference"
692  #elif defined(__GNUC__)
693  # pragma GCC diagnostic push
694  # if __GNUC__ >= 6
695  # pragma GCC diagnostic ignored "-Wnull-dereference"
696  # endif
697  #endif
698 
699  //! create and insert a new sibling of n. insert after "after"
700  C4_ALWAYS_INLINE id_type insert_sibling(id_type node, id_type after)
701  {
702  return insert_child(_p(node)->m_parent, after);
703  }
704  /** create and insert a node as the first node of @p parent */
705  C4_ALWAYS_INLINE id_type prepend_sibling(id_type node) { return prepend_child(_p(node)->m_parent); }
706  C4_ALWAYS_INLINE id_type append_sibling(id_type node) { return append_child(_p(node)->m_parent); }
707 
708 public:
709 
710  /** remove an entire branch at once: ie remove the children and the node itself */
711  inline void remove(id_type node)
712  {
713  remove_children(node);
714  _release(node);
715  }
716 
717  /** remove all the node's children, but keep the node itself */
718  void remove_children(id_type node);
719 
720  /** change the @p type of the node to one of MAP, SEQ or VAL. @p
721  * type must have one and only one of MAP,SEQ,VAL; @p type may
722  * possibly have KEY, but if it does, then the @p node must also
723  * have KEY. Changing to the same type is a no-op. Otherwise,
724  * changing to a different type will initialize the node with an
725  * empty value of the desired type: changing to VAL will
726  * initialize with a null scalar (~), changing to MAP will
727  * initialize with an empty map ({}), and changing to SEQ will
728  * initialize with an empty seq ([]). */
729  bool change_type(id_type node, NodeType type);
730 
731  bool change_type(id_type node, type_bits type)
732  {
733  return change_type(node, (NodeType)type);
734  }
735 
736  #if defined(__clang__)
737  # pragma clang diagnostic pop
738  #elif defined(__GNUC__)
739  # pragma GCC diagnostic pop
740  #endif
741 
742 public:
743 
744  /** change the node's position in the parent */
745  void move(id_type node, id_type after);
746 
747  /** change the node's parent and position */
748  void move(id_type node, id_type new_parent, id_type after);
749 
750  /** change the node's parent and position to a different tree
751  * @return the index of the new node in the destination tree */
752  id_type move(Tree * src, id_type node, id_type new_parent, id_type after);
753 
754  /** ensure the first node is a stream. Eg, change this tree
755  *
756  * DOCMAP
757  * MAP
758  * KEYVAL
759  * KEYVAL
760  * SEQ
761  * VAL
762  *
763  * to
764  *
765  * STREAM
766  * DOCMAP
767  * MAP
768  * KEYVAL
769  * KEYVAL
770  * SEQ
771  * VAL
772  *
773  * If the root is already a stream, this is a no-op.
774  */
775  void set_root_as_stream();
776 
777 public:
778 
779  /** recursively duplicate a node from this tree into a new parent,
780  * placing it after one of its children
781  * @return the index of the copy */
782  id_type duplicate(id_type node, id_type new_parent, id_type after);
783  /** recursively duplicate a node from a different tree into a new parent,
784  * placing it after one of its children
785  * @return the index of the copy */
786  id_type duplicate(Tree const* src, id_type node, id_type new_parent, id_type after);
787 
788  /** recursively duplicate the node's children (but not the node)
789  * @return the index of the last duplicated child */
790  id_type duplicate_children(id_type node, id_type parent, id_type after);
791  /** recursively duplicate the node's children (but not the node), where
792  * the node is from a different tree
793  * @return the index of the last duplicated child */
794  id_type duplicate_children(Tree const* src, id_type node, id_type parent, id_type after);
795 
796  void duplicate_contents(id_type node, id_type where);
797  void duplicate_contents(Tree const* src, id_type node, id_type where);
798 
799  /** duplicate the node's children (but not the node) in a new parent, but
800  * omit repetitions where a duplicated node has the same key (in maps) or
801  * value (in seqs). If one of the duplicated children has the same key
802  * (in maps) or value (in seqs) as one of the parent's children, the one
803  * that is placed closest to the end will prevail. */
804  id_type duplicate_children_no_rep(id_type node, id_type parent, id_type after);
805  id_type duplicate_children_no_rep(Tree const* src, id_type node, id_type parent, id_type after);
806 
807 public:
808 
809  void merge_with(Tree const* src, id_type src_node=NONE, id_type dst_root=NONE);
810 
811  /** @} */
812 
813 public:
814 
815  /** @name internal string arena */
816  /** @{ */
817 
818  /** get the current size of the tree's internal arena */
819  RYML_DEPRECATED("use arena_size() instead") size_t arena_pos() const { return m_arena_pos; }
820  /** get the current size of the tree's internal arena */
821  inline size_t arena_size() const { return m_arena_pos; }
822  /** get the current capacity of the tree's internal arena */
823  inline size_t arena_capacity() const { return m_arena.len; }
824  /** get the current slack of the tree's internal arena */
825  inline size_t arena_slack() const { _RYML_CB_ASSERT(m_callbacks, m_arena.len >= m_arena_pos); return m_arena.len - m_arena_pos; }
826 
827  /** get the current arena */
828  csubstr arena() const { return m_arena.first(m_arena_pos); }
829  /** get the current arena */
830  substr arena() { return m_arena.first(m_arena_pos); }
831 
832  /** return true if the given substring is part of the tree's string arena */
833  bool in_arena(csubstr s) const
834  {
835  return m_arena.is_super(s);
836  }
837 
838  /** serialize the given floating-point variable to the tree's
839  * arena, growing it as needed to accomodate the serialization.
840  *
841  * @note Growing the arena may cause relocation of the entire
842  * existing arena, and thus change the contents of individual
843  * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
844  * cost, ensure that the arena is reserved to an appropriate size
845  * using .reserve_arena()
846  *
847  * @see alloc_arena() */
848  template<class T>
849  typename std::enable_if<std::is_floating_point<T>::value, csubstr>::type
850  to_arena(T const& C4_RESTRICT a)
851  {
852  substr rem(m_arena.sub(m_arena_pos));
853  size_t num = to_chars_float(rem, a);
854  if(num > rem.len)
855  {
856  rem = _grow_arena(num);
857  num = to_chars_float(rem, a);
858  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
859  }
860  rem = _request_span(num);
861  return rem;
862  }
863 
864  /** serialize the given non-floating-point variable to the tree's
865  * arena, growing it as needed to accomodate the serialization.
866  *
867  * @note Growing the arena may cause relocation of the entire
868  * existing arena, and thus change the contents of individual
869  * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
870  * cost, ensure that the arena is reserved to an appropriate size
871  * using .reserve_arena()
872  *
873  * @see alloc_arena() */
874  template<class T>
875  typename std::enable_if<!std::is_floating_point<T>::value, csubstr>::type
876  to_arena(T const& C4_RESTRICT a)
877  {
878  substr rem(m_arena.sub(m_arena_pos));
879  size_t num = to_chars(rem, a);
880  if(num > rem.len)
881  {
882  rem = _grow_arena(num);
883  num = to_chars(rem, a);
884  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
885  }
886  rem = _request_span(num);
887  return rem;
888  }
889 
890  /** serialize the given csubstr to the tree's arena, growing the
891  * arena as needed to accomodate the serialization.
892  *
893  * @note Growing the arena may cause relocation of the entire
894  * existing arena, and thus change the contents of individual
895  * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
896  * cost, ensure that the arena is reserved to an appropriate size
897  * using .reserve_arena()
898  *
899  * @see alloc_arena() */
900  csubstr to_arena(csubstr a)
901  {
902  if(a.len > 0)
903  {
904  substr rem(m_arena.sub(m_arena_pos));
905  size_t num = to_chars(rem, a);
906  if(num > rem.len)
907  {
908  rem = _grow_arena(num);
909  num = to_chars(rem, a);
910  _RYML_CB_ASSERT(m_callbacks, num <= rem.len);
911  }
912  return _request_span(num);
913  }
914  else
915  {
916  if(a.str == nullptr)
917  {
918  return csubstr{};
919  }
920  else if(m_arena.str == nullptr)
921  {
922  // Arena is empty and we want to store a non-null
923  // zero-length string.
924  // Even though the string has zero length, we need
925  // some "memory" to store a non-nullptr string
926  _grow_arena(1);
927  }
928  return _request_span(0);
929  }
930  }
931  C4_ALWAYS_INLINE csubstr to_arena(const char *s)
932  {
933  return to_arena(to_csubstr(s));
934  }
935  C4_ALWAYS_INLINE csubstr to_arena(std::nullptr_t)
936  {
937  return csubstr{};
938  }
939 
940  /** copy the given substr to the tree's arena, growing it by the
941  * required size
942  *
943  * @note Growing the arena may cause relocation of the entire
944  * existing arena, and thus change the contents of individual
945  * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
946  * cost, ensure that the arena is reserved to an appropriate size
947  * using .reserve_arena()
948  *
949  * @see alloc_arena() */
950  substr copy_to_arena(csubstr s)
951  {
952  substr cp = alloc_arena(s.len);
953  _RYML_CB_ASSERT(m_callbacks, cp.len == s.len);
954  _RYML_CB_ASSERT(m_callbacks, !s.overlaps(cp));
955  #if (!defined(__clang__)) && (defined(__GNUC__) && __GNUC__ >= 10)
956  C4_SUPPRESS_WARNING_GCC_PUSH
957  C4_SUPPRESS_WARNING_GCC("-Wstringop-overflow=") // no need for terminating \0
958  C4_SUPPRESS_WARNING_GCC( "-Wrestrict") // there's an assert to ensure no violation of restrict behavior
959  #endif
960  if(s.len)
961  memcpy(cp.str, s.str, s.len);
962  #if (!defined(__clang__)) && (defined(__GNUC__) && __GNUC__ >= 10)
963  C4_SUPPRESS_WARNING_GCC_POP
964  #endif
965  return cp;
966  }
967 
968  /** grow the tree's string arena by the given size and return a substr
969  * of the added portion
970  *
971  * @note Growing the arena may cause relocation of the entire
972  * existing arena, and thus change the contents of individual
973  * nodes, and thus cost O(numnodes)+O(arenasize). To avoid this
974  * cost, ensure that the arena is reserved to an appropriate size
975  * using .reserve_arena().
976  *
977  * @see reserve_arena() */
978  substr alloc_arena(size_t sz)
979  {
980  if(sz > arena_slack())
981  _grow_arena(sz - arena_slack());
982  substr s = _request_span(sz);
983  return s;
984  }
985 
986  /** ensure the tree's internal string arena is at least the given capacity
987  * @warning This operation may be expensive, with a potential complexity of O(numNodes)+O(arenasize).
988  * @warning Growing the arena may cause relocation of the entire
989  * existing arena, and thus change the contents of individual nodes. */
990  void reserve_arena(size_t arena_cap)
991  {
992  if(arena_cap > m_arena.len)
993  {
994  substr buf;
995  buf.str = (char*) m_callbacks.m_allocate(arena_cap, m_arena.str, m_callbacks.m_user_data);
996  buf.len = arena_cap;
997  if(m_arena.str)
998  {
999  _RYML_CB_ASSERT(m_callbacks, m_arena.len >= 0);
1000  _relocate(buf); // does a memcpy and changes nodes using the arena
1001  m_callbacks.m_free(m_arena.str, m_arena.len, m_callbacks.m_user_data);
1002  }
1003  m_arena = buf;
1004  }
1005  }
1006 
1007  /** @} */
1008 
1009 private:
1010 
1011  substr _grow_arena(size_t more)
1012  {
1013  size_t cap = m_arena.len + more;
1014  cap = cap < 2 * m_arena.len ? 2 * m_arena.len : cap;
1015  cap = cap < 64 ? 64 : cap;
1016  reserve_arena(cap);
1017  return m_arena.sub(m_arena_pos);
1018  }
1019 
1020  substr _request_span(size_t sz)
1021  {
1022  _RYML_CB_ASSERT(m_callbacks, m_arena_pos + sz <= m_arena.len);
1023  substr s;
1024  s = m_arena.sub(m_arena_pos, sz);
1025  m_arena_pos += sz;
1026  return s;
1027  }
1028 
1029  substr _relocated(csubstr s, substr next_arena) const
1030  {
1031  _RYML_CB_ASSERT(m_callbacks, m_arena.is_super(s));
1032  _RYML_CB_ASSERT(m_callbacks, m_arena.sub(0, m_arena_pos).is_super(s));
1033  auto pos = (s.str - m_arena.str); // this is larger than 0 based on the assertions above
1034  substr r(next_arena.str + pos, s.len);
1035  _RYML_CB_ASSERT(m_callbacks, r.str - next_arena.str == pos);
1036  _RYML_CB_ASSERT(m_callbacks, next_arena.sub(0, m_arena_pos).is_super(r));
1037  return r;
1038  }
1039 
1040 public:
1041 
1042  /** @name lookup */
1043  /** @{ */
1044 
1046  {
1049  size_t path_pos;
1050  csubstr path;
1051 
1052  inline operator bool() const { return target != NONE; }
1053 
1054  lookup_result() : target(NONE), closest(NONE), path_pos(0), path() {}
1055  lookup_result(csubstr path_, id_type start) : target(NONE), closest(start), path_pos(0), path(path_) {}
1056 
1057  /** get the part ot the input path that was resolved */
1058  csubstr resolved() const;
1059  /** get the part ot the input path that was unresolved */
1060  csubstr unresolved() const;
1061  };
1062 
1063  /** for example foo.bar[0].baz */
1064  lookup_result lookup_path(csubstr path, id_type start=NONE) const;
1065 
1066  /** defaulted lookup: lookup @p path; if the lookup fails, recursively modify
1067  * the tree so that the corresponding lookup_path() would return the
1068  * default value.
1069  * @see lookup_path() */
1070  id_type lookup_path_or_modify(csubstr default_value, csubstr path, id_type start=NONE);
1071 
1072  /** defaulted lookup: lookup @p path; if the lookup fails, recursively modify
1073  * the tree so that the corresponding lookup_path() would return the
1074  * branch @p src_node (from the tree @p src).
1075  * @see lookup_path() */
1076  id_type lookup_path_or_modify(Tree const *src, id_type src_node, csubstr path, id_type start=NONE);
1077 
1078  /** @} */
1079 
1080 private:
1081 
1082  struct _lookup_path_token
1083  {
1084  csubstr value;
1085  NodeType type;
1086  _lookup_path_token() : value(), type() {}
1087  _lookup_path_token(csubstr v, NodeType t) : value(v), type(t) {}
1088  inline operator bool() const { return type != NOTYPE; }
1089  bool is_index() const { return value.begins_with('[') && value.ends_with(']'); }
1090  };
1091 
1092  id_type _lookup_path_or_create(csubstr path, id_type start);
1093 
1094  void _lookup_path (lookup_result *r) const;
1095  void _lookup_path_modify(lookup_result *r);
1096 
1097  id_type _next_node (lookup_result *r, _lookup_path_token *parent) const;
1098  id_type _next_node_modify(lookup_result *r, _lookup_path_token *parent);
1099 
1100  void _advance(lookup_result *r, size_t more) const;
1101 
1102  _lookup_path_token _next_token(lookup_result *r, _lookup_path_token const& parent) const;
1103 
1104 private:
1105 
1106  void _clear();
1107  void _free();
1108  void _copy(Tree const& that);
1109  void _move(Tree & that) noexcept;
1110 
1111  void _relocate(substr next_arena);
1112 
1113 public:
1114 
1115  /** @cond dev*/
1116 
1117  #if ! RYML_USE_ASSERT
1118  C4_ALWAYS_INLINE void _check_next_flags(id_type, type_bits) {}
1119  #else
1120  void _check_next_flags(id_type node, type_bits f)
1121  {
1122  auto n = _p(node);
1123  type_bits o = n->m_type; // old
1124  C4_UNUSED(o);
1125  if(f & MAP)
1126  {
1127  RYML_ASSERT_MSG((f & SEQ) == 0, "cannot mark simultaneously as map and seq");
1128  RYML_ASSERT_MSG((f & VAL) == 0, "cannot mark simultaneously as map and val");
1129  RYML_ASSERT_MSG((o & SEQ) == 0, "cannot turn a seq into a map; clear first");
1130  RYML_ASSERT_MSG((o & VAL) == 0, "cannot turn a val into a map; clear first");
1131  }
1132  else if(f & SEQ)
1133  {
1134  RYML_ASSERT_MSG((f & MAP) == 0, "cannot mark simultaneously as seq and map");
1135  RYML_ASSERT_MSG((f & VAL) == 0, "cannot mark simultaneously as seq and val");
1136  RYML_ASSERT_MSG((o & MAP) == 0, "cannot turn a map into a seq; clear first");
1137  RYML_ASSERT_MSG((o & VAL) == 0, "cannot turn a val into a seq; clear first");
1138  }
1139  if(f & KEY)
1140  {
1141  _RYML_CB_ASSERT(m_callbacks, !is_root(node));
1142  auto pid = parent(node); C4_UNUSED(pid);
1143  _RYML_CB_ASSERT(m_callbacks, is_map(pid));
1144  }
1145  if((f & VAL) && !is_root(node))
1146  {
1147  auto pid = parent(node); C4_UNUSED(pid);
1148  _RYML_CB_ASSERT(m_callbacks, is_map(pid) || is_seq(pid));
1149  }
1150  }
1151  #endif
1152 
1153  inline void _set_flags(id_type node, NodeType_e f) { _check_next_flags(node, f); _p(node)->m_type = f; }
1154  inline void _set_flags(id_type node, type_bits f) { _check_next_flags(node, f); _p(node)->m_type = f; }
1155 
1156  inline void _add_flags(id_type node, NodeType_e f) { NodeData *d = _p(node); type_bits fb = f | d->m_type; _check_next_flags(node, fb); d->m_type = (NodeType_e) fb; }
1157  inline void _add_flags(id_type node, type_bits f) { NodeData *d = _p(node); f |= d->m_type; _check_next_flags(node, f); d->m_type = f; }
1158 
1159  inline void _rem_flags(id_type node, NodeType_e f) { NodeData *d = _p(node); type_bits fb = d->m_type & ~f; _check_next_flags(node, fb); d->m_type = (NodeType_e) fb; }
1160  inline void _rem_flags(id_type node, type_bits f) { NodeData *d = _p(node); f = d->m_type & ~f; _check_next_flags(node, f); d->m_type = f; }
1161 
1162  void _set_key(id_type node, csubstr key, type_bits more_flags=0)
1163  {
1164  _p(node)->m_key.scalar = key;
1165  _add_flags(node, KEY|more_flags);
1166  }
1167  void _set_key(id_type node, NodeScalar const& key, type_bits more_flags=0)
1168  {
1169  _p(node)->m_key = key;
1170  _add_flags(node, KEY|more_flags);
1171  }
1172 
1173  void _set_val(id_type node, csubstr val, type_bits more_flags=0)
1174  {
1175  _RYML_CB_ASSERT(m_callbacks, num_children(node) == 0);
1176  _RYML_CB_ASSERT(m_callbacks, !is_seq(node) && !is_map(node));
1177  _p(node)->m_val.scalar = val;
1178  _add_flags(node, VAL|more_flags);
1179  }
1180  void _set_val(id_type node, NodeScalar const& val, type_bits more_flags=0)
1181  {
1182  _RYML_CB_ASSERT(m_callbacks, num_children(node) == 0);
1183  _RYML_CB_ASSERT(m_callbacks, ! is_container(node));
1184  _p(node)->m_val = val;
1185  _add_flags(node, VAL|more_flags);
1186  }
1187 
1188  void _set(id_type node, NodeInit const& i)
1189  {
1190  _RYML_CB_ASSERT(m_callbacks, i._check());
1191  NodeData *n = _p(node);
1192  _RYML_CB_ASSERT(m_callbacks, n->m_key.scalar.empty() || i.key.scalar.empty() || i.key.scalar == n->m_key.scalar);
1193  _add_flags(node, i.type);
1194  if(n->m_key.scalar.empty())
1195  {
1196  if( ! i.key.scalar.empty())
1197  {
1198  _set_key(node, i.key.scalar);
1199  }
1200  }
1201  n->m_key.tag = i.key.tag;
1202  n->m_val = i.val;
1203  }
1204 
1205  void _set_parent_as_container_if_needed(id_type in)
1206  {
1207  NodeData const* n = _p(in);
1208  id_type ip = parent(in);
1209  if(ip != NONE)
1210  {
1211  if( ! (is_seq(ip) || is_map(ip)))
1212  {
1213  if((in == first_child(ip)) && (in == last_child(ip)))
1214  {
1215  if( ! n->m_key.empty() || has_key(in))
1216  {
1217  _add_flags(ip, MAP);
1218  }
1219  else
1220  {
1221  _add_flags(ip, SEQ);
1222  }
1223  }
1224  }
1225  }
1226  }
1227 
1228  void _seq2map(id_type node)
1229  {
1230  _RYML_CB_ASSERT(m_callbacks, is_seq(node));
1231  for(id_type i = first_child(node); i != NONE; i = next_sibling(i))
1232  {
1233  NodeData *C4_RESTRICT ch = _p(i);
1234  if(ch->m_type.is_keyval())
1235  continue;
1236  ch->m_type.add(KEY);
1237  ch->m_key = ch->m_val;
1238  }
1239  auto *C4_RESTRICT n = _p(node);
1240  n->m_type.rem(SEQ);
1241  n->m_type.add(MAP);
1242  }
1243 
1244  id_type _do_reorder(id_type *node, id_type count);
1245 
1246  void _swap(id_type n_, id_type m_);
1247  void _swap_props(id_type n_, id_type m_);
1248  void _swap_hierarchy(id_type n_, id_type m_);
1249  void _copy_hierarchy(id_type dst_, id_type src_);
1250 
1251  inline void _copy_props(id_type dst_, id_type src_)
1252  {
1253  _copy_props(dst_, this, src_);
1254  }
1255 
1256  inline void _copy_props_wo_key(id_type dst_, id_type src_)
1257  {
1258  _copy_props_wo_key(dst_, this, src_);
1259  }
1260 
1261  void _copy_props(id_type dst_, Tree const* that_tree, id_type src_)
1262  {
1263  auto & C4_RESTRICT dst = *_p(dst_);
1264  auto const& C4_RESTRICT src = *that_tree->_p(src_);
1265  dst.m_type = src.m_type;
1266  dst.m_key = src.m_key;
1267  dst.m_val = src.m_val;
1268  }
1269 
1270  void _copy_props(id_type dst_, Tree const* that_tree, id_type src_, type_bits src_mask)
1271  {
1272  auto & C4_RESTRICT dst = *_p(dst_);
1273  auto const& C4_RESTRICT src = *that_tree->_p(src_);
1274  dst.m_type = (src.m_type & src_mask) | (dst.m_type & ~src_mask);
1275  dst.m_key = src.m_key;
1276  dst.m_val = src.m_val;
1277  }
1278 
1279  void _copy_props_wo_key(id_type dst_, Tree const* that_tree, id_type src_)
1280  {
1281  auto & C4_RESTRICT dst = *_p(dst_);
1282  auto const& C4_RESTRICT src = *that_tree->_p(src_);
1283  dst.m_type = (src.m_type & ~_KEYMASK) | (dst.m_type & _KEYMASK);
1284  dst.m_val = src.m_val;
1285  }
1286 
1287  void _copy_props_wo_key(id_type dst_, Tree const* that_tree, id_type src_, type_bits src_mask)
1288  {
1289  auto & C4_RESTRICT dst = *_p(dst_);
1290  auto const& C4_RESTRICT src = *that_tree->_p(src_);
1291  dst.m_type = (src.m_type & ((~_KEYMASK)|src_mask)) | (dst.m_type & (_KEYMASK|~src_mask));
1292  dst.m_val = src.m_val;
1293  }
1294 
1295  inline void _clear_type(id_type node)
1296  {
1297  _p(node)->m_type = NOTYPE;
1298  }
1299 
1300  inline void _clear(id_type node)
1301  {
1302  auto *C4_RESTRICT n = _p(node);
1303  n->m_type = NOTYPE;
1304  n->m_key.clear();
1305  n->m_val.clear();
1306  n->m_parent = NONE;
1307  n->m_first_child = NONE;
1308  n->m_last_child = NONE;
1309  }
1310 
1311  inline void _clear_key(id_type node)
1312  {
1313  _p(node)->m_key.clear();
1314  _rem_flags(node, KEY);
1315  }
1316 
1317  inline void _clear_val(id_type node)
1318  {
1319  _p(node)->m_val.clear();
1320  _rem_flags(node, VAL);
1321  }
1322 
1323  /** @endcond */
1324 
1325 private:
1326 
1327  void _clear_range(id_type first, id_type num);
1328 
1329 public:
1330  id_type _claim();
1331 private:
1332  void _claim_root();
1333  void _release(id_type node);
1334  void _free_list_add(id_type node);
1335  void _free_list_rem(id_type node);
1336 
1337  void _set_hierarchy(id_type node, id_type parent, id_type after_sibling);
1338  void _rem_hierarchy(id_type node);
1339 
1340 public:
1341 
1342  // members are exposed, but you should NOT access them directly
1343 
1346 
1348 
1351 
1352  substr m_arena;
1353  size_t m_arena_pos;
1354 
1356 
1358 
1359 };
1360 
1361 /** @} */
1362 
1363 } // namespace yml
1364 } // namespace c4
1365 
1366 
1367 C4_SUPPRESS_WARNING_MSVC_POP
1368 C4_SUPPRESS_WARNING_GCC_CLANG_POP
1369 
1370 
1371 #endif /* _C4_YML_TREE_HPP_ */
Lightweight generic type-safe wrappers for converting individual values to/from strings.
Holds a pointer to an existing tree, and a node id.
Definition: node.hpp:836
A reference to a node in an existing yaml tree, offering a more convenient API than the index-based A...
Definition: node.hpp:975
csubstr to_arena(csubstr a)
serialize the given csubstr to the tree's arena, growing the arena as needed to accomodate the serial...
Definition: tree.hpp:900
id_type num_other_siblings(id_type node) const
does not count with this
Definition: tree.hpp:508
NodeData * m_buf
Definition: tree.hpp:1344
void reserve_arena(size_t arena_cap)
ensure the tree's internal string arena is at least the given capacity
Definition: tree.hpp:990
id_type m_free_head
Definition: tree.hpp:1349
bool has_sibling(id_type node, csubstr key) const
true if one of the node's siblings has the given key
Definition: tree.hpp:469
csubstr resolve_tag_sub(substr output, csubstr tag, id_type node_id) const
Definition: tree.hpp:644
bool is_key_plain(id_type node) const
Definition: tree.hpp:543
id_type num_siblings(id_type node) const
O(num_siblings)
Definition: tree.hpp:506
id_type first_child(id_type node) const
Definition: tree.hpp:499
bool is_stream(id_type node) const
Definition: tree.hpp:400
NodeType type(id_type node) const
Definition: tree.hpp:374
id_type append_sibling(id_type node)
Definition: tree.hpp:706
void set_val_ref(id_type node, csubstr ref)
Definition: tree.hpp:578
id_type root_id() const
Get the id of the root node.
Definition: tree.hpp:327
size_t arena_slack() const
get the current slack of the tree's internal arena
Definition: tree.hpp:825
bool has_key_tag(id_type node) const
Definition: tree.hpp:409
id_type prev_sibling(id_type node) const
Definition: tree.hpp:493
NodeData * get(id_type node)
get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned
Definition: tree.hpp:300
id_type prepend_sibling(id_type node)
create and insert a node as the first node of parent
Definition: tree.hpp:705
std::enable_if<!std::is_floating_point< T >::value, csubstr >::type to_arena(T const &a)
serialize the given non-floating-point variable to the tree's arena, growing it as needed to accomoda...
Definition: tree.hpp:876
size_t m_arena_pos
Definition: tree.hpp:1353
bool is_map(id_type node) const
Definition: tree.hpp:403
csubstr const & key_ref(id_type node) const
Definition: tree.hpp:379
void callbacks(Callbacks const &cb)
Definition: tree.hpp:279
id_type sibling(id_type node, id_type pos) const
Definition: tree.hpp:512
c4::yml::TagDirectiveRange tag_directives() const
Definition: tree.hpp:652
bool is_key_quoted(id_type node) const
Definition: tree.hpp:545
bool has_val_anchor(id_type node) const
Definition: tree.hpp:412
void clear_arena()
Definition: tree.hpp:270
bool is_root(id_type node) const
Definition: tree.hpp:452
bool key_is_null(id_type node) const
true if the node key does not have any KEYQUO flags, and its scalar verifies scalar_is_null().
Definition: tree.hpp:427
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:978
bool is_keyval(id_type node) const
Definition: tree.hpp:408
bool is_val_folded(id_type node) const
Definition: tree.hpp:538
id_type m_size
Definition: tree.hpp:1347
id_type last_sibling(id_type node) const
Definition: tree.hpp:511
bool has_key(id_type node) const
Definition: tree.hpp:405
bool has_other_siblings(id_type node) const
true if node is not a single child
Definition: tree.hpp:471
bool in_arena(csubstr s) const
return true if the given substring is part of the tree's string arena
Definition: tree.hpp:833
id_type parent(id_type node) const
Definition: tree.hpp:491
void set_key_style(id_type node, NodeType_e style)
Definition: tree.hpp:550
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:435
void rem_key_anchor(id_type node)
Definition: tree.hpp:580
Callbacks const & callbacks() const
Definition: tree.hpp:278
TagDirective const * end_tag_directives() const
Definition: tree.hpp:651
bool is_key_literal(id_type node) const
Definition: tree.hpp:535
bool is_block(id_type node) const
Definition: tree.hpp:528
id_type prepend_child(id_type parent)
create and insert a node as the first child of parent
Definition: tree.hpp:677
bool is_val(id_type node) const
Definition: tree.hpp:407
NodeScalar const & valsc(id_type node) const
Definition: tree.hpp:387
bool type_has_any(id_type node, NodeType_e bits) const
Definition: tree.hpp:396
bool is_container_styled(id_type node) const
Definition: tree.hpp:527
bool empty() const
Definition: tree.hpp:272
csubstr to_arena(const char *s)
Definition: tree.hpp:931
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:322
id_type sibling_pos(id_type node, id_type sib) const
Definition: tree.hpp:509
id_type append_child(id_type parent)
create and insert a node as the last child of parent
Definition: tree.hpp:679
bool has_sibling(id_type node, id_type sib) const
true if node has a sibling with id sib
Definition: tree.hpp:467
id_type next_sibling(id_type node) const
Definition: tree.hpp:494
bool is_key_squo(id_type node) const
Definition: tree.hpp:539
bool parent_is_seq(id_type node) const
Definition: tree.hpp:418
csubstr const & key(id_type node) const
Definition: tree.hpp:377
csubstr const & val_ref(id_type node) const
Definition: tree.hpp:385
bool has_anchor(id_type node) const
Definition: tree.hpp:413
id_type insert_sibling(id_type node, id_type after)
create and insert a new sibling of n. insert after "after"
Definition: tree.hpp:700
bool has_val_tag(id_type node) const
Definition: tree.hpp:410
bool is_key_dquo(id_type node) const
Definition: tree.hpp:541
void set_key_tag(id_type node, csubstr tag)
Definition: tree.hpp:572
id_type m_cap
Definition: tree.hpp:1345
TagDirective const * begin_tag_directives() const
Definition: tree.hpp:650
void rem_anchor_ref(id_type node)
Definition: tree.hpp:584
id_type last_child(id_type node) const
Definition: tree.hpp:500
id_type id(NodeData const *n) const
get the index of a node belonging to this tree. n can be nullptr, in which case NONE is returned
Definition: tree.hpp:290
substr m_arena
Definition: tree.hpp:1352
NodeData const * get(id_type node) const
get a pointer to a node's NodeData. i can be NONE, in which case a nullptr is returned.
Definition: tree.hpp:309
void set_val_anchor(id_type node, csubstr anchor)
Definition: tree.hpp:576
bool is_val_plain(id_type node) const
Definition: tree.hpp:544
bool is_doc(id_type node) const
Definition: tree.hpp:401
bool has_child(id_type node, csubstr key) const
true if node has a child with key key
Definition: tree.hpp:462
void set_container_style(id_type node, NodeType_e style)
Definition: tree.hpp:549
csubstr to_arena(std::nullptr_t)
Definition: tree.hpp:935
bool is_key_ref(id_type node) const
Definition: tree.hpp:414
void set_key(id_type node, csubstr key)
Definition: tree.hpp:569
bool has_val(id_type node) const
Definition: tree.hpp:406
bool is_val_dquo(id_type node) const
Definition: tree.hpp:542
NodeScalar const & keysc(id_type node) const
Definition: tree.hpp:381
size_t arena_capacity() const
get the current capacity of the tree's internal arena
Definition: tree.hpp:823
csubstr const & key_anchor(id_type node) const
Definition: tree.hpp:380
id_type doc(id_type i) const
gets the i document node index.
Definition: tree.hpp:515
bool is_flow_ml(id_type node) const
Definition: tree.hpp:530
bool is_key_folded(id_type node) const
Definition: tree.hpp:537
csubstr const & val_tag(id_type node) const
Definition: tree.hpp:384
bool type_has_all(id_type node, NodeType_e bits) const
Definition: tree.hpp:397
id_type m_free_tail
Definition: tree.hpp:1350
id_type slack() const
Definition: tree.hpp:276
void set_val(id_type node, csubstr val)
Definition: tree.hpp:570
csubstr const & val(id_type node) const
Definition: tree.hpp:383
id_type find_sibling(id_type node, csubstr const &key) const
Definition: tree.hpp:513
bool is_seq(id_type node) const
Definition: tree.hpp:404
bool is_val_styled(id_type node) const
Definition: tree.hpp:534
bool parent_is_map(id_type node) const
Definition: tree.hpp:419
std::enable_if< std::is_floating_point< T >::value, csubstr >::type to_arena(T const &a)
serialize the given floating-point variable to the tree's arena, growing it as needed to accomodate t...
Definition: tree.hpp:850
bool has_key_anchor(id_type node) const
Definition: tree.hpp:411
void remove(id_type node)
remove an entire branch at once: ie remove the children and the node itself
Definition: tree.hpp:711
bool is_quoted(id_type node) const
Definition: tree.hpp:547
bool change_type(id_type node, type_bits type)
Definition: tree.hpp:731
Tree(id_type node_capacity, size_t arena_capacity=0)
Definition: tree.hpp:246
csubstr const & key_tag(id_type node) const
Definition: tree.hpp:378
void set_val_style(id_type node, NodeType_e style)
Definition: tree.hpp:551
substr arena()
get the current arena
Definition: tree.hpp:830
id_type root_id()
Get the id of the root node.
Definition: tree.hpp:325
void rem_val_ref(id_type node)
Definition: tree.hpp:583
bool has_parent(id_type node) const
Definition: tree.hpp:454
bool val_is_null(id_type node) const
true if the node key does not have any VALQUO flags, and its scalar verifies scalar_is_null().
Definition: tree.hpp:431
id_type first_sibling(id_type node) const
Definition: tree.hpp:510
bool is_flow(id_type node) const
Definition: tree.hpp:531
bool is_val_quoted(id_type node) const
Definition: tree.hpp:546
TagDirective const * tag_directive_const_iterator
Definition: tree.hpp:654
bool type_has_none(id_type node, NodeType_e bits) const
Definition: tree.hpp:398
bool empty(id_type node) const
true when key and val are empty, and has no children
Definition: tree.hpp:457
bool is_val_squo(id_type node) const
Definition: tree.hpp:540
substr copy_to_arena(csubstr s)
copy the given substr to the tree's arena, growing it by the required size
Definition: tree.hpp:950
csubstr arena() const
get the current arena
Definition: tree.hpp:828
id_type size() const
Definition: tree.hpp:274
bool is_container(id_type node) const
Definition: tree.hpp:402
size_t arena_size() const
get the current size of the tree's internal arena
Definition: tree.hpp:821
bool is_ref(id_type node) const
Definition: tree.hpp:416
void set_val_tag(id_type node, csubstr tag)
Definition: tree.hpp:573
bool is_key_styled(id_type node) const
Definition: tree.hpp:533
void set_key_anchor(id_type node, csubstr anchor)
Definition: tree.hpp:575
bool is_val_ref(id_type node) const
Definition: tree.hpp:415
void set_key_ref(id_type node, csubstr ref)
Definition: tree.hpp:577
id_type _append_child__unprotected(id_type parent)
Definition: tree.hpp:680
bool has_child(id_type node, id_type ch) const
true if node has a child with id ch
Definition: tree.hpp:460
bool is_val_literal(id_type node) const
Definition: tree.hpp:536
void rem_val_anchor(id_type node)
Definition: tree.hpp:581
Callbacks m_callbacks
Definition: tree.hpp:1355
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:438
void rem_key_ref(id_type node)
Definition: tree.hpp:582
csubstr const & val_anchor(id_type node) const
Definition: tree.hpp:386
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:319
id_type capacity() const
Definition: tree.hpp:275
bool is_flow_sl(id_type node) const
Definition: tree.hpp:529
bool has_children(id_type node) const
true if node has any children key
Definition: tree.hpp:464
bool has_anchor(id_type node, csubstr a) const
true when the node has an anchor named a
Definition: tree.hpp:422
const char * type_str(id_type node) const
Definition: tree.hpp:375
Common utilities and infrastructure used by ryml.
#define RYML_NOEXCEPT
Conditionally expands to noexcept when RYML_USE_ASSERT is 0 and is empty otherwise.
Definition: common.hpp:166
#define RYML_EXPORT
Definition: export.hpp:15
forward declarations
Callbacks const & get_callbacks()
get the global callbacks
Definition: common.cpp:118
bool scalar_is_null(csubstr s) noexcept
YAML-sense query of nullity.
Definition: node_type.hpp:251
uint32_t type_bits
the integral type necessary to cover all the bits for NodeType_e
Definition: node_type.hpp:26
NodeType_e
a bit mask for marking node types and styles
Definition: node_type.hpp:30
@ 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
@ KEY
is member of a map, must have non-empty key
Definition: node_type.hpp:33
@ _KEYMASK
Definition: node_type.hpp:95
@ 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
@ KEYREF
a *reference: the key references an &anchor
Definition: node_type.hpp:39
@ KEYANCH
the key has an &anchor
Definition: node_type.hpp:41
bool from_chars(ryml::csubstr buf, vec2< T > *v)
size_t to_chars(ryml::substr buf, vec2< T > v)
Key< K > key(K &k)
Definition: node.hpp:43
csubstr to_csubstr(substr s) noexcept
neutral version for use in generic code
Definition: substr.hpp:2189
#define RYML_MAX_TAG_DIRECTIVES
the maximum number of tag directives in a Tree
Definition: tag.hpp:19
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:252
size_t to_chars_float(substr buf, T val)
encode a floating point value to a string.
Definition: tree.hpp:42
bool from_chars_float(csubstr buf, T *val)
decode a floating point from string.
Definition: tree.hpp:60
@ NONE
an index to none
Definition: common.hpp:259
Definition: common.cpp:12
a c-style callbacks class.
Definition: common.hpp:375
contains the data for each YAML node.
Definition: tree.hpp:218
NodeType m_type
Definition: tree.hpp:219
id_type m_next_sibling
Definition: tree.hpp:227
id_type m_parent
Definition: tree.hpp:224
NodeScalar m_key
Definition: tree.hpp:221
id_type m_prev_sibling
Definition: tree.hpp:228
id_type m_first_child
Definition: tree.hpp:225
NodeScalar m_val
Definition: tree.hpp:222
id_type m_last_child
Definition: tree.hpp:226
convenience class to initialize nodes
Definition: tree.hpp:152
NodeInit(NodeScalar const &k, NodeScalar const &v)
initialize as a mapping member
Definition: tree.hpp:169
bool _check() const
Definition: tree.hpp:197
NodeInit(NodeType_e t, NodeScalar const &k, NodeScalar const &v)
initialize as a mapping member with explicit type
Definition: tree.hpp:171
NodeType type
Definition: tree.hpp:154
void _add_flags(type_bits more_flags=0)
Definition: tree.hpp:184
NodeScalar key
Definition: tree.hpp:155
NodeInit(NodeScalar const &v, NodeType_e t)
initialize as a sequence member with explicit type
Definition: tree.hpp:167
NodeInit(NodeScalar const &v)
initialize as a sequence member
Definition: tree.hpp:165
NodeScalar val
Definition: tree.hpp:156
void clear()
Definition: tree.hpp:177
NodeInit(NodeType_e t)
initialize as a typed node
Definition: tree.hpp:163
NodeInit(NodeType_e t, NodeScalar const &k)
initialize as a mapping member with explicit type (eg for SEQ or MAP)
Definition: tree.hpp:173
NodeInit()
initialize as an empty node
Definition: tree.hpp:161
a node scalar is a csubstr, which may be tagged and anchored.
Definition: tree.hpp:101
NodeScalar(const char(&t)[N], const char(&s)[N]) noexcept
initialize as a tagged scalar
Definition: tree.hpp:118
csubstr scalar
Definition: tree.hpp:103
bool empty() const noexcept
Definition: tree.hpp:131
NodeScalar() noexcept
initialize as an empty scalar
Definition: tree.hpp:109
NodeScalar(csubstr s) noexcept
Definition: tree.hpp:114
csubstr anchor
Definition: tree.hpp:104
NodeScalar(csubstr t, csubstr s) noexcept
Definition: tree.hpp:119
void clear() noexcept
Definition: tree.hpp:133
NodeScalar(const char(&s)[N]) noexcept
initialize as an untagged scalar
Definition: tree.hpp:113
~NodeScalar() noexcept=default
void set_ref_maybe_replacing_scalar(csubstr ref, bool has_scalar) RYML_NOEXCEPT
Definition: tree.hpp:135
wraps a NodeType_e element with some syntactic sugar and predicates
Definition: node_type.hpp:115
void clear() noexcept
Definition: node_type.hpp:135
Reusable object to resolve references/aliases in the tree.
lookup_result(csubstr path_, id_type start)
Definition: tree.hpp:1055