rapidyaml  0.7.0
parse and emit YAML, and do it fast
reference_resolver.hpp
Go to the documentation of this file.
1 #ifndef _C4_YML_REFERENCE_RESOLVER_HPP_
2 #define _C4_YML_REFERENCE_RESOLVER_HPP_
3 
4 #include "c4/yml/tree.hpp"
5 #include "c4/yml/detail/stack.hpp"
6 
7 
8 namespace c4 {
9 namespace yml {
10 
11 /** @addtogroup doc_ref_utils
12  * @{
13  */
14 
15 /** Reusable object to resolve references/aliases in the tree. */
17 {
18  ReferenceResolver() = default;
19 
20  /** Resolve references: for each reference, look for a matching
21  * anchor, and copy its contents to the ref node.
22  *
23  * This method first does a full traversal of the tree to gather
24  * all anchors and references in a separate collection, then it
25  * goes through that collection to locate the names, which it does
26  * by obeying the YAML standard diktat that "an alias node refers
27  * to the most recent node in the serialization having the
28  * specified anchor"
29  *
30  * So, depending on the number of anchor/alias nodes, this is a
31  * potentially expensive operation, with a best-case linear
32  * complexity (from the initial traversal).
33  *
34  * @todo verify sanity against anchor-ref attacks (https://en.wikipedia.org/wiki/Billion_laughs_attack )
35  */
36  void resolve(Tree *t_);
37 
38 public:
39 
40  /** @cond dev */
41 
42  struct RefData
43  {
44  NodeType type;
45  id_type node;
46  id_type prev_anchor;
47  id_type target;
48  id_type parent_ref;
49  id_type parent_ref_sibling;
50  };
51 
52  void reset_(Tree *t_);
53  void gather_anchors_and_refs_();
54  void gather_anchors_and_refs__(id_type n);
55  id_type count_anchors_and_refs_(id_type n);
56 
57  id_type lookup_(RefData *C4_RESTRICT ra);
58 
59 public:
60 
61  Tree *C4_RESTRICT m_tree;
62  /** We're using this stack purely as an array. */
63  detail::stack<RefData> m_refs;
64 
65  /** @endcond */
66 };
67 
68 /** @} */
69 
70 } // namespace ryml
71 } // namespace c4
72 
73 
74 #endif // _C4_YML_REFERENCE_RESOLVER_HPP_
#define RYML_EXPORT
Definition: export.hpp:15
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
Definition: common.cpp:12
wraps a NodeType_e element with some syntactic sugar and predicates
Definition: node_type.hpp:115
Reusable object to resolve references/aliases in the tree.