1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00
Grid/lib/serialisation/XmlIO.h

103 lines
2.4 KiB
C
Raw Normal View History

#ifndef GRID_SERIALISATION_XML_READER_H
#define GRID_SERIALISATION_XML_READER_H
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <math.h>
#include <vector>
#include <cassert>
#include "pugixml/pugixml.h"
2015-11-16 18:14:37 +00:00
namespace Grid
{
class XmlWriter: public Writer<XmlWriter>
{
2015-11-16 18:14:37 +00:00
public:
XmlWriter(const std::string &fileName);
virtual ~XmlWriter(void);
void push(const std::string &s);
void pop(void);
template <typename U>
void writeDefault(const std::string &s, const U &x);
template <typename U>
void writeDefault(const std::string &s, const std::vector<U> &x);
private:
pugi::xml_document doc_;
pugi::xml_node node_;
std::string fileName_;
};
class XmlReader: public Reader<XmlReader>
{
2015-11-16 18:14:37 +00:00
public:
XmlReader(const std::string &fileName);
virtual ~XmlReader(void) = default;
void push(const std::string &s);
void pop(void);
template <typename U>
void readDefault(const std::string &s, U &output);
template <typename U>
void readDefault(const std::string &s, std::vector<U> &output);
private:
pugi::xml_document doc_;
pugi::xml_node node_;
std::string fileName_;
};
2015-11-16 18:14:37 +00:00
// Writer template implementation ////////////////////////////////////////////
template <typename U>
void XmlWriter::writeDefault(const std::string &s, const U &x)
{
std::ostringstream os;
2015-11-16 18:14:37 +00:00
os << std::boolalpha << x;
pugi::xml_node leaf = node_.append_child(s.c_str());
leaf.append_child(pugi::node_pcdata).set_value(os.str().c_str());
}
2015-11-16 18:14:37 +00:00
template <typename U>
void XmlWriter::writeDefault(const std::string &s, const std::vector<U> &x)
{
2015-11-16 18:14:37 +00:00
push(s);
for (auto &x_i: x)
{
write("elem", x_i);
}
2015-11-16 18:14:37 +00:00
pop();
}
2015-11-16 18:14:37 +00:00
// Reader template implementation ////////////////////////////////////////////
template <typename U>
void XmlReader::readDefault(const std::string &s, U &output)
{
2015-11-16 18:14:37 +00:00
std::string buf;
readDefault(s, buf);
fromString(output, buf);
}
2015-11-16 18:14:37 +00:00
template <typename U>
void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
{
2015-11-16 18:14:37 +00:00
std::string buf;
unsigned int i = 0;
push(s);
while (node_.child("elem"))
{
output.resize(i + 1);
read("elem", output[i]);
node_.child("elem").set_name("elem-done");
i++;
}
2015-11-16 18:14:37 +00:00
pop();
}
}
#endif