1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/lib/serialisation/BaseIO.h

529 lines
12 KiB
C
Raw Normal View History

2018-01-12 23:39:49 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/serialisation/BaseIO.h
Copyright (C) 2015
Author: Antonin Portelli <antonin.portelli@me.com>
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
2017-01-25 12:11:58 +00:00
Author: Guido Cossu <guido.cossu@ed.ac.uk>
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
2018-01-12 23:39:49 +00:00
*************************************************************************************/
/* END LEGAL */
#ifndef GRID_SERIALISATION_ABSTRACT_READER_H
#define GRID_SERIALISATION_ABSTRACT_READER_H
2015-11-16 18:14:37 +00:00
#include <type_traits>
2018-01-12 23:39:49 +00:00
NAMESPACE_BEGIN(Grid);
// Vector IO utilities ///////////////////////////////////////////////////////
// helper function to read space-separated values
template <typename T>
std::vector<T> strToVec(const std::string s)
{
std::istringstream sstr(s);
T buf;
std::vector<T> v;
2018-01-12 23:39:49 +00:00
while(!sstr.eof())
{
sstr >> buf;
v.push_back(buf);
}
2018-01-12 23:39:49 +00:00
return v;
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
// output to streams for vectors
template < class T >
inline std::ostream & operator<<(std::ostream &os, const std::vector<T> &v)
{
os << "[";
for (auto &x: v)
2016-05-03 03:32:11 +01:00
{
os << x << " ";
}
2018-01-12 23:39:49 +00:00
if (v.size() > 0)
2016-05-03 03:32:11 +01:00
{
os << "\b";
}
2018-01-12 23:39:49 +00:00
os << "]";
2016-05-03 03:32:11 +01:00
2018-01-12 23:39:49 +00:00
return os;
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
// Vector element trait //////////////////////////////////////////////////////
template <typename T>
struct element
{
typedef T type;
static constexpr bool is_number = false;
};
template <typename T>
struct element<std::vector<T>>
{
typedef typename element<T>::type type;
static constexpr bool is_number = std::is_arithmetic<T>::value
or is_complex<T>::value
or element<T>::is_number;
};
// Vector flattening utility class ////////////////////////////////////////////
// Class to flatten a multidimensional std::vector
template <typename V>
class Flatten
{
public:
typedef typename element<V>::type Element;
public:
explicit Flatten(const V &vector);
const V & getVector(void);
const std::vector<Element> & getFlatVector(void);
const std::vector<size_t> & getDim(void);
private:
void accumulate(const Element &e);
template <typename W>
void accumulate(const W &v);
void accumulateDim(const Element &e);
template <typename W>
void accumulateDim(const W &v);
private:
const V &vector_;
std::vector<Element> flatVector_;
std::vector<size_t> dim_;
};
// Class to reconstruct a multidimensional std::vector
template <typename V>
class Reconstruct
{
public:
typedef typename element<V>::type Element;
public:
Reconstruct(const std::vector<Element> &flatVector,
const std::vector<size_t> &dim);
const V & getVector(void);
const std::vector<Element> & getFlatVector(void);
const std::vector<size_t> & getDim(void);
private:
void fill(std::vector<Element> &v);
template <typename W>
void fill(W &v);
void resize(std::vector<Element> &v, const unsigned int dim);
template <typename W>
void resize(W &v, const unsigned int dim);
private:
V vector_;
const std::vector<Element> &flatVector_;
std::vector<size_t> dim_;
size_t ind_{0};
unsigned int dimInd_{0};
};
// Pair IO utilities /////////////////////////////////////////////////////////
// helper function to parse input in the format "<obj1 obj2>"
template <typename T1, typename T2>
inline std::istream & operator>>(std::istream &is, std::pair<T1, T2> &buf)
{
T1 buf1;
T2 buf2;
char c;
2018-01-12 23:39:49 +00:00
// Search for "pair" delimiters.
do
{
is.get(c);
} while (c != '<' && !is.eof());
2018-01-12 23:39:49 +00:00
if (c == '<')
{
int start = is.tellg();
do
2018-01-12 23:39:49 +00:00
{
is.get(c);
} while (c != '>' && !is.eof());
if (c == '>')
2018-01-12 23:39:49 +00:00
{
int end = is.tellg();
int psize = end - start - 1;
2018-01-12 23:39:49 +00:00
// 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);
}
}
2018-01-12 23:39:49 +00:00
is.peek();
return is;
}
2018-01-12 23:39:49 +00:00
// output to streams for pairs
template <class T1, class T2>
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{
os << "<" << p.first << " " << p.second << ">";
return os;
}
2018-01-12 23:39:49 +00:00
// Abstract writer/reader classes ////////////////////////////////////////////
// static polymorphism implemented using CRTP idiom
class Serializable;
// 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;
bool 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;
};
2017-01-16 10:18:09 +00:00
2018-01-12 23:39:49 +00:00
// What is the vtype
template<typename T> struct isReader {
static const bool value = false;
};
template<typename T> struct isWriter {
static const bool value = false;
};
2017-01-25 12:11:58 +00:00
2018-01-12 23:39:49 +00:00
// Generic writer interface
// serializable base class
class Serializable
{
public:
template <typename T>
static inline void write(Writer<T> &WR,const std::string &s,
const Serializable &obj)
{}
2018-01-12 23:39:49 +00:00
template <typename T>
static inline void read(Reader<T> &RD,const std::string &s,
Serializable &obj)
{}
2018-01-12 23:39:49 +00:00
friend inline std::ostream & operator<<(std::ostream &os,
const Serializable &obj)
2017-01-19 00:50:21 +00:00
{
2018-01-12 23:39:49 +00:00
return os;
2017-01-19 00:50:21 +00:00
}
2018-01-12 23:39:49 +00:00
};
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
// Flatten class template implementation /////////////////////////////////////
template <typename V>
void Flatten<V>::accumulate(const Element &e)
{
flatVector_.push_back(e);
}
template <typename V>
template <typename W>
void Flatten<V>::accumulate(const W &v)
{
for (auto &e: v)
2017-01-19 00:50:21 +00:00
{
accumulate(e);
}
2018-01-12 23:39:49 +00:00
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
void Flatten<V>::accumulateDim(const Element &e) {};
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
template <typename W>
void Flatten<V>::accumulateDim(const W &v)
{
dim_.push_back(v.size());
accumulateDim(v[0]);
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
Flatten<V>::Flatten(const V &vector)
2017-01-19 00:50:21 +00:00
: vector_(vector)
2018-01-12 23:39:49 +00:00
{
accumulate(vector_);
accumulateDim(vector_);
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const V & Flatten<V>::getVector(void)
{
return vector_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const std::vector<typename Flatten<V>::Element> &
Flatten<V>::getFlatVector(void)
{
return flatVector_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const std::vector<size_t> & Flatten<V>::getDim(void)
{
return dim_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
// Reconstruct class template implementation /////////////////////////////////
template <typename V>
void Reconstruct<V>::fill(std::vector<Element> &v)
{
for (auto &e: v)
2017-01-19 00:50:21 +00:00
{
e = flatVector_[ind_++];
}
2018-01-12 23:39:49 +00:00
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
template <typename W>
void Reconstruct<V>::fill(W &v)
{
for (auto &e: v)
2017-01-19 00:50:21 +00:00
{
fill(e);
}
2018-01-12 23:39:49 +00:00
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
void Reconstruct<V>::resize(std::vector<Element> &v, const unsigned int dim)
{
v.resize(dim_[dim]);
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
template <typename W>
void Reconstruct<V>::resize(W &v, const unsigned int dim)
{
v.resize(dim_[dim]);
for (auto &e: v)
2017-01-19 00:50:21 +00:00
{
resize(e, dim + 1);
}
2018-01-12 23:39:49 +00:00
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
Reconstruct<V>::Reconstruct(const std::vector<Element> &flatVector,
const std::vector<size_t> &dim)
2017-01-19 00:50:21 +00:00
: flatVector_(flatVector)
, dim_(dim)
2018-01-12 23:39:49 +00:00
{
resize(vector_, 0);
fill(vector_);
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const V & Reconstruct<V>::getVector(void)
{
return vector_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const std::vector<typename Reconstruct<V>::Element> &
Reconstruct<V>::getFlatVector(void)
{
return flatVector_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
template <typename V>
const std::vector<size_t> & Reconstruct<V>::getDim(void)
{
return dim_;
}
2017-01-19 00:50:21 +00:00
2018-01-12 23:39:49 +00:00
// Generic writer interface //////////////////////////////////////////////////
template <typename T>
inline void push(Writer<T> &w, const std::string &s) {
w.push(s);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
inline void push(Writer<T> &w, const char *s)
{
w.push(std::string(s));
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
inline void pop(Writer<T> &w)
{
w.pop();
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T, typename U>
inline void write(Writer<T> &w, const std::string& s, const U &output)
{
w.write(s, output);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
// Generic reader interface
template <typename T>
inline bool push(Reader<T> &r, const std::string &s)
{
return r.push(s);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
inline bool push(Reader<T> &r, const char *s)
{
return r.push(std::string(s));
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
inline void pop(Reader<T> &r)
{
r.pop();
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T, typename U>
inline void read(Reader<T> &r, const std::string &s, U &output)
{
r.read(s, output);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
// Writer template implementation ////////////////////////////////////////////
template <typename T>
Writer<T>::Writer(void)
{
upcast = static_cast<T *>(this);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
void Writer<T>::push(const std::string &s)
{
upcast->push(s);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
void Writer<T>::pop(void)
{
upcast->pop();
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
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);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
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);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
// Reader template implementation
template <typename T>
Reader<T>::Reader(void)
{
upcast = static_cast<T *>(this);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
bool Reader<T>::push(const std::string &s)
{
return upcast->push(s);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
void Reader<T>::pop(void)
{
upcast->pop();
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
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);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
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);
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
template <typename T>
template <typename U>
void Reader<T>::fromString(U &output, const std::string &s)
{
std::istringstream is(s);
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
is.exceptions(std::ios::failbit);
try
2015-11-16 18:14:37 +00:00
{
is >> std::boolalpha >> output;
}
2018-01-12 23:39:49 +00:00
catch(std::istringstream::failure &e)
2015-11-16 18:14:37 +00:00
{
std::cerr << "numerical conversion failure on '" << s << "' ";
std::cerr << "(typeid: " << typeid(U).name() << ")" << std::endl;
abort();
}
}
2015-11-16 18:14:37 +00:00
2018-01-12 23:39:49 +00:00
NAMESPACE_END(Grid);
#endif