/************************************************************************************* 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 namespace Grid { // Vector IO utilities /////////////////////////////////////////////////////// // helper function to read space-separated values template std::vector strToVec(const std::string s) { std::istringstream sstr(s); T buf; std::vector v; while(!sstr.eof()) { sstr >> buf; v.push_back(buf); } return v; } // output to streams for vectors template < class T > inline std::ostream & operator<<(std::ostream &os, const std::vector &v) { os << "["; for (auto &x: v) { os << x << " "; } if (v.size() > 0) { os << "\b"; } os << "]"; return os; } // Vector element trait ////////////////////////////////////////////////////// template struct element { typedef T type; static constexpr bool is_number = false; }; template struct element> { typedef typename element::type type; static constexpr bool is_number = std::is_arithmetic::value or is_complex::value or element::is_number; }; // Vector flatening utility class //////////////////////////////////////////// // Class to flatten a multidimensional std::vector template class Flatten { public: typedef typename element::type Element; public: explicit Flatten(const V &vector); const V & getVector(void); const std::vector & getFlatVector(void); const std::vector & getDim(void); private: void accumulate(const Element &e); template void accumulate(const W &v); void accumulateDim(const Element &e); template void accumulateDim(const W &v); private: const V &vector_; std::vector flatVector_; std::vector dim_; }; // Class to reconstruct a multidimensional std::vector template class Reconstruct { public: typedef typename element::type Element; public: Reconstruct(const std::vector &flatVector, const std::vector &dim); const V & getVector(void); const std::vector & getFlatVector(void); const std::vector & getDim(void); private: void fill(std::vector &v); template void fill(W &v); void resize(std::vector &v, const unsigned int dim); template void resize(W &v, const unsigned int dim); private: V vector_; const std::vector &flatVector_; std::vector dim_; size_t ind_{0}; unsigned int dimInd_{0}; }; // Pair IO utilities ///////////////////////////////////////////////////////// // helper function to parse input in the format "" template inline std::istream & operator>>(std::istream &is, std::pair &buf) { T1 buf1; T2 buf2; char c; // Search for "pair" delimiters. do { is.get(c); } while (c != '<' && !is.eof()); if (c == '<') { int start = is.tellg(); do { is.get(c); } while (c != '>' && !is.eof()); if (c == '>') { int end = is.tellg(); int psize = end - start - 1; // Only read data between pair limiters. is.seekg(start); std::string tmpstr(psize, ' '); is.read(&tmpstr[0], psize); std::istringstream temp(tmpstr); temp >> buf1 >> buf2; buf = std::make_pair(buf1, buf2); is.seekg(end); } } is.peek(); return is; } // output to streams for pairs template inline std::ostream & operator<<(std::ostream &os, const std::pair &p) { os << "<" << p.first << " " << p.second << ">"; return os; } // 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); private: T *upcast; }; // 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); 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; }; // Generic writer interface // 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; } }; // Flatten class template implementation ///////////////////////////////////// template void Flatten::accumulate(const Element &e) { flatVector_.push_back(e); } template template void Flatten::accumulate(const W &v) { for (auto &e: v) { accumulate(e); } } template void Flatten::accumulateDim(const Element &e) {}; template template void Flatten::accumulateDim(const W &v) { dim_.push_back(v.size()); accumulateDim(v[0]); } template Flatten::Flatten(const V &vector) : vector_(vector) { accumulate(vector_); accumulateDim(vector_); } template const V & Flatten::getVector(void) { return vector_; } template const std::vector::Element> & Flatten::getFlatVector(void) { return flatVector_; } template const std::vector & Flatten::getDim(void) { return dim_; } // Reconstruct class template implementation ///////////////////////////////// template void Reconstruct::fill(std::vector &v) { for (auto &e: v) { e = flatVector_[ind_++]; } } template template void Reconstruct::fill(W &v) { for (auto &e: v) { fill(e); } } template void Reconstruct::resize(std::vector &v, const unsigned int dim) { v.resize(dim_[dim]); } template template void Reconstruct::resize(W &v, const unsigned int dim) { v.resize(dim_[dim]); for (auto &e: v) { resize(e, dim + 1); } } template Reconstruct::Reconstruct(const std::vector &flatVector, const std::vector &dim) : flatVector_(flatVector) , dim_(dim) { resize(vector_, 0); fill(vector_); } template const V & Reconstruct::getVector(void) { return vector_; } template const std::vector::Element> & Reconstruct::getFlatVector(void) { return flatVector_; } template const std::vector & Reconstruct::getDim(void) { return dim_; } // 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); } // 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); } // 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::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