aGrUM  0.16.0
ticpp.cpp
Go to the documentation of this file.
1 /*
2 http://code.google.com/p/ticpp/
3 Copyright (c) 2006 Ryan Pusztai, Ryan Mulder
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of
6 this software and associated documentation files (the "Software"), to deal in
7 the Software without restriction, including without limitation the rights to
8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 the Software, and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17 FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18 COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19 IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22 #define TIXML_USE_TICPP
23 #ifdef TIXML_USE_TICPP
24 
25 #include "ticpp.h"
26 #include "ticpprc.h"
27 #include "tinyxml.h"
28 #include <sstream>
29 
30 using namespace ticpp;
31 
32 // In the following Visitor functions, casting away const should be safe, as the
33 // object can only be referred to by a const &
35  return VisitEnter(Document(const_cast< TiXmlDocument* >(&doc)));
36 }
37 
39  return VisitEnter(Document(const_cast< TiXmlDocument* >(&doc)));
40 }
41 
42 bool Visitor::VisitEnter(const TiXmlElement& element,
43  const TiXmlAttribute* firstAttribute) {
44  if (0 != firstAttribute) {
45  Attribute attribute(const_cast< TiXmlAttribute* >(firstAttribute));
46  return VisitEnter(Element(const_cast< TiXmlElement* >(&element)), &attribute);
47  } else {
48  return VisitEnter(Element(const_cast< TiXmlElement* >(&element)), 0);
49  }
50 }
51 
52 bool Visitor::VisitExit(const TiXmlElement& element) {
53  return VisitExit(Element(const_cast< TiXmlElement* >(&element)));
54 }
55 
56 bool Visitor::Visit(const TiXmlDeclaration& declaration) {
57  return Visit(Declaration(const_cast< TiXmlDeclaration* >(&declaration)));
58 }
59 
60 bool Visitor::Visit(const TiXmlStylesheetReference& stylesheet) {
61  return Visit(
62  StylesheetReference(const_cast< TiXmlStylesheetReference* >(&stylesheet)));
63 }
64 
65 bool Visitor::Visit(const TiXmlText& text) {
66  return Visit(Text(const_cast< TiXmlText* >(&text)));
67 }
68 
69 bool Visitor::Visit(const TiXmlComment& comment) {
70  return Visit(Comment(const_cast< TiXmlComment* >(&comment)));
71 }
72 
73 bool Visitor::Visit(const TiXmlUnknown& x) { return TiXmlVisitor::Visit(x); }
74 
76  SetTiXmlPointer(new TiXmlAttribute());
77  m_impRC->InitRef();
78 }
79 
81  SetTiXmlPointer(attribute);
82  m_impRC->IncRef();
83 }
84 
85 Attribute::Attribute(const std::string& name, const std::string& value) {
86  SetTiXmlPointer(new TiXmlAttribute(name, value));
87  m_impRC->InitRef();
88 }
89 
90 void Attribute::operator=(const Attribute& copy) {
91  // Dropping the reference to the old object
92  this->m_impRC->DecRef();
93 
94  // Pointing to the new Object
95  SetTiXmlPointer(copy.m_tiXmlPointer);
96 
97  // The internal tixml pointer changed in the above line
98  this->m_impRC->IncRef();
99 }
100 
102  : Base() {
103  // Dropping the reference to the old object
104  this->m_impRC->DecRef();
105 
106  // Pointing to the new Object
108 
109  // The internal tixml pointer changed in the above line
110  this->m_impRC->IncRef();
111 }
112 
114 
115 std::string Attribute::Value() const {
116  ValidatePointer();
117  return m_tiXmlPointer->ValueStr();
118 }
119 
120 std::string Attribute::Name() const {
121  ValidatePointer();
122  return m_tiXmlPointer->Name();
123 }
124 
125 Attribute* Attribute::Next(bool throwIfNoAttribute) const {
126  ValidatePointer();
127  TiXmlAttribute* attribute = m_tiXmlPointer->Next();
128 
129  if (0 == attribute) {
130  if (throwIfNoAttribute) {
131  TICPPTHROW("No more attributes found")
132  } else {
133  return 0;
134  }
135  }
136 
137  Attribute* temp = new Attribute(attribute);
138  attribute->m_spawnedWrappers.push_back(temp);
139 
140  return temp;
141 }
142 
143 Attribute* Attribute::Previous(bool throwIfNoAttribute) const {
144  ValidatePointer();
145  TiXmlAttribute* attribute = m_tiXmlPointer->Previous();
146 
147  if (0 == attribute) {
148  if (throwIfNoAttribute) {
149  TICPPTHROW("No more attributes found")
150  } else {
151  return 0;
152  }
153  }
154 
155  Attribute* temp = new Attribute(attribute);
156  attribute->m_spawnedWrappers.push_back(temp);
157 
158  return temp;
159 }
160 
161 void Attribute::IterateNext(const std::string&, Attribute** next) const {
162  *next = Next(false);
163 }
164 
165 void Attribute::IteratePrevious(const std::string&, Attribute** previous) const {
166  *previous = Previous(false);
167 }
168 
169 void Attribute::Print(FILE* file, int depth) const {
170  ValidatePointer();
171  m_tiXmlPointer->Print(file, depth);
172 }
173 
175  m_tiXmlPointer = newPointer;
176  SetImpRC(newPointer);
177 }
178 
179 //*****************************************************************************
180 
182  bool throwIfNull,
183  bool rememberSpawnedWrapper) const {
184  if (0 == tiXmlNode) {
185  if (throwIfNull) {
186  TICPPTHROW("tiXmlNode is nullptr")
187  } else {
188  return 0;
189  }
190  }
191 
192  Node* temp;
193 
194  switch (tiXmlNode->Type()) {
195  case TiXmlNode::DOCUMENT:
196  temp = new Document(tiXmlNode->ToDocument());
197  break;
198 
199  case TiXmlNode::ELEMENT:
200  temp = new Element(tiXmlNode->ToElement());
201  break;
202 
203  case TiXmlNode::COMMENT:
204  temp = new Comment(tiXmlNode->ToComment());
205  break;
206 
207  case TiXmlNode::TEXT:
208  temp = new Text(tiXmlNode->ToText());
209  break;
210 
212  temp = new Declaration(tiXmlNode->ToDeclaration());
213  break;
214 
216  temp = new StylesheetReference(tiXmlNode->ToStylesheetReference());
217  break;
218 
219  default:
220  TICPPTHROW("Type is unsupported")
221  }
222 
223  if (rememberSpawnedWrapper) {
224  tiXmlNode->m_spawnedWrappers.push_back(temp);
225  }
226 
227  return temp;
228 }
229 
230 std::string Node::Value() const { return GetTiXmlPointer()->ValueStr(); }
231 
232 void Node::Clear() { GetTiXmlPointer()->Clear(); }
233 
234 Node* Node::Parent(bool throwIfNoParent) const {
235  TiXmlNode* parent = GetTiXmlPointer()->Parent();
236 
237  if ((0 == parent) && throwIfNoParent) {
238  TICPPTHROW("No parent exists");
239  }
240 
241  return NodeFactory(parent, false);
242 }
243 
244 Node* Node::FirstChild(bool throwIfNoChildren) const {
245  return FirstChild("", throwIfNoChildren);
246 }
247 
248 Node* Node::FirstChild(const std::string& value, bool throwIfNoChildren) const {
249  return FirstChild(value.c_str(), throwIfNoChildren);
250 }
251 
252 Node* Node::FirstChild(const char* value, bool throwIfNoChildren) const {
253  TiXmlNode* childNode;
254 
255  if (0 == strlen(value)) {
256  childNode = GetTiXmlPointer()->FirstChild();
257  } else {
258  childNode = GetTiXmlPointer()->FirstChild(value);
259  }
260 
261  if ((0 == childNode) && throwIfNoChildren) {
262  TICPPTHROW("Child with the value of \"" << value << "\" not found");
263  }
264 
265  return NodeFactory(childNode, false);
266 }
267 
268 Node* Node::LastChild(bool throwIfNoChildren) const {
269  return LastChild("", throwIfNoChildren);
270 }
271 
272 Node* Node::LastChild(const std::string& value, bool throwIfNoChildren) const {
273  return LastChild(value.c_str(), throwIfNoChildren);
274 }
275 
276 Node* Node::LastChild(const char* value, bool throwIfNoChildren) const {
277  TiXmlNode* childNode;
278 
279  if (0 == strlen(value)) {
280  childNode = GetTiXmlPointer()->LastChild();
281  } else {
282  childNode = GetTiXmlPointer()->LastChild(value);
283  }
284 
285  if ((0 == childNode) && throwIfNoChildren) {
286  TICPPTHROW("Child with the value of \"" << value << "\" not found");
287  }
288 
289  return NodeFactory(childNode, false);
290 }
291 
292 Node* Node::IterateChildren(Node* previous) const {
293  TiXmlNode* pointer;
294 
295  if (0 == previous) {
296  pointer = GetTiXmlPointer()->IterateChildren(0);
297  } else {
298  pointer = GetTiXmlPointer()->IterateChildren(previous->GetTiXmlPointer());
299  }
300 
301  return NodeFactory(pointer, false);
302 }
303 
304 Node* Node::IterateChildren(const std::string& value, Node* previous) const {
305  TiXmlNode* pointer;
306 
307  if (0 == previous) {
308  pointer = GetTiXmlPointer()->IterateChildren(value, 0);
309  } else {
310  pointer =
311  GetTiXmlPointer()->IterateChildren(value, previous->GetTiXmlPointer());
312  }
313 
314  return NodeFactory(pointer, false);
315 }
316 
318  if (addThis.Type() == TiXmlNode::DOCUMENT) {
319  TICPPTHROW("Node is a Document and can't be inserted");
320  }
321 
322  TiXmlNode* pointer =
323  GetTiXmlPointer()->InsertEndChild(*addThis.GetTiXmlPointer());
324 
325  if (0 == pointer) {
326  TICPPTHROW("Node can't be inserted");
327  }
328 
329  return NodeFactory(pointer);
330 }
331 
333  if (childNode->Type() == TiXmlNode::DOCUMENT) {
334  TICPPTHROW("Node is a Document and can't be linked");
335  }
336 
337  // Increment reference count when adding to the tree
338  childNode->m_impRC->IncRef();
339 
340  if (0 == GetTiXmlPointer()->LinkEndChild(childNode->GetTiXmlPointer())) {
341  TICPPTHROW("Node can't be linked");
342  }
343 
344  return childNode;
345 }
346 
347 Node* Node::InsertBeforeChild(Node* beforeThis, Node& addThis) {
348  if (addThis.Type() == TiXmlNode::DOCUMENT) {
349  TICPPTHROW("Node is a Document and can't be inserted");
350  }
351 
352  // Increment reference count when adding to the tree
353  addThis.m_impRC->IncRef();
354 
355  TiXmlNode* pointer = GetTiXmlPointer()->InsertBeforeChild(
356  beforeThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer());
357 
358  if (0 == pointer) {
359  TICPPTHROW("Node can't be inserted");
360  }
361 
362  return NodeFactory(pointer);
363 }
364 
365 Node* Node::InsertAfterChild(Node* afterThis, Node& addThis) {
366  if (addThis.Type() == TiXmlNode::DOCUMENT) {
367  TICPPTHROW("Node is a Document and can't be inserted");
368  }
369 
370  // Increment reference count when adding to the tree
371  addThis.m_impRC->IncRef();
372 
373  TiXmlNode* pointer = GetTiXmlPointer()->InsertAfterChild(
374  afterThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer());
375 
376  if (0 == pointer) {
377  TICPPTHROW("Node can't be inserted");
378  }
379 
380  return NodeFactory(pointer);
381 }
382 
383 Node* Node::ReplaceChild(Node* replaceThis, Node& withThis) {
384  if (withThis.Type() == TiXmlNode::DOCUMENT) {
385  TICPPTHROW("Node is a Document and can't be inserted");
386  }
387 
388  // Increment reference count when adding to the tree
389  withThis.m_impRC->IncRef();
390 
391  TiXmlNode* pointer = GetTiXmlPointer()->ReplaceChild(
392  replaceThis->GetTiXmlPointer(), *withThis.GetTiXmlPointer());
393 
394  if (0 == pointer) {
395  TICPPTHROW("Node can't be inserted");
396  }
397 
398  return NodeFactory(pointer);
399 }
400 
401 void Node::RemoveChild(Node* removeThis) {
402  if (!GetTiXmlPointer()->RemoveChild(removeThis->GetTiXmlPointer())) {
403  TICPPTHROW("Node to remove (" << removeThis->Value()
404  << ") is not a child of this Node ("
405  << Value()
406  << ")")
407  }
408 }
409 
410 Node* Node::PreviousSibling(bool throwIfNoSiblings) const {
411  return PreviousSibling("", throwIfNoSiblings);
412 }
413 
414 Node* Node::PreviousSibling(const std::string& value,
415  bool throwIfNoSiblings) const {
416  return PreviousSibling(value.c_str(), throwIfNoSiblings);
417 }
418 
419 Node* Node::PreviousSibling(const char* value, bool throwIfNoSiblings) const {
420  TiXmlNode* sibling;
421 
422  if (0 == strlen(value)) {
423  sibling = GetTiXmlPointer()->PreviousSibling();
424  } else {
425  sibling = GetTiXmlPointer()->PreviousSibling(value);
426  }
427 
428  if ((0 == sibling) && throwIfNoSiblings) {
429  TICPPTHROW("No Siblings found with value, '" << value
430  << "', Prior to this Node ("
431  << Value()
432  << ")")
433  }
434 
435  return NodeFactory(sibling, false);
436 }
437 
438 Node* Node::NextSibling(bool throwIfNoSiblings) const {
439  return NextSibling("", throwIfNoSiblings);
440 }
441 
442 Node* Node::NextSibling(const std::string& value, bool throwIfNoSiblings) const {
443  return NextSibling(value.c_str(), throwIfNoSiblings);
444 }
445 
446 Node* Node::NextSibling(const char* value, bool throwIfNoSiblings) const {
447  TiXmlNode* sibling;
448 
449  if (0 == strlen(value)) {
450  sibling = GetTiXmlPointer()->NextSibling();
451  } else {
452  sibling = GetTiXmlPointer()->NextSibling(value);
453  }
454 
455  if ((0 == sibling) && throwIfNoSiblings) {
456  TICPPTHROW("No Siblings found with value, '" << value << "', After this Node ("
457  << Value()
458  << ")")
459  }
460 
461  return NodeFactory(sibling, false);
462 }
463 
464 Element* Node::NextSiblingElement(bool throwIfNoSiblings) const {
465  return NextSiblingElement("", throwIfNoSiblings);
466 }
467 
468 Element* Node::NextSiblingElement(const std::string& value,
469  bool throwIfNoSiblings) const {
470  return NextSiblingElement(value.c_str(), throwIfNoSiblings);
471 }
472 
474  bool throwIfNoSiblings) const {
475  TiXmlElement* sibling;
476 
477  if (0 == strlen(value)) {
478  sibling = GetTiXmlPointer()->NextSiblingElement();
479  } else {
480  sibling = GetTiXmlPointer()->NextSiblingElement(value);
481  }
482 
483  if (0 == sibling) {
484  if (throwIfNoSiblings) {
485  TICPPTHROW("No Element Siblings found with value, '"
486  << value
487  << "', After this Node ("
488  << Value()
489  << ")")
490  } else {
491  return 0;
492  }
493  }
494 
495  Element* temp = new Element(sibling);
496  sibling->m_spawnedWrappers.push_back(temp);
497 
498  return temp;
499 }
500 
501 Element* Node::FirstChildElement(bool throwIfNoChildren) const {
502  return FirstChildElement("", throwIfNoChildren);
503 }
504 
505 Element* Node::FirstChildElement(const std::string& value,
506  bool throwIfNoChildren) const {
507  return FirstChildElement(value.c_str(), throwIfNoChildren);
508 }
509 
510 Element* Node::FirstChildElement(const char* value, bool throwIfNoChildren) const {
511  TiXmlElement* element;
512 
513  if (0 == strlen(value)) {
514  element = GetTiXmlPointer()->FirstChildElement();
515  } else {
516  element = GetTiXmlPointer()->FirstChildElement(value);
517  }
518 
519  if (0 == element) {
520  if (throwIfNoChildren) {
521  TICPPTHROW("Element (" << Value()
522  << ") does NOT contain a child with the value of '"
523  << value
524  << "'")
525  } else {
526  return 0;
527  }
528  }
529 
530  Element* temp = new Element(element);
531  element->m_spawnedWrappers.push_back(temp);
532 
533  return temp;
534 }
535 
536 int Node::Type() const { return GetTiXmlPointer()->Type(); }
537 
538 Document* Node::GetDocument(bool throwIfNoDocument) const {
539  TiXmlDocument* doc = GetTiXmlPointer()->GetDocument();
540 
541  if (0 == doc) {
542  if (throwIfNoDocument) {
543  TICPPTHROW("This node (" << Value() << ") is not linked under a document")
544  } else {
545  return 0;
546  }
547  }
548 
549  Document* temp = new Document(doc);
550  doc->m_spawnedWrappers.push_back(temp);
551 
552  return temp;
553 }
554 
555 bool Node::NoChildren() const { return GetTiXmlPointer()->NoChildren(); }
556 
558  TiXmlDocument* doc = GetTiXmlPointer()->ToDocument();
559 
560  if (0 == doc) {
561  TICPPTHROW("This node (" << Value() << ") is not a Document")
562  }
563 
564  Document* temp = new Document(doc);
565  doc->m_spawnedWrappers.push_back(temp);
566 
567  return temp;
568 }
569 
571  TiXmlElement* doc = GetTiXmlPointer()->ToElement();
572 
573  if (0 == doc) {
574  TICPPTHROW("This node (" << Value() << ") is not a Element")
575  }
576 
577  Element* temp = new Element(doc);
578  doc->m_spawnedWrappers.push_back(temp);
579 
580  return temp;
581 }
582 
584  TiXmlComment* doc = GetTiXmlPointer()->ToComment();
585 
586  if (0 == doc) {
587  TICPPTHROW("This node (" << Value() << ") is not a Comment")
588  }
589 
590  Comment* temp = new Comment(doc);
591  doc->m_spawnedWrappers.push_back(temp);
592 
593  return temp;
594 }
595 
596 Text* Node::ToText() const {
597  TiXmlText* doc = GetTiXmlPointer()->ToText();
598 
599  if (0 == doc) {
600  TICPPTHROW("This node (" << Value() << ") is not a Text")
601  }
602 
603  Text* temp = new Text(doc);
604  doc->m_spawnedWrappers.push_back(temp);
605 
606  return temp;
607 }
608 
610  TiXmlDeclaration* doc = GetTiXmlPointer()->ToDeclaration();
611 
612  if (0 == doc) {
613  TICPPTHROW("This node (" << Value() << ") is not a Declaration")
614  }
615 
616  Declaration* temp = new Declaration(doc);
617  doc->m_spawnedWrappers.push_back(temp);
618 
619  return temp;
620 }
621 
623  TiXmlStylesheetReference* doc = GetTiXmlPointer()->ToStylesheetReference();
624 
625  if (0 == doc) {
626  TICPPTHROW("This node (" << Value() << ") is not a StylesheetReference")
627  }
628 
629  StylesheetReference* temp = new StylesheetReference(doc);
630  doc->m_spawnedWrappers.push_back(temp);
631 
632  return temp;
633 }
634 
635 std::unique_ptr< Node > Node::Clone() const {
636  TiXmlNode* node = GetTiXmlPointer()->Clone();
637 
638  if (0 == node) {
639  TICPPTHROW("Node could not be cloned");
640  }
641 
642  std::unique_ptr< Node > temp(NodeFactory(node, false, false));
643 
644  // Take ownership of the memory from TiXml
645  temp->m_impRC->InitRef();
646 
647  return temp;
648 }
649 
650 bool Node::Accept(TiXmlVisitor* visitor) const {
651  return GetTiXmlPointer()->Accept(visitor);
652 }
653 
654 //*****************************************************************************
655 
657  : NodeImp< TiXmlComment >(new TiXmlComment()) {
658  m_impRC->InitRef();
659 }
660 
662  : NodeImp< TiXmlComment >(comment) {}
663 
664 Comment::Comment(const std::string& comment)
665  : NodeImp< TiXmlComment >(new TiXmlComment()) {
666  m_impRC->InitRef();
667  m_tiXmlPointer->SetValue(comment);
668 }
669 
670 //*****************************************************************************
671 
673  : NodeImp< TiXmlText >(new TiXmlText("")) {
674  m_impRC->InitRef();
675 }
676 
677 Text::Text(const std::string& value)
678  : NodeImp< TiXmlText >(new TiXmlText(value)) {
679  m_impRC->InitRef();
680 }
681 
683  : NodeImp< TiXmlText >(text) {}
684 
685 //*****************************************************************************
686 
688  : NodeImp< TiXmlDocument >(new TiXmlDocument()) {
689  m_impRC->InitRef();
690 }
691 
693  : NodeImp< TiXmlDocument >(document) {}
694 
695 Document::Document(const char* documentName)
696  : NodeImp< TiXmlDocument >(new TiXmlDocument(documentName)) {
697  m_impRC->InitRef();
698 }
699 
700 Document::Document(const std::string& documentName)
701  : NodeImp< TiXmlDocument >(new TiXmlDocument(documentName)) {
702  m_impRC->InitRef();
703 }
704 
706  if (!m_tiXmlPointer->LoadFile(encoding)) {
707  TICPPTHROW("Couldn't load " << m_tiXmlPointer->Value());
708  }
709 }
710 
711 void Document::SaveFile() const {
712  if (!m_tiXmlPointer->SaveFile()) {
713  TICPPTHROW("Couldn't save " << m_tiXmlPointer->Value());
714  }
715 }
716 
717 void Document::LoadFile(const std::string& filename, TiXmlEncoding encoding) {
718  if (!m_tiXmlPointer->LoadFile(filename.c_str(), encoding)) {
719  TICPPTHROW("Couldn't load " << filename);
720  }
721 }
722 
723 void Document::LoadFile(const char* filename, TiXmlEncoding encoding) {
724  if (!m_tiXmlPointer->LoadFile(filename, encoding)) {
725  TICPPTHROW("Couldn't load " << filename);
726  }
727 }
728 
729 void Document::SaveFile(const std::string& filename) const {
730  if (!m_tiXmlPointer->SaveFile(filename.c_str())) {
731  TICPPTHROW("Couldn't save " << filename);
732  }
733 }
734 
735 void Document::Parse(const std::string& xml,
736  bool throwIfParseError,
737  TiXmlEncoding encoding) {
738  m_tiXmlPointer->Parse(xml.c_str(), 0, encoding);
739 
740  if (throwIfParseError && m_tiXmlPointer->Error()) {
741  TICPPTHROW("Error parsing xml.");
742  }
743 }
744 
745 //*****************************************************************************
746 
749  "DefaultValueCausedByCreatingAnElementWithNoParameters")) {
750  m_impRC->InitRef();
751 }
752 
753 Element::Element(const std::string& value)
754  : NodeImp< TiXmlElement >(new TiXmlElement(value)) {
755  m_impRC->InitRef();
756 }
757 
758 Element::Element(const char* value)
759  : NodeImp< TiXmlElement >(new TiXmlElement(value)) {
760  m_impRC->InitRef();
761 }
762 
764  : NodeImp< TiXmlElement >(element) {}
765 
766 Attribute* Element::FirstAttribute(bool throwIfNoAttributes) const {
767  ValidatePointer();
769 
770  if ((0 == attribute) && throwIfNoAttributes) {
771  TICPPTHROW("This Element (" << Value() << ") has no attributes")
772  }
773 
774  if (0 == attribute) {
775  if (throwIfNoAttributes) {
776  TICPPTHROW("Element (" << Value() << ") has no attributes")
777  } else {
778  return 0;
779  }
780  }
781 
782  Attribute* temp = new Attribute(attribute);
783  attribute->m_spawnedWrappers.push_back(temp);
784 
785  return temp;
786 }
787 
788 Attribute* Element::LastAttribute(bool throwIfNoAttributes) const {
789  ValidatePointer();
791 
792  if ((0 == attribute) && throwIfNoAttributes) {
793  TICPPTHROW("This Element (" << Value() << ") has no attributes")
794  }
795 
796  if (0 == attribute) {
797  if (throwIfNoAttributes) {
798  TICPPTHROW("Element (" << Value() << ") has no attributes")
799  } else {
800  return 0;
801  }
802  }
803 
804  Attribute* temp = new Attribute(attribute);
805  attribute->m_spawnedWrappers.push_back(temp);
806 
807  return temp;
808 }
809 
810 std::string Element::GetAttributeOrDefault(const std::string& name,
811  const std::string& defaultValue) const {
812  std::string value;
813 
814  if (!GetAttributeImp(name, &value)) {
815  return defaultValue;
816  }
817 
818  return value;
819 }
820 
821 std::string Element::GetAttribute(const std::string& name) const {
822  return GetAttributeOrDefault(name, std::string());
823 }
824 
825 bool Element::HasAttribute(const std::string& name) const {
826  ValidatePointer();
827  return (0 != m_tiXmlPointer->Attribute(name.c_str()));
828 }
829 
830 void Element::RemoveAttribute(const std::string& name) {
831  ValidatePointer();
832  m_tiXmlPointer->RemoveAttribute(name.c_str());
833 }
834 
835 bool Element::GetAttributeImp(const std::string& name, std::string* value) const {
836  ValidatePointer();
837 
838  // Get value from TinyXML, if the attribute exists
839  const char* retVal = m_tiXmlPointer->Attribute(name.c_str());
840 
841  // TinyXML returns nullptr if the attribute doesn't exist
842  if (0 == retVal) {
843  return false;
844  } else {
845  *value = retVal;
846  return true;
847  }
848 }
849 
850 bool Element::GetTextImp(std::string* value) const {
851  ValidatePointer();
852 
853  // Get value from TinyXML, if the attribute exists
854  const char* retVal = m_tiXmlPointer->GetText();
855 
856  // TinyXML returns nullptr if the attribute doesn't exist
857  if (0 == retVal) {
858  return false;
859  } else {
860  *value = retVal;
861  return true;
862  }
863 }
864 
865 //*****************************************************************************
866 
869  m_impRC->InitRef();
870 }
871 
873  : NodeImp< TiXmlDeclaration >(declaration) {}
874 
875 Declaration::Declaration(const std::string& version,
876  const std::string& encoding,
877  const std::string& standalone)
879  new TiXmlDeclaration(version, encoding, standalone)) {
880  m_impRC->InitRef();
881 }
882 
883 std::string Declaration::Version() const { return m_tiXmlPointer->Version(); }
884 
885 std::string Declaration::Encoding() const { return m_tiXmlPointer->Encoding(); }
886 
887 std::string Declaration::Standalone() const {
888  return m_tiXmlPointer->Standalone();
889 }
890 
891 //*****************************************************************************
892 
895  m_impRC->InitRef();
896 }
897 
899  TiXmlStylesheetReference* stylesheetReference)
900  : NodeImp< TiXmlStylesheetReference >(stylesheetReference) {}
901 
903  const std::string& href)
905  new TiXmlStylesheetReference(type, href)) {
906  m_impRC->InitRef();
907 }
908 
909 std::string StylesheetReference::Type() const { return m_tiXmlPointer->Type(); }
910 
911 std::string StylesheetReference::Href() const { return m_tiXmlPointer->Href(); }
912 
913 //*****************************************************************************
914 
915 Exception::Exception(const std::string& details)
916  : m_details(details) {}
917 
919 
920 const char* Exception::what() const throw() { return m_details.c_str(); }
921 
922 //*****************************************************************************
923 
925  // Spawn reference counter for this object
926  m_tiRC = new TiCppRCImp(this);
927 }
928 
930  std::vector< Base* >::reverse_iterator wrapper;
931 
932  for (wrapper = m_spawnedWrappers.rbegin(); wrapper != m_spawnedWrappers.rend();
933  ++wrapper) {
934  delete *wrapper;
935  }
936 
937  m_spawnedWrappers.clear();
938 }
939 
941  DeleteSpawnedWrappers();
942 
943  // Set pointer held by reference counter to nullptr
944  this->m_tiRC->Nullify();
945 
946  // Decrement reference - so reference counter will delete itself if necessary
947  this->m_tiRC->DecRef();
948 }
949 
950 //*****************************************************************************
951 
953  : m_count(1)
954  , m_tiCppRC(tiCppRC) {}
955 
957 
959  m_count--;
960 
961  if (0 == m_count) {
962  delete m_tiCppRC;
963  delete this;
964  }
965 }
966 
968 
970 
972 
973 bool TiCppRCImp::IsNull() { return 0 == m_tiCppRC; }
974 
975 #endif // TIXML_USE_TICPP
Wrapper around TiXmlNode.
Definition: ticpp.h:460
std::unique_ptr< Node > Clone() const
Create an exact duplicate of this node and return it.
Definition: ticpp.cpp:635
void RemoveChild(Node *removeThis)
Delete a child of this node.
Definition: ticpp.cpp:401
An attribute is a name-value pair.
Definition: tinyxml.h:915
void operator=(const Attribute &copy)
Definition: ticpp.cpp:90
If you call the Accept() method, it requires being passed a TiXmlVisitor class to handle callbacks...
Definition: tinyxml.h:144
Wrapper around TiXmlComment.
Definition: ticpp.h:1349
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1296
Node * PreviousSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
Definition: ticpp.cpp:410
void Clear()
Clear all Nodes below this.
Definition: ticpp.cpp:232
const char * Type() const
Type. Will return an empty string if none was found.
Definition: tinyxml.h:1563
std::vector< ticpp::Base *> m_spawnedWrappers
Remember all wrappers that we&#39;ve created with &#39;new&#39; - ( e.g.
Definition: ticpprc.h:63
Wrapper around TiXmlElement.
Definition: ticpp.h:1493
void IteratePrevious(const std::string &, Attribute **previous) const
Definition: ticpp.cpp:165
const TiXmlNode * LastChild() const
Definition: tinyxml.h:572
Comment * ToComment() const
Pointer conversion - replaces TiXmlNode::ToComment.
Definition: ticpp.cpp:583
Document * ToDocument() const
Pointer conversion - replaces TiXmlNode::ToDocument.
Definition: ticpp.cpp:557
virtual const TiXmlDocument * ToDocument() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1827
const char * Name() const
Return the name of this attribute.
Definition: tinyxml.h:944
virtual bool Visit(const TiXmlDeclaration &declaration)
Visit a declaration.
Definition: ticpp.cpp:56
#define TICPPTHROW(message)
This allows you to stream your exceptions in.
Definition: ticpp.h:92
void GetAttributeOrDefault(const std::string &name, T *value, const DefaulT &defaultValue) const
Gets an attribute of name from an element, if it doesn&#39;t exist it will return the defaultValue...
Definition: ticpp.h:1746
StylesheetReference()
Default Constructor.
Definition: ticpp.cpp:893
virtual const TiXmlElement * ToElement() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:788
bool Accept(TiXmlVisitor *visitor) const
Accept a hierchical visit the nodes in the TinyXML DOM.
Definition: ticpp.cpp:650
TiCppRC * Get()
Get internal pointer to the TiCppRC object - not reference counted, use at your own risk...
Definition: ticpp.cpp:971
virtual ~TiCppRC()
Destructor Nullifies the pointer to this held by the reference counter Decrements reference count...
Definition: ticpp.cpp:940
bool NoChildren() const
Check if this node has no children.
Definition: ticpp.cpp:555
const char * GetText() const
Convenience function for easy access to the text inside an element.
Definition: tinyxml.cpp:755
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:800
TiCppRC * m_tiCppRC
Holds pointer to an object inheriting TiCppRC.
Definition: ticpprc.h:78
bool Error() const
If an error occurs, Error will be set to true.
Definition: tinyxml.h:1740
Attribute * Previous(bool throwIfNoAttribute=true) const
Get the previous sibling attribute in the DOM.
Definition: ticpp.cpp:143
void ValidatePointer() const
Definition: ticpp.h:281
void Nullify()
Allows the TiCppRC object to set the pointer to itself ( m_tiCppRc ) to nullptr when the TiCppRC obje...
Definition: ticpp.cpp:969
void SetTiXmlPointer(TiXmlAttribute *newPointer)
Definition: ticpp.cpp:174
TiXmlNode * ReplaceChild(TiXmlNode *replaceThis, const TiXmlNode &withThis)
Replace a child of this node.
Definition: tinyxml.cpp:265
Declaration()
Default Constructor.
Definition: ticpp.cpp:867
Document()
Default Constructor.
Definition: ticpp.cpp:687
const char * Standalone() const
Is this a standalone document?
Definition: tinyxml.h:1497
bool GetTextImp(std::string *value) const
Definition: ticpp.cpp:850
Node * LastChild(bool throwIfNoChildren=true) const
The last child of this node.
Definition: ticpp.cpp:268
Implementation of Node wrapper.
Definition: ticpp.h:1260
virtual void Print(FILE *cfile, int depth) const
All TinyXml classes can print themselves to a filestream or the string class (TiXmlString in non-STL ...
Definition: tinyxml.h:1016
const char * Href() const
Href. Will return an empty string if none was found.
Definition: tinyxml.h:1565
bool LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
Definition: tinyxml.cpp:804
void SetValue(const char *_value)
Changes the value of the node.
Definition: tinyxml.h:538
TiXmlNode * InsertEndChild(const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cpp:181
TiXmlNode * InsertBeforeChild(TiXmlNode *beforeThis, const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cpp:197
void LoadFile(TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Load a file using the current document value.
Definition: ticpp.cpp:705
bool IsNull()
Returns state of internal pointer - will be null if the object was deleted.
Definition: ticpp.cpp:973
Wrapper around TiXmlText.
Definition: ticpp.h:1368
const std::string & ValueStr() const
Return the value of this attribute.
Definition: tinyxml.h:951
StylesheetReference * ToStylesheetReference() const
Pointer conversion - replaces TiXmlNode::ToStylesheetReference.
Definition: ticpp.cpp:622
std::string Standalone() const
StandAlone.
Definition: ticpp.cpp:887
bool SaveFile() const
Save a file using the current document value. Returns true if successful.
Definition: tinyxml.cpp:811
void SaveFile() const
Save a file using the current document value.
Definition: ticpp.cpp:711
const TiXmlElement * FirstChildElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:384
const char * Encoding() const
Encoding. Will return an empty string if none was found.
Definition: tinyxml.h:1495
virtual TiXmlNode * GetTiXmlPointer() const =0
A stylesheet reference looks like this:
Definition: tinyxml.h:1543
In correct XML the declaration is the first entry in the file.
Definition: tinyxml.h:1469
Element * ToElement() const
Pointer conversion - replaces TiXmlNode::ToElement.
Definition: ticpp.cpp:570
std::string Value() const
Get the value of this node.
Definition: ticpp.cpp:230
const TiXmlNode * PreviousSibling() const
Navigate to a sibling node.
Definition: tinyxml.h:678
virtual const TiXmlDocument * ToDocument() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:784
std::string Href() const
Href.
Definition: ticpp.cpp:911
void Parse(const std::string &xml, bool throwIfParseError=true, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given xml data.
Definition: ticpp.cpp:735
Element()
Default Constructor.
Definition: ticpp.cpp:747
Any tag that tinyXml doesn&#39;t recognize is saved as an unknown.
Definition: tinyxml.h:1608
void IterateNext(const std::string &, Attribute **next) const
Definition: ticpp.cpp:161
virtual const TiXmlText * ToText() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1427
virtual const TiXmlComment * ToComment() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:792
int Type() const
Query the type (as TiXmlNode::NodeType ) of this node.
Definition: ticpp.cpp:536
const char * Version() const
Version. Will return an empty string if none was found.
Definition: tinyxml.h:1493
Node * ReplaceChild(Node *replaceThis, Node &withThis)
Replace a child of this node.
Definition: ticpp.cpp:383
const char * Value() const
The meaning of &#39;value&#39; changes for the specific type of TiXmlNode.
Definition: tinyxml.h:517
const TiXmlAttribute * LastAttribute() const
Access the last attribute in this element.
Definition: tinyxml.h:1241
bool GetAttributeImp(const std::string &name, std::string *value) const
Definition: ticpp.cpp:835
const char * what() const
Override std::exception::what() to return m_details.
Definition: ticpp.cpp:920
void RemoveAttribute(const std::string &name)
Removes attribute from element.
Definition: ticpp.cpp:830
void RemoveAttribute(const char *name)
Deletes an attribute with the given name.
Definition: tinyxml.cpp:370
void IncRef()
Increment Reference Count.
Definition: ticpp.cpp:956
Node * IterateChildren(Node *previous) const
An alternate way to walk the children of a node.
Definition: ticpp.cpp:292
Attribute * FirstAttribute(bool throwIfNoAttributes=true) const
Access the first attribute in this element.
Definition: ticpp.cpp:766
Wrapper around TiXmlDeclaration.
Definition: ticpp.h:1886
Wrapper around TiXmlBase.
Definition: ticpp.h:161
Text()
Constructor.
Definition: ticpp.cpp:672
const TiXmlElement * NextSiblingElement() const
Convenience function to get through elements.
Definition: tinyxml.cpp:404
Always the top level node.
Definition: tinyxml.h:1655
TiXmlEncoding
Definition: tinyxml.h:179
int m_count
Holds reference count to me, and to the node I point to.
Definition: ticpprc.h:76
std::string Type() const
Type.
Definition: ticpp.cpp:909
TiXmlNode * InsertAfterChild(TiXmlNode *afterThis, const TiXmlNode &addThis)
Add a new node related to this.
Definition: tinyxml.cpp:231
std::string Name() const
Get the value of this attribute.
Definition: ticpp.cpp:120
const TiXmlAttribute * Next() const
Get the next sibling attribute in the DOM. Returns null at end.
Definition: tinyxml.cpp:1043
Node * Parent(bool throwIfNoParent=true) const
The Parent of this Node.
Definition: ticpp.cpp:234
const TiXmlDocument * GetDocument() const
Return a pointer to the Document this node lives in.
Definition: tinyxml.cpp:424
void SetImpRC(TiXmlBase *nodeBase)
Definition: ticpp.h:279
virtual bool VisitExit(const TiXmlDocument &doc)
Visit a document.
Definition: ticpp.cpp:38
std::string Encoding() const
Encoding.
Definition: ticpp.cpp:885
TiXmlNode * Parent()
One step up the DOM.
Definition: tinyxml.h:549
int Type() const
Query the type (as an enumerated value, above) of this node.
Definition: tinyxml.h:770
std::string m_details
Exception Details.
Definition: ticpp.h:85
TiXmlAttribute * m_tiXmlPointer
Definition: ticpp.h:299
Element * NextSiblingElement(bool throwIfNoSiblings=true) const
Navigate to a sibling element.
Definition: ticpp.cpp:464
Definition: ticpp.h:70
const TiXmlNode * FirstChild() const
The first child of this node. Will be null if there are no children.
Definition: tinyxml.h:552
virtual const char * Parse(const char *p, TiXmlParsingData *data=0, TiXmlEncoding encoding=TIXML_DEFAULT_ENCODING)
Parse the given null terminated block of xml data.
virtual const TiXmlDeclaration * ToDeclaration() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:804
Base class for reference counting functionality.
Definition: ticpprc.h:41
The parent class for everything in the Document Object Model.
Definition: tinyxml.h:454
XML text.
Definition: tinyxml.h:1386
TiXmlComment * m_tiXmlPointer
Internal pointer to the TiXml Class which is being wrapped.
Definition: ticpp.h:1262
std::string Version() const
Version.
Definition: ticpp.cpp:883
void InitRef()
Set Reference Count to 1 - dangerous! - Use only if you are sure of the consequences.
Definition: ticpp.cpp:967
TiCppRCImp(TiCppRC *tiCppRC)
Initializes m_tiCppRC pointer, and set reference count to 1.
Definition: ticpp.cpp:952
Node * NextSibling(bool throwIfNoSiblings=true) const
Navigate to a sibling node.
Definition: ticpp.cpp:438
virtual const TiXmlComment * ToComment() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1356
const char * Attribute(const char *name) const
Given an attribute name, Attribute() returns the value for the attribute of that name, or null if none exists.
Definition: tinyxml.cpp:471
Node * InsertEndChild(Node &addThis)
Adds a child past the LastChild.
Definition: ticpp.cpp:317
virtual const TiXmlStylesheetReference * ToStylesheetReference() const
Cast to a more defined type. Will return null if not of the requested type.
Definition: tinyxml.h:808
virtual const TiXmlDeclaration * ToDeclaration() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1508
virtual void Print(FILE *file, int depth) const
All TinyXml classes can print themselves to a filestream.
Definition: ticpp.cpp:169
Text * ToText() const
Pointer conversion - replaces TiXmlNode::ToText.
Definition: ticpp.cpp:596
Node * InsertAfterChild(Node *afterThis, Node &addThis)
Adds a child after the specified child.
Definition: ticpp.cpp:365
virtual const TiXmlStylesheetReference * ToStylesheetReference() const
Cast to a more defined type. Will return null not of the requested type.
Definition: tinyxml.h:1576
Wrapper around TiXmlDocument.
Definition: ticpp.h:1404
std::string Value() const
Get the value of this attribute.
Definition: ticpp.cpp:115
Attribute * Next(bool throwIfNoAttribute=true) const
Get the next sibling attribute in the DOM.
Definition: ticpp.cpp:125
TiCppRCImp * m_impRC
Holds status of internal TiXmlPointer - use this to determine if object has been deleted already...
Definition: ticpp.h:267
Attribute()
Construct an empty attribute.
Definition: ticpp.cpp:75
Node * LinkEndChild(Node *childNode)
Adds a child past the LastChild.
Definition: ticpp.cpp:332
An XML comment.
Definition: tinyxml.h:1330
T GetAttribute(const std::string &name, bool throwIfNotFound=true) const
Returns an attribute of name from an element.
Definition: ticpp.h:1789
virtual bool VisitEnter(const TiXmlDocument &doc)
Visit a document.
Definition: ticpp.cpp:34
Element * FirstChildElement(bool throwIfNoChildren=true) const
The first child element of this node.
Definition: ticpp.cpp:501
void DecRef()
Decrement Reference Count.
Definition: ticpp.cpp:958
const TiXmlAttribute * Previous() const
Get the previous sibling attribute in the DOM. Returns null at beginning.
Definition: tinyxml.cpp:1062
Node * FirstChild(bool throwIfNoChildren=true) const
The first child of this node.
Definition: ticpp.cpp:244
const TiXmlNode * IterateChildren(const TiXmlNode *previous) const
An alternate way to walk the children of a node.
Definition: tinyxml.cpp:331
Wrapper around TiXmlStylesheetReference.
Definition: ticpp.h:1922
Node * InsertBeforeChild(Node *beforeThis, Node &addThis)
Adds a child before the specified child.
Definition: ticpp.cpp:347
Attribute * LastAttribute(bool throwIfNoAttributes=true) const
Access the last attribute in this element.
Definition: ticpp.cpp:788
Exception(const std::string &details)
Construct an exception with a message.
Definition: ticpp.cpp:915
TiCppRC()
Constructor Spawns new reference counter with a pointer to this.
Definition: ticpp.cpp:924
Comment()
Constructor.
Definition: ticpp.cpp:656
virtual TiXmlNode * Clone() const =0
Create an exact duplicate of this node and return it.
Node * NodeFactory(TiXmlNode *tiXmlNode, bool throwIfNull=true, bool rememberSpawnedWrapper=true) const
Definition: ticpp.cpp:181
Declaration * ToDeclaration() const
Pointer conversion - replaces TiXmlNode::ToDeclaration.
Definition: ticpp.cpp:609
const TiXmlNode * NextSibling(const std::string &_value) const
STL std::string form.
Definition: tinyxml.h:695
Wrapper around TiXmlAttribute.
Definition: ticpp.h:297
Document * GetDocument(bool throwIfNoDocument=true) const
Return a pointer to the Document this node lives in.
Definition: ticpp.cpp:538
The element is a container class.
Definition: tinyxml.h:1095
bool HasAttribute(const std::string &name) const
Returns true, if attribute exists.
Definition: ticpp.cpp:825
void DeleteSpawnedWrappers()
Delete all container objects we&#39;ve spawned with &#39;new&#39;.
Definition: ticpp.cpp:929
virtual bool Visit(const TiXmlDeclaration &)
Visit a declaration.
Definition: tinyxml.h:162
const TiXmlAttribute * FirstAttribute() const
Access the first attribute in this element.
Definition: tinyxml.h:1237