nodebuilder.cpp
Go to the documentation of this file.
1 #include "nodebuilder.h"
2 #include "yaml-cpp-pm/mark.h"
3 #include "yaml-cpp-pm/node.h"
4 #include <cassert>
5 
6 namespace YAML_PM
7 {
8  NodeBuilder::NodeBuilder(Node& root): m_root(root), m_initializedRoot(false), m_finished(false)
9  {
10  m_root.Clear();
11  m_anchors.push_back(0); // since the anchors start at 1
12  }
13 
15  {
16  }
17 
19  {
20  }
21 
23  {
24  assert(m_finished);
25  }
26 
27  void NodeBuilder::OnNull(const Mark& mark, anchor_t anchor)
28  {
29  Node& node = Push(anchor);
30  node.Init(NodeType::Null, mark, "");
31  Pop();
32  }
33 
34  void NodeBuilder::OnAlias(const Mark& /*mark*/, anchor_t anchor)
35  {
36  Node& node = *m_anchors[anchor];
37  Insert(node);
38  node.MarkAsAliased();
39  }
40 
41  void NodeBuilder::OnScalar(const Mark& mark, const std::string& tag, anchor_t anchor, const std::string& value)
42  {
43  Node& node = Push(anchor);
44  node.Init(NodeType::Scalar, mark, tag);
45  node.SetScalarData(value);
46  Pop();
47  }
48 
49  void NodeBuilder::OnSequenceStart(const Mark& mark, const std::string& tag, anchor_t anchor)
50  {
51  Node& node = Push(anchor);
52  node.Init(NodeType::Sequence, mark, tag);
53  }
54 
56  {
57  Pop();
58  }
59 
60  void NodeBuilder::OnMapStart(const Mark& mark, const std::string& tag, anchor_t anchor)
61  {
62  Node& node = Push(anchor);
63  node.Init(NodeType::Map, mark, tag);
64  m_didPushKey.push(false);
65  }
66 
68  {
69  m_didPushKey.pop();
70  Pop();
71  }
72 
74  {
75  Node& node = Push();
76  RegisterAnchor(anchor, node);
77  return node;
78  }
79 
81  {
82  if(!m_initializedRoot) {
83  m_initializedRoot = true;
84  return m_root;
85  }
86 
87  Node& node = m_root.CreateNode();
88  m_stack.push(&node);
89  return node;
90  }
91 
93  {
94  return m_stack.empty() ? m_root : *m_stack.top();
95  }
96 
98  {
99  assert(!m_finished);
100  if(m_stack.empty()) {
101  m_finished = true;
102  return;
103  }
104 
105  Node& node = *m_stack.top();
106  m_stack.pop();
107  Insert(node);
108  }
109 
111  {
112  Node& curTop = Top();
113  switch(curTop.Type()) {
114  case NodeType::Null:
115  case NodeType::Scalar:
116  assert(false);
117  break;
118  case NodeType::Sequence:
119  curTop.Append(node);
120  break;
121  case NodeType::Map:
122  assert(!m_didPushKey.empty());
123  if(m_didPushKey.top()) {
124  assert(!m_pendingKeys.empty());
125 
126  Node& key = *m_pendingKeys.top();
127  m_pendingKeys.pop();
128  curTop.Insert(key, node);
129  m_didPushKey.top() = false;
130  } else {
131  m_pendingKeys.push(&node);
132  m_didPushKey.top() = true;
133  }
134  break;
135  }
136  }
137 
139  {
140  if(anchor) {
141  assert(anchor == m_anchors.size());
142  m_anchors.push_back(&node);
143  }
144  }
145 }
YAML_PM::NodeBuilder::OnMapEnd
virtual void OnMapEnd()
Definition: nodebuilder.cpp:67
YAML_PM::NodeBuilder::m_pendingKeys
std::stack< Node * > m_pendingKeys
Definition: nodebuilder.h:52
YAML_PM::NodeBuilder::Pop
void Pop()
Definition: nodebuilder.cpp:97
YAML_PM::NodeBuilder::Top
Node & Top()
Definition: nodebuilder.cpp:92
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::Node::SetScalarData
void SetScalarData(const std::string &data)
Definition: node.cpp:121
YAML_PM::NodeBuilder::Push
Node & Push()
Definition: nodebuilder.cpp:80
YAML_PM::NodeBuilder::OnAlias
virtual void OnAlias(const Mark &mark, anchor_t anchor)
Definition: nodebuilder.cpp:34
YAML_PM::Node::MarkAsAliased
void MarkAsAliased()
Definition: node.cpp:116
YAML_PM::NodeBuilder::m_finished
bool m_finished
Definition: nodebuilder.h:49
YAML_PM::Node::Init
void Init(NodeType::value type, const Mark &mark, const std::string &tag)
Definition: node.cpp:108
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::NodeBuilder::m_anchors
Anchors m_anchors
Definition: nodebuilder.h:56
YAML_PM::NodeBuilder::m_root
Node & m_root
Definition: nodebuilder.h:47
node.h
YAML_PM::NodeBuilder::~NodeBuilder
virtual ~NodeBuilder()
Definition: nodebuilder.cpp:14
YAML_PM::NodeBuilder::OnNull
virtual void OnNull(const Mark &mark, anchor_t anchor)
Definition: nodebuilder.cpp:27
YAML_PM::NodeBuilder::OnSequenceStart
virtual void OnSequenceStart(const Mark &mark, const std::string &tag, anchor_t anchor)
Definition: nodebuilder.cpp:49
nodebuilder.h
YAML_PM::NodeBuilder::OnSequenceEnd
virtual void OnSequenceEnd()
Definition: nodebuilder.cpp:55
YAML_PM::NodeBuilder::NodeBuilder
NodeBuilder(Node &root)
Definition: nodebuilder.cpp:8
YAML_PM::NodeBuilder::RegisterAnchor
void RegisterAnchor(anchor_t anchor, Node &node)
Definition: nodebuilder.cpp:138
YAML_PM::Node::Append
void Append(Node &node)
Definition: node.cpp:127
YAML_PM::NodeBuilder::OnDocumentStart
virtual void OnDocumentStart(const Mark &mark)
Definition: nodebuilder.cpp:18
YAML_PM::Node::CreateNode
Node & CreateNode()
Definition: node.cpp:49
YAML_PM::Node
Definition: node.h:33
YAML_PM::NodeBuilder::m_stack
std::stack< Node * > m_stack
Definition: nodebuilder.h:51
YAML_PM::NodeBuilder::OnMapStart
virtual void OnMapStart(const Mark &mark, const std::string &tag, anchor_t anchor)
Definition: nodebuilder.cpp:60
YAML_PM::NodeBuilder::m_initializedRoot
bool m_initializedRoot
Definition: nodebuilder.h:48
YAML_PM::Mark
Definition: mark.h:13
YAML_PM::NodeType::Sequence
@ Sequence
Definition: node.h:31
YAML_PM::NodeBuilder::Insert
void Insert(Node &node)
Definition: nodebuilder.cpp:110
YAML_PM::Node::Clear
void Clear()
Definition: node.cpp:34
YAML_PM::NodeType::Scalar
@ Scalar
Definition: node.h:31
YAML_PM::NodeBuilder::OnScalar
virtual void OnScalar(const Mark &mark, const std::string &tag, anchor_t anchor, const std::string &value)
Definition: nodebuilder.cpp:41
YAML_PM::NodeBuilder::m_didPushKey
std::stack< bool > m_didPushKey
Definition: nodebuilder.h:53
YAML_PM::anchor_t
std::size_t anchor_t
Definition: anchor.h:12
YAML_PM::Node::Insert
void Insert(Node &key, Node &value)
Definition: node.cpp:133
YAML_PM::NodeType::Null
@ Null
Definition: node.h:31
YAML_PM::NodeType::Map
@ Map
Definition: node.h:31
YAML_PM::NodeBuilder::OnDocumentEnd
virtual void OnDocumentEnd()
Definition: nodebuilder.cpp:22
mark.h
YAML_PM::Node::Type
NodeType::value Type() const
Definition: node.h:47


mp2p_icp
Author(s):
autogenerated on Fri Dec 20 2024 03:45:59