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 
25  Node::Node(NodeOwnership& owner): m_pOwnership(new NodeOwnership(&owner)), m_type(NodeType::Null)
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 
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 }
YAML_PM::Node::begin
Iterator begin() const
Definition: node.cpp:141
YAML_PM::NodeOwnership
Definition: nodeownership.h:17
YAML_PM::NodeBuilder
Definition: nodebuilder.h:18
aliasmanager.h
YAML_PM
Definition: aliasmanager.h:11
YAML_PM::Node::Compare
int Compare(const Node &rhs) const
Definition: node.cpp:226
YAML_PM::Null
YAML_CPP_API _Null Null
Definition: null.cpp:6
YAML_PM::Node::SetScalarData
void SetScalarData(const std::string &data)
Definition: node.cpp:121
s
XmlRpcServer s
YAML_PM::Node::m_type
NodeType::value m_type
Definition: node.h:125
YAML_PM::EventHandler
Definition: eventhandler.h:15
YAML_PM::Node::MarkAsAliased
void MarkAsAliased()
Definition: node.cpp:116
YAML_PM::Node::GetScalar
bool GetScalar(std::string &s) const
Definition: node.cpp:201
YAML_PM::Node::end
Iterator end() const
Definition: node.cpp:159
YAML_PM::EventHandler::OnSequenceStart
virtual void OnSequenceStart(const Mark &mark, const std::string &tag, anchor_t anchor)=0
YAML_PM::Node::Clone
std::auto_ptr< Node > Clone() const
Definition: node.cpp:54
YAML_PM::Node::Init
void Init(NodeType::value type, const Mark &mark, const std::string &tag)
Definition: node.cpp:108
eventhandler.h
YAML_PM::Node::~Node
~Node()
Definition: node.cpp:29
testing::internal::string
::std::string string
Definition: gtest.h:1979
YAML_PM::EventHandler::OnDocumentStart
virtual void OnDocumentStart(const Mark &mark)=0
YAML_PM::NodeType
Definition: node.h:31
YAML_PM::AliasManager::LookupAnchor
anchor_t LookupAnchor(const Node &node) const
Definition: aliasmanager.cpp:17
YAML_PM::ltnode::operator()
bool operator()(const Node *pNode1, const Node *pNode2) const
Definition: node.cpp:17
node.h
YAML_PM::NullAnchor
const anchor_t NullAnchor
Definition: anchor.h:13
kitti-batch-convert.out
string out
Definition: kitti-batch-convert.py:7
YAML_PM::Node::Node
Node()
Definition: node.cpp:21
YAML_PM::EmitFromEvents
Definition: emitfromevents.h:15
YAML_PM::IterPriv
Definition: iterpriv.h:19
nodebuilder.h
YAML_PM::Node::IsAliased
bool IsAliased() const
Definition: node.cpp:44
YAML_PM::Node::m_scalarData
std::string m_scalarData
Definition: node.h:126
YAML_PM::EventHandler::OnSequenceEnd
virtual void OnSequenceEnd()=0
YAML_PM::EventHandler::OnScalar
virtual void OnScalar(const Mark &mark, const std::string &tag, anchor_t anchor, const std::string &value)=0
YAML_PM::Node::EmitEvents
void EmitEvents(EventHandler &eventHandler) const
Definition: node.cpp:62
YAML_PM::Node::Append
void Append(Node &node)
Definition: node.cpp:127
YAML_PM::Node::m_pOwnership
std::auto_ptr< NodeOwnership > m_pOwnership
Definition: node.h:117
iterpriv.h
YAML_PM::Node::size
std::size_t size() const
Definition: node.cpp:178
YAML_PM::Node::CreateNode
Node & CreateNode()
Definition: node.cpp:49
YAML_PM::Node
Definition: node.h:33
YAML_PM::AliasManager
Definition: aliasmanager.h:15
YAML_PM::EventHandler::OnAlias
virtual void OnAlias(const Mark &mark, anchor_t anchor)=0
YAML_PM::Node::m_seqData
node_seq m_seqData
Definition: node.h:127
YAML_PM::Node::FindAtIndex
const Node * FindAtIndex(std::size_t i) const
Definition: node.cpp:194
YAML_PM::operator<<
Emitter & operator<<(Emitter &emitter, const std::string &v)
Definition: emitter.h:148
emitfromevents.h
YAML_PM::Emitter
Definition: emitter.h:23
YAML_PM::Mark
Definition: mark.h:13
YAML_PM::EventHandler::OnNull
virtual void OnNull(const Mark &mark, anchor_t anchor)=0
YAML_PM::Node::m_mapData
node_map m_mapData
Definition: node.h:128
nodeownership.h
YAML_PM::NodeType::Sequence
@ Sequence
Definition: node.h:31
YAML_PM::Node::m_tag
std::string m_tag
Definition: node.h:120
YAML_PM::Iterator
Definition: iterator.h:16
tag.h
YAML_PM::Node::Clear
void Clear()
Definition: node.cpp:34
YAML_PM::NodeType::Scalar
@ Scalar
Definition: node.h:31
scanner.h
YAML_PM::AliasManager::RegisterReference
void RegisterReference(const Node &node)
Definition: aliasmanager.cpp:12
YAML_PM::anchor_t
std::size_t anchor_t
Definition: anchor.h:12
YAML_PM::Node::m_mark
Mark m_mark
Definition: node.h:119
token.h
YAML_PM::operator<
bool operator<(const Node &n1, const Node &n2)
Definition: node.cpp:265
emitter.h
YAML_PM::Node::Insert
void Insert(Node &key, Node &value)
Definition: node.cpp:133
YAML_PM::NodeType::value
value
Definition: node.h:31
YAML_PM::NodeType::Null
@ Null
Definition: node.h:31
YAML_PM::NodeType::Map
@ Map
Definition: node.h:31
YAML_PM::EventHandler::OnDocumentEnd
virtual void OnDocumentEnd()=0
YAML_PM::EventHandler::OnMapEnd
virtual void OnMapEnd()=0
YAML_PM::EventHandler::OnMapStart
virtual void OnMapStart(const Mark &mark, const std::string &tag, anchor_t anchor)=0


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