mirror of
https://github.com/paboyle/Grid.git
synced 2024-11-10 07:55:35 +00:00
IO: serialisable enums
This commit is contained in:
parent
d68a72e28b
commit
200de272ed
@ -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);
|
||||
|
@ -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
|
||||
|
@ -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,
|
||||
|
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user