]> icculus.org git repositories - divverent/netradiant.git/blob - libs/entityxml.h
s/GtkRadiant/NetRadiant/
[divverent/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   }
37   void pushElement(const XMLElement& element)
38   {
39     if(strcmp(element.name(), "epair") == 0)
40       m_entity.setKeyValue(element.attribute("key"), element.attribute("value")); 
41   }
42   void popElement(const char* name)
43   {
44   }
45   std::size_t write(const char* data, std::size_t length)
46   {
47     return length;
48   }
49 };
50
51 class entity_export : public XMLExporter
52 {
53   class ExportXMLVisitor : public Entity::Visitor
54   {
55     XMLImporter& m_importer;
56   public:
57     ExportXMLVisitor(XMLImporter& importer) : m_importer(importer)
58     {
59     }
60     void visit(const char* key, const char* value)
61     {
62       StaticElement element("epair");
63       element.insertAttribute("key", key);
64       element.insertAttribute("value", value);
65       m_importer.pushElement(element);
66       m_importer.popElement(element.name());
67     }
68   };
69
70   const Entity& m_entity;
71
72 public:
73   entity_export(const Entity& entity) : m_entity(entity)
74   {
75   }
76   void exportXML(XMLImporter& observer)
77   {
78     ExportXMLVisitor visitor(observer);
79
80     m_entity.forEachKeyValue(visitor);
81   }
82 };
83
84 inline void entity_copy(Entity& entity, const Entity& other)
85 {
86   entity_export exporter(other);
87   entity_import importer(entity);
88   exporter.exportXML(importer);
89 }
90
91 template<typename EntityType>
92 class EntityConstruction
93 {
94 public:
95   typedef EntityClass* type;
96   static type get(const EntityType& entity)
97   {
98     return &entity.getEntity().getEntityClass();
99   }
100   static void copy(EntityType& entity, const EntityType& other)
101   {
102     entity_copy(entity.getEntity(), other.getEntity());
103   }
104 };
105
106
107
108 #endif