]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/entityxml.h
Merge commit '830125fad042fad35dc029b6eb57c8156ad7e176'
[xonotic/netradiant.git] / libs / entityxml.h
1 /*
2    Copyright (C) 2001-2006, William Joseph.
3    All Rights Reserved.
4
5    This file is part of GtkRadiant.
6
7    GtkRadiant is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    GtkRadiant is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GtkRadiant; if not, write to the Free Software
19    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21
22 #if !defined( INCLUDED_ENTITYXML_H )
23 #define INCLUDED_ENTITYXML_H
24
25 #include "ientity.h"
26 #include "xml/ixml.h"
27 #include "xml/xmlelement.h"
28
29 class entity_import : public XMLImporter
30 {
31 Entity& m_entity;
32 public:
33 entity_import( Entity& entity )
34         : m_entity( entity ){
35 }
36 void pushElement( const XMLElement& element ){
37         if ( strcmp( element.name(), "epair" ) == 0 ) {
38                 m_entity.setKeyValue( element.attribute( "key" ), element.attribute( "value" ) );
39         }
40 }
41 void popElement( const char* name ){
42 }
43 std::size_t write( const char* data, std::size_t length ){
44         return length;
45 }
46 };
47
48 class entity_export : public XMLExporter
49 {
50 class ExportXMLVisitor : public Entity::Visitor
51 {
52 XMLImporter& m_importer;
53 public:
54 ExportXMLVisitor( XMLImporter& importer ) : m_importer( importer ){
55 }
56 void visit( const char* key, const char* value ){
57         StaticElement element( "epair" );
58         element.insertAttribute( "key", key );
59         element.insertAttribute( "value", value );
60         m_importer.pushElement( element );
61         m_importer.popElement( element.name() );
62 }
63 };
64
65 const Entity& m_entity;
66
67 public:
68 entity_export( const Entity& entity ) : m_entity( entity ){
69 }
70 void exportXML( XMLImporter& observer ){
71         ExportXMLVisitor visitor( observer );
72
73         m_entity.forEachKeyValue( visitor );
74 }
75 };
76
77 inline void entity_copy( Entity& entity, const Entity& other ){
78         entity_export exporter( other );
79         entity_import importer( entity );
80         exporter.exportXML( importer );
81 }
82
83 template<typename EntityType>
84 class EntityConstruction
85 {
86 public:
87 typedef EntityClass* type;
88 static type get( const EntityType& entity ){
89         return &entity.getEntity().getEntityClass();
90 }
91 static void copy( EntityType& entity, const EntityType& other ){
92         entity_copy( entity.getEntity(), other.getEntity() );
93 }
94 };
95
96
97
98 #endif