From 1ab31e6c197b68b7ea4f35fd0feb2fd49a84e3ed Mon Sep 17 00:00:00 2001 From: Guilhem Lavaux Date: Thu, 12 Feb 2015 08:50:07 +0100 Subject: [PATCH] hdf5_read_array now supports array witout resize. --- src/hdf5_array.hpp | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/src/hdf5_array.hpp b/src/hdf5_array.hpp index c6ce5c9..318181f 100644 --- a/src/hdf5_array.hpp +++ b/src/hdf5_array.hpp @@ -180,7 +180,49 @@ namespace CosmoTool { return boost::extents; } }; + + + template class array_has_resize { + struct Fallback { int resize; }; + struct Derived: Array, Fallback {}; + typedef char yes[1]; + typedef char no[2]; + + template struct Check; + + template + static yes& func(Check *); + + template + static no& func(...); + public: + typedef array_has_resize type; + enum { value = sizeof(func(0)) == sizeof(no) }; + }; + + + + template + typename boost::enable_if< + array_has_resize + >::type + hdf5_resize_array(ArrayType& data, std::vector& dims) { + data.resize( + hdf5_extent_gen::build(dims.data()) + ); + } + + template + typename boost::disable_if< + array_has_resize + >::type + hdf5_resize_array(ArrayType& data, std::vector& dims) { + for (long i = 0; i < data.num_dimensions(); i++) { + assert (data.shape()[i] == dims[i]); + } + } + template void hdf5_read_array(H5::CommonFG& fg, const std::string& data_set_name, ArrayType& data, @@ -196,7 +238,7 @@ namespace CosmoTool { } dataspace.getSimpleExtentDims(dimensions.data()); - data.resize(hdf5_extent_gen::build(dimensions.data())); + hdf5_resize_array(data, dimensions); dataset.read(data.data(), datatype); }