30#ifndef GRID_SERIALISATION_XML_READER_H
31#define GRID_SERIALISATION_XML_READER_H
41#include <Grid/pugixml/pugixml.h>
46 void xmlCheckParse(
const pugi::xml_parse_result &result,
const std::string name);
51 XmlWriter(
const std::string &fileName,std::string toplev = std::string(
"grid") );
53 void push(
const std::string &s);
59 void writeDefault(
const std::string &s,
const std::vector<U> &x);
61 void writeMultiDim(
const std::string &s,
const std::vector<size_t> & Dimensions,
const U * pDataRowMajor,
size_t NumElements);
74 XmlReader(
const std::string &fileName,
const bool isBuffer =
false,
75 std::string toplev = std::string(
"grid") );
77 bool push(
const std::string &s =
"");
82 template <
typename U>
void readDefault(
const std::string &s, std::vector<U> &output);
84 void readMultiDim(
const std::string &s, std::vector<U> &buf, std::vector<size_t> &dim);
88 void checkParse(
const pugi::xml_parse_result &result,
const std::string name);
107 template <
typename U>
110 std::ostringstream os;
118 os << std::scientific;
120 os << std::boolalpha << x;
121 pugi::xml_node leaf =
node_.append_child(s.c_str());
122 leaf.append_child(pugi::node_pcdata).set_value(os.str().c_str());
125 template <
typename U>
136 template <
typename U>
137 void XmlWriter::writeMultiDim(
const std::string &s,
const std::vector<size_t> & Dimensions,
const U * pDataRowMajor,
size_t NumElements)
141 const int Rank =
static_cast<int>( Dimensions.size() );
142 write(
"rank", Rank );
143 std::vector<size_t> MyIndex( Rank );
144 for(
auto d : Dimensions ) {
148 assert( count == NumElements &&
"XmlIO : element count doesn't match dimensions" );
149 static const char sName[] =
"tensor";
150 for(
int i = 0 ; i < Rank ; i++ ) {
154 while (NumElements--) {
155 write(
"elem", *pDataRowMajor++);
157 for( i = Rank - 1 ; i != -1 && ++MyIndex[i] == Dimensions[i] ; i-- )
159 int Rollover = Rank - 1 - i;
160 for( i = 0 ; i < Rollover ; i++ )
162 for( i = 0 ; NumElements && i < Rollover ; i++ )
170 template <
typename U>
179 template <
typename U>
184 std::cout <<
GridLogWarning <<
"XML: cannot open node '" << s <<
"'";
185 std::cout << std::endl;
187 for(
unsigned int i = 0;
node_.child(
"elem"); )
189 output.resize(i + 1);
190 read(
"elem", output[i++]);
191 node_.child(
"elem").set_name(
"elem-done");
197 template <
typename U>
202 std::cout <<
GridLogWarning <<
"XML: cannot open node '" << s <<
"'";
203 std::cout << std::endl;
205 static const char sName[] =
"tensor";
206 static const char sNameDone[] =
"tensor-done";
210 size_t NumElements = 1;
214 node_.child(
"dim").set_name(
"dim-done");
217 buf.resize( NumElements );
218 std::vector<size_t> MyIndex( Rank );
219 for(
int i = 0 ; i < Rank ; i++ ) {
228 node_.child(
"elem").set_name(
"elem-done");
230 for( i = Rank - 1 ; i != -1 && ++MyIndex[i] == dim[i] ; i-- )
232 int Rollover = Rank - 1 - i;
233 for( i = 0 ; i < Rollover ; i++ ) {
234 node_.set_name(sNameDone);
237 for( i = 0 ; NumElements && i < Rollover ; i++ )
GridLogger GridLogWarning(1, "Warning", GridLogColours, "YELLOW")
static INTERNAL_PRECISION U
void fromString(U &output, const std::string &s)
std::enable_if< std::is_base_of< Serializable, U >::value, void >::type read(const std::string &s, U &output)
std::enable_if< std::is_base_of< Serializable, U >::value >::type write(const std::string &s, const U &output)
unsigned int getPrecision(void)
XmlReader(const std::string &fileName, const bool isBuffer=false, std::string toplev=std::string("grid"))
bool push(const std::string &s="")
void readCurrentSubtree(std::string &s)
void readDefault(const std::string &s, U &output)
const std::string indent_
void readMultiDim(const std::string &s, std::vector< U > &buf, std::vector< size_t > &dim)
void checkParse(const pugi::xml_parse_result &result, const std::string name)
bool nextElement(const std::string &s="")
virtual ~XmlReader(void)=default
std::string docString(void)
void writeDefault(const std::string &s, const U &x)
XmlWriter(const std::string &fileName, std::string toplev=std::string("grid"))
void writeMultiDim(const std::string &s, const std::vector< size_t > &Dimensions, const U *pDataRowMajor, size_t NumElements)
void pushXmlString(const std::string &s)
void push(const std::string &s)
const std::string indent_
void xmlCheckParse(const pugi::xml_parse_result &result, const std::string name)