diff --git a/sample/testHDF5.cpp b/sample/testHDF5.cpp index 4011889..ab08c98 100644 --- a/sample/testHDF5.cpp +++ b/sample/testHDF5.cpp @@ -51,6 +51,10 @@ struct MyStruct2 int d; }; +enum MyColors +{ + RED, GREEN, BLUE +}; CTOOL_STRUCT_TYPE(MyStruct, hdf5t_MyStruct, ((int, a)) @@ -63,11 +67,17 @@ CTOOL_STRUCT_TYPE(MyStruct2, hdf5t_MyStruct2, ((int, d)) ) +CTOOL_ENUM_TYPE(MyColors, hdf5t_MyColors, + (RED) (GREEN) (BLUE) +) + int main() { typedef boost::multi_array array_type; typedef boost::multi_array array3_type; typedef boost::multi_array array_mys_type; + typedef boost::multi_array array_mys_color; + typedef boost::multi_array array_mys_bool; typedef boost::multi_array array_mys2_type; typedef boost::multi_array, 2> arrayc_type; typedef array_type::index index; @@ -82,6 +92,13 @@ int main() arrayc_type D, E; array_mys_type F(boost::extents[10]), G; 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; 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_struct", F); 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); int verify = 0; diff --git a/src/hdf5_array.hpp b/src/hdf5_array.hpp index 59effef..c3052b1 100644 --- a/src/hdf5_array.hpp +++ b/src/hdf5_array.hpp @@ -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 { + static H5::DataType type() { + return hdf5_BoolType::ctype()->type; + } + }; template 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 { \ + static H5::DataType type() { return TNAME::ctype()->type; }; \ + }; \ +}; + #define CTOOL_ARRAY_TYPE(ARRAY_TYPE, DIM, TNAME) \ namespace CosmoTool { \ class TNAME { \