node.cpp
Go to the documentation of this file.
1 #include "yaml-cpp-pm/node.h"
4 #include "yaml-cpp-pm/emitter.h"
6 #include "iterpriv.h"
7 #include "nodebuilder.h"
8 #include "nodeownership.h"
9 #include "scanner.h"
10 #include "tag.h"
11 #include "token.h"
12 #include <cassert>
13 #include <stdexcept>
14 
15 namespace YAML_PM
16 {
17  bool ltnode::operator()(const Node *pNode1, const Node *pNode2) const {
18  return *pNode1 < *pNode2;
19  }
20 
21  Node::Node(): m_pOwnership(new NodeOwnership), m_type(NodeType::Null)
22  {
23  }
24 
26  {
27  }
28 
30  {
31  Clear();
32  }
33 
34  void Node::Clear()
35  {
36  m_pOwnership.reset(new NodeOwnership);
38  m_tag.clear();
39  m_scalarData.clear();
40  m_seqData.clear();
41  m_mapData.clear();
42  }
43 
44  bool Node::IsAliased() const
45  {
46  return m_pOwnership->IsAliased(*this);
47  }
48 
50  {
51  return m_pOwnership->Create();
52  }
53 
54  std::auto_ptr<Node> Node::Clone() const
55  {
56  std::auto_ptr<Node> pNode(new Node);
57  NodeBuilder nodeBuilder(*pNode);
58  EmitEvents(nodeBuilder);
59  return pNode;
60  }
61 
62  void Node::EmitEvents(EventHandler& eventHandler) const
63  {
64  eventHandler.OnDocumentStart(m_mark);
65  AliasManager am;
66  EmitEvents(am, eventHandler);
67  eventHandler.OnDocumentEnd();
68  }
69 
70  void Node::EmitEvents(AliasManager& am, EventHandler& eventHandler) const
71  {
72  anchor_t anchor = NullAnchor;
73  if(IsAliased()) {
74  anchor = am.LookupAnchor(*this);
75  if(anchor) {
76  eventHandler.OnAlias(m_mark, anchor);
77  return;
78  }
79 
80  am.RegisterReference(*this);
81  anchor = am.LookupAnchor(*this);
82  }
83 
84  switch(m_type) {
85  case NodeType::Null:
86  eventHandler.OnNull(m_mark, anchor);
87  break;
88  case NodeType::Scalar:
89  eventHandler.OnScalar(m_mark, m_tag, anchor, m_scalarData);
90  break;
91  case NodeType::Sequence:
92  eventHandler.OnSequenceStart(m_mark, m_tag, anchor);
93  for(std::size_t i=0;i<m_seqData.size();i++)
94  m_seqData[i]->EmitEvents(am, eventHandler);
95  eventHandler.OnSequenceEnd();
96  break;
97  case NodeType::Map:
98  eventHandler.OnMapStart(m_mark, m_tag, anchor);
99  for(node_map::const_iterator it=m_mapData.begin();it!=m_mapData.end();++it) {
100  it->first->EmitEvents(am, eventHandler);
101  it->second->EmitEvents(am, eventHandler);
102  }
103  eventHandler.OnMapEnd();
104  break;
105  }
106  }
107 
108  void Node::Init(NodeType::value type, const Mark& mark, const std::string& tag)
109  {
110  Clear();
111  m_mark = mark;
112  m_type = type;
113  m_tag = tag;
114  }
115 
117  {
118  m_pOwnership->MarkAsAliased(*this);
119  }
120 
122  {
123  assert(m_type == NodeType::Scalar); // TODO: throw?
124  m_scalarData = data;
125  }
126 
127  void Node::Append(Node& node)
128  {
129  assert(m_type == NodeType::Sequence); // TODO: throw?
130  m_seqData.push_back(&node);
131  }
132 
133  void Node::Insert(Node& key, Node& value)
134  {
135  assert(m_type == NodeType::Map); // TODO: throw?
136  m_mapData[&key] = &value;
137  }
138 
139  // begin
140  // Returns an iterator to the beginning of this (sequence or map).
142  {
143  switch(m_type) {
144  case NodeType::Null:
145  case NodeType::Scalar:
146  return Iterator();
147  case NodeType::Sequence:
148  return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_seqData.begin())));
149  case NodeType::Map:
150  return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_mapData.begin())));
151  }
152 
153  assert(false);
154  return Iterator();
155  }
156 
157  // end
158  // . Returns an iterator to the end of this (sequence or map).
160  {
161  switch(m_type) {
162  case NodeType::Null:
163  case NodeType::Scalar:
164  return Iterator();
165  case NodeType::Sequence:
166  return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_seqData.end())));
167  case NodeType::Map:
168  return Iterator(std::auto_ptr<IterPriv>(new IterPriv(m_mapData.end())));
169  }
170 
171  assert(false);
172  return Iterator();
173  }
174 
175  // size
176  // . Returns the size of a sequence or map node
177  // . Otherwise, returns zero.
178  std::size_t Node::size() const
179  {
180  switch(m_type) {
181  case NodeType::Null:
182  case NodeType::Scalar:
183  return 0;
184  case NodeType::Sequence:
185  return m_seqData.size();
186  case NodeType::Map:
187  return m_mapData.size();
188  }
189 
190  assert(false);
191  return 0;
192  }
193 
194  const Node *Node::FindAtIndex(std::size_t i) const
195  {
197  return m_seqData[i];
198  return 0;
199  }
200 
202  {
203  switch(m_type) {
204  case NodeType::Null:
205  s = "~";
206  return true;
207  case NodeType::Scalar:
208  s = m_scalarData;
209  return true;
210  case NodeType::Sequence:
211  case NodeType::Map:
212  return false;
213  }
214 
215  assert(false);
216  return false;
217  }
218 
219  Emitter& operator << (Emitter& out, const Node& node)
220  {
221  EmitFromEvents emitFromEvents(out);
222  node.EmitEvents(emitFromEvents);
223  return out;
224  }
225 
226  int Node::Compare(const Node& rhs) const
227  {
228  if(m_type != rhs.m_type)
229  return rhs.m_type - m_type;
230 
231  switch(m_type) {
232  case NodeType::Null:
233  return 0;
234  case NodeType::Scalar:
235  return m_scalarData.compare(rhs.m_scalarData);
236  case NodeType::Sequence:
237  if(m_seqData.size() < rhs.m_seqData.size())
238  return 1;
239  else if(m_seqData.size() > rhs.m_seqData.size())
240  return -1;
241  for(std::size_t i=0;i<m_seqData.size();i++)
242  if(int cmp = m_seqData[i]->Compare(*rhs.m_seqData[i]))
243  return cmp;
244  return 0;
245  case NodeType::Map:
246  if(m_mapData.size() < rhs.m_mapData.size())
247  return 1;
248  else if(m_mapData.size() > rhs.m_mapData.size())
249  return -1;
250  node_map::const_iterator it = m_mapData.begin();
251  node_map::const_iterator jt = rhs.m_mapData.begin();
252  for(;it!=m_mapData.end() && jt!=rhs.m_mapData.end();it++, jt++) {
253  if(int cmp = it->first->Compare(*jt->first))
254  return cmp;
255  if(int cmp = it->second->Compare(*jt->second))
256  return cmp;
257  }
258  return 0;
259  }
260 
261  assert(false);
262  return 0;
263  }
264 
265  bool operator < (const Node& n1, const Node& n2)
266  {
267  return n1.Compare(n2) < 0;
268  }
269 }
node_seq m_seqData
Definition: node.h:127
node_map m_mapData
Definition: node.h:128
void RegisterReference(const Node &node)
void Clear()
Definition: node.cpp:34
std::size_t anchor_t
Definition: anchor.h:12
friend YAML_CPP_API Emitter & operator<<(Emitter &out, const Node &node)
Definition: node.cpp:219
::std::string string
Definition: gtest.h:1979
data
Definition: icp.py:50
virtual void OnSequenceEnd()=0
Mark m_mark
Definition: node.h:119
std::size_t size() const
Definition: node.cpp:178
anchor_t LookupAnchor(const Node &node) const
virtual void OnSequenceStart(const Mark &mark, const std::string &tag, anchor_t anchor)=0
Node & CreateNode()
Definition: node.cpp:49
Iterator begin() const
Definition: node.cpp:141
virtual void OnDocumentEnd()=0
const Node * FindAtIndex(std::size_t i) const
Definition: node.cpp:194
void SetScalarData(const std::string &data)
Definition: node.cpp:121
YAML_CPP_API _Null Null
Definition: null.cpp:6
virtual void OnNull(const Mark &mark, anchor_t anchor)=0
bool GetScalar(std::string &s) const
Definition: node.cpp:201
Iterator end() const
Definition: node.cpp:159
virtual void OnMapEnd()=0
std::auto_ptr< Node > Clone() const
Definition: node.cpp:54
std::string m_tag
Definition: node.h:120
NodeType::value m_type
Definition: node.h:125
void Insert(Node &key, Node &value)
Definition: node.cpp:133
virtual void OnMapStart(const Mark &mark, const std::string &tag, anchor_t anchor)=0
bool operator()(const Node *pNode1, const Node *pNode2) const
Definition: node.cpp:17
friend bool operator<(const Node &n1, const Node &n2)
Definition: node.cpp:265
virtual void OnDocumentStart(const Mark &mark)=0
virtual void OnAlias(const Mark &mark, anchor_t anchor)=0
bool IsAliased() const
Definition: node.cpp:44
int Compare(const Node &rhs) const
Definition: node.cpp:226
std::string m_scalarData
Definition: node.h:126
const anchor_t NullAnchor
Definition: anchor.h:13
void MarkAsAliased()
Definition: node.cpp:116
void Append(Node &node)
Definition: node.cpp:127
void Init(NodeType::value type, const Mark &mark, const std::string &tag)
Definition: node.cpp:108
void EmitEvents(EventHandler &eventHandler) const
Definition: node.cpp:62
std::auto_ptr< NodeOwnership > m_pOwnership
Definition: node.h:117
virtual void OnScalar(const Mark &mark, const std::string &tag, anchor_t anchor, const std::string &value)=0


libpointmatcher
Author(s):
autogenerated on Sat May 27 2023 02:38:02