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

Merge branch 'aportelli-master'

This commit is contained in:
paboyle 2015-12-10 23:15:16 +00:00
commit 78ca15fdd8
11 changed files with 169 additions and 64 deletions

1
.gitignore vendored
View File

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

View File

@ -22,7 +22,12 @@ namespace Grid {
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
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);
private:
T *upcast;
@ -41,7 +46,12 @@ namespace Grid {
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
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);
protected:
template <typename U>
@ -146,7 +156,17 @@ namespace Grid {
template <typename T>
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)
{
upcast->writeDefault(s, output);
@ -181,7 +201,17 @@ namespace Grid {
template <typename T>
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)
{
upcast->readDefault(s, output);
@ -205,7 +235,7 @@ namespace Grid {
abort();
}
}
}
#endif

View File

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

View File

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

View File

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

View File

@ -20,9 +20,9 @@ namespace Grid
void push(const std::string &s);
void pop(void);
template <typename U>
void writeDefault(const std::string &s, const U &output);
void writeDefault(const std::string &s, const U &x);
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:
void indent(void);
private:
@ -50,21 +50,21 @@ namespace Grid
// Writer template implementation ////////////////////////////////////////////
template <typename U>
void TextWriter::writeDefault(const std::string &s, const U &output)
void TextWriter::writeDefault(const std::string &s, const U &x)
{
indent();
file_ << std::boolalpha << output << std::endl;
file_ << std::boolalpha << x << std::endl;
}
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);
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);
}
template <>
void TextReader::readDefault(const std::string &s, std::string &output);
template <typename U>
void TextReader::readDefault(const std::string &s, std::vector<U> &output)
{

View File

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

View File

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

View File

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

View File

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