1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 07:55:35 +00:00

Merge branch 'master' of https://github.com/aportelli/Grid into aportelli-master

This commit is contained in:
paboyle 2015-12-10 23:14:52 +00:00
commit 47fe6b5a7c
11 changed files with 169 additions and 64 deletions

1
.gitignore vendored
View File

@ -44,6 +44,7 @@
Makefile.in Makefile.in
Makefile Makefile
Config.h Config.h
Config.h.in
config.log config.log
config.status config.status
.deps .deps

View File

@ -22,7 +22,12 @@ namespace Grid {
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
write(const std::string& s, const U &output); write(const std::string& s, const U &output);
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_enum<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 or std::is_enum<U>::value),
void>::type
write(const std::string& s, const U &output); write(const std::string& s, const U &output);
private: private:
T *upcast; T *upcast;
@ -41,7 +46,12 @@ namespace Grid {
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
read(const std::string& s, U &output); read(const std::string& s, U &output);
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_enum<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 or std::is_enum<U>::value),
void>::type
read(const std::string& s, U &output); read(const std::string& s, U &output);
protected: protected:
template <typename U> template <typename U>
@ -146,7 +156,17 @@ namespace Grid {
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_enum<U>::value, void>::type
Writer<T>::write(const std::string &s, const U &output)
{
EnumIO<U>::write(*this, s, output);
}
template <typename T>
template <typename U>
typename std::enable_if<
!(std::is_base_of<Serializable, U>::value or std::is_enum<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);
@ -181,7 +201,17 @@ namespace Grid {
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_enum<U>::value, void>::type
Reader<T>::read(const std::string &s, U &output)
{
EnumIO<U>::read(*this, s, output);
}
template <typename T>
template <typename U>
typename std::enable_if<
!(std::is_base_of<Serializable, U>::value or std::is_enum<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);
@ -205,7 +235,7 @@ namespace Grid {
abort(); abort();
} }
} }
} }
#endif #endif

View File

@ -1,36 +1,43 @@
#include <Grid.h> #include <Grid.h>
using namespace Grid;
using namespace std;
namespace Grid {
// Writer implementation /////////////////////////////////////////////////////// // Writer implementation ///////////////////////////////////////////////////////
BinaryWriter::BinaryWriter(const std::string &fileName) BinaryWriter::BinaryWriter(const string &fileName)
: file_(fileName, std::ios::binary|std::ios::out) : file_(fileName, ios::binary|ios::out)
{} {}
template <> template <>
void BinaryWriter::writeDefault(const std::string &s, const std::string &output) void BinaryWriter::writeDefault(const string &s, const string &x)
{ {
uint64_t sz = output.size(); uint64_t sz = x.size();
write("", sz);
for (uint64_t i = 0; i < sz; ++i)
{
write("", x[i]);
}
}
void BinaryWriter::writeDefault(const string &s, const char *x)
{
string sx(x);
write("", sz); writeDefault(s, sx);
for (uint64_t i = 0; i < sz; ++i)
{
write("", output[i]);
}
} }
// Reader implementation /////////////////////////////////////////////////////// // Reader implementation ///////////////////////////////////////////////////////
BinaryReader::BinaryReader(const std::string &fileName) BinaryReader::BinaryReader(const string &fileName)
: file_(fileName, std::ios::binary|std::ios::in) : file_(fileName, ios::binary|ios::in)
{} {}
template <> template <>
void BinaryReader::readDefault(const std::string &s, std::string &output) void BinaryReader::readDefault(const string &s, string &output)
{ {
uint64_t sz; uint64_t sz;
read("", sz); read("", sz);
output.reserve(sz); output.resize(sz);
file_.read((char *)output.data(), sz); file_.read((char *)output.data(), sz);
}
} }

View File

@ -22,6 +22,7 @@ namespace Grid {
void writeDefault(const std::string &s, const U &x); void writeDefault(const std::string &s, const U &x);
template <typename U> template <typename U>
void writeDefault(const std::string &s, const std::vector<U> &x); void writeDefault(const std::string &s, const std::vector<U> &x);
void writeDefault(const std::string &s, const char *x);
private: private:
std::ofstream file_; std::ofstream file_;
}; };
@ -48,6 +49,9 @@ namespace Grid {
file_.write((char *)&x, sizeof(U)); file_.write((char *)&x, sizeof(U));
} }
template <>
void BinaryWriter::writeDefault(const std::string &s, const std::string &x);
template <typename U> template <typename U>
void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x) void BinaryWriter::writeDefault(const std::string &s, const std::vector<U> &x)
{ {
@ -67,6 +71,9 @@ namespace Grid {
file_.read((char *)&output, sizeof(U)); file_.read((char *)&output, sizeof(U));
} }
template <>
void BinaryReader::readDefault(const std::string &s, std::string &output);
template <typename U> template <typename U>
void BinaryReader::readDefault(const std::string &s, std::vector<U> &output) void BinaryReader::readDefault(const std::string &s, std::vector<U> &output)
{ {

View File

@ -109,12 +109,11 @@ THE SOFTWARE.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define GRID_MACRO_MEMBER(A,B) A B; #define GRID_MACRO_MEMBER(A,B) A B;
#define GRID_MACRO_OS_WRITE_MEMBER(A,B) os<< #A <<" "#B <<" = "<< obj. B <<" ; " <<std::endl; #define GRID_MACRO_OS_WRITE_MEMBER(A,B) os<< #A <<" "#B <<" = "<< obj. B <<" ; " <<std::endl;
#define GRID_MACRO_READ_MEMBER(A,B) Grid::read(RD,#B,obj. B); #define GRID_MACRO_READ_MEMBER(A,B) Grid::read(RD,#B,obj. B);
#define GRID_MACRO_WRITE_MEMBER(A,B) Grid::write(WR,#B,obj. B); #define GRID_MACRO_WRITE_MEMBER(A,B) Grid::write(WR,#B,obj. B);
#define GRID_DECL_CLASS_MEMBERS(cname,...) \ #define GRID_SERIALIZABLE_CLASS_MEMBERS(cname,...) \
\ \
\ \
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_MEMBER,__VA_ARGS__)) \ GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_MEMBER,__VA_ARGS__)) \
@ -144,4 +143,51 @@ THE SOFTWARE.
}; };
#define GRID_ENUM_TYPE(obj) std::remove_reference<decltype(obj)>::type
#define GRID_MACRO_ENUMVAL(A,B) A = B,
#define GRID_MACRO_ENUMCASE(A,B) case GRID_ENUM_TYPE(obj)::A: Grid::write(WR,s,#A); break;
#define GRID_MACRO_ENUMTEST(A,B) else if (buf == #A) {obj = GRID_ENUM_TYPE(obj)::A;}
#define GRID_MACRO_ENUMCASEIO(A,B) case GRID_ENUM_TYPE(obj)::A: os << #A; break;
namespace Grid {
template <typename U>
class EnumIO {};
}
#define GRID_SERIALIZABLE_ENUM(name,undefname,...)\
enum class name {\
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_ENUMVAL,__VA_ARGS__))\
undefname = -1\
};\
\
template<>\
class EnumIO<name> {\
public:\
template <typename T>\
static void write(Writer<T> &WR,const std::string &s, const name &obj){ \
switch (obj) {\
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_ENUMCASE,__VA_ARGS__))\
default: Grid::write(WR,s,#undefname); break;\
}\
}\
\
template <typename T>\
static void read(Reader<T> &RD,const std::string &s, name &obj){ \
std::string buf;\
Grid::read(RD, s, buf);\
if (buf == #undefname) {obj = name::undefname;}\
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_ENUMTEST,__VA_ARGS__))\
else {obj = name::undefname;}\
}\
};\
\
std::ostream & operator << (std::ostream &os, const name &obj ) { \
switch (obj) {\
GRID_MACRO_EVAL(GRID_MACRO_MAP(GRID_MACRO_ENUMCASEIO,__VA_ARGS__))\
default: os << #undefname; break;\
}\
return os;\
};
#endif #endif

View File

@ -1,12 +1,14 @@
#include <Grid.h> #include <Grid.h>
namespace Grid { using namespace Grid;
using namespace std;
// Writer implementation /////////////////////////////////////////////////////// // Writer implementation ///////////////////////////////////////////////////////
TextWriter::TextWriter(const std::string &fileName) TextWriter::TextWriter(const string &fileName)
: file_(fileName, std::ios::out) : file_(fileName, ios::out)
{} {}
void TextWriter::push(const std::string &s) void TextWriter::push(const string &s)
{ {
level_++; level_++;
}; };
@ -25,11 +27,11 @@ void TextWriter::indent(void)
}; };
// Reader implementation /////////////////////////////////////////////////////// // Reader implementation ///////////////////////////////////////////////////////
TextReader::TextReader(const std::string &fileName) TextReader::TextReader(const string &fileName)
: file_(fileName, std::ios::in) : file_(fileName, ios::in)
{} {}
void TextReader::push(const std::string &s) void TextReader::push(const string &s)
{ {
level_++; level_++;
}; };
@ -48,9 +50,9 @@ void TextReader::checkIndent(void)
file_.get(c); file_.get(c);
if (c != '\t') if (c != '\t')
{ {
std::cerr << "mismatch on tab " << c << " level " << level_; cerr << "mismatch on tab " << c << " level " << level_;
std::cerr << " i "<< i <<std::endl; cerr << " i "<< i << endl;
std::abort(); abort();
} }
} }
} }
@ -58,8 +60,7 @@ void TextReader::checkIndent(void)
template <> template <>
void TextReader::readDefault(const std::string &s, std::string &output) void TextReader::readDefault(const std::string &s, std::string &output)
{ {
checkIndent(); checkIndent();
output.clear(); output.clear();
getline(file_, output); getline(file_, output);
}
} }

View File

@ -20,9 +20,9 @@ namespace Grid
void push(const std::string &s); void push(const std::string &s);
void pop(void); void pop(void);
template <typename U> template <typename U>
void writeDefault(const std::string &s, const U &output); void writeDefault(const std::string &s, const U &x);
template <typename U> template <typename U>
void writeDefault(const std::string &s, const std::vector<U> &output); void writeDefault(const std::string &s, const std::vector<U> &x);
private: private:
void indent(void); void indent(void);
private: private:
@ -50,21 +50,21 @@ namespace Grid
// Writer template implementation //////////////////////////////////////////// // Writer template implementation ////////////////////////////////////////////
template <typename U> template <typename U>
void TextWriter::writeDefault(const std::string &s, const U &output) void TextWriter::writeDefault(const std::string &s, const U &x)
{ {
indent(); indent();
file_ << std::boolalpha << output << std::endl; file_ << std::boolalpha << x << std::endl;
} }
template <typename U> template <typename U>
void TextWriter::writeDefault(const std::string &s, const std::vector<U> &output) void TextWriter::writeDefault(const std::string &s, const std::vector<U> &x)
{ {
uint64_t sz = output.size(); uint64_t sz = x.size();
write(s, sz); write(s, sz);
for (uint64_t i = 0; i < sz; ++i) for (uint64_t i = 0; i < sz; ++i)
{ {
write(s, output[i]); write(s, x[i]);
} }
} }
@ -78,6 +78,9 @@ namespace Grid
fromString(output, buf); fromString(output, buf);
} }
template <>
void TextReader::readDefault(const std::string &s, std::string &output);
template <typename U> template <typename U>
void TextReader::readDefault(const std::string &s, std::vector<U> &output) void TextReader::readDefault(const std::string &s, std::vector<U> &output)
{ {

View File

@ -1,8 +1,10 @@
#include <Grid.h> #include <Grid.h>
namespace Grid { using namespace Grid;
using namespace std;
// Writer implementation /////////////////////////////////////////////////////// // Writer implementation ///////////////////////////////////////////////////////
XmlWriter::XmlWriter(const std::string &fileName) XmlWriter::XmlWriter(const string &fileName)
: fileName_(fileName) : fileName_(fileName)
{ {
node_ = doc_.append_child(); node_ = doc_.append_child();
@ -14,7 +16,7 @@ XmlWriter::~XmlWriter(void)
doc_.save_file(fileName_.c_str(), " "); doc_.save_file(fileName_.c_str(), " ");
} }
void XmlWriter::push(const std::string &s) void XmlWriter::push(const string &s)
{ {
node_ = node_.append_child(s.c_str()); node_ = node_.append_child(s.c_str());
} }
@ -25,22 +27,22 @@ void XmlWriter::pop(void)
} }
// Reader implementation /////////////////////////////////////////////////////// // Reader implementation ///////////////////////////////////////////////////////
XmlReader::XmlReader(const std::string &fileName) XmlReader::XmlReader(const string &fileName)
: fileName_(fileName) : fileName_(fileName)
{ {
pugi::xml_parse_result result = doc_.load_file(fileName_.c_str()); pugi::xml_parse_result result = doc_.load_file(fileName_.c_str());
if ( !result ) if ( !result )
{ {
std::cerr << "XML error description: " << result.description() << "\n"; cerr << "XML error description: " << result.description() << "\n";
std::cerr << "XML error offset : " << result.offset << "\n"; cerr << "XML error offset : " << result.offset << "\n";
std::abort(); abort();
} }
node_ = doc_.child("grid"); node_ = doc_.child("grid");
} }
void XmlReader::push(const std::string &s) void XmlReader::push(const string &s)
{ {
node_ = node_.child(s.c_str()); node_ = node_.child(s.c_str());
} }
@ -51,8 +53,7 @@ void XmlReader::pop(void)
} }
template <> template <>
void XmlReader::readDefault(const std::string &s, std::string &output) void XmlReader::readDefault(const string &s, string &output)
{ {
output = node_.child(s.c_str()).first_child().value(); output = node_.child(s.c_str()).first_child().value();
} }
}

View File

@ -81,10 +81,12 @@ namespace Grid
fromString(output, buf); fromString(output, buf);
} }
template <>
void XmlReader::readDefault(const std::string &s, std::string &output);
template <typename U> template <typename U>
void XmlReader::readDefault(const std::string &s, std::vector<U> &output) void XmlReader::readDefault(const std::string &s, std::vector<U> &output)
{ {
pugi::xml_node nodeCpy;
std::string buf; std::string buf;
unsigned int i = 0; unsigned int i = 0;
@ -96,7 +98,6 @@ namespace Grid
node_.child("elem").set_name("elem-done"); node_.child("elem").set_name("elem-done");
i++; i++;
} }
// assert( is.tellg()==-1);
pop(); pop();
} }

View File

@ -9,7 +9,7 @@ using namespace Grid::QCD;
class myclass: Serializable { class myclass: Serializable {
public: public:
GRID_DECL_CLASS_MEMBERS(myclass, GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
int, domaindecompose, int, domaindecompose,
int, domainsize, int, domainsize,
int, order, int, order,

View File

@ -1,30 +1,39 @@
#include <Grid.h> #include <Grid.h>
namespace Grid { namespace Grid {
GRID_SERIALIZABLE_ENUM(myenum, undef, red, 1, blue, 2, green, 3);
class myclass: Serializable { class myclass: Serializable {
public: public:
GRID_DECL_CLASS_MEMBERS(myclass, GRID_SERIALIZABLE_CLASS_MEMBERS(myclass,
myenum, e,
std::vector<myenum>, ve,
std::string, name,
int, x, int, x,
double, y, double, y,
bool , b, bool , b,
std::string, name,
std::vector<double>, array, std::vector<double>, array,
std::vector<std::vector<double>>, twodimarray, std::vector<std::vector<double>>, twodimarray,
); );
myclass() {} myclass() {}
myclass(int i) myclass(int i)
: array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)) : array(4,5.1), twodimarray(3,std::vector<double>(2,1.23456)), ve(2, myenum::blue)
{ {
e=myenum::red;
x=i; x=i;
y=2*i; y=2*i;
b=true; b=true;
name="bother said pooh"; name="bother said pooh";
} }
}; };
} }
using namespace Grid;
int16_t i16 = 1; int16_t i16 = 1;
uint16_t u16 = 2; uint16_t u16 = 2;
int32_t i32 = 3; int32_t i32 = 3;
@ -35,8 +44,6 @@ float f = M_PI;
double d = 2*M_PI; double d = 2*M_PI;
bool b = false; bool b = false;
using namespace Grid;
int main(int argc,char **argv) int main(int argc,char **argv)
{ {
{ {
@ -59,6 +66,7 @@ int main(int argc,char **argv)
myclass obj(1234); // non-trivial constructor myclass obj(1234); // non-trivial constructor
write(WR,"obj",obj); write(WR,"obj",obj);
WR.write("obj2", obj); WR.write("obj2", obj);
std::cout << obj << std::endl;
std::vector<myclass> vec; std::vector<myclass> vec;
vec.push_back(myclass(1234)); vec.push_back(myclass(1234));