|
| 1 | +#include "pch.h" |
| 2 | +#include "CppReflect.h" |
| 3 | +#include "../pugixml/pugixml.hpp" //pugi::xml_node |
| 4 | + |
| 5 | +using namespace pugi; |
| 6 | + |
| 7 | +// |
| 8 | +// Serializes class instance to xml node. |
| 9 | +// |
| 10 | +void DataToNode( xml_node& _node, void* pclass, CppTypeInfo& type ) |
| 11 | +{ |
| 12 | + USES_CONVERSION; |
| 13 | + xml_node node = _node.append_child( CA2W( type.name ) ); |
| 14 | + |
| 15 | + for (FieldInfo fi : type.fields) |
| 16 | + { |
| 17 | + void* p = ((char*)pclass) + fi.offset; |
| 18 | + CppTypeInfo* arrayType = nullptr; |
| 19 | + |
| 20 | + if( !fi.fieldType->GetArrayElementType( arrayType ) ) |
| 21 | + { |
| 22 | + // Simple type, append as attribute. |
| 23 | + CStringW s = fi.fieldType->ToString( p ); |
| 24 | + if( s.GetLength() ) // Don't serialize empty values. |
| 25 | + node.append_attribute( CA2W( fi.name ) ) = s; |
| 26 | + continue; |
| 27 | + } |
| 28 | + |
| 29 | + if( !arrayType ) |
| 30 | + continue; |
| 31 | + |
| 32 | + xml_node fieldNode = node.append_child( CA2W( fi.name ) ); |
| 33 | + |
| 34 | + size_t size = fi.fieldType->ArraySize( p ); |
| 35 | + for( size_t i = 0; i < size; i++ ) |
| 36 | + { |
| 37 | + void* pstr2 = fi.fieldType->ArrayElement( p, i ); |
| 38 | + DataToNode( fieldNode, pstr2, *arrayType ); |
| 39 | + } |
| 40 | + } //for each |
| 41 | +} //DataToNode |
| 42 | + |
| 43 | +// Helper class. |
| 44 | +struct xml_string_writer : xml_writer |
| 45 | +{ |
| 46 | + CStringA result; |
| 47 | + virtual void write( const void* data, size_t size ) |
| 48 | + { |
| 49 | + result += CStringA( (const char*)data, (int)size ); |
| 50 | + } |
| 51 | +}; |
| 52 | + |
| 53 | +CStringA ToXML_UTF8( void* pclass, CppTypeInfo& type ) |
| 54 | +{ |
| 55 | + xml_document doc; |
| 56 | + xml_node decl = doc.prepend_child( pugi::node_declaration ); |
| 57 | + decl.append_attribute( _T( "version" ) ) = _T( "1.0" ); |
| 58 | + decl.append_attribute( _T( "encoding" ) ) = _T( "utf-8" ); |
| 59 | + DataToNode( doc, pclass, type ); |
| 60 | + |
| 61 | + xml_string_writer writer; |
| 62 | + doc.save( writer ); |
| 63 | + return writer.result; |
| 64 | +} |
| 65 | + |
| 66 | +CStringW ToXML( void* pclass, CppTypeInfo& type ) |
| 67 | +{ |
| 68 | + xml_document doc; |
| 69 | + xml_node decl = doc.prepend_child( pugi::node_declaration ); |
| 70 | + decl.append_attribute( _T( "version" ) ) = _T( "1.0" ); |
| 71 | + decl.append_attribute( _T( "encoding" ) ) = _T( "utf-8" ); |
| 72 | + DataToNode( doc, pclass, type ); |
| 73 | + |
| 74 | + xml_string_writer writer; |
| 75 | + doc.save( writer ); |
| 76 | + return CStringW(CA2T(writer.result, CP_UTF8)); |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | +// |
| 81 | +// Deserializes xml to class structure, returns true if succeeded, false if fails. |
| 82 | +// error holds error information if any. |
| 83 | +// |
| 84 | +bool NodeToData( xml_node node, void* pclass, CppTypeInfo& type, CStringW& error ) |
| 85 | +{ |
| 86 | + CStringA name = node.name(); |
| 87 | + |
| 88 | + if( type.name != name ) |
| 89 | + { |
| 90 | + error.AppendFormat( _T( "Expected xml tag '%S', but found '%S' instead" ), type.name.GetBuffer(), name.GetBuffer() ); |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + for (FieldInfo fi: type.fields) |
| 95 | + { |
| 96 | + void* p = ((char*)pclass) + fi.offset; |
| 97 | + CppTypeInfo* arrayType = nullptr; |
| 98 | + |
| 99 | + if( !fi.fieldType->GetArrayElementType( arrayType ) ) |
| 100 | + { |
| 101 | + // Simple type, query from attribute value. |
| 102 | + xml_attribute attr = node.attribute( CA2W( fi.name ) ); |
| 103 | + fi.fieldType->FromString( p, attr.value() ); |
| 104 | + continue; |
| 105 | + } |
| 106 | + |
| 107 | + if( !arrayType ) |
| 108 | + continue; |
| 109 | + |
| 110 | + xml_node fieldNode = node.child( CA2W( fi.name ) ); |
| 111 | + if( fieldNode.empty() ) |
| 112 | + continue; |
| 113 | + |
| 114 | + int size = 0; |
| 115 | + for( auto it = fieldNode.children().begin(); it != fieldNode.children().end(); it++ ) |
| 116 | + size++; |
| 117 | + |
| 118 | + fi.fieldType->SetArraySize( p, size ); |
| 119 | + int i = 0; |
| 120 | + for( auto it = fieldNode.children().begin(); it != fieldNode.children().end(); it++ ) |
| 121 | + { |
| 122 | + void* pstr2 = fi.fieldType->ArrayElement( p, i ); |
| 123 | + if( !NodeToData( *it, pstr2, *arrayType, error ) ) |
| 124 | + return false; |
| 125 | + i++; |
| 126 | + } |
| 127 | + } //for each |
| 128 | + |
| 129 | + return true; |
| 130 | +} //NodeToData |
| 131 | + |
| 132 | +bool FromXml( void* pclass, CppTypeInfo& type, const wchar_t* xml, CStringW& error ) |
| 133 | +{ |
| 134 | + xml_document doc2; |
| 135 | + |
| 136 | + xml_parse_result res = doc2.load_string( xml ); |
| 137 | + if( !res ) |
| 138 | + { |
| 139 | + error = L"Failed to load xml: "; |
| 140 | + error += res.description(); |
| 141 | + return false; |
| 142 | + } |
| 143 | + |
| 144 | + return NodeToData( doc2.first_child(), pclass, type, error ); |
| 145 | +} |
| 146 | + |
0 commit comments