mirror of
https://github.com/paboyle/Grid.git
synced 2025-06-16 23:07:05 +01:00
Separated IO reader/writers into a proper abstract base,
derived relationship. Have Text/Binary/Xml versions of Reader & Writer. Any new Reader/Writer class inheriting the interface can give object serialisation to any desired format now. new file: lib/serialisation/BaseIO.h modified: lib/serialisation/BinaryIO.h modified: lib/serialisation/Serialisation.h modified: lib/serialisation/TextIO.h modified: lib/serialisation/XmlIO.h The test uses the Xml, Binary and Text formats as well as cout << Object.
This commit is contained in:
@ -2,40 +2,45 @@
|
||||
#define GRID_SERIALISATION_READER_H
|
||||
|
||||
#include <serialisation/MacroMagic.h>
|
||||
#include <serialisation/BaseIO.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();}
|
||||
|
||||
template< class Writer> void push(Writer & WR,const std::string &s) { WR.push(s);}
|
||||
template< class Writer> void push(Writer & WR,const char *s) { WR.push(std::string(s));}
|
||||
template< class Writer> 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, int16_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, uint16_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, int32_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, uint32_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, int64_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, uint64_t output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, float output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, double output ) { wr.write(s,output); };
|
||||
inline void write(Writer& wr, const std::string& s, bool output ) { wr.write(s,output); };
|
||||
|
||||
template< class Writer> void write(Writer& wr, const std::string& s,const char * output ) { wr.iwrite(s,std::string(output)); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s,const std::string &output) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, int16_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, uint16_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, int32_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, uint32_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, int64_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, uint64_t output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, float output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, double output ) { wr.iwrite(s,output); };
|
||||
template< class Writer> void write(Writer& wr, const std::string& s, bool output ) { wr.iwrite(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();}
|
||||
|
||||
template< class Reader> void read(Reader& rd, const std::string& s,std::string &output) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, int16_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, uint16_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, int32_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, uint32_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, int64_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, uint64_t &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, float &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, double &output ) { rd.iread(s,output); };
|
||||
template< class Reader> void read(Reader& rd, const std::string& s, bool &output ) { rd.iread(s,output); };
|
||||
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 Writer, class T> void write(Writer& wr, const std::string& s,const std::vector<T> 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);
|
||||
@ -45,14 +50,28 @@ namespace Grid {
|
||||
}
|
||||
pop(wr);
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class Reader, class T>
|
||||
void read(Reader& rd, const std::string& s,std::vector<T> &output ) {
|
||||
rd.iread(s,output);
|
||||
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)
|
||||
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)
|
||||
@ -77,7 +96,7 @@ namespace Grid {
|
||||
// Select the default serialiser use ifdef's
|
||||
//////////////////////////////////////////
|
||||
namespace Grid {
|
||||
typedef XMLReader Reader;
|
||||
typedef XMLWriter Writer;
|
||||
typedef XMLReader DefaultReader;
|
||||
typedef XMLWriter DefaultWriter;
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user