mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-10 07:55:35 +00:00
Text and Binary readers
This commit is contained in:
parent
091785e5f5
commit
35818fdf6c
@ -30,7 +30,7 @@
|
||||
///////////////////
|
||||
// Grid headers
|
||||
///////////////////
|
||||
#include <Serialisation.h>
|
||||
#include <serialisation/Serialisation.h>
|
||||
#include <Config.h>
|
||||
#include <Timer.h>
|
||||
#include <Log.h>
|
||||
|
@ -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/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/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/MacroMagic.h ./serialisation/Reader.h ./serialisation/XMLReader.h ./simd/Grid_avx.h ./simd/Grid_avx512.h ./simd/Grid_empty.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
|
||||
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/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/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/BinaryIO.h ./serialisation/MacroMagic.h ./serialisation/Serialisation.h ./serialisation/XmlIO.h ./simd/Grid_avx.h ./simd/Grid_avx512.h ./simd/Grid_empty.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/hmc/integrators/Integrator.cc ./qcd/spin/Dirac.cc ./qcd/utils/SpaceTimeGrid.cc ./stencil/Lebesgue.cc ./stencil/Stencil_common.cc
|
||||
|
@ -1,6 +0,0 @@
|
||||
#ifndef GRID_SERIALISATION_H
|
||||
#define GRID_SERIALISATION_H
|
||||
|
||||
#include <serialisation/Reader.h> // subdir aggregate
|
||||
|
||||
#endif
|
@ -441,7 +441,7 @@ inline void readNerscObject(Lattice<vobj> &Umu,std::string file,munger munge,int
|
||||
|
||||
template<class vobj,class sobj,class fobj,class munger>
|
||||
inline void writeNerscObject(Lattice<vobj> &Umu,std::string file,munger munge,int offset,
|
||||
int sequence,double lt,double pl)
|
||||
int sequence,double lt,double pl)
|
||||
{
|
||||
GridBase *grid = Umu._grid;
|
||||
NerscField header;
|
||||
|
124
lib/serialisation/BinaryIO.h
Normal file
124
lib/serialisation/BinaryIO.h
Normal file
@ -0,0 +1,124 @@
|
||||
#ifndef GRID_SERIALISATION_BINARY_READER_H
|
||||
#define GRID_SERIALISATION_BINARY_READER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class BinaryWriter {
|
||||
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 iwrite(const std::string& s,const std::string &output) {
|
||||
uint32_t sz = output.size();
|
||||
iwrite(s,sz);
|
||||
const char * cstr = output.c_str();
|
||||
for(int c=0;c<output.size();c++){
|
||||
iwrite(s,cstr[c]);
|
||||
}
|
||||
};
|
||||
void iwrite( const std::string& s, char output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
// FIXME --- htons, htonl, htno64 etc..
|
||||
file.write((char *)&output,sizeof(T));
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
class BinaryReader {
|
||||
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 iread( 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<class T> void iread( const std::string& s, std::vector<T> &output ) {
|
||||
|
||||
T tmp;
|
||||
uint64_t n;
|
||||
|
||||
iread("N",n);
|
||||
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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void iread( 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
|
@ -35,9 +35,10 @@ namespace Grid {
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, bool &output ) { rd.iread(s,output); };
|
||||
|
||||
|
||||
template<class Writer, class T>
|
||||
void write(Writer& wr, const std::string& s,const std::vector<T> output ) {
|
||||
template<class Writer, 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]);
|
||||
@ -66,21 +67,17 @@ namespace Grid {
|
||||
//////////////////////////////////////////
|
||||
// Todo:
|
||||
//////////////////////////////////////////
|
||||
//#include <serialisation/CoutReader.h>
|
||||
//#include <serialisation/TextReader.h>
|
||||
//#include <serialisation/JSONReader.h>
|
||||
//#include <serialisation/YAMLReader.h>
|
||||
|
||||
#include <serialisation/XMLReader.h>
|
||||
#include <serialisation/BinaryIO.h>
|
||||
#include <serialisation/TextIO.h>
|
||||
//#include <serialisation/JsonIO.h>
|
||||
//#include <serialisation/YamlIO.h>
|
||||
#include <serialisation/XmlIO.h>
|
||||
|
||||
//////////////////////////////////////////
|
||||
// Select the default serialiser
|
||||
// Select the default serialiser use ifdef's
|
||||
//////////////////////////////////////////
|
||||
namespace Grid {
|
||||
|
||||
using XMLPolicy::Reader;
|
||||
using XMLPolicy::Writer;
|
||||
|
||||
typedef XMLReader Reader;
|
||||
typedef XMLWriter Writer;
|
||||
}
|
||||
|
||||
#endif
|
147
lib/serialisation/TextIO.h
Normal file
147
lib/serialisation/TextIO.h
Normal file
@ -0,0 +1,147 @@
|
||||
#ifndef GRID_SERIALISATION_TEXT_READER_H
|
||||
#define GRID_SERIALISATION_TEXT_READER_H
|
||||
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
#include <math.h>
|
||||
#include <vector>
|
||||
#include <cassert>
|
||||
|
||||
namespace Grid {
|
||||
|
||||
class TextWriter {
|
||||
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)
|
||||
{
|
||||
// std::string tmp = s;
|
||||
// iwrite(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) {
|
||||
level--;
|
||||
}
|
||||
|
||||
void iwrite( const std::string& s,const std::string &output ) {
|
||||
indent();
|
||||
file<<output<<std::endl;
|
||||
};
|
||||
void iwrite( const std::string& s, int16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint16_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint32_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, int64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, uint64_t output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, float output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, double output ) { writeInternal(s,output); };
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
indent();
|
||||
file << std::boolalpha << output<<std::endl;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
class TextReader {
|
||||
private:
|
||||
|
||||
std::ifstream file;
|
||||
int level;
|
||||
|
||||
public:
|
||||
|
||||
|
||||
TextReader(const std::string &_file) : file(_file,std::ios::in) { level = 0;};
|
||||
|
||||
~TextReader() { }
|
||||
|
||||
void iread( 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; iread(s,tmp);
|
||||
level++;
|
||||
}
|
||||
void pop(void) { level--; }
|
||||
|
||||
template<class T>
|
||||
void iread( const std::string& s, std::vector<T> &output ) {
|
||||
|
||||
push(s);
|
||||
|
||||
uint64_t n; iread("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);
|
||||
}
|
||||
|
||||
pop();
|
||||
|
||||
};
|
||||
|
||||
void iread( const std::string& s, int16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint16_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint32_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, int64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, uint64_t &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, float &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, double &output ) { readInternal(s,output); };
|
||||
void iread( const std::string& s, bool &output ) { readInternal(s,output); };
|
||||
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void readInternal( const std::string& path, T &output ){
|
||||
std::string asString;
|
||||
iread(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);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
@ -60,6 +60,7 @@ public:
|
||||
void iwrite( const std::string& s, bool output ) { writeInternal(s,output); };
|
||||
|
||||
private:
|
||||
|
||||
template<class T> void writeInternal( const std::string& s, T output ){
|
||||
std::ostringstream os;
|
||||
os << std::boolalpha << output;
|
||||
@ -95,9 +96,7 @@ public:
|
||||
node= doc.child("document");
|
||||
}
|
||||
|
||||
~XMLReader()
|
||||
{
|
||||
}
|
||||
~XMLReader() { }
|
||||
|
||||
void iread( const std::string& s,std::string &output ) {
|
||||
output=node.child(s.c_str()).first_child().value();
|
||||
@ -112,17 +111,25 @@ public:
|
||||
|
||||
template<class T>
|
||||
void iread( const std::string& s, std::vector<T> &output ) {
|
||||
output.resize(0);
|
||||
|
||||
T tmp;
|
||||
push(s);
|
||||
|
||||
uint64_t n;
|
||||
|
||||
pugi::xml_node it=node.first_child();
|
||||
|
||||
// skip the vector length
|
||||
T tmp;
|
||||
int i=0;
|
||||
for(pugi::xml_node it=node.first_child(); it; it = it.next_sibling() ){
|
||||
output.resize(0);
|
||||
for(it = it.next_sibling(); it; it = it.next_sibling() ){
|
||||
std::ostringstream oss; oss << "elem" << i;
|
||||
read(*this,oss.str(),tmp);
|
||||
output.push_back(tmp);
|
||||
i++;
|
||||
}
|
||||
|
||||
assert(i == n );
|
||||
pop();
|
||||
|
||||
};
|
||||
@ -159,10 +166,5 @@ private:
|
||||
|
||||
};
|
||||
|
||||
namespace XMLPolicy
|
||||
{
|
||||
typedef XMLReader Reader;
|
||||
typedef XMLWriter Writer;
|
||||
};
|
||||
}
|
||||
#endif
|
@ -14,10 +14,11 @@ public:
|
||||
std::vector<std::vector<double> >, twodimarray,
|
||||
);
|
||||
|
||||
myclass() : array(4,5.0), twodimarray(3,std::vector<double>(2,2.0)) {
|
||||
x=1;
|
||||
y=2;
|
||||
b=false;
|
||||
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";
|
||||
}
|
||||
|
||||
@ -51,15 +52,39 @@ int main(int argc,char **argv)
|
||||
write(WR,"b",b);
|
||||
pop(WR);
|
||||
|
||||
myclass obj;
|
||||
myclass obj(1234); // non-trivial constructor
|
||||
write(WR,"obj",obj);
|
||||
|
||||
};
|
||||
|
||||
Reader RD("bother2.xml");
|
||||
Reader RD("bother.xml");
|
||||
|
||||
myclass copy;
|
||||
myclass copy1;
|
||||
myclass copy2;
|
||||
myclass copy3;
|
||||
|
||||
read(RD,"obj",copy);
|
||||
std::cout << "Loaded " << copy<<std::endl;
|
||||
read(RD,"obj",copy1);
|
||||
std::cout << "Loaded " << copy1<<std::endl;
|
||||
|
||||
{
|
||||
BinaryWriter BWR("bother.bin");
|
||||
write(BWR,"discard",copy1 );
|
||||
}
|
||||
{
|
||||
BinaryReader BRD("bother.bin");
|
||||
read (BRD,"discard",copy2 );
|
||||
std::cout<<copy2<<std::endl;
|
||||
}
|
||||
|
||||
|
||||
{
|
||||
TextWriter TWR("bother.txt");
|
||||
write(TWR,"discard",copy1 );
|
||||
}
|
||||
{
|
||||
TextReader TRD("bother.txt");
|
||||
read (TRD,"discard",copy3 );
|
||||
std::cout<<copy3<<std::endl;
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user