diff --git a/lib/Grid.h b/lib/Grid.h index 0f57c8a6..cb55d0c8 100644 --- a/lib/Grid.h +++ b/lib/Grid.h @@ -60,12 +60,12 @@ Author: paboyle // Grid headers /////////////////// #include "Config.h" -#include #include #include #include #include #include +#include #include #include #include diff --git a/lib/serialisation/BaseIO.h b/lib/serialisation/BaseIO.h index eca5cff2..0357915d 100644 --- a/lib/serialisation/BaseIO.h +++ b/lib/serialisation/BaseIO.h @@ -68,20 +68,21 @@ namespace Grid { return os; } - // Vector element trait ////////////////////////////////////////////////////// + // Vector element trait ////////////////////////////////////////////////////// template struct element { typedef T type; - static constexpr bool is_arithmetic = false; + static constexpr bool is_number = false; }; template struct element> { typedef typename element::type type; - static constexpr bool is_arithmetic = std::is_arithmetic::value - or element::is_arithmetic; + static constexpr bool is_number = std::is_arithmetic::value + or is_complex::value + or element::is_number; }; // Vector flatening utility class //////////////////////////////////////////// diff --git a/lib/serialisation/Hdf5IO.h b/lib/serialisation/Hdf5IO.h index 3edb7d10..2f891cd4 100644 --- a/lib/serialisation/Hdf5IO.h +++ b/lib/serialisation/Hdf5IO.h @@ -32,10 +32,10 @@ namespace Grid template void writeDefault(const std::string &s, const U &x); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type writeDefault(const std::string &s, const std::vector &x); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type writeDefault(const std::string &s, const std::vector &x); private: template @@ -59,10 +59,10 @@ namespace Grid template void readDefault(const std::string &s, U &output); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type readDefault(const std::string &s, std::vector &x); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type readDefault(const std::string &s, std::vector &x); private: template @@ -99,7 +99,7 @@ namespace Grid void Hdf5Writer::writeDefault(const std::string &s, const std::string &x); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type Hdf5Writer::writeDefault(const std::string &s, const std::vector &x) { // alias to element type @@ -135,7 +135,7 @@ namespace Grid } template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type Hdf5Writer::writeDefault(const std::string &s, const std::vector &x) { push(s); @@ -169,7 +169,7 @@ namespace Grid void Hdf5Reader::readDefault(const std::string &s, std::string &x); template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type Hdf5Reader::readDefault(const std::string &s, std::vector &x) { // alias to element type @@ -222,7 +222,7 @@ namespace Grid } template - typename std::enable_if>::is_arithmetic, void>::type + typename std::enable_if>::is_number, void>::type Hdf5Reader::readDefault(const std::string &s, std::vector &x) { uint64_t size; diff --git a/lib/serialisation/Hdf5Type.h b/lib/serialisation/Hdf5Type.h index 2e02128e..cf682138 100644 --- a/lib/serialisation/Hdf5Type.h +++ b/lib/serialisation/Hdf5Type.h @@ -10,13 +10,14 @@ #define HDF5_NATIVE_TYPE(predType, cType)\ template <>\ -struct Hdf5Type\ +class Hdf5Type\ {\ -static inline const H5NS::PredType & type(void)\ -{\ - return H5NS::PredType::predType;\ -}\ -static constexpr bool isNative = true;\ +public:\ + static inline const H5NS::DataType & type(void)\ + {\ + return H5NS::PredType::predType;\ + }\ + static constexpr bool isNative = true;\ }; #define DEFINE_HDF5_NATIVE_TYPES \ @@ -38,12 +39,36 @@ HDF5_NATIVE_TYPE(NATIVE_LDOUBLE, long double); namespace Grid { - template struct Hdf5Type + template class Hdf5Type { + public: static constexpr bool isNative = false; }; DEFINE_HDF5_NATIVE_TYPES; + + template + class Hdf5Type> + { + public: + static inline const H5NS::DataType & type(void) + { + if (typePtr_ == nullptr) + { + typePtr_.reset(new H5NS::CompType(sizeof(std::complex))); + typePtr_->insertMember("re", 0, Hdf5Type::type()); + typePtr_->insertMember("im", sizeof(R), Hdf5Type::type()); + } + + return *typePtr_; + } + static constexpr bool isNative = false; + private: + static std::unique_ptr typePtr_; + }; + + template + std::unique_ptr Hdf5Type>::typePtr_ = nullptr; } #undef HDF5_NATIVE_TYPE diff --git a/tests/IO/Test_serialisation.cc b/tests/IO/Test_serialisation.cc index b7158b2b..8204b05b 100644 --- a/tests/IO/Test_serialisation.cc +++ b/tests/IO/Test_serialisation.cc @@ -43,10 +43,14 @@ public: bool , b, std::vector, array, std::vector>, twodimarray, + std::vector>>, cmplx3darray ); myclass() {} myclass(int i) - : array(4,5.1), twodimarray(3,std::vector(2,1.23456)), ve(2, myenum::blue) + : array(4,5.1) + , twodimarray(3,std::vector(5, 1.23456)) + , cmplx3darray(3,std::vector>(5, std::vector(7, Complex(1.2, 3.4)))) + , ve(2, myenum::blue) { e=myenum::red; x=i;