24 template<
typename T >
28 const bool mWriteMode ;
34 hvl_t * mData =
nullptr;
42 Dataset(
hid_t aFile,
const string & aLabel,
const hsize_t aSize=0,
43 const bool aForceWrite=
false ) :
46 mWriteMode( aForceWrite || aSize > 0 ),
60 BELFEM_ERROR(
false,
"Dataset %s does not exist", aLabel.c_str() );
63 mDataset = H5Dopen1( aFile, aLabel.c_str() );
78 hid_t tFileType = H5Dget_type( mDataset );
79 const H5T_class_t tOuterClass = H5Tget_class( tFileType );
82 const H5T_class_t tWantClass = H5Tget_class( tWantBase );
83 const size_t tWantBytes = H5Tget_size( tWantBase );
84 const H5T_sign_t tWantSign = ( tWantClass == H5T_INTEGER )
85 ? H5Tget_sign( tWantBase ) : H5T_SGN_NONE ;
90 H5T_class_t tBaseClass = H5T_NO_CLASS ;
91 size_t tBaseBytes = 0 ;
92 H5T_sign_t tBaseSign = H5T_SGN_NONE ;
94 if ( tOuterClass == H5T_VLEN )
96 hid_t tFileBase = H5Tget_super( tFileType );
97 tBaseClass = H5Tget_class( tFileBase );
98 tBaseBytes = H5Tget_size( tFileBase );
99 tBaseSign = ( tWantClass == H5T_INTEGER )
100 ? H5Tget_sign( tFileBase ) : H5T_SGN_NONE ;
101 H5Tclose( tFileBase );
105 H5Tclose( tFileType );
108 auto tFail = [ & ] () { H5Dclose( mDataset ); H5Tclose( mType ); };
110 if ( tOuterClass != H5T_VLEN )
114 "Dataset %s is not a variable-length array on disk", aLabel.c_str() );
116 if ( tBaseClass != tWantClass )
120 "On-disk element type of dataset %s does not match the requested type", aLabel.c_str() );
122 if ( tBaseBytes != tWantBytes )
126 "On-disk element size (%lu) of dataset %s does not match requested size (%lu)",
127 (
long unsigned int ) tBaseBytes, aLabel.c_str(), (
long unsigned int ) tWantBytes );
129 if ( tBaseSign != tWantSign )
133 "On-disk element signedness of dataset %s does not match the requested type", aLabel.c_str() );
137 mSpace = H5Dget_space( mDataset );
140 hssize_t tNumPoints = H5Sget_simple_extent_npoints( mSpace );
141 if ( tNumPoints < 0 )
144 H5Dclose( mDataset );
146 BELFEM_ERROR(
false,
"Failed to read extent of dataset %s", aLabel.c_str() );
148 mSize =
static_cast< hsize_t >( tNumPoints );
151 mData =
new hvl_t[ mSize ]() ;
153 herr_t tStatus = H5Dread( mDataset, mType, H5S_ALL, H5S_ALL, H5P_DEFAULT, mData );
161 H5Dclose( mDataset );
163 BELFEM_ERROR(
false,
"Failed to read dataset %s", aLabel.c_str() );
169 mSpace = H5Screate_simple( 1, &aSize,
nullptr );
170 mDataset = H5Dcreate2(
179 mData =
new hvl_t[ aSize ]() ;
184 Dataset(
const Dataset & ) = delete ;
185 Dataset & operator=(
const Dataset & ) = delete ;
186 Dataset( Dataset && ) = delete ;
187 Dataset & operator=( Dataset && ) = delete ;
192 BELFEM_ASSERT( mWriteMode,
"set_size() called on read-mode dataset %s", mLabel.c_str() );
194 BELFEM_ASSERT( aIndex < mSize ,
"Index %lu out of range (must be < %lu)",
195 (
long unsigned int ) aIndex,
196 (
long unsigned int ) mSize );
198 BELFEM_ASSERT( mData[ aIndex ].len == 0 ,
"Index %lu of dataset %s already allocated",
199 (
long unsigned int ) aIndex, mLabel.c_str() );
201 mData[ aIndex ].p =
new T[ aMemory ];
202 mData[ aIndex ].len = aMemory;
203 return static_cast< T *
>( mData[ aIndex ].p );
206 operator[](
const hsize_t aIndex )
208 BELFEM_ASSERT( aIndex < mSize ,
"Index %lu out of range (must be < %lu)",
209 (
long unsigned int ) aIndex,
210 (
long unsigned int ) mSize );
211 return static_cast< T *
>( mData[ aIndex ].p );
215 operator[](
const hsize_t aIndex )
const
217 BELFEM_ASSERT( aIndex < mSize ,
"Index %lu out of range (must be < %lu)",
218 (
long unsigned int ) aIndex,
219 (
long unsigned int ) mSize );
220 return static_cast< const T *
>( mData[ aIndex ].p );
226 length(
const hsize_t aIndex )
const
228 BELFEM_ASSERT( aIndex < mSize ,
"Index %lu out of range (must be < %lu)",
229 (
long unsigned int ) aIndex,
230 (
long unsigned int ) mSize );
231 return mData[ aIndex ].len ;
237 if ( mData ==
nullptr ) return ;
242 for (
hsize_t k=0; k<mSize; ++k )
244 delete[]
static_cast< T *
>( mData[ k ].p );
250 H5Dvlen_reclaim( mType, mSpace, H5P_DEFAULT, mData );
257 H5Dclose( mDataset );
275 BELFEM_ASSERT( mWriteMode,
"dataset %s not in write mode", mLabel.c_str() );
277 herr_t tStatus = H5Dwrite( mDataset, mType, H5S_ALL, H5S_ALL, H5P_DEFAULT, mData );
279 BELFEM_ERROR( tStatus == 0 ,
"Failed to write dataset %s", mLabel.c_str() );
bool dataset_exists(hid_t aLoc, const std::string &aLabel)
test if a dataset exists ( tests for a link of that name; the link type is not checked )
Definition hdf5_tools.hpp:37