/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/serialisation/BaseIO.h Copyright (C) 2015 Author: Antonin Portelli Author: Peter Boyle Author: Guido Cossu This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. See the full license in the file "LICENSE" in the top level distribution directory *************************************************************************************/ /* END LEGAL */ #ifndef GRID_SERIALISATION_ABSTRACT_READER_H #define GRID_SERIALISATION_ABSTRACT_READER_H #include #include #include namespace Grid { // Abstract writer/reader classes //////////////////////////////////////////// // static polymorphism implemented using CRTP idiom class Serializable; // Static abstract writer template class Writer { public: Writer(void); virtual ~Writer(void) = default; void push(const std::string &s); void pop(void); template typename std::enable_if::value, void>::type write(const std::string& s, const U &output); template typename std::enable_if::value, void>::type write(const std::string& s, const U &output); template void write(const std::string &s, const iScalar &output); template void write(const std::string &s, const iVector &output); template void write(const std::string &s, const iMatrix &output); void scientificFormat(const bool set); bool isScientific(void); void setPrecision(const unsigned int prec); unsigned int getPrecision(void); private: T *upcast; bool scientific_{false}; unsigned int prec_{0}; }; // Static abstract reader template class Reader { public: Reader(void); virtual ~Reader(void) = default; bool push(const std::string &s); void pop(void); template typename std::enable_if::value, void>::type read(const std::string& s, U &output); template typename std::enable_if::value, void>::type read(const std::string& s, U &output); template void read(const std::string &s, iScalar &output); template void read(const std::string &s, iVector &output); template void read(const std::string &s, iMatrix &output); protected: template void fromString(U &output, const std::string &s); private: T *upcast; }; // What is the vtype template struct isReader { static const bool value = false; }; template struct isWriter { static const bool value = false; }; // Writer template implementation template Writer::Writer(void) { upcast = static_cast(this); } template void Writer::push(const std::string &s) { upcast->push(s); } template void Writer::pop(void) { upcast->pop(); } template template typename std::enable_if::value, void>::type Writer::write(const std::string &s, const U &output) { U::write(*this, s, output); } template template typename std::enable_if::value, void>::type Writer::write(const std::string &s, const U &output) { upcast->writeDefault(s, output); } template template void Writer::write(const std::string &s, const iScalar &output) { upcast->writeDefault(s, tensorToVec(output)); } template template void Writer::write(const std::string &s, const iVector &output) { upcast->writeDefault(s, tensorToVec(output)); } template template void Writer::write(const std::string &s, const iMatrix &output) { upcast->writeDefault(s, tensorToVec(output)); } template void Writer::scientificFormat(const bool set) { scientific_ = set; } template bool Writer::isScientific(void) { return scientific_; } template void Writer::setPrecision(const unsigned int prec) { prec_ = prec; } template unsigned int Writer::getPrecision(void) { return prec_; } // Reader template implementation template Reader::Reader(void) { upcast = static_cast(this); } template bool Reader::push(const std::string &s) { return upcast->push(s); } template void Reader::pop(void) { upcast->pop(); } template template typename std::enable_if::value, void>::type Reader::read(const std::string &s, U &output) { U::read(*this, s, output); } template template typename std::enable_if::value, void>::type Reader::read(const std::string &s, U &output) { upcast->readDefault(s, output); } template template void Reader::read(const std::string &s, iScalar &output) { typename TensorToVec>::type v; upcast->readDefault(s, v); vecToTensor(output, v); } template template void Reader::read(const std::string &s, iVector &output) { typename TensorToVec>::type v; upcast->readDefault(s, v); vecToTensor(output, v); } template template void Reader::read(const std::string &s, iMatrix &output) { typename TensorToVec>::type v; upcast->readDefault(s, v); vecToTensor(output, v); } template template void Reader::fromString(U &output, const std::string &s) { std::istringstream is(s); is.exceptions(std::ios::failbit); try { is >> std::boolalpha >> output; } catch(std::ios_base::failure &e) { std::cerr << "numerical conversion failure on '" << s << "' "; std::cerr << "(typeid: " << typeid(U).name() << ")" << std::endl; abort(); } } // serializable base class /////////////////////////////////////////////////// class Serializable { public: template static inline void write(Writer &WR,const std::string &s, const Serializable &obj) {} template static inline void read(Reader &RD,const std::string &s, Serializable &obj) {} friend inline std::ostream & operator<<(std::ostream &os, const Serializable &obj) { return os; } }; // Generic writer interface ////////////////////////////////////////////////// template inline void push(Writer &w, const std::string &s) { w.push(s); } template inline void push(Writer &w, const char *s) { w.push(std::string(s)); } template inline void pop(Writer &w) { w.pop(); } template inline void write(Writer &w, const std::string& s, const U &output) { w.write(s, output); } // Generic reader interface ////////////////////////////////////////////////// template inline bool push(Reader &r, const std::string &s) { return r.push(s); } template inline bool push(Reader &r, const char *s) { return r.push(std::string(s)); } template inline void pop(Reader &r) { r.pop(); } template inline void read(Reader &r, const std::string &s, U &output) { r.read(s, output); } } #endif