Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/buildsystem/src/tinyxml/ticpp.cc @ 2139

Last change on this file since 2139 was 2139, checked in by rgrieder, 15 years ago

Forgot one USE_TICPP in a source file

  • Property svn:eol-style set to native
File size: 24.6 KB
Line 
1#define TIXML_USE_TICPP
2
3/*
4http://code.google.com/p/ticpp/
5Copyright (c) 2006 Ryan Pusztai, Ryan Mulder
6
7Permission is hereby granted, free of charge, to any person obtaining a copy of
8this software and associated documentation files (the "Software"), to deal in
9the Software without restriction, including without limitation the rights to
10use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
11the Software, and to permit persons to whom the Software is furnished to do so,
12subject to the following conditions:
13
14The above copyright notice and this permission notice shall be included in all
15copies or substantial portions of the Software.
16
17THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
19FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
20COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
21IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23*/
24
25#ifdef TIXML_USE_TICPP
26
27#include "ticpp.h"
28#include "ticpprc.h"
29#include "tinyxml.h"
30#include <sstream>
31
32using namespace ticpp;
33
34// In the following Visitor functions, casting away const should be safe, as the object can only be referred to by a const &
35bool Visitor::VisitEnter( const TiXmlDocument& doc )
36{
37        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
38}
39
40bool Visitor::VisitExit( const TiXmlDocument& doc )
41{
42        return VisitEnter( Document( const_cast< TiXmlDocument* >( &doc ) ) );
43}
44
45bool Visitor::VisitEnter( const TiXmlElement& element, const TiXmlAttribute* firstAttribute )
46{
47        if ( 0 != firstAttribute )
48        {
49                Attribute attribute( const_cast< TiXmlAttribute* >( firstAttribute ) );
50                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), &attribute );
51        }
52        else
53        {
54                return VisitEnter( Element( const_cast< TiXmlElement* >( &element ) ), 0 );
55        }
56}
57
58bool Visitor::VisitExit( const TiXmlElement& element )
59{
60        return VisitExit( Element( const_cast< TiXmlElement* >( &element ) ) );
61}
62
63bool Visitor::Visit( const TiXmlDeclaration& declaration )
64{
65        return Visit( Declaration( const_cast< TiXmlDeclaration* >( &declaration ) ) );
66}
67
68bool Visitor::Visit( const TiXmlStylesheetReference& stylesheet )
69{
70        return Visit( StylesheetReference( const_cast< TiXmlStylesheetReference* >( &stylesheet ) ) );
71}
72
73bool Visitor::Visit( const TiXmlText& text )
74{
75        return Visit( Text( const_cast< TiXmlText* >( &text ) ) );
76}
77
78bool Visitor::Visit( const TiXmlComment& comment )
79{
80        return Visit( Comment( const_cast< TiXmlComment* >( &comment ) ) );
81}
82
83Attribute::Attribute()
84{
85        SetTiXmlPointer( new TiXmlAttribute() );
86        m_impRC->InitRef();
87}
88
89Attribute::Attribute( TiXmlAttribute* attribute )
90{
91        SetTiXmlPointer( attribute );
92        m_impRC->IncRef();
93}
94
95Attribute::Attribute( const std::string& name, const std::string& value )
96{
97        SetTiXmlPointer( new TiXmlAttribute( name, value ) );
98        m_impRC->InitRef();
99}
100
101void Attribute::operator=( const Attribute& copy )
102{
103        // Dropping the reference to the old object
104        this->m_impRC->DecRef();
105
106        // Pointing to the new Object
107        SetTiXmlPointer( copy.m_tiXmlPointer );
108
109        // The internal tixml pointer changed in the above line
110        this->m_impRC->IncRef();
111}
112
113Attribute::Attribute( const Attribute& copy ) : Base()
114{
115        // Dropping the reference to the old object
116        this->m_impRC->DecRef();
117
118        // Pointing to the new Object
119        SetTiXmlPointer( copy.m_tiXmlPointer );
120
121        // The internal tixml pointer changed in the above line
122        this->m_impRC->IncRef();
123}
124
125Attribute::~Attribute()
126{
127        m_impRC->DecRef();
128}
129
130std::string Attribute::Value() const
131{
132        ValidatePointer();
133        return m_tiXmlPointer->ValueStr();
134}
135
136std::string Attribute::Name() const
137{
138        ValidatePointer();
139        return m_tiXmlPointer->Name();
140}
141
142Attribute* Attribute::Next( bool throwIfNoAttribute ) const
143{
144        ValidatePointer();
145        TiXmlAttribute* attribute = m_tiXmlPointer->Next();
146        if ( 0 == attribute )
147        {
148                if ( throwIfNoAttribute )
149                {
150                        TICPPTHROW( "No more attributes found" )
151                }
152                else
153                {
154                        return 0;
155                }
156        }
157
158        Attribute* temp = new Attribute( attribute );
159        attribute->m_spawnedWrappers.push_back( temp );
160
161        return temp;
162}
163
164Attribute* Attribute::Previous( bool throwIfNoAttribute ) const
165{
166        ValidatePointer();
167        TiXmlAttribute* attribute = m_tiXmlPointer->Previous();
168        if ( 0 == attribute )
169        {
170                if ( throwIfNoAttribute )
171                {
172                        TICPPTHROW( "No more attributes found" )
173                }
174                else
175                {
176                        return 0;
177                }
178        }
179
180        Attribute* temp = new Attribute( attribute );
181        attribute->m_spawnedWrappers.push_back( temp );
182
183        return temp;
184}
185
186void Attribute::IterateNext( const std::string&, Attribute** next ) const
187{
188        *next = Next( false );
189}
190
191void Attribute::IteratePrevious( const std::string&, Attribute** previous ) const
192{
193        *previous = Previous( false );
194}
195
196void Attribute::Print( FILE* file, int depth ) const
197{
198        ValidatePointer();
199        m_tiXmlPointer->Print( file, depth );
200}
201
202void Attribute::SetTiXmlPointer( TiXmlAttribute* newPointer )
203{
204        m_tiXmlPointer = newPointer;
205        SetImpRC( newPointer );
206}
207
208//*****************************************************************************
209
210Node* Node::NodeFactory( TiXmlNode* tiXmlNode, bool throwIfNull, bool rememberSpawnedWrapper ) const
211{
212        if ( 0 == tiXmlNode )
213        {
214                if ( throwIfNull )
215                {
216                        TICPPTHROW( "tiXmlNode is NULL" )
217                }
218                else
219                {
220                        return 0;
221                }
222        }
223
224        Node* temp;
225        switch ( tiXmlNode->Type() )
226        {
227                case TiXmlNode::DOCUMENT:
228                        temp = new Document( tiXmlNode->ToDocument() );
229                        break;
230
231                case TiXmlNode::ELEMENT:
232                        temp = new Element( tiXmlNode->ToElement() );
233                        break;
234
235                case TiXmlNode::COMMENT:
236                        temp = new Comment( tiXmlNode->ToComment() );
237                        break;
238
239                case TiXmlNode::TEXT:
240                        temp = new Text( tiXmlNode->ToText() );
241                        break;
242
243                case TiXmlNode::DECLARATION:
244                        temp = new Declaration( tiXmlNode->ToDeclaration() );
245                        break;
246
247                case TiXmlNode::STYLESHEETREFERENCE:
248                        temp = new StylesheetReference( tiXmlNode->ToStylesheetReference() );
249                        break;
250
251                default:
252                        TICPPTHROW( "Type is unsupported" )
253        }
254
255        if ( rememberSpawnedWrapper )
256        {
257                tiXmlNode->m_spawnedWrappers.push_back( temp );
258        }
259        return temp;
260}
261
262
263std::string Node::Value() const
264{
265        return GetTiXmlPointer()->ValueStr();
266}
267
268void Node::Clear()
269{
270        GetTiXmlPointer()->Clear();
271}
272
273Node* Node::Parent( bool throwIfNoParent ) const
274{
275        TiXmlNode* parent = GetTiXmlPointer()->Parent();
276        if ( ( 0 == parent ) && throwIfNoParent )
277        {
278                TICPPTHROW( "No parent exists" );
279        }
280
281        return NodeFactory( parent, false );
282}
283
284Node* Node::FirstChild( bool throwIfNoChildren ) const
285{
286        return FirstChild( "", throwIfNoChildren );
287}
288
289Node* Node::FirstChild( const std::string& value, bool throwIfNoChildren ) const
290{
291        return FirstChild( value.c_str(), throwIfNoChildren );
292}
293
294Node* Node::FirstChild( const char* value, bool throwIfNoChildren ) const
295{
296        TiXmlNode* childNode;
297        if ( 0 == strlen( value ) )
298        {
299                childNode = GetTiXmlPointer()->FirstChild();
300        }
301        else
302        {
303                childNode = GetTiXmlPointer()->FirstChild( value );
304        }
305
306        if ( ( 0 == childNode ) && throwIfNoChildren )
307        {
308                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
309        }
310
311        return NodeFactory( childNode, false );
312}
313
314Node* Node::LastChild( bool throwIfNoChildren ) const
315{
316        return LastChild( "", throwIfNoChildren );
317}
318
319Node* Node::LastChild( const std::string& value, bool throwIfNoChildren ) const
320{
321        return LastChild( value.c_str(), throwIfNoChildren );
322}
323
324Node* Node::LastChild( const char* value, bool throwIfNoChildren ) const
325{
326        TiXmlNode* childNode;
327        if ( 0 == strlen( value ) )
328        {
329                childNode = GetTiXmlPointer()->LastChild();
330        }
331        else
332        {
333                childNode = GetTiXmlPointer()->LastChild( value );
334        }
335
336        if ( ( 0 == childNode ) && throwIfNoChildren )
337        {
338                TICPPTHROW( "Child with the value of \"" << value << "\" not found" );
339        }
340
341        return NodeFactory( childNode, false );
342}
343
344Node* Node::IterateChildren ( Node* previous ) const
345{
346        TiXmlNode* pointer;
347        if ( 0 == previous )
348        {
349                pointer = GetTiXmlPointer()->IterateChildren( 0 );
350        }
351        else
352        {
353                pointer = GetTiXmlPointer()->IterateChildren( previous->GetTiXmlPointer() );
354        }
355
356        return NodeFactory( pointer, false );
357}
358
359Node* Node::IterateChildren( const std::string& value, Node* previous ) const
360{
361        TiXmlNode* pointer;
362        if ( 0 == previous )
363        {
364                pointer = GetTiXmlPointer()->IterateChildren( value, 0 );
365        }
366        else
367        {
368                pointer = GetTiXmlPointer()->IterateChildren( value, previous->GetTiXmlPointer() );
369        }
370
371        return NodeFactory( pointer, false );
372}
373
374Node* Node::InsertEndChild( Node& addThis )
375{
376        if ( addThis.Type() == TiXmlNode::DOCUMENT )
377        {
378                TICPPTHROW( "Node is a Document and can't be inserted" );
379        }
380
381        TiXmlNode* pointer = GetTiXmlPointer()->InsertEndChild( *addThis.GetTiXmlPointer() );
382        if ( 0 == pointer )
383        {
384                TICPPTHROW( "Node can't be inserted" );
385        }
386
387        return NodeFactory( pointer );
388}
389
390Node* Node::LinkEndChild( Node* childNode )
391{
392        if ( childNode->Type() == TiXmlNode::DOCUMENT )
393        {
394                TICPPTHROW( "Node is a Document and can't be linked" );
395        }
396
397        // Increment reference count when adding to the tree
398        childNode->m_impRC->IncRef();
399
400        if ( 0 == GetTiXmlPointer()->LinkEndChild( childNode->GetTiXmlPointer() ) )
401        {
402                TICPPTHROW( "Node can't be linked" );
403        }
404
405        return childNode;
406}
407
408Node* Node::InsertBeforeChild( Node* beforeThis, Node& addThis )
409{
410        if ( addThis.Type() == TiXmlNode::DOCUMENT )
411        {
412                TICPPTHROW( "Node is a Document and can't be inserted" );
413        }
414
415        // Increment reference count when adding to the tree
416        addThis.m_impRC->IncRef();
417
418        TiXmlNode* pointer = GetTiXmlPointer()->InsertBeforeChild( beforeThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
419        if ( 0 == pointer )
420        {
421                TICPPTHROW( "Node can't be inserted" );
422        }
423
424        return NodeFactory( pointer );
425}
426
427Node* Node::InsertAfterChild( Node* afterThis, Node& addThis )
428{
429        if ( addThis.Type() == TiXmlNode::DOCUMENT )
430        {
431                TICPPTHROW( "Node is a Document and can't be inserted" );
432        }
433
434        // Increment reference count when adding to the tree
435        addThis.m_impRC->IncRef();
436
437        TiXmlNode* pointer = GetTiXmlPointer()->InsertAfterChild( afterThis->GetTiXmlPointer(), *addThis.GetTiXmlPointer() );
438        if ( 0 == pointer )
439        {
440                TICPPTHROW( "Node can't be inserted" );
441        }
442
443        return NodeFactory( pointer );
444}
445
446Node* Node::ReplaceChild( Node* replaceThis, Node& withThis )
447{
448        if ( withThis.Type() == TiXmlNode::DOCUMENT )
449        {
450                TICPPTHROW( "Node is a Document and can't be inserted" );
451        }
452
453        // Increment reference count when adding to the tree
454        withThis.m_impRC->IncRef();
455
456        TiXmlNode* pointer = GetTiXmlPointer()->ReplaceChild( replaceThis->GetTiXmlPointer(), *withThis.GetTiXmlPointer() );
457        if ( 0 == pointer )
458        {
459                TICPPTHROW( "Node can't be inserted" );
460        }
461
462        return NodeFactory( pointer );
463}
464
465void Node::RemoveChild( Node* removeThis )
466{
467        if  ( !GetTiXmlPointer()->RemoveChild( removeThis->GetTiXmlPointer() ) )
468        {
469                TICPPTHROW( "Node to remove (" << removeThis->Value() << ") is not a child of this Node (" << Value() << ")" )
470        }
471}
472
473Node* Node::PreviousSibling( bool throwIfNoSiblings ) const
474{
475        return PreviousSibling( "", throwIfNoSiblings );
476}
477
478Node* Node::PreviousSibling( const std::string& value, bool throwIfNoSiblings ) const
479{
480        return PreviousSibling( value.c_str(), throwIfNoSiblings );
481}
482
483Node* Node::PreviousSibling( const char* value, bool throwIfNoSiblings ) const
484{
485        TiXmlNode* sibling;
486        if ( 0 == strlen( value ) )
487        {
488                sibling = GetTiXmlPointer()->PreviousSibling();
489        }
490        else
491        {
492                sibling = GetTiXmlPointer()->PreviousSibling( value );
493        }
494
495        if ( ( 0 == sibling ) && throwIfNoSiblings )
496        {
497                TICPPTHROW( "No Siblings found with value, '" << value << "', Prior to this Node (" << Value() << ")" )
498        }
499
500        return NodeFactory( sibling, false );
501}
502
503Node* Node::NextSibling( bool throwIfNoSiblings ) const
504{
505        return NextSibling( "", throwIfNoSiblings );
506}
507
508Node* Node::NextSibling( const std::string& value, bool throwIfNoSiblings ) const
509{
510        return NextSibling( value.c_str(), throwIfNoSiblings );
511}
512
513Node* Node::NextSibling( const char* value, bool throwIfNoSiblings ) const
514{
515        TiXmlNode* sibling;
516        if ( 0 == strlen( value ) )
517        {
518                sibling = GetTiXmlPointer()->NextSibling();
519        }
520        else
521        {
522                sibling = GetTiXmlPointer()->NextSibling( value );
523        }
524
525        if ( ( 0 == sibling ) && throwIfNoSiblings )
526        {
527                TICPPTHROW( "No Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
528        }
529
530        return NodeFactory( sibling, false );
531}
532
533Element* Node::NextSiblingElement( bool throwIfNoSiblings ) const
534{
535        return NextSiblingElement( "", throwIfNoSiblings );
536}
537
538Element* Node::NextSiblingElement( const std::string& value, bool throwIfNoSiblings ) const
539{
540        return NextSiblingElement( value.c_str(), throwIfNoSiblings );
541}
542
543Element* Node::NextSiblingElement( const char* value, bool throwIfNoSiblings ) const
544{
545        TiXmlElement* sibling;
546        if ( 0 == strlen( value ) )
547        {
548                sibling = GetTiXmlPointer()->NextSiblingElement();
549        }
550        else
551        {
552                sibling = GetTiXmlPointer()->NextSiblingElement( value );
553        }
554
555        if ( 0 == sibling )
556        {
557                if ( throwIfNoSiblings )
558                {
559                        TICPPTHROW( "No Element Siblings found with value, '" << value << "', After this Node (" << Value() << ")" )
560                }
561                else
562                {
563                        return 0;
564                }
565        }
566
567        Element* temp = new Element( sibling );
568        sibling->m_spawnedWrappers.push_back( temp );
569
570        return temp;
571}
572
573Element* Node::FirstChildElement( bool throwIfNoChildren ) const
574{
575        return FirstChildElement( "", throwIfNoChildren );
576}
577
578Element* Node::FirstChildElement( const std::string& value, bool throwIfNoChildren ) const
579{
580        return FirstChildElement( value.c_str(), throwIfNoChildren );
581}
582
583Element* Node::FirstChildElement( const char* value, bool throwIfNoChildren ) const
584{
585        TiXmlElement* element;
586        if ( 0 == strlen( value ) )
587        {
588                element = GetTiXmlPointer()->FirstChildElement();
589        }
590        else
591        {
592                element = GetTiXmlPointer()->FirstChildElement( value );
593        }
594
595        if ( 0 == element )
596        {
597                if( throwIfNoChildren )
598                {
599                        TICPPTHROW( "Element (" << Value() << ") does NOT contain a child with the value of '" << value << "'" )
600                }
601                else
602                {
603                        return 0;
604                }
605        }
606
607        Element* temp = new Element( element );
608        element->m_spawnedWrappers.push_back( temp );
609
610        return temp;
611}
612
613int Node::Type() const
614{
615        return GetTiXmlPointer()->Type();
616}
617
618Document* Node::GetDocument( bool throwIfNoDocument ) const
619{
620        TiXmlDocument* doc = GetTiXmlPointer()->GetDocument();
621        if ( 0 == doc )
622        {
623                if( throwIfNoDocument )
624                {
625                        TICPPTHROW( "This node (" << Value() << ") is not linked under a document" )
626                }
627                else
628                {
629                        return 0;
630                }
631        }
632        Document* temp = new Document( doc );
633        doc->m_spawnedWrappers.push_back( temp );
634
635        return temp;
636}
637
638bool Node::NoChildren() const
639{
640        return GetTiXmlPointer()->NoChildren();
641}
642
643Document* Node::ToDocument() const
644{
645        TiXmlDocument* doc = GetTiXmlPointer()->ToDocument();
646        if ( 0 == doc )
647        {
648                TICPPTHROW( "This node (" << Value() << ") is not a Document" )
649        }
650        Document* temp = new Document( doc );
651        doc->m_spawnedWrappers.push_back( temp );
652
653        return temp;
654}
655
656Element* Node::ToElement() const
657{
658        TiXmlElement* doc = GetTiXmlPointer()->ToElement();
659        if ( 0 == doc )
660        {
661                TICPPTHROW( "This node (" << Value() << ") is not a Element" )
662        }
663        Element* temp = new Element( doc );
664        doc->m_spawnedWrappers.push_back( temp );
665
666        return temp;
667}
668
669Comment* Node::ToComment() const
670{
671        TiXmlComment* doc = GetTiXmlPointer()->ToComment();
672        if ( 0 == doc )
673        {
674                TICPPTHROW( "This node (" << Value() << ") is not a Comment" )
675        }
676        Comment* temp = new Comment( doc );
677        doc->m_spawnedWrappers.push_back( temp );
678
679        return temp;
680}
681
682Text* Node::ToText() const
683{
684        TiXmlText* doc = GetTiXmlPointer()->ToText();
685        if ( 0 == doc )
686        {
687                TICPPTHROW( "This node (" << Value() << ") is not a Text" )
688        }
689        Text* temp = new Text( doc );
690        doc->m_spawnedWrappers.push_back( temp );
691
692        return temp;
693}
694
695Declaration* Node::ToDeclaration() const
696{
697        TiXmlDeclaration* doc = GetTiXmlPointer()->ToDeclaration();
698        if ( 0 == doc )
699        {
700                TICPPTHROW( "This node (" << Value() << ") is not a Declaration" )
701        }
702        Declaration* temp = new Declaration( doc );
703        doc->m_spawnedWrappers.push_back( temp );
704
705        return temp;
706}
707
708StylesheetReference* Node::ToStylesheetReference() const
709{
710        TiXmlStylesheetReference* doc = GetTiXmlPointer()->ToStylesheetReference();
711        if ( 0 == doc )
712        {
713                TICPPTHROW( "This node (" << Value() << ") is not a StylesheetReference" )
714        }
715        StylesheetReference* temp = new StylesheetReference( doc );
716        doc->m_spawnedWrappers.push_back( temp );
717
718        return temp;
719}
720
721std::auto_ptr< Node > Node::Clone() const
722{
723        TiXmlNode* node = GetTiXmlPointer()->Clone();
724        if ( 0 == node )
725        {
726                TICPPTHROW( "Node could not be cloned" );
727        }
728        std::auto_ptr< Node > temp( NodeFactory( node, false, false ) );
729
730        // Take ownership of the memory from TiXml
731        temp->m_impRC->InitRef();
732
733        return temp;
734}
735
736bool Node::Accept( TiXmlVisitor* visitor ) const
737{
738        return GetTiXmlPointer()->Accept( visitor );
739}
740
741//*****************************************************************************
742
743Comment::Comment()
744: NodeImp< TiXmlComment >( new TiXmlComment() )
745{
746        m_impRC->InitRef();
747}
748
749Comment::Comment( TiXmlComment* comment )
750: NodeImp< TiXmlComment >( comment )
751{
752}
753
754Comment::Comment( const std::string& comment )
755: NodeImp< TiXmlComment >( new TiXmlComment() )
756{
757        m_impRC->InitRef();
758        m_tiXmlPointer->SetValue( comment );
759}
760
761//*****************************************************************************
762
763Text::Text()
764: NodeImp< TiXmlText >( new TiXmlText("") )
765{
766        m_impRC->InitRef();
767}
768
769
770Text::Text( const std::string& value )
771: NodeImp< TiXmlText >( new TiXmlText( value ) )
772{
773        m_impRC->InitRef();
774}
775
776Text::Text( TiXmlText* text )
777: NodeImp< TiXmlText >( text )
778{
779}
780
781
782//*****************************************************************************
783
784Document::Document()
785: NodeImp< TiXmlDocument >( new TiXmlDocument() )
786{
787        m_impRC->InitRef();
788}
789
790Document::Document( TiXmlDocument* document )
791: NodeImp< TiXmlDocument >( document )
792{
793}
794
795Document::Document( const char* documentName )
796: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
797{
798        m_impRC->InitRef();
799}
800
801Document::Document( const std::string& documentName )
802: NodeImp< TiXmlDocument >( new TiXmlDocument( documentName ) )
803{
804        m_impRC->InitRef();
805}
806
807void Document::LoadFile( TiXmlEncoding encoding )
808{
809        if ( !m_tiXmlPointer->LoadFile( encoding ) )
810        {
811                TICPPTHROW( "Couldn't load " << m_tiXmlPointer->Value() );
812        }
813}
814
815void Document::SaveFile( void ) const
816{
817        if ( !m_tiXmlPointer->SaveFile() )
818        {
819                TICPPTHROW( "Couldn't save " << m_tiXmlPointer->Value() );
820        }
821}
822
823void Document::LoadFile( const std::string& filename, TiXmlEncoding encoding )
824{
825        if ( !m_tiXmlPointer->LoadFile( filename.c_str(), encoding ) )
826        {
827                TICPPTHROW( "Couldn't load " << filename );
828        }
829}
830
831void Document::LoadFile( const char* filename, TiXmlEncoding encoding )
832{
833        if ( !m_tiXmlPointer->LoadFile( filename, encoding ) )
834        {
835                TICPPTHROW( "Couldn't load " << filename );
836        }
837}
838
839void Document::SaveFile( const std::string& filename ) const
840{
841        if ( !m_tiXmlPointer->SaveFile( filename.c_str() ) )
842        {
843                TICPPTHROW( "Couldn't save " << filename );
844        }
845}
846
847void Document::Parse( const std::string& xml, bool throwIfParseError, TiXmlEncoding encoding )
848{
849        m_tiXmlPointer->Parse( xml.c_str(), 0, encoding );
850        if( throwIfParseError && m_tiXmlPointer->Error() )
851        {
852                TICPPTHROW( "Error parsing xml." );
853        }
854}
855
856//*****************************************************************************
857
858Element::Element()
859: NodeImp< TiXmlElement >( new TiXmlElement( "DefaultValueCausedByCreatingAnElementWithNoParameters" ) )
860{
861        m_impRC->InitRef();
862}
863
864Element::Element( const std::string& value )
865: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
866{
867        m_impRC->InitRef();
868}
869
870Element::Element( const char* value )
871: NodeImp< TiXmlElement >( new TiXmlElement( value ) )
872{
873        m_impRC->InitRef();
874}
875
876Element::Element( TiXmlElement* element )
877: NodeImp< TiXmlElement >( element )
878{
879}
880
881Attribute* Element::FirstAttribute( bool throwIfNoAttributes ) const
882{
883        ValidatePointer();
884        TiXmlAttribute* attribute = m_tiXmlPointer->FirstAttribute();
885        if ( ( 0 == attribute ) && throwIfNoAttributes )
886        {
887                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
888        }
889
890        if ( 0 == attribute )
891        {
892                if( throwIfNoAttributes )
893                {
894                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
895                }
896                else
897                {
898                        return 0;
899                }
900        }
901
902        Attribute* temp = new Attribute( attribute );
903        attribute->m_spawnedWrappers.push_back( temp );
904
905        return temp;
906}
907
908Attribute* Element::LastAttribute( bool throwIfNoAttributes ) const
909{
910        ValidatePointer();
911        TiXmlAttribute* attribute = m_tiXmlPointer->LastAttribute();
912        if ( ( 0 == attribute ) && throwIfNoAttributes )
913        {
914                TICPPTHROW( "This Element (" << Value() << ") has no attributes" )
915        }
916
917        if ( 0 == attribute )
918        {
919                if( throwIfNoAttributes )
920                {
921                        TICPPTHROW( "Element (" << Value() << ") has no attributes" )
922                }
923                else
924                {
925                        return 0;
926                }
927        }
928
929        Attribute* temp = new Attribute( attribute );
930        attribute->m_spawnedWrappers.push_back( temp );
931
932        return temp;
933}
934
935std::string Element::GetAttributeOrDefault( const std::string& name, const std::string& defaultValue ) const
936{
937        std::string value;
938        if ( !GetAttributeImp( name, &value ) )
939        {
940                return defaultValue;
941        }
942        return value;
943}
944
945std::string Element::GetAttribute( const std::string& name ) const
946{
947        return GetAttributeOrDefault( name, std::string() );
948}
949
950bool Element::HasAttribute( const std::string& name ) const
951{
952        ValidatePointer();
953        return ( 0 != m_tiXmlPointer->Attribute( name.c_str() ) );
954}
955
956void Element::RemoveAttribute( const std::string& name )
957{
958        ValidatePointer();
959        m_tiXmlPointer->RemoveAttribute( name.c_str() );
960}
961
962bool Element::GetAttributeImp( const std::string& name, std::string* value ) const
963{
964        ValidatePointer();
965
966        // Get value from TinyXML, if the attribute exists
967        const char* retVal = m_tiXmlPointer->Attribute( name.c_str() );
968
969        // TinyXML returns NULL if the attribute doesn't exist
970        if ( 0 == retVal )
971        {
972                return false;
973        }
974        else
975        {
976                *value = retVal;
977                return true;
978        }
979}
980
981bool Element::GetTextImp( std::string* value ) const
982{
983        ValidatePointer();
984
985        // Get value from TinyXML, if the attribute exists
986        const char* retVal = m_tiXmlPointer->GetText();
987
988        // TinyXML returns NULL if the attribute doesn't exist
989        if ( 0 == retVal )
990        {
991                return false;
992        }
993        else
994        {
995                *value = retVal;
996                return true;
997        }
998}
999
1000//*****************************************************************************
1001
1002Declaration::Declaration()
1003: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration() )
1004{
1005        m_impRC->InitRef();
1006}
1007
1008Declaration::Declaration( TiXmlDeclaration* declaration )
1009: NodeImp< TiXmlDeclaration >( declaration )
1010{
1011}
1012
1013Declaration::Declaration( const std::string& version, const std::string& encoding, const std::string& standalone )
1014: NodeImp< TiXmlDeclaration >( new TiXmlDeclaration( version, encoding, standalone ) )
1015{
1016        m_impRC->InitRef();
1017}
1018
1019std::string Declaration::Version() const
1020{
1021        return m_tiXmlPointer->Version();
1022}
1023
1024std::string Declaration::Encoding() const
1025{
1026        return m_tiXmlPointer->Encoding();
1027}
1028
1029std::string Declaration::Standalone() const
1030{
1031        return m_tiXmlPointer->Standalone();
1032}
1033
1034//*****************************************************************************
1035
1036StylesheetReference::StylesheetReference()
1037: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference() )
1038{
1039        m_impRC->InitRef();
1040}
1041
1042StylesheetReference::StylesheetReference( TiXmlStylesheetReference* stylesheetReference )
1043: NodeImp< TiXmlStylesheetReference >( stylesheetReference )
1044{
1045}
1046
1047StylesheetReference::StylesheetReference( const std::string& type, const std::string& href )
1048: NodeImp< TiXmlStylesheetReference >( new TiXmlStylesheetReference( type, href ) )
1049{
1050        m_impRC->InitRef();
1051}
1052
1053std::string StylesheetReference::Type() const
1054{
1055        return m_tiXmlPointer->Type();
1056}
1057
1058std::string StylesheetReference::Href() const
1059{
1060        return m_tiXmlPointer->Href();
1061}
1062
1063//*****************************************************************************
1064
1065Exception::Exception(const std::string &details)
1066:
1067m_details( details )
1068{
1069
1070}
1071
1072Exception::~Exception() throw()
1073{
1074}
1075
1076const char* Exception::what() const throw()
1077{
1078        return m_details.c_str();
1079}
1080
1081//*****************************************************************************
1082
1083TiCppRC::TiCppRC()
1084{
1085        // Spawn reference counter for this object
1086        m_tiRC = new TiCppRCImp( this );
1087}
1088
1089void TiCppRC::DeleteSpawnedWrappers()
1090{
1091        std::vector< Base* >::reverse_iterator wrapper;
1092        for ( wrapper = m_spawnedWrappers.rbegin(); wrapper != m_spawnedWrappers.rend(); ++wrapper )
1093        {
1094                delete *wrapper;
1095        }
1096        m_spawnedWrappers.clear();
1097}
1098               
1099TiCppRC::~TiCppRC()
1100{       
1101        DeleteSpawnedWrappers();
1102       
1103        // Set pointer held by reference counter to NULL
1104        this->m_tiRC->Nullify();
1105
1106        // Decrement reference - so reference counter will delete itself if necessary
1107        this->m_tiRC->DecRef();
1108}
1109
1110//*****************************************************************************
1111
1112TiCppRCImp::TiCppRCImp( TiCppRC* tiCppRC )
1113        : m_count( 1 ), m_tiCppRC ( tiCppRC )
1114{
1115}
1116
1117void TiCppRCImp::IncRef()
1118{
1119        m_count++;
1120}
1121
1122void TiCppRCImp::DecRef()
1123{
1124        m_count--;
1125        if ( 0 == m_count )
1126        {
1127                delete m_tiCppRC;
1128                delete this;
1129        }
1130}
1131
1132void TiCppRCImp::InitRef()
1133{
1134        m_count = 1;
1135}
1136
1137void TiCppRCImp::Nullify()
1138{
1139        m_tiCppRC = 0;
1140}
1141
1142TiCppRC* TiCppRCImp::Get()
1143{
1144        return m_tiCppRC;
1145}
1146
1147bool TiCppRCImp::IsNull()
1148{
1149        return 0 == m_tiCppRC;
1150}
1151
1152#endif // TIXML_USE_TICPP
Note: See TracBrowser for help on using the repository browser.