Utopia 2
Framework for studying models of complex & adaptive systems.
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Private Attributes | List of all members
Utopia::DataIO::HDFType Class Referencefinal

Class which handles the conversion of C-types into hdf5types. More...

#include <hdftype.hh>

Inheritance diagram for Utopia::DataIO::HDFType:
Inheritance graph
[legend]
Collaboration diagram for Utopia::DataIO::HDFType:
Collaboration graph
[legend]

Public Types

using Base = HDFObject< HDFCategory::datatype >
 
using Variant = std::variant< float, double, long double, int, short int, long int, long long int, unsigned int, unsigned short int, std::size_t, unsigned long long, bool, char, std::vector< float >, std::vector< double >, std::vector< long double >, std::vector< int >, std::vector< short int >, std::vector< long int >, std::vector< long long int >, std::vector< unsigned int >, std::vector< unsigned short int >, std::vector< std::size_t >, std::vector< unsigned long long >, std::vector< char >, std::vector< std::string >, std::string, const char * >
 

Public Member Functions

bool is_mutable () const
 Get if the type is mutable or not.
 
auto type_category () const
 Get the type category of the held type, i.e., scala, string, varlen,...
 
std::size_t size () const
 Size of the type held in bytes.
 
template<typename T >
void open (T &&object)
 Open the HDF5 type associated with an HDFObject, i.e., a dataset or an attribute.
 
template<typename T >
void open (std::string name, hsize_t typesize)
 Create an HDF datatype corresponding to the C datatype given as template argument.
 
void close ()
 Construct close from the given arguments.
 
virtual bool is_valid () const override
 Check if the held type identifier is still valid. Primitive types are valid by definition, because we have no control over them and hence they cannot be invalidated.
 
 HDFType (HDFType &&other)
 Construct HDFType from the given arguments by move, deleted, because apparently incompatible with HDF5 C backend.
 
HDFTypeoperator= (HDFType &&other)
 Move assign the type.
 
 HDFType (const HDFType &other)=default
 Construct HDFType from the given arguments by copy.
 
HDFTypeoperator= (const HDFType &other)=default
 Copy assign type.
 
virtual ~HDFType ()
 Destroy the HDFType object.
 
 HDFType ()
 Construct HDFType from by default.
 
template<typename T >
 HDFType (T &&object_or_size, std::enable_if_t< not std::is_same_v< HDFType, std::decay_t< T > >, int >=0)
 Construct HDFType from the given arguments.
 
- Public Member Functions inherited from Utopia::DataIO::HDFObject< HDFCategory::datatype >
void swap (HDFObject &other)
 swap the state of the caller with the state of the argument
 
std::string get_path () const
 Get the name or path object.
 
auto get_id_object () const
 Get the id object.
 
auto get_logger () const
 Get the logger object.
 
hid_t get_C_id () const
 Get the C id object.
 
auto get_refcount ()
 Get the reference count of object.
 
void close ()
 Close function which takes care of correctly closing the object and managing the reference counter.
 
void bind_to (hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
 Open the object and bind it to a HDF5 object identified by 'id' with name 'path'. Object should be created beforehand.
 
 HDFObject ()
 Construct HDFObject from the given arguments.
 
 HDFObject (HDFObject &&other)
 Construct HDFObject by moving.
 
 HDFObject (const HDFObject &other)=default
 Construct HDFObject by copying another object.
 
 HDFObject (hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
 Construct HDFObject from the given argument.
 
HDFObjectoperator= (const HDFObject &other)
 Copy assignment operator.
 
HDFObjectoperator= (HDFObject &&other)
 move assignment operator
 
virtual ~HDFObject ()
 Destroy the HDFObject object. Has to be implemented in subclass!
 

Private Attributes

bool _mutable = false
 
H5T_class_t _classid
 

Additional Inherited Members

- Static Public Attributes inherited from Utopia::DataIO::HDFObject< HDFCategory::datatype >
static constexpr HDFCategory category
 Named variable for template arg.
 
- Protected Attributes inherited from Utopia::DataIO::HDFObject< HDFCategory::datatype >
HDFIdentifier _id
 Identifier object that binds an instance of this class to an HDF5 object.
 
std::string _path
 Name of the object.
 
std::shared_ptr< spdlog::logger > _log
 pointer to the logger for dataio
 

Detailed Description

Class which handles the conversion of C-types into hdf5types.

Member Typedef Documentation

◆ Base

◆ Variant

using Utopia::DataIO::HDFType::Variant = std::variant< float, double, long double, int, short int, long int, long long int, unsigned int, unsigned short int, std::size_t, unsigned long long, bool, char, std::vector< float >, std::vector< double >, std::vector< long double >, std::vector< int >, std::vector< short int >, std::vector< long int >, std::vector< long long int >, std::vector< unsigned int >, std::vector< unsigned short int >, std::vector< std::size_t >, std::vector< unsigned long long >, std::vector< char >, std::vector< std::string >, std::string, const char* >

Constructor & Destructor Documentation

◆ HDFType() [1/4]

Utopia::DataIO::HDFType::HDFType ( HDFType &&  other)
inline

Construct HDFType from the given arguments by move, deleted, because apparently incompatible with HDF5 C backend.

363 : Base(static_cast< Base&& >(other))
364 {
365 _mutable = std::move(other._mutable);
366 _classid = std::move(other._classid);
367
368 other._mutable = false;
369 other._classid = H5T_NO_CLASS;
370 }
HDFObject< HDFCategory::datatype > Base
Definition hdftype.hh:146
H5T_class_t _classid
Definition hdftype.hh:143
bool _mutable
Definition hdftype.hh:140
Container select_entities(const Manager &mngr, const DataIO::Config &sel_cfg)
Select entities according to parameters specified in a configuration.
Definition select.hh:213

◆ HDFType() [2/4]

Utopia::DataIO::HDFType::HDFType ( const HDFType other)
default

Construct HDFType from the given arguments by copy.

◆ ~HDFType()

virtual Utopia::DataIO::HDFType::~HDFType ( )
inlinevirtual

Destroy the HDFType object.

409 {
410 close();
411 }
void close()
Construct close from the given arguments.
Definition hdftype.hh:322

◆ HDFType() [3/4]

Utopia::DataIO::HDFType::HDFType ( )
inline

Construct HDFType from by default.

417 : Base(), _mutable(false), _classid(H5T_NO_CLASS)
418 {
419 }

◆ HDFType() [4/4]

template<typename T >
Utopia::DataIO::HDFType::HDFType ( T &&  object_or_size,
std::enable_if_t< not std::is_same_v< HDFType, std::decay_t< T > >, int = 0 
)
inline

Construct HDFType from the given arguments.

Parameters
sizeSize of the type underlying the type to be created, or an HDFdataset or attribute to determine the held datatype of
429 :
430 HDFType()
431 {
433 }
void open(T &&object)
Open the HDF5 type associated with an HDFObject, i.e., a dataset or an attribute.
Definition hdftype.hh:224
HDFType()
Construct HDFType from by default.
Definition hdftype.hh:417

Member Function Documentation

◆ close()

void Utopia::DataIO::HDFType::close ( )
inline

Construct close from the given arguments.

323 {
324
325 // everything that is obtained via H5Tcopy, H5Topen or H5Tcreate
326 // needs to be released explicitly. This is given by the _mutable
327 // flag
328 Base::close();
329 _mutable = false;
331 }
void close()
Close function which takes care of correctly closing the object and managing the reference counter.
Definition hdfobject.hh:161

◆ is_mutable()

bool Utopia::DataIO::HDFType::is_mutable ( ) const
inline

Get if the type is mutable or not.

Returns
true
false
188 {
189 return _mutable;
190 }

◆ is_valid()

virtual bool Utopia::DataIO::HDFType::is_valid ( ) const
inlineoverridevirtual

Check if the held type identifier is still valid. Primitive types are valid by definition, because we have no control over them and hence they cannot be invalidated.

Returns
true
false

Reimplemented from Utopia::DataIO::HDFObject< HDFCategory::datatype >.

343 {
344 // this distinction is important because identifiers are not always
345 // checkable via H5Iis_valid
348 not(get_C_id() == -1))
349 {
350 return true;
351 }
352 else
353 {
354 return Base::is_valid();
355 }
356 }
virtual bool is_valid() const
Check if the object is still valid.
Definition hdfobject.hh:143
hid_t get_C_id() const
Get the C id object.
Definition hdfobject.hh:120

◆ open() [1/2]

template<typename T >
void Utopia::DataIO::HDFType::open ( std::string  name,
hsize_t  typesize 
)
inline

Create an HDF datatype corresponding to the C datatype given as template argument.

Template Parameters
T
Parameters
object_or_sizesize of the datatype in bytes, if not given, automatically determined: containers and strings become variable length size, everything else becomes scalar
256 {
257 this->_log->debug("Opening HDFType from scratch");
258
259 if (is_valid())
260 {
261 throw std::runtime_error(
262 "Error, cannot open HDFType '" + name + "' while it's still bound "
263 "to another valid type object! Close it first.");
264 }
265
266 // include const char* which is a c-string
267 if constexpr (Utils::is_container_v< T >)
268 {
269 if (typesize == 0ul)
270 {
272 Detail::get_type< typename T::value_type >()),
273 &H5Tclose,
274 name);
275 }
276 else
277 {
278 hsize_t dim[1] = { typesize };
279 bind_to(
281 Detail::get_type< typename T::value_type >(), 1, dim),
282 &H5Tclose,
283 name);
284 }
285 _mutable = true;
286 }
287 else if constexpr (Utils::is_string_v< T >)
288 {
289 hid_t type = H5Tcopy(H5T_C_S1);
290
291 if (typesize == 0)
292 {
294 }
295 else
296 {
297 H5Tset_size(type, typesize);
298 }
299 bind_to(std::move(type), &H5Tclose, name);
300
301 _mutable = true;
302 }
303 else
304 {
305 // native type objects like H5T_NATIVE_INT are interpreted as
306 // invalid by H5Iis_valid, hence at this point the `bind_to`
307 // function is not used, because it checks validity
308 _id.open(Detail::get_type< T >(),
309 [](hid_t) -> herr_t { return 0; });
310 _path = name;
311 _mutable = false;
312 }
313
315 }
void open(hid_t id, std::function< herr_t(hid_t) > closing_func)
Open the object and bind it to another C-Level id.
Definition hdfidentifier.hh:156
std::string _path
Name of the object.
Definition hdfobject.hh:50
std::shared_ptr< spdlog::logger > _log
pointer to the logger for dataio
Definition hdfobject.hh:56
HDFIdentifier _id
Identifier object that binds an instance of this class to an HDF5 object.
Definition hdfobject.hh:44
void bind_to(hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
Open the object and bind it to a HDF5 object identified by 'id' with name 'path'. Object should be cr...
Definition hdfobject.hh:186
virtual bool is_valid() const override
Check if the held type identifier is still valid. Primitive types are valid by definition,...
Definition hdftype.hh:342

◆ open() [2/2]

template<typename T >
void Utopia::DataIO::HDFType::open ( T &&  object)
inline

Open the HDF5 type associated with an HDFObject, i.e., a dataset or an attribute.

Template Parameters
T
Parameters
object
225 {
226 this->_log->debug("Opening HDFType from existing object {}",
227 object.get_path());
228
229 if (is_valid())
230 {
231 throw std::runtime_error(
232 "Error, cannot open HDFType while it's still bound to another "
233 "valid type object, close it first");
234 }
235
236 bind_to(open_type(object),
237 &H5Tclose, // we lock the type, hence no H5Tclose needed
238 "datatype of " + object.get_path());
239
240 _mutable = true;
242 }
std::string get_path() const
Get the name or path object.
Definition hdfobject.hh:88
hid_t open_type(Object &&object)
Depending on object category, invokes H5Dget_type or H5Aget_type.
Definition hdfutilities.hh:261

◆ operator=() [1/2]

HDFType & Utopia::DataIO::HDFType::operator= ( const HDFType other)
default

Copy assign type.

Returns
HDFType&

◆ operator=() [2/2]

HDFType & Utopia::DataIO::HDFType::operator= ( HDFType &&  other)
inline

Move assign the type.

Returns
HDFType&
379 {
380 static_cast<Base&>(*this) = static_cast< Base&& >(other);
381 _mutable = std::move(other._mutable);
382 _classid = std::move(other._classid);
383
384 other._mutable = false;
385 other._classid = H5T_NO_CLASS;
386
387 return *this;
388 }

◆ size()

std::size_t Utopia::DataIO::HDFType::size ( ) const
inline

Size of the type held in bytes.

Returns
std::size_t
211 {
212 return H5Tget_size(get_C_id());
213 }

◆ type_category()

auto Utopia::DataIO::HDFType::type_category ( ) const
inline

Get the type category of the held type, i.e., scala, string, varlen,...

Returns
auto type category of held type
200 {
201 return _classid;
202 }

Member Data Documentation

◆ _classid

H5T_class_t Utopia::DataIO::HDFType::_classid
private

◆ _mutable

bool Utopia::DataIO::HDFType::_mutable = false
private

The documentation for this class was generated from the following file: