Added support for enum type in HDF5

This commit is contained in:
Guilhem Lavaux 2015-02-16 10:59:33 +01:00
parent 188af485c9
commit 22aa572370
2 changed files with 79 additions and 1 deletions

View File

@ -51,6 +51,10 @@ struct MyStruct2
int d; int d;
}; };
enum MyColors
{
RED, GREEN, BLUE
};
CTOOL_STRUCT_TYPE(MyStruct, hdf5t_MyStruct, CTOOL_STRUCT_TYPE(MyStruct, hdf5t_MyStruct,
((int, a)) ((int, a))
@ -63,11 +67,17 @@ CTOOL_STRUCT_TYPE(MyStruct2, hdf5t_MyStruct2,
((int, d)) ((int, d))
) )
CTOOL_ENUM_TYPE(MyColors, hdf5t_MyColors,
(RED) (GREEN) (BLUE)
)
int main() int main()
{ {
typedef boost::multi_array<float, 2> array_type; typedef boost::multi_array<float, 2> array_type;
typedef boost::multi_array<float, 3> array3_type; typedef boost::multi_array<float, 3> array3_type;
typedef boost::multi_array<MyStruct, 1> array_mys_type; typedef boost::multi_array<MyStruct, 1> array_mys_type;
typedef boost::multi_array<MyColors, 1> array_mys_color;
typedef boost::multi_array<bool, 1> array_mys_bool;
typedef boost::multi_array<MyStruct2, 1> array_mys2_type; typedef boost::multi_array<MyStruct2, 1> array_mys2_type;
typedef boost::multi_array<std::complex<double>, 2> arrayc_type; typedef boost::multi_array<std::complex<double>, 2> arrayc_type;
typedef array_type::index index; typedef array_type::index index;
@ -82,6 +92,13 @@ int main()
arrayc_type D, E; arrayc_type D, E;
array_mys_type F(boost::extents[10]), G; array_mys_type F(boost::extents[10]), G;
array_mys2_type H(boost::extents[10]); array_mys2_type H(boost::extents[10]);
array_mys_color I(boost::extents[2]);
array_mys_bool J(boost::extents[2]);
I[0] = RED;
I[1] = BLUE;
J[0] = false;
J[1] = true;
int values = 0; int values = 0;
for (index i = 0; i != 2; i++) for (index i = 0; i != 2; i++)
@ -101,7 +118,8 @@ int main()
CosmoTool::hdf5_write_array(g, "test_data", A); CosmoTool::hdf5_write_array(g, "test_data", A);
CosmoTool::hdf5_write_array(g, "test_struct", F); CosmoTool::hdf5_write_array(g, "test_struct", F);
CosmoTool::hdf5_write_array(g, "test_struct2", H); CosmoTool::hdf5_write_array(g, "test_struct2", H);
CosmoTool::hdf5_write_array(g, "colors", I);
CosmoTool::hdf5_write_array(g, "bools", J);
CosmoTool::hdf5_read_array(g, "test_data", B); CosmoTool::hdf5_read_array(g, "test_data", B);
int verify = 0; int verify = 0;

View File

@ -146,6 +146,34 @@ namespace CosmoTool {
} }
}; };
class hdf5_BoolType
{
public:
H5::EnumType type;
hdf5_BoolType()
: type(sizeof(bool))
{
bool v;
v = true;
type.insert("TRUE", &v);
v = false;
type.insert("FALSE", &v);
}
static const hdf5_BoolType *ctype()
{
static hdf5_BoolType singleton;
return &singleton;
}
};
template<> struct get_hdf5_data_type<bool> {
static H5::DataType type() {
return hdf5_BoolType::ctype()->type;
}
};
template<typename ArrayType> template<typename ArrayType>
void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const ArrayType& data ) void hdf5_write_array(H5::CommonFG& fg, const std::string& data_set_name, const ArrayType& data )
@ -295,6 +323,38 @@ namespace CosmoTool { \
}; \ }; \
}; };
#define CTOOL_HDF5_INSERT_ENUM_ELEMENT(r, STRUCT, element) \
{ \
const char *field_name = BOOST_PP_STRINGIZE(element); \
STRUCT a = element; \
type.insert(field_name, &a); \
}
#define CTOOL_ENUM_TYPE(STRUCT, TNAME, ATTRIBUTES) \
namespace CosmoTool { \
class TNAME { \
public: \
H5::EnumType type; \
\
TNAME() : type(sizeof(STRUCT)) \
{ \
long position; \
BOOST_PP_SEQ_FOR_EACH(CTOOL_HDF5_INSERT_ENUM_ELEMENT, STRUCT, ATTRIBUTES) \
} \
\
static const TNAME *ctype() \
{ \
static TNAME singleton; \
return &singleton; \
} \
}; \
template<> struct get_hdf5_data_type<STRUCT> { \
static H5::DataType type() { return TNAME::ctype()->type; }; \
}; \
};
#define CTOOL_ARRAY_TYPE(ARRAY_TYPE, DIM, TNAME) \ #define CTOOL_ARRAY_TYPE(ARRAY_TYPE, DIM, TNAME) \
namespace CosmoTool { \ namespace CosmoTool { \
class TNAME { \ class TNAME { \