1 #include "yaml-cpp/node.h"
2 #include "yaml-cpp/aliasmanager.h"
3 #include "yaml-cpp/emitfromevents.h"
4 #include "yaml-cpp/emitter.h"
5 #include "yaml-cpp/eventhandler.h"
7 #include "nodebuilder.h"
8 #include "nodeownership.h"
17 bool ltnode::operator()(const Node
*pNode1
, const Node
*pNode2
) const {
18 return *pNode1
< *pNode2
;
21 Node::Node(): m_pOwnership(new NodeOwnership
), m_type(NodeType::Null
)
25 Node::Node(NodeOwnership
& owner
): m_pOwnership(new NodeOwnership(&owner
)), m_type(NodeType::Null
)
36 m_pOwnership
.reset(new NodeOwnership
);
37 m_type
= NodeType::Null
;
44 bool Node::IsAliased() const
46 return m_pOwnership
->IsAliased(*this);
49 Node
& Node::CreateNode()
51 return m_pOwnership
->Create();
54 std::auto_ptr
<Node
> Node::Clone() const
56 std::auto_ptr
<Node
> pNode(new Node
);
57 NodeBuilder
nodeBuilder(*pNode
);
58 EmitEvents(nodeBuilder
);
62 void Node::EmitEvents(EventHandler
& eventHandler
) const
64 eventHandler
.OnDocumentStart(m_mark
);
66 EmitEvents(am
, eventHandler
);
67 eventHandler
.OnDocumentEnd();
70 void Node::EmitEvents(AliasManager
& am
, EventHandler
& eventHandler
) const
72 anchor_t anchor
= NullAnchor
;
74 anchor
= am
.LookupAnchor(*this);
76 eventHandler
.OnAlias(m_mark
, anchor
);
80 am
.RegisterReference(*this);
81 anchor
= am
.LookupAnchor(*this);
86 eventHandler
.OnNull(m_mark
, anchor
);
88 case NodeType::Scalar
:
89 eventHandler
.OnScalar(m_mark
, m_tag
, anchor
, m_scalarData
);
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();
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
);
103 eventHandler
.OnMapEnd();
108 void Node::Init(NodeType::value type
, const Mark
& mark
, const std::string
& tag
)
116 void Node::MarkAsAliased()
118 m_pOwnership
->MarkAsAliased(*this);
121 void Node::SetScalarData(const std::string
& data
)
123 assert(m_type
== NodeType::Scalar
); // TODO: throw?
127 void Node::Append(Node
& node
)
129 assert(m_type
== NodeType::Sequence
); // TODO: throw?
130 m_seqData
.push_back(&node
);
133 void Node::Insert(Node
& key
, Node
& value
)
135 assert(m_type
== NodeType::Map
); // TODO: throw?
136 m_mapData
[&key
] = &value
;
140 // Returns an iterator to the beginning of this (sequence or map).
141 Iterator
Node::begin() const
145 case NodeType::Scalar
:
147 case NodeType::Sequence
:
148 return Iterator(std::auto_ptr
<IterPriv
>(new IterPriv(m_seqData
.begin())));
150 return Iterator(std::auto_ptr
<IterPriv
>(new IterPriv(m_mapData
.begin())));
158 // . Returns an iterator to the end of this (sequence or map).
159 Iterator
Node::end() const
163 case NodeType::Scalar
:
165 case NodeType::Sequence
:
166 return Iterator(std::auto_ptr
<IterPriv
>(new IterPriv(m_seqData
.end())));
168 return Iterator(std::auto_ptr
<IterPriv
>(new IterPriv(m_mapData
.end())));
176 // . Returns the size of a sequence or map node
177 // . Otherwise, returns zero.
178 std::size_t Node::size() const
182 case NodeType::Scalar
:
184 case NodeType::Sequence
:
185 return m_seqData
.size();
187 return m_mapData
.size();
194 const Node
*Node::FindAtIndex(std::size_t i
) const
196 if(m_type
== NodeType::Sequence
)
201 bool Node::GetScalar(std::string
& s
) const
207 case NodeType::Scalar
:
210 case NodeType::Sequence
:
219 Emitter
& operator << (Emitter
& out
, const Node
& node
)
221 EmitFromEvents
emitFromEvents(out
);
222 node
.EmitEvents(emitFromEvents
);
226 int Node::Compare(const Node
& rhs
) const
228 if(m_type
!= rhs
.m_type
)
229 return rhs
.m_type
- m_type
;
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())
239 else if(m_seqData
.size() > rhs
.m_seqData
.size())
241 for(std::size_t i
=0;i
<m_seqData
.size();i
++)
242 if(int cmp
= m_seqData
[i
]->Compare(*rhs
.m_seqData
[i
]))
246 if(m_mapData
.size() < rhs
.m_mapData
.size())
248 else if(m_mapData
.size() > rhs
.m_mapData
.size())
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
))
255 if(int cmp
= it
->second
->Compare(*jt
->second
))
265 bool operator < (const Node
& n1
, const Node
& n2
)
267 return n1
.Compare(n2
) < 0;