From df66a22cdf7a32e31d7c1441d69d6fa26bc1ffb8 Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Wed, 8 Aug 2018 21:51:24 -0500 Subject: [PATCH] Use overloads for templated functions rather than specializations --- src/hdf5_interface.h | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/src/hdf5_interface.h b/src/hdf5_interface.h index c859f221a..65a9e2c99 100644 --- a/src/hdf5_interface.h +++ b/src/hdf5_interface.h @@ -149,7 +149,7 @@ template struct H5TypeMap { static const hid_t type_id; }; //============================================================================== -// Template functions used to provide simple interface to lower-level functions +// Templates/overloads for read_attribute //============================================================================== // Scalar version @@ -196,9 +196,9 @@ void read_attribute(hid_t obj_id, const char* name, xt::xarray& arr) arr = xt::adapt(buffer, size, xt::acquire_ownership(), shape); } -// specialization for std::string -template<> inline void -read_attribute(hid_t obj_id, const char* name, std::string& str) +// overload for std::string +inline void +read_attribute(hid_t obj_id, const char* name, std::string& str) { // Create buffer to read data into auto n = attribute_typesize(obj_id, name); @@ -209,6 +209,10 @@ read_attribute(hid_t obj_id, const char* name, std::string& str) str = std::string{buffer, n}; } +//============================================================================== +// Templates/overloads for read_dataset +//============================================================================== + template void read_dataset(hid_t obj_id, const char* name, T buffer, bool indep=false) { @@ -264,14 +268,18 @@ void read_dataset(hid_t obj_id, const char* name, xt::xarray& arr, bool indep close_dataset(dset); } +//============================================================================== +// Templates/overloads for write_attribute +//============================================================================== + template inline void write_attribute(hid_t obj_id, const char* name, T buffer) { write_attr(obj_id, name, 0, nullptr, H5TypeMap::type_id, &buffer); } -template<> inline void -write_attribute(hid_t obj_id, const char* name, const char* buffer) +inline void +write_attribute(hid_t obj_id, const char* name, const char* buffer) { write_attr_string(obj_id, name, buffer); } @@ -283,14 +291,18 @@ write_attribute(hid_t obj_id, const char* name, const std::array& buffer) write_attr(obj_id, 1, dims, name, H5TypeMap::type_id, buffer.data()); } +//============================================================================== +// Templates/overloads for write_dataset +//============================================================================== + template inline void write_dataset(hid_t obj_id, const char* name, T buffer) { write_dataset(obj_id, 0, nullptr, name, H5TypeMap::type_id, &buffer, false); } -template<> inline void -write_dataset(hid_t obj_id, const char* name, const char* buffer) +inline void +write_dataset(hid_t obj_id, const char* name, const char* buffer) { write_string(obj_id, name, buffer, false); }