| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 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 |