scide: implement selectionLength for openDocument
[supercollider.git] / external_libraries / yaml-cpp-0.3.0 / src / node.cpp
blob360b4ad92a74b1d03df5ee365661b484ba8e0acb
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"
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>
15 namespace YAML
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)
29 Node::~Node()
31 Clear();
34 void Node::Clear()
36 m_pOwnership.reset(new NodeOwnership);
37 m_type = NodeType::Null;
38 m_tag.clear();
39 m_scalarData.clear();
40 m_seqData.clear();
41 m_mapData.clear();
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);
59 return pNode;
62 void Node::EmitEvents(EventHandler& eventHandler) const
64 eventHandler.OnDocumentStart(m_mark);
65 AliasManager am;
66 EmitEvents(am, eventHandler);
67 eventHandler.OnDocumentEnd();
70 void Node::EmitEvents(AliasManager& am, EventHandler& eventHandler) const
72 anchor_t anchor = NullAnchor;
73 if(IsAliased()) {
74 anchor = am.LookupAnchor(*this);
75 if(anchor) {
76 eventHandler.OnAlias(m_mark, anchor);
77 return;
80 am.RegisterReference(*this);
81 anchor = am.LookupAnchor(*this);
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);
103 eventHandler.OnMapEnd();
104 break;
108 void Node::Init(NodeType::value type, const Mark& mark, const std::string& tag)
110 Clear();
111 m_mark = mark;
112 m_type = type;
113 m_tag = 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?
124 m_scalarData = data;
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;
139 // begin
140 // Returns an iterator to the beginning of this (sequence or map).
141 Iterator Node::begin() const
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())));
153 assert(false);
154 return Iterator();
157 // end
158 // . Returns an iterator to the end of this (sequence or map).
159 Iterator Node::end() const
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())));
171 assert(false);
172 return Iterator();
175 // size
176 // . Returns the size of a sequence or map node
177 // . Otherwise, returns zero.
178 std::size_t Node::size() const
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();
190 assert(false);
191 return 0;
194 const Node *Node::FindAtIndex(std::size_t i) const
196 if(m_type == NodeType::Sequence)
197 return m_seqData[i];
198 return 0;
201 bool Node::GetScalar(std::string& s) const
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;
215 assert(false);
216 return false;
219 Emitter& operator << (Emitter& out, const Node& node)
221 EmitFromEvents emitFromEvents(out);
222 node.EmitEvents(emitFromEvents);
223 return out;
226 int Node::Compare(const Node& rhs) const
228 if(m_type != rhs.m_type)
229 return rhs.m_type - m_type;
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;
258 return 0;
261 assert(false);
262 return 0;
265 bool operator < (const Node& n1, const Node& n2)
267 return n1.Compare(n2) < 0;