mirror of
https://github.com/paboyle/Grid.git
synced 2024-12-23 19:35:26 +00:00
new I/O interface
This commit is contained in:
parent
8709117aea
commit
af19118113
@ -1,4 +1,4 @@
|
||||
|
||||
HFILES=./algorithms/approx/bigfloat.h ./algorithms/approx/bigfloat_double.h ./algorithms/approx/Chebyshev.h ./algorithms/approx/MultiShiftFunction.h ./algorithms/approx/Remez.h ./algorithms/approx/Zolotarev.h ./algorithms/CoarsenedMatrix.h ./algorithms/iterative/AdefGeneric.h ./algorithms/iterative/ConjugateGradient.h ./algorithms/iterative/ConjugateGradientMultiShift.h ./algorithms/iterative/ConjugateResidual.h ./algorithms/iterative/ImplicitlyRestartedLanczos.h ./algorithms/iterative/MatrixUtils.h ./algorithms/iterative/NormalEquations.h ./algorithms/iterative/PrecConjugateResidual.h ./algorithms/iterative/PrecGeneralisedConjugateResidual.h ./algorithms/iterative/SchurRedBlack.h ./algorithms/LinearOperator.h ./algorithms/Preconditioner.h ./algorithms/SparseMatrix.h ./Algorithms.h ./AlignedAllocator.h ./cartesian/Cartesian_base.h ./cartesian/Cartesian_full.h ./cartesian/Cartesian_red_black.h ./Cartesian.h ./communicator/Communicator_base.h ./Communicator.h ./Config.h ./cshift/Cshift_common.h ./cshift/Cshift_mpi.h ./cshift/Cshift_none.h ./Cshift.h ./Grid.h ./Init.h ./lattice/Lattice_arith.h ./lattice/Lattice_base.h ./lattice/Lattice_comparison.h ./lattice/Lattice_comparison_utils.h ./lattice/Lattice_conformable.h ./lattice/Lattice_coordinate.h ./lattice/Lattice_ET.h ./lattice/Lattice_local.h ./lattice/Lattice_overload.h ./lattice/Lattice_peekpoke.h ./lattice/Lattice_reality.h ./lattice/Lattice_reduction.h ./lattice/Lattice_rng.h ./lattice/Lattice_trace.h ./lattice/Lattice_transfer.h ./lattice/Lattice_transpose.h ./lattice/Lattice_unary.h ./lattice/Lattice_where.h ./Lattice.h ./Log.h ./Old/Tensor_peek.h ./Old/Tensor_poke.h ./parallelIO/BinaryIO.h ./parallelIO/NerscIO.h ./pugixml/pugixml.h ./qcd/action/ActionBase.h ./qcd/action/ActionParams.h ./qcd/action/Actions.h ./qcd/action/fermion/CayleyFermion5D.h ./qcd/action/fermion/ContinuedFractionFermion5D.h ./qcd/action/fermion/DomainWallFermion.h ./qcd/action/fermion/FermionOperator.h ./qcd/action/fermion/FermionOperatorImpl.h ./qcd/action/fermion/g5HermitianLinop.h ./qcd/action/fermion/MobiusFermion.h ./qcd/action/fermion/MobiusZolotarevFermion.h ./qcd/action/fermion/OverlapWilsonCayleyTanhFermion.h ./qcd/action/fermion/OverlapWilsonCayleyZolotarevFermion.h ./qcd/action/fermion/OverlapWilsonContfracTanhFermion.h ./qcd/action/fermion/OverlapWilsonContfracZolotarevFermion.h ./qcd/action/fermion/OverlapWilsonPartialFractionTanhFermion.h ./qcd/action/fermion/OverlapWilsonPartialFractionZolotarevFermion.h ./qcd/action/fermion/PartialFractionFermion5D.h ./qcd/action/fermion/ScaledShamirFermion.h ./qcd/action/fermion/ShamirZolotarevFermion.h ./qcd/action/fermion/WilsonCompressor.h ./qcd/action/fermion/WilsonFermion.h ./qcd/action/fermion/WilsonFermion5D.h ./qcd/action/fermion/WilsonKernels.h ./qcd/action/gauge/WilsonGaugeAction.h ./qcd/action/pseudofermion/EvenOddSchurDifferentiable.h ./qcd/action/pseudofermion/OneFlavourEvenOddRational.h ./qcd/action/pseudofermion/OneFlavourEvenOddRationalRatio.h ./qcd/action/pseudofermion/OneFlavourRational.h ./qcd/action/pseudofermion/OneFlavourRationalRatio.h ./qcd/action/pseudofermion/TwoFlavour.h ./qcd/action/pseudofermion/TwoFlavourEvenOdd.h ./qcd/action/pseudofermion/TwoFlavourEvenOddRatio.h ./qcd/action/pseudofermion/TwoFlavourRatio.h ./qcd/hmc/HMC.h ./qcd/hmc/integrators/Integrator.h ./qcd/hmc/integrators/Integrator_algorithm.h ./qcd/QCD.h ./qcd/spin/Dirac.h ./qcd/spin/TwoSpinor.h ./qcd/utils/CovariantCshift.h ./qcd/utils/LinalgUtils.h ./qcd/utils/SpaceTimeGrid.h ./qcd/utils/SUn.h ./qcd/utils/WilsonLoops.h ./serialisation/BaseIO.h ./serialisation/BinaryIO.h ./serialisation/MacroMagic.h ./serialisation/Serialisation.h ./serialisation/TextIO.h ./serialisation/XmlIO.h ./simd/Grid_avx.h ./simd/Grid_avx512.h ./simd/Grid_empty.h ./simd/Grid_imci.h ./simd/Grid_neon.h ./simd/Grid_qpx.h ./simd/Grid_sse4.h ./simd/Grid_vector_types.h ./simd/Grid_vector_unops.h ./Simd.h ./stencil/Lebesgue.h ./Stencil.h ./tensors/Tensor_arith.h ./tensors/Tensor_arith_add.h ./tensors/Tensor_arith_mac.h ./tensors/Tensor_arith_mul.h ./tensors/Tensor_arith_scalar.h ./tensors/Tensor_arith_sub.h ./tensors/Tensor_class.h ./tensors/Tensor_determinant.h ./tensors/Tensor_exp.h ./tensors/Tensor_extract_merge.h ./tensors/Tensor_index.h ./tensors/Tensor_inner.h ./tensors/Tensor_logical.h ./tensors/Tensor_outer.h ./tensors/Tensor_reality.h ./tensors/Tensor_Ta.h ./tensors/Tensor_trace.h ./tensors/Tensor_traits.h ./tensors/Tensor_transpose.h ./tensors/Tensor_unary.h ./Tensors.h ./Threads.h ./Timer.h
|
||||
|
||||
CCFILES=./algorithms/approx/MultiShiftFunction.cc ./algorithms/approx/Remez.cc ./algorithms/approx/Zolotarev.cc ./Init.cc ./Log.cc ./pugixml/pugixml.cc ./qcd/action/fermion/CayleyFermion5D.cc ./qcd/action/fermion/ContinuedFractionFermion5D.cc ./qcd/action/fermion/PartialFractionFermion5D.cc ./qcd/action/fermion/WilsonFermion.cc ./qcd/action/fermion/WilsonFermion5D.cc ./qcd/action/fermion/WilsonKernels.cc ./qcd/action/fermion/WilsonKernelsHand.cc ./qcd/hmc/HMC.cc ./qcd/spin/Dirac.cc ./qcd/utils/SpaceTimeGrid.cc ./stencil/Lebesgue.cc ./stencil/Stencil_common.cc
|
||||
CCFILES=./algorithms/approx/MultiShiftFunction.cc ./algorithms/approx/Remez.cc ./algorithms/approx/Zolotarev.cc ./Init.cc ./Log.cc ./pugixml/pugixml.cc ./qcd/action/fermion/CayleyFermion5D.cc ./qcd/action/fermion/ContinuedFractionFermion5D.cc ./qcd/action/fermion/PartialFractionFermion5D.cc ./qcd/action/fermion/WilsonFermion.cc ./qcd/action/fermion/WilsonFermion5D.cc ./qcd/action/fermion/WilsonKernels.cc ./qcd/action/fermion/WilsonKernelsHand.cc ./qcd/hmc/HMC.cc ./qcd/spin/Dirac.cc ./qcd/utils/SpaceTimeGrid.cc ./serialisation/BinaryIO.cc ./serialisation/TextIO.cc ./serialisation/XmlIO.cc ./stencil/Lebesgue.cc ./stencil/Stencil_common.cc
|
||||
|
@ -1,49 +1,211 @@
|
||||
#ifndef GRID_SERIALISATION_ABSTRACT_READER_H
|
||||
#define GRID_SERIALISATION_ABSTRACT_READER_H
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace Grid {
|
||||
class Writer {
|
||||
public:
|
||||
|
||||
virtual ~Writer() {};
|
||||
|
||||
virtual void push(const std::string &s) = 0;
|
||||
virtual void pop(void) =0;
|
||||
virtual void write( const std::string& s,const std::string &output ) =0;
|
||||
virtual void write( const std::string& s,const int16_t output ) =0;
|
||||
virtual void write( const std::string& s,const uint16_t output ) =0;
|
||||
virtual void write( const std::string& s,const int32_t output ) =0;
|
||||
virtual void write( const std::string& s,const uint32_t output ) =0;
|
||||
virtual void write( const std::string& s,const int64_t output ) =0;
|
||||
virtual void write( const std::string& s,const uint64_t output ) =0;
|
||||
virtual void write( const std::string& s,const float output ) =0;
|
||||
virtual void write( const std::string& s,const double output ) =0;
|
||||
virtual void write( const std::string& s,const bool output ) =0;
|
||||
|
||||
};
|
||||
|
||||
class Reader {
|
||||
public:
|
||||
|
||||
|
||||
|
||||
virtual ~Reader() {};
|
||||
|
||||
virtual void read( const std::string& s,std::string &output ) =0;
|
||||
virtual void push(const std::string &s) =0;
|
||||
virtual void pop(void) = 0;
|
||||
|
||||
virtual void read( const std::string& s, int16_t &output ) =0;
|
||||
virtual void read( const std::string& s, uint16_t &output ) =0;
|
||||
virtual void read( const std::string& s, int32_t &output ) =0;
|
||||
virtual void read( const std::string& s, uint32_t &output ) =0;
|
||||
virtual void read( const std::string& s, int64_t &output ) =0;
|
||||
virtual void read( const std::string& s, uint64_t &output ) =0;
|
||||
virtual void read( const std::string& s, float &output ) =0;
|
||||
virtual void read( const std::string& s, double &output ) =0;
|
||||
virtual void read( const std::string& s, bool &output ) =0;
|
||||
class Serializable {};
|
||||
|
||||
// static polymorphism implemented using CRTP idiom
|
||||
|
||||
// Static abstract writer
|
||||
template <typename T>
|
||||
class Writer
|
||||
{
|
||||
public:
|
||||
Writer(void);
|
||||
virtual ~Writer(void) = default;
|
||||
void push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
|
||||
write(const std::string& s, const U &output);
|
||||
template <typename U>
|
||||
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
|
||||
write(const std::string& s, const U &output);
|
||||
private:
|
||||
T *upcast;
|
||||
};
|
||||
|
||||
// Static abstract reader
|
||||
template <typename T>
|
||||
class Reader
|
||||
{
|
||||
public:
|
||||
Reader(void);
|
||||
virtual ~Reader(void) = default;
|
||||
void push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
|
||||
read(const std::string& s, U &output);
|
||||
template <typename U>
|
||||
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
|
||||
read(const std::string& s, U &output);
|
||||
protected:
|
||||
template <typename U>
|
||||
void fromString(U &output, const std::string &s);
|
||||
private:
|
||||
T *upcast;
|
||||
};
|
||||
|
||||
// Generic writer interface
|
||||
template <typename T>
|
||||
inline void push(Writer<T> &w, const std::string &s)
|
||||
{
|
||||
w.push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void push(Writer<T> &w, const char *s)
|
||||
{
|
||||
w.push(std::string(s));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void pop(Writer<T> &w)
|
||||
{
|
||||
w.pop();
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
inline void write(Writer<T> &w, const std::string& s, const U &output)
|
||||
{
|
||||
w.write(s, output);
|
||||
}
|
||||
|
||||
// Generic reader interface
|
||||
template <typename T>
|
||||
inline void push(Reader<T> &r, const std::string &s)
|
||||
{
|
||||
r.push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void push(Reader<T> &r, const char *s)
|
||||
{
|
||||
r.push(std::string(s));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
inline void pop(Reader<T> &r)
|
||||
{
|
||||
r.pop();
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
inline void read(Reader<T> &r, const std::string &s, U &output)
|
||||
{
|
||||
r.read(s, output);
|
||||
}
|
||||
|
||||
template < class T >
|
||||
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
os << "[";
|
||||
for (auto &x: v)
|
||||
{
|
||||
os << x << " ";
|
||||
}
|
||||
if (v.size() > 0)
|
||||
{
|
||||
os << "\b";
|
||||
}
|
||||
os << "]";
|
||||
|
||||
return os;
|
||||
}
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Writer<T>::Writer(void)
|
||||
{
|
||||
upcast = static_cast<T *>(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Writer<T>::push(const std::string &s)
|
||||
{
|
||||
upcast->push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Writer<T>::pop(void)
|
||||
{
|
||||
upcast->pop();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
|
||||
Writer<T>::write(const std::string &s, const U &output)
|
||||
{
|
||||
U::write(*this, s, output);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
|
||||
Writer<T>::write(const std::string &s, const U &output)
|
||||
{
|
||||
upcast->writeDefault(s, output);
|
||||
}
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename T>
|
||||
Reader<T>::Reader(void)
|
||||
{
|
||||
upcast = static_cast<T *>(this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Reader<T>::push(const std::string &s)
|
||||
{
|
||||
upcast->push(s);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void Reader<T>::pop(void)
|
||||
{
|
||||
upcast->pop();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
|
||||
Reader<T>::read(const std::string &s, U &output)
|
||||
{
|
||||
U::read(*this, s, output);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
|
||||
Reader<T>::read(const std::string &s, U &output)
|
||||
{
|
||||
upcast->readDefault(s, output);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <typename U>
|
||||
void Reader<T>::fromString(U &output, const std::string &s)
|
||||
{
|
||||
std::istringstream is(s);
|
||||
|
||||
is.exceptions(std::ios::failbit);
|
||||
try
|
||||
{
|
||||
is >> std::boolalpha >> output;
|
||||
}
|
||||
catch(std::istringstream::failure &e)
|
||||
{
|
||||
std::cerr << "numerical conversion failure on '" << s << "' ";
|
||||
std::cerr << "(typeid: " << typeid(U).name() << ")" << std::endl;
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
36
lib/serialisation/BinaryIO.cc
Normal file
36
lib/serialisation/BinaryIO.cc
Normal file
@ -0,0 +1,36 @@
|
||||
#include <Grid.h>
|
||||
|
||||
using namespace Grid;
|
||||
using namespace std;
|
||||
|
||||
// Writer implementation ///////////////////////////////////////////////////////
|
||||
BinaryWriter::BinaryWriter(const string &fileName)
|
||||
: file_(fileName, ios::binary|ios::out)
|
||||
{}
|
||||
|
||||
template <>
|
||||
void BinaryWriter::writeDefault(const string &s, const string &output)
|
||||
{
|
||||
uint64_t sz = output.size();
|
||||
|
||||
write("", sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
write("", output[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reader implementation ///////////////////////////////////////////////////////
|
||||
BinaryReader::BinaryReader(const string &fileName)
|
||||
: file_(fileName, ios::binary|ios::in)
|
||||
{}
|
||||
|
||||
template <>
|
||||
void BinaryReader::readDefault(const string &s, string &output)
|
||||
{
|
||||
uint64_t sz;
|
||||
|
||||
read("", sz);
|
||||
output.reserve(sz);
|
||||
file_.read((char *)output.data(), sz);
|
||||
}
|
@ -9,101 +9,76 @@
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class BinaryWriter : public Writer {
|
||||
private:
|
||||
|
||||
std::ofstream file;
|
||||
|
||||
public:
|
||||
|
||||
BinaryWriter(const std::string &_file) : file(_file,std::ios::binary|std::ios::out) {}
|
||||
|
||||
~BinaryWriter() {}
|
||||
|
||||
// Binary is scopeless
|
||||
void push(const std::string &s) {}
|
||||
void pop(void) {}
|
||||
|
||||
void write(const std::string& s,const std::string &output) {
|
||||
uint32_t sz = output.size();
|
||||
write(s,sz);
|
||||
const char * cstr = output.c_str();
|
||||
for(int c=0;c<output.size();c++){
|
||||
write(s,cstr[c]);
|
||||
}
|
||||
class BinaryWriter: public Writer<BinaryWriter>
|
||||
{
|
||||
public:
|
||||
BinaryWriter(const std::string &fileName);
|
||||
virtual ~BinaryWriter(void) = default;
|
||||
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:
|
||||
std::ofstream file_;
|
||||
};
|
||||
void write( const std::string& s,const char output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const float output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const double output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
template<class T> void writeInternal( const std::string& s,const T output ){
|
||||
// FIXME --- htons, htonl, htno64 etc..
|
||||
file.write((char *)&output,sizeof(T));
|
||||
|
||||
class BinaryReader: public Reader<BinaryReader>
|
||||
{
|
||||
public:
|
||||
BinaryReader(const std::string &fileName);
|
||||
virtual ~BinaryReader(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:
|
||||
std::ifstream file_;
|
||||
};
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void BinaryWriter::writeDefault(const std::string &s, const U &x)
|
||||
{
|
||||
file_.write((char *)&x, sizeof(U));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class BinaryReader : public Reader{
|
||||
private:
|
||||
|
||||
std::ifstream file;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
BinaryReader(const std::string &_file) : file(_file,std::ios::binary|std::ios::in) {}
|
||||
|
||||
~BinaryReader() {}
|
||||
|
||||
// Binary is scopeless
|
||||
void push(const std::string &s) { }
|
||||
void pop(void) { }
|
||||
|
||||
void read( const std::string& s,std::string &output ) {
|
||||
|
||||
output.clear();
|
||||
|
||||
uint32_t sz;
|
||||
file.read((char *)&sz,sizeof(sz));
|
||||
|
||||
for(int c=0;c<sz;c++){
|
||||
char ch;
|
||||
file.read(&ch,sizeof(ch));
|
||||
output.push_back(ch);
|
||||
template <typename U>
|
||||
void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
||||
{
|
||||
uint64_t sz = x.size();
|
||||
|
||||
write("", sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
write("", x[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void BinaryReader::readDefault(const std::string &s, U &output)
|
||||
{
|
||||
file_.read((char *)&output, sizeof(U));
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||
{
|
||||
uint64_t sz;
|
||||
|
||||
read("", sz);
|
||||
output.resize(sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
read("", output[i]);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
file.read((char *)&output,sizeof(output)); // byte order??
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -111,8 +111,8 @@ THE SOFTWARE.
|
||||
#define GRID_MACRO_MEMBER(A,B) A B;
|
||||
|
||||
#define GRID_MACRO_OS_WRITE_MEMBER(A,B) os<< #A <<" "#B <<" = "<< obj. B <<" ; " <<std::endl;
|
||||
#define GRID_MACRO_READ_MEMBER(A,B) read(RD,#B,obj. B);
|
||||
#define GRID_MACRO_WRITE_MEMBER(A,B) write(WR,#B,obj. B);
|
||||
#define GRID_MACRO_READ_MEMBER(A,B) Grid::read(RD,#B,obj. B);
|
||||
#define GRID_MACRO_WRITE_MEMBER(A,B) Grid::write(WR,#B,obj. B);
|
||||
|
||||
#define GRID_DECL_CLASS_MEMBERS(cname,...) \
|
||||
\
|
||||
@ -120,14 +120,16 @@ THE SOFTWARE.
|
||||
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_MEMBER,__VA_ARGS__)) \
|
||||
\
|
||||
\
|
||||
friend void write(Writer &WR,const std::string &s, const cname &obj){ \
|
||||
template <typename T>\
|
||||
static void write(Writer<T> &WR,const std::string &s, const cname &obj){ \
|
||||
push(WR,s);\
|
||||
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_WRITE_MEMBER,__VA_ARGS__)) \
|
||||
pop(WR);\
|
||||
} \
|
||||
\
|
||||
\
|
||||
friend void read(Reader &RD,const std::string &s, cname &obj){ \
|
||||
template <typename T>\
|
||||
static void read(Reader<T> &RD,const std::string &s, cname &obj){ \
|
||||
push(RD,s);\
|
||||
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_READ_MEMBER,__VA_ARGS__)) \
|
||||
pop(RD);\
|
||||
|
@ -5,85 +5,6 @@
|
||||
#include <serialisation/BaseIO.h>
|
||||
#include <stdint.h>
|
||||
|
||||
namespace Grid {
|
||||
|
||||
|
||||
// Generic reader writer interface
|
||||
inline void push(Writer & WR,const std::string &s) { WR.push(s);}
|
||||
inline void push(Writer & WR,const char *s) { WR.push(std::string(s));}
|
||||
inline void pop (Writer & WR) { WR.pop();}
|
||||
|
||||
// inline void write(Writer& wr, const std::string& s,const char * output ) { wr.write(s,std::string(output)); };
|
||||
inline void write(Writer& wr, const std::string& s,const std::string &output) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const int16_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const uint16_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const int32_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const uint32_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const int64_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const uint64_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const float output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const double output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s,const bool output ) { wr.write(s,output); };
|
||||
|
||||
inline void push(Reader & WR,const std::string &s) { WR.push(s);}
|
||||
inline void push(Reader & WR,const char *s) { WR.push(std::string(s));}
|
||||
inline void pop (Reader & WR) { WR.pop();}
|
||||
|
||||
inline void read(Reader& rd, const std::string& s,std::string &output) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, int16_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, uint16_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, int32_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, uint32_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, int64_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, uint64_t &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, float &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, double &output ) { rd.read(s,output); };
|
||||
inline void read(Reader& rd, const std::string& s, bool &output ) { rd.read(s,output); };
|
||||
|
||||
|
||||
template<class T> void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
|
||||
push(wr,s);
|
||||
uint64_t sz =output.size();
|
||||
write(wr,"N",sz);
|
||||
for(int i=0;i<output.size();i++){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
write(wr,oss.str(),output[i]);
|
||||
}
|
||||
pop(wr);
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
void read(Reader& rd, const std::string& s,std::vector<T> &output ) {
|
||||
|
||||
push(rd,s);
|
||||
|
||||
uint64_t sz; read(rd,"N",sz);
|
||||
// skip the vector length
|
||||
T tmp;
|
||||
output.resize(0);
|
||||
for(int i=0;i<sz;i++){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
read(rd,oss.str(),tmp);
|
||||
output.push_back(tmp);
|
||||
}
|
||||
pop(rd);
|
||||
};
|
||||
|
||||
template < class T >
|
||||
inline std::ostream& operator << (std::ostream& os, const std::vector<T>& v)
|
||||
{
|
||||
os << "[";
|
||||
for (typename std::vector<T>::const_iterator ii = v.begin(); ii != v.end(); ++ii)
|
||||
{
|
||||
os << " " << *ii;
|
||||
}
|
||||
os << " ]";
|
||||
return os;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////
|
||||
// Todo:
|
||||
//////////////////////////////////////////
|
||||
@ -97,7 +18,7 @@ namespace Grid {
|
||||
// Select the default serialiser use ifdef's
|
||||
//////////////////////////////////////////
|
||||
namespace Grid {
|
||||
typedef XMLReader DefaultReader;
|
||||
typedef XMLWriter DefaultWriter;
|
||||
typedef XmlReader DefaultReader;
|
||||
typedef XmlWriter DefaultWriter;
|
||||
}
|
||||
#endif
|
||||
|
66
lib/serialisation/TextIO.cc
Normal file
66
lib/serialisation/TextIO.cc
Normal file
@ -0,0 +1,66 @@
|
||||
#include <Grid.h>
|
||||
|
||||
using namespace Grid;
|
||||
using namespace std;
|
||||
|
||||
// Writer implementation ///////////////////////////////////////////////////////
|
||||
TextWriter::TextWriter(const string &fileName)
|
||||
: file_(fileName, ios::out)
|
||||
{}
|
||||
|
||||
void TextWriter::push(const string &s)
|
||||
{
|
||||
level_++;
|
||||
};
|
||||
|
||||
void TextWriter::pop(void)
|
||||
{
|
||||
level_--;
|
||||
};
|
||||
|
||||
void TextWriter::indent(void)
|
||||
{
|
||||
for (int i = 0; i < level_; ++i)
|
||||
{
|
||||
file_ << '\t';
|
||||
}
|
||||
};
|
||||
|
||||
// Reader implementation ///////////////////////////////////////////////////////
|
||||
TextReader::TextReader(const string &fileName)
|
||||
: file_(fileName, ios::in)
|
||||
{}
|
||||
|
||||
void TextReader::push(const string &s)
|
||||
{
|
||||
level_++;
|
||||
};
|
||||
|
||||
void TextReader::pop(void)
|
||||
{
|
||||
level_--;
|
||||
};
|
||||
|
||||
void TextReader::checkIndent(void)
|
||||
{
|
||||
char c;
|
||||
|
||||
for (int i = 0; i < level_; ++i)
|
||||
{
|
||||
file_.get(c);
|
||||
if (c != '\t')
|
||||
{
|
||||
cerr << "mismatch on tab " << c << " level " << level_;
|
||||
cerr << " i "<< i <<endl;
|
||||
abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template <>
|
||||
void TextReader::readDefault(const string &s, string &output)
|
||||
{
|
||||
checkIndent();
|
||||
output.clear();
|
||||
getline(file_, output);
|
||||
}
|
@ -9,140 +9,88 @@
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class TextWriter : public Writer {
|
||||
private:
|
||||
|
||||
std::ofstream file;
|
||||
int level;
|
||||
void indent(void) {
|
||||
for(int i=0;i<level;i++){
|
||||
file <<"\t";
|
||||
}
|
||||
}
|
||||
public:
|
||||
|
||||
TextWriter(const std::string &_file) : file(_file,std::ios::out) {
|
||||
level=0;
|
||||
}
|
||||
|
||||
~TextWriter() { }
|
||||
|
||||
void push(const std::string &s)
|
||||
namespace Grid
|
||||
{
|
||||
|
||||
class TextWriter: public Writer<TextWriter>
|
||||
{
|
||||
// std::string tmp = s;
|
||||
// write(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) {
|
||||
level--;
|
||||
}
|
||||
|
||||
void write( const std::string& s,const std::string &output ) {
|
||||
indent();
|
||||
file<<output<<std::endl;
|
||||
public:
|
||||
TextWriter(const std::string &fileName);
|
||||
virtual ~TextWriter(void) = default;
|
||||
void push(const std::string &s);
|
||||
void pop(void);
|
||||
template <typename U>
|
||||
void writeDefault(const std::string &s, const U &output);
|
||||
template <typename U>
|
||||
void writeDefault(const std::string &s, const std::vector<U> &output);
|
||||
private:
|
||||
void indent(void);
|
||||
private:
|
||||
std::ofstream file_;
|
||||
int level_{0};
|
||||
};
|
||||
void write( const std::string& s,const int16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const float output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const double output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s,const T output ){
|
||||
|
||||
class TextReader: public Reader<TextReader>
|
||||
{
|
||||
public:
|
||||
TextReader(const std::string &fileName);
|
||||
virtual ~TextReader(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:
|
||||
void checkIndent(void);
|
||||
private:
|
||||
std::ifstream file_;
|
||||
int level_{0};
|
||||
};
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void TextWriter::writeDefault(const std::string &s, const U &output)
|
||||
{
|
||||
indent();
|
||||
file << std::boolalpha << output<<std::endl;
|
||||
file_ << std::boolalpha << output << std::endl;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class TextReader : public Reader {
|
||||
private:
|
||||
|
||||
std::ifstream file;
|
||||
int level;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
TextReader(const std::string &_file) : file(_file,std::ios::in) { level = 0;};
|
||||
|
||||
~TextReader() { }
|
||||
|
||||
void read( const std::string& s,std::string &output ) {
|
||||
char c='a';
|
||||
for(int i=0;i<level;i++){
|
||||
file.get(c);
|
||||
if ( c != '\t' )
|
||||
std::cout << "mismatch on tab "<<c<<" level "<< level<< " i "<< i<<std::endl;
|
||||
}
|
||||
output.clear();
|
||||
std::getline(file,output);
|
||||
};
|
||||
void push(const std::string &s) {
|
||||
// std::string tmp; read(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) { level--; }
|
||||
|
||||
template<class T>
|
||||
void read( const std::string& s, std::vector<T> &output ) {
|
||||
template <typename U>
|
||||
void TextWriter::writeDefault(const std::string &s, const std::vector<U> &output)
|
||||
{
|
||||
uint64_t sz = output.size();
|
||||
|
||||
push(s);
|
||||
|
||||
uint64_t n; read("N",n);
|
||||
|
||||
// skip the vector length
|
||||
T tmp;
|
||||
output.resize(0);
|
||||
for(int i=0;i<n;i++){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
read(*this,oss.str(),tmp);
|
||||
output.push_back(tmp);
|
||||
write(s, sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
write(s, output[i]);
|
||||
}
|
||||
|
||||
pop();
|
||||
|
||||
};
|
||||
|
||||
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
std::string asString;
|
||||
read(path,asString);
|
||||
convert(asString,output);
|
||||
}
|
||||
|
||||
template<class T> void convert(const std::string &asString,T &output)
|
||||
{
|
||||
std::istringstream is(asString); is.exceptions(std::ios::failbit);
|
||||
try {
|
||||
is >> std::boolalpha >> output;
|
||||
} catch(std::istringstream::failure e) {
|
||||
std::cerr << "XML read failure on "<<" "<<asString<<" "<<typeid(T).name()<<std::endl;
|
||||
}
|
||||
assert( is.tellg()==-1);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void TextReader::readDefault(const std::string &s, U &output)
|
||||
{
|
||||
std::string buf;
|
||||
|
||||
readDefault(s, buf);
|
||||
fromString(output, buf);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
void TextReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||
{
|
||||
uint64_t sz;
|
||||
|
||||
read("", sz);
|
||||
output.resize(sz);
|
||||
for (uint64_t i = 0; i < sz; ++i)
|
||||
{
|
||||
read("", output[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
59
lib/serialisation/XmlIO.cc
Normal file
59
lib/serialisation/XmlIO.cc
Normal file
@ -0,0 +1,59 @@
|
||||
#include <Grid.h>
|
||||
|
||||
using namespace Grid;
|
||||
using namespace std;
|
||||
|
||||
// Writer implementation ///////////////////////////////////////////////////////
|
||||
XmlWriter::XmlWriter(const string &fileName)
|
||||
: fileName_(fileName)
|
||||
{
|
||||
node_ = doc_.append_child();
|
||||
node_.set_name("grid");
|
||||
}
|
||||
|
||||
XmlWriter::~XmlWriter(void)
|
||||
{
|
||||
doc_.save_file(fileName_.c_str(), " ");
|
||||
}
|
||||
|
||||
void XmlWriter::push(const string &s)
|
||||
{
|
||||
node_ = node_.append_child(s.c_str());
|
||||
}
|
||||
|
||||
void XmlWriter::pop(void)
|
||||
{
|
||||
node_ = node_.parent();
|
||||
}
|
||||
|
||||
// Reader implementation ///////////////////////////////////////////////////////
|
||||
XmlReader::XmlReader(const string &fileName)
|
||||
: fileName_(fileName)
|
||||
{
|
||||
pugi::xml_parse_result result = doc_.load_file(fileName_.c_str());
|
||||
|
||||
if ( !result )
|
||||
{
|
||||
cerr << "XML error description: " << result.description() << "\n";
|
||||
cerr << "XML error offset : " << result.offset << "\n";
|
||||
abort();
|
||||
}
|
||||
|
||||
node_ = doc_.child("grid");
|
||||
}
|
||||
|
||||
void XmlReader::push(const string &s)
|
||||
{
|
||||
node_ = node_.child(s.c_str());
|
||||
}
|
||||
|
||||
void XmlReader::pop(void)
|
||||
{
|
||||
node_ = node_.parent();
|
||||
}
|
||||
|
||||
template <>
|
||||
void XmlReader::readDefault(const string &s, string &output)
|
||||
{
|
||||
output = node_.child(s.c_str()).first_child().value();
|
||||
}
|
@ -11,136 +11,93 @@
|
||||
|
||||
#include "pugixml/pugixml.h"
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class XMLWriter : public Writer {
|
||||
private:
|
||||
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_node node;
|
||||
std::string file;
|
||||
|
||||
public:
|
||||
|
||||
XMLWriter(const std::string &_file) : file(_file)
|
||||
namespace Grid
|
||||
{
|
||||
|
||||
class XmlWriter: public Writer<XmlWriter>
|
||||
{
|
||||
node=doc.append_child();
|
||||
node.set_name("document");
|
||||
}
|
||||
|
||||
~XMLWriter()
|
||||
{
|
||||
// simple_walker walker;
|
||||
// doc.traverse(walker);
|
||||
|
||||
doc.save_file(file.c_str()," ");
|
||||
}
|
||||
|
||||
void push(const std::string &s)
|
||||
{
|
||||
node = node.append_child(s.c_str());
|
||||
}
|
||||
void pop(void) {
|
||||
node = node.parent();
|
||||
}
|
||||
|
||||
void write( const std::string& s,const std::string &output ) {
|
||||
pugi::xml_node leaf=node.append_child(s.c_str());
|
||||
leaf.append_child(pugi::node_pcdata).set_value(output.c_str());
|
||||
|
||||
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_;
|
||||
};
|
||||
|
||||
void write( const std::string& s,const int16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint16_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint32_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const int64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const uint64_t output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const float output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const double output ) { writeInternal(s,output); };
|
||||
void write( const std::string& s,const bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s,const T output ){
|
||||
|
||||
class XmlReader: public Reader<XmlReader>
|
||||
{
|
||||
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_;
|
||||
};
|
||||
|
||||
// Writer template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void XmlWriter::writeDefault(const std::string &s, const U &x)
|
||||
{
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << output;
|
||||
write(s,os.str());
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class XMLReader : public Reader {
|
||||
private:
|
||||
|
||||
pugi::xml_document doc;
|
||||
pugi::xml_node node;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
XMLReader(const std::string &_file)
|
||||
template <typename U>
|
||||
void XmlWriter::writeDefault(const std::string &s, const std::vector<U> &x)
|
||||
{
|
||||
pugi::xml_parse_result result = doc.load_file(_file.c_str());
|
||||
|
||||
if ( !result ) {
|
||||
std::cout << "XML error description: " << result.description() << "\n";
|
||||
std::cout << "XML error offset: " << result.offset << "\n";
|
||||
push(s);
|
||||
for (auto &x_i: x)
|
||||
{
|
||||
write("elem", x_i);
|
||||
}
|
||||
|
||||
assert(result);
|
||||
|
||||
// simple_walker walker;
|
||||
// doc.traverse(walker);
|
||||
|
||||
node= doc.child("document");
|
||||
}
|
||||
|
||||
~XMLReader() { }
|
||||
|
||||
void read( const std::string& s,std::string &output ) {
|
||||
output=node.child(s.c_str()).first_child().value();
|
||||
};
|
||||
void push(const std::string &s)
|
||||
{
|
||||
node = node.child(s.c_str());
|
||||
}
|
||||
void pop(void) {
|
||||
node = node.parent();
|
||||
}
|
||||
|
||||
void read( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void read( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
std::string asString;
|
||||
read(path,asString);
|
||||
convert(asString,output);
|
||||
}
|
||||
|
||||
template<class T> void convert(const std::string &asString,T &output)
|
||||
{
|
||||
std::istringstream is(asString); is.exceptions(std::ios::failbit);
|
||||
try {
|
||||
is >> std::boolalpha >> output;
|
||||
} catch(std::istringstream::failure e) {
|
||||
std::cerr << "XML read failure on "<<" "<<asString<<" "<<typeid(T).name()<<std::endl;
|
||||
}
|
||||
assert( is.tellg()==-1);
|
||||
pop();
|
||||
}
|
||||
|
||||
// Reader template implementation ////////////////////////////////////////////
|
||||
template <typename U>
|
||||
void XmlReader::readDefault(const std::string &s, U &output)
|
||||
{
|
||||
std::string buf;
|
||||
|
||||
readDefault(s, buf);
|
||||
fromString(output, buf);
|
||||
}
|
||||
|
||||
template <typename U>
|
||||
void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
|
||||
{
|
||||
pugi::xml_node nodeCpy;
|
||||
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++;
|
||||
}
|
||||
pop();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
|
@ -1,36 +1,35 @@
|
||||
#include <Grid.h>
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class myclass {
|
||||
public:
|
||||
|
||||
GRID_DECL_CLASS_MEMBERS(myclass,
|
||||
int, x,
|
||||
double, y,
|
||||
bool , b,
|
||||
std::string, name,
|
||||
std::vector<double>, array,
|
||||
std::vector<std::vector<double> >, twodimarray,
|
||||
);
|
||||
|
||||
myclass(){}
|
||||
myclass(int i) : array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)) {
|
||||
x=i;
|
||||
y=2*i;
|
||||
b=true;
|
||||
name="bother said pooh";
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class myclass: Serializable {
|
||||
public:
|
||||
|
||||
GRID_DECL_CLASS_MEMBERS(myclass,
|
||||
int, x,
|
||||
double, y,
|
||||
bool , b,
|
||||
std::string, name,
|
||||
std::vector<double>, array,
|
||||
std::vector<std::vector<double>>, twodimarray,
|
||||
);
|
||||
|
||||
myclass() {}
|
||||
myclass(int i)
|
||||
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456))
|
||||
{
|
||||
x=i;
|
||||
y=2*i;
|
||||
b=true;
|
||||
name="bother said pooh";
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
int16_t i16 = 1;
|
||||
int16_t i16 = 1;
|
||||
uint16_t u16 = 2;
|
||||
int32_t i32 = 3;
|
||||
int32_t i32 = 3;
|
||||
uint32_t u32 = 4;
|
||||
int64_t i64 = 5;
|
||||
int64_t i64 = 5;
|
||||
uint64_t u64 = 6;
|
||||
float f = M_PI;
|
||||
double d = 2*M_PI;
|
||||
@ -41,8 +40,9 @@ using namespace Grid;
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
{
|
||||
XMLWriter WR("bother.xml");
|
||||
|
||||
XmlWriter WR("bother.xml");
|
||||
|
||||
// test basic type writing
|
||||
push(WR,"BasicTypes");
|
||||
write(WR,std::string("i16"),i16);
|
||||
write(WR,"u16",u16);
|
||||
@ -54,40 +54,54 @@ int main(int argc,char **argv)
|
||||
write(WR,"d",d);
|
||||
write(WR,"b",b);
|
||||
pop(WR);
|
||||
|
||||
|
||||
// test serializable class writing
|
||||
myclass obj(1234); // non-trivial constructor
|
||||
write(WR,"obj",obj);
|
||||
|
||||
WR.write("obj2", obj);
|
||||
|
||||
std::vector<myclass> vec;
|
||||
vec.push_back(myclass(1234));
|
||||
vec.push_back(myclass(5678));
|
||||
vec.push_back(myclass(3838));
|
||||
write(WR, "objvec", vec);
|
||||
};
|
||||
|
||||
XMLReader RD("bother.xml");
|
||||
|
||||
myclass copy1;
|
||||
myclass copy2;
|
||||
myclass copy3;
|
||||
|
||||
read(RD,"obj",copy1);
|
||||
std::cout << "Loaded " << copy1<<std::endl;
|
||||
|
||||
|
||||
// read tests
|
||||
myclass copy1, copy2, copy3;
|
||||
std::vector<myclass> veccopy1, veccopy2, veccopy3;
|
||||
//// XML
|
||||
{
|
||||
XmlReader RD("bother.xml");
|
||||
read(RD,"obj",copy1);
|
||||
read(RD,"objvec", veccopy1);
|
||||
std::cout << "Loaded (XML) -----------------" << std::endl;
|
||||
std::cout << copy1 << std::endl << veccopy1 << std::endl;
|
||||
}
|
||||
//// binary
|
||||
{
|
||||
BinaryWriter BWR("bother.bin");
|
||||
write(BWR,"discard",copy1 );
|
||||
write(BWR,"discard",veccopy1 );
|
||||
}
|
||||
{
|
||||
{
|
||||
BinaryReader BRD("bother.bin");
|
||||
read (BRD,"discard",copy2 );
|
||||
std::cout<<copy2<<std::endl;
|
||||
read (BRD,"discard",veccopy2 );
|
||||
std::cout << "Loaded (bin) -----------------" << std::endl;
|
||||
std::cout << copy2 << std::endl << veccopy2 << std::endl;
|
||||
}
|
||||
|
||||
|
||||
//// text
|
||||
{
|
||||
TextWriter TWR("bother.txt");
|
||||
write(TWR,"discard",copy1 );
|
||||
write(TWR,"discard",veccopy1 );
|
||||
}
|
||||
{
|
||||
{
|
||||
TextReader TRD("bother.txt");
|
||||
read (TRD,"discard",copy3 );
|
||||
std::cout<<copy3<<std::endl;
|
||||
read (TRD,"discard",veccopy3 );
|
||||
std::cout << "Loaded (txt) -----------------" << std::endl;
|
||||
std::cout << copy3 << std::endl << veccopy3 << std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user