1
0
mirror of https://github.com/paboyle/Grid.git synced 2025-04-09 21:50:45 +01:00

C++ indentation

This commit is contained in:
paboyle 2018-01-12 23:39:49 +00:00
parent 176a021ce9
commit 4be31ad1f6

View File

@ -1,4 +1,4 @@
/************************************************************************************* /*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid Grid physics library, www.github.com/paboyle/Grid
@ -25,502 +25,504 @@ Author: Guido Cossu <guido.cossu@ed.ac.uk>
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory See the full license in the file "LICENSE" in the top level distribution directory
*************************************************************************************/ *************************************************************************************/
/* END LEGAL */ /* END LEGAL */
#ifndef GRID_SERIALISATION_ABSTRACT_READER_H #ifndef GRID_SERIALISATION_ABSTRACT_READER_H
#define GRID_SERIALISATION_ABSTRACT_READER_H #define GRID_SERIALISATION_ABSTRACT_READER_H
#include <type_traits> #include <type_traits>
namespace Grid { NAMESPACE_BEGIN(Grid);
// Vector IO utilities ///////////////////////////////////////////////////////
// helper function to read space-separated values // Vector IO utilities ///////////////////////////////////////////////////////
template <typename T> // helper function to read space-separated values
std::vector<T> strToVec(const std::string s) template <typename T>
{ std::vector<T> strToVec(const std::string s)
std::istringstream sstr(s); {
T buf; std::istringstream sstr(s);
std::vector<T> v; T buf;
std::vector<T> v;
while(!sstr.eof()) while(!sstr.eof())
{ {
sstr >> buf; sstr >> buf;
v.push_back(buf); v.push_back(buf);
} }
return v; return v;
} }
// output to streams for vectors // output to streams for vectors
template < class T > 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 << "["; os << "[";
for (auto &x: v) for (auto &x: v)
{ {
os << x << " "; os << x << " ";
} }
if (v.size() > 0) if (v.size() > 0)
{ {
os << "\b"; os << "\b";
} }
os << "]"; os << "]";
return os; return os;
} }
// Vector element trait ////////////////////////////////////////////////////// // Vector element trait //////////////////////////////////////////////////////
template <typename T> template <typename T>
struct element struct element
{ {
typedef T type; typedef T type;
static constexpr bool is_number = false; static constexpr bool is_number = false;
}; };
template <typename T> template <typename T>
struct element<std::vector<T>> struct element<std::vector<T>>
{ {
typedef typename element<T>::type type; typedef typename element<T>::type type;
static constexpr bool is_number = std::is_arithmetic<T>::value static constexpr bool is_number = std::is_arithmetic<T>::value
or is_complex<T>::value or is_complex<T>::value
or element<T>::is_number; or element<T>::is_number;
}; };
// Vector flattening utility class //////////////////////////////////////////// // Vector flattening utility class ////////////////////////////////////////////
// Class to flatten a multidimensional std::vector // Class to flatten a multidimensional std::vector
template <typename V> template <typename V>
class Flatten class Flatten
{ {
public: public:
typedef typename element<V>::type Element; typedef typename element<V>::type Element;
public: public:
explicit Flatten(const V &vector); explicit Flatten(const V &vector);
const V & getVector(void); const V & getVector(void);
const std::vector<Element> & getFlatVector(void); const std::vector<Element> & getFlatVector(void);
const std::vector<size_t> & getDim(void); const std::vector<size_t> & getDim(void);
private: private:
void accumulate(const Element &e); void accumulate(const Element &e);
template <typename W> template <typename W>
void accumulate(const W &v); void accumulate(const W &v);
void accumulateDim(const Element &e); void accumulateDim(const Element &e);
template <typename W> template <typename W>
void accumulateDim(const W &v); void accumulateDim(const W &v);
private: private:
const V &vector_; const V &vector_;
std::vector<Element> flatVector_; std::vector<Element> flatVector_;
std::vector<size_t> dim_; std::vector<size_t> dim_;
}; };
// Class to reconstruct a multidimensional std::vector // Class to reconstruct a multidimensional std::vector
template <typename V> template <typename V>
class Reconstruct class Reconstruct
{ {
public: public:
typedef typename element<V>::type Element; typedef typename element<V>::type Element;
public: public:
Reconstruct(const std::vector<Element> &flatVector, Reconstruct(const std::vector<Element> &flatVector,
const std::vector<size_t> &dim); const std::vector<size_t> &dim);
const V & getVector(void); const V & getVector(void);
const std::vector<Element> & getFlatVector(void); const std::vector<Element> & getFlatVector(void);
const std::vector<size_t> & getDim(void); const std::vector<size_t> & getDim(void);
private: private:
void fill(std::vector<Element> &v); void fill(std::vector<Element> &v);
template <typename W> template <typename W>
void fill(W &v); void fill(W &v);
void resize(std::vector<Element> &v, const unsigned int dim); void resize(std::vector<Element> &v, const unsigned int dim);
template <typename W> template <typename W>
void resize(W &v, const unsigned int dim); void resize(W &v, const unsigned int dim);
private: private:
V vector_; V vector_;
const std::vector<Element> &flatVector_; const std::vector<Element> &flatVector_;
std::vector<size_t> dim_; std::vector<size_t> dim_;
size_t ind_{0}; size_t ind_{0};
unsigned int dimInd_{0}; unsigned int dimInd_{0};
}; };
// Pair IO utilities ///////////////////////////////////////////////////////// // Pair IO utilities /////////////////////////////////////////////////////////
// helper function to parse input in the format "<obj1 obj2>" // helper function to parse input in the format "<obj1 obj2>"
template <typename T1, typename T2> template <typename T1, typename T2>
inline std::istream & operator>>(std::istream &is, std::pair<T1, T2> &buf) inline std::istream & operator>>(std::istream &is, std::pair<T1, T2> &buf)
{ {
T1 buf1; T1 buf1;
T2 buf2; T2 buf2;
char c; char c;
// Search for "pair" delimiters. // Search for "pair" delimiters.
do do
{ {
is.get(c); is.get(c);
} while (c != '<' && !is.eof()); } while (c != '<' && !is.eof());
if (c == '<') if (c == '<')
{ {
int start = is.tellg(); int start = is.tellg();
do do
{ {
is.get(c); is.get(c);
} while (c != '>' && !is.eof()); } while (c != '>' && !is.eof());
if (c == '>') if (c == '>')
{ {
int end = is.tellg(); int end = is.tellg();
int psize = end - start - 1; int psize = end - start - 1;
// Only read data between pair limiters. // Only read data between pair limiters.
is.seekg(start); is.seekg(start);
std::string tmpstr(psize, ' '); std::string tmpstr(psize, ' ');
is.read(&tmpstr[0], psize); is.read(&tmpstr[0], psize);
std::istringstream temp(tmpstr); std::istringstream temp(tmpstr);
temp >> buf1 >> buf2; temp >> buf1 >> buf2;
buf = std::make_pair(buf1, buf2); buf = std::make_pair(buf1, buf2);
is.seekg(end); is.seekg(end);
} }
} }
is.peek(); is.peek();
return is; return is;
} }
// output to streams for pairs // output to streams for pairs
template <class T1, class T2> template <class T1, class T2>
inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p) inline std::ostream & operator<<(std::ostream &os, const std::pair<T1, T2> &p)
{
os << "<" << p.first << " " << p.second << ">";
return os;
}
// 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;
};
// What is the vtype
template<typename T> struct isReader {
static const bool value = false;
};
template<typename T> struct isWriter {
static const bool value = false;
};
// 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)
{}
template <typename T>
static inline void read(Reader<T> &RD,const std::string &s,
Serializable &obj)
{}
friend inline std::ostream & operator<<(std::ostream &os,
const Serializable &obj)
{ {
os << "<" << p.first << " " << p.second << ">";
return os; return os;
} }
};
// Abstract writer/reader classes ////////////////////////////////////////////
// static polymorphism implemented using CRTP idiom
class Serializable;
// Static abstract writer // Flatten class template implementation /////////////////////////////////////
template <typename T> template <typename V>
class Writer void Flatten<V>::accumulate(const Element &e)
{ {
public: flatVector_.push_back(e);
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 V>
template <typename T> template <typename W>
class Reader void Flatten<V>::accumulate(const W &v)
{ {
public: for (auto &e: v)
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;
};
// What is the vtype
template<typename T> struct isReader {
static const bool value = false;
};
template<typename T> struct isWriter {
static const bool value = false;
};
// 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)
{}
template <typename T>
static inline void read(Reader<T> &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 <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)
{ {
accumulate(e); accumulate(e);
} }
} }
template <typename V> template <typename V>
void Flatten<V>::accumulateDim(const Element &e) {}; void Flatten<V>::accumulateDim(const Element &e) {};
template <typename V> template <typename V>
template <typename W> template <typename W>
void Flatten<V>::accumulateDim(const W &v) void Flatten<V>::accumulateDim(const W &v)
{ {
dim_.push_back(v.size()); dim_.push_back(v.size());
accumulateDim(v[0]); accumulateDim(v[0]);
} }
template <typename V> template <typename V>
Flatten<V>::Flatten(const V &vector) Flatten<V>::Flatten(const V &vector)
: vector_(vector) : vector_(vector)
{ {
accumulate(vector_); accumulate(vector_);
accumulateDim(vector_); accumulateDim(vector_);
} }
template <typename V> template <typename V>
const V & Flatten<V>::getVector(void) const V & Flatten<V>::getVector(void)
{ {
return vector_; return vector_;
} }
template <typename V> template <typename V>
const std::vector<typename Flatten<V>::Element> & const std::vector<typename Flatten<V>::Element> &
Flatten<V>::getFlatVector(void) Flatten<V>::getFlatVector(void)
{ {
return flatVector_; return flatVector_;
} }
template <typename V> template <typename V>
const std::vector<size_t> & Flatten<V>::getDim(void) const std::vector<size_t> & Flatten<V>::getDim(void)
{ {
return dim_; return dim_;
} }
// Reconstruct class template implementation ///////////////////////////////// // Reconstruct class template implementation /////////////////////////////////
template <typename V> template <typename V>
void Reconstruct<V>::fill(std::vector<Element> &v) void Reconstruct<V>::fill(std::vector<Element> &v)
{ {
for (auto &e: v) for (auto &e: v)
{ {
e = flatVector_[ind_++]; e = flatVector_[ind_++];
} }
} }
template <typename V> template <typename V>
template <typename W> template <typename W>
void Reconstruct<V>::fill(W &v) void Reconstruct<V>::fill(W &v)
{ {
for (auto &e: v) for (auto &e: v)
{ {
fill(e); fill(e);
} }
} }
template <typename V> template <typename V>
void Reconstruct<V>::resize(std::vector<Element> &v, const unsigned int dim) void Reconstruct<V>::resize(std::vector<Element> &v, const unsigned int dim)
{ {
v.resize(dim_[dim]); v.resize(dim_[dim]);
} }
template <typename V> template <typename V>
template <typename W> template <typename W>
void Reconstruct<V>::resize(W &v, const unsigned int dim) void Reconstruct<V>::resize(W &v, const unsigned int dim)
{ {
v.resize(dim_[dim]); v.resize(dim_[dim]);
for (auto &e: v) for (auto &e: v)
{ {
resize(e, dim + 1); resize(e, dim + 1);
} }
} }
template <typename V> template <typename V>
Reconstruct<V>::Reconstruct(const std::vector<Element> &flatVector, Reconstruct<V>::Reconstruct(const std::vector<Element> &flatVector,
const std::vector<size_t> &dim) const std::vector<size_t> &dim)
: flatVector_(flatVector) : flatVector_(flatVector)
, dim_(dim) , dim_(dim)
{ {
resize(vector_, 0); resize(vector_, 0);
fill(vector_); fill(vector_);
} }
template <typename V> template <typename V>
const V & Reconstruct<V>::getVector(void) const V & Reconstruct<V>::getVector(void)
{ {
return vector_; return vector_;
} }
template <typename V> template <typename V>
const std::vector<typename Reconstruct<V>::Element> & const std::vector<typename Reconstruct<V>::Element> &
Reconstruct<V>::getFlatVector(void) Reconstruct<V>::getFlatVector(void)
{ {
return flatVector_; return flatVector_;
} }
template <typename V> template <typename V>
const std::vector<size_t> & Reconstruct<V>::getDim(void) const std::vector<size_t> & Reconstruct<V>::getDim(void)
{ {
return dim_; return dim_;
} }
// Generic writer interface ////////////////////////////////////////////////// // Generic writer interface //////////////////////////////////////////////////
template <typename T> template <typename T>
inline void push(Writer<T> &w, const std::string &s) { inline void push(Writer<T> &w, const std::string &s) {
w.push(s); w.push(s);
} }
template <typename T> template <typename T>
inline void push(Writer<T> &w, const char *s) inline void push(Writer<T> &w, const char *s)
{ {
w.push(std::string(s)); w.push(std::string(s));
} }
template <typename T> template <typename T>
inline void pop(Writer<T> &w) inline void pop(Writer<T> &w)
{ {
w.pop(); w.pop();
} }
template <typename T, typename U> template <typename T, typename U>
inline void write(Writer<T> &w, const std::string& s, const U &output) inline void write(Writer<T> &w, const std::string& s, const U &output)
{ {
w.write(s, output); w.write(s, output);
} }
// Generic reader interface // Generic reader interface
template <typename T> template <typename T>
inline bool push(Reader<T> &r, const std::string &s) inline bool push(Reader<T> &r, const std::string &s)
{ {
return r.push(s); return r.push(s);
} }
template <typename T> template <typename T>
inline bool push(Reader<T> &r, const char *s) inline bool push(Reader<T> &r, const char *s)
{ {
return r.push(std::string(s)); return r.push(std::string(s));
} }
template <typename T> template <typename T>
inline void pop(Reader<T> &r) inline void pop(Reader<T> &r)
{ {
r.pop(); r.pop();
} }
template <typename T, typename U> template <typename T, typename U>
inline void read(Reader<T> &r, const std::string &s, U &output) inline void read(Reader<T> &r, const std::string &s, U &output)
{ {
r.read(s, output); r.read(s, output);
} }
// Writer template implementation //////////////////////////////////////////// // Writer template implementation ////////////////////////////////////////////
template <typename T> template <typename T>
Writer<T>::Writer(void) Writer<T>::Writer(void)
{ {
upcast = static_cast<T *>(this); upcast = static_cast<T *>(this);
} }
template <typename T> template <typename T>
void Writer<T>::push(const std::string &s) void Writer<T>::push(const std::string &s)
{ {
upcast->push(s); upcast->push(s);
} }
template <typename T> template <typename T>
void Writer<T>::pop(void) void Writer<T>::pop(void)
{ {
upcast->pop(); upcast->pop();
} }
template <typename T> template <typename T>
template <typename U> template <typename U>
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
Writer<T>::write(const std::string &s, const U &output) Writer<T>::write(const std::string &s, const U &output)
{ {
U::write(*this, s, output); U::write(*this, s, output);
} }
template <typename T> template <typename T>
template <typename U> template <typename U>
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
Writer<T>::write(const std::string &s, const U &output) Writer<T>::write(const std::string &s, const U &output)
{ {
upcast->writeDefault(s, output); upcast->writeDefault(s, output);
} }
// Reader template implementation // Reader template implementation
template <typename T> template <typename T>
Reader<T>::Reader(void) Reader<T>::Reader(void)
{ {
upcast = static_cast<T *>(this); upcast = static_cast<T *>(this);
} }
template <typename T> template <typename T>
bool Reader<T>::push(const std::string &s) bool Reader<T>::push(const std::string &s)
{ {
return upcast->push(s); return upcast->push(s);
} }
template <typename T> template <typename T>
void Reader<T>::pop(void) void Reader<T>::pop(void)
{ {
upcast->pop(); upcast->pop();
} }
template <typename T> template <typename T>
template <typename U> template <typename U>
typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type typename std::enable_if<std::is_base_of<Serializable, U>::value, void>::type
Reader<T>::read(const std::string &s, U &output) Reader<T>::read(const std::string &s, U &output)
{ {
U::read(*this, s, output); U::read(*this, s, output);
} }
template <typename T> template <typename T>
template <typename U> template <typename U>
typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type typename std::enable_if<!std::is_base_of<Serializable, U>::value, void>::type
Reader<T>::read(const std::string &s, U &output) Reader<T>::read(const std::string &s, U &output)
{ {
upcast->readDefault(s, output); upcast->readDefault(s, output);
} }
template <typename T> template <typename T>
template <typename U> template <typename U>
void Reader<T>::fromString(U &output, const std::string &s) void Reader<T>::fromString(U &output, const std::string &s)
{ {
std::istringstream is(s); std::istringstream is(s);
is.exceptions(std::ios::failbit); is.exceptions(std::ios::failbit);
try try
{ {
is >> std::boolalpha >> output; is >> std::boolalpha >> output;
} }
catch(std::istringstream::failure &e) catch(std::istringstream::failure &e)
{ {
std::cerr << "numerical conversion failure on '" << s << "' "; std::cerr << "numerical conversion failure on '" << s << "' ";
std::cerr << "(typeid: " << typeid(U).name() << ")" << std::endl; std::cerr << "(typeid: " << typeid(U).name() << ")" << std::endl;
abort(); abort();
} }
}
} }
NAMESPACE_END(Grid);
#endif #endif