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