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

Class for hdf5 attribute, which can be attached to groups and datasets. More...

#include <hdfattribute.hh>

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

Public Types

using Base = HDFObject< HDFCategory::attribute >
 alias base class More...
 

Public Member Functions

void swap (HDFAttribute &other)
 Exchange states between caller and argument. More...
 
HDFDataspace get_filespace ()
 Get the file-dataspace object. More...
 
auto get_type ()
 Get the type object. More...
 
HDFIdentifier get_parent_id ()
 Get the hdf5 identifier to which the attribute belongs. More...
 
void close ()
 closes the attribute More...
 
auto get_shape ()
 Get the shape object. More...
 
template<HDFCategory cat>
void open (const HDFObject< cat > &parent, std::string name)
 Open a new attribute on HDFObject 'parent' with name 'name'. More...
 
void open (const HDFIdentifier &parent, std::string name)
 Open a new attribute on HDFObject 'parent' with name 'name'. More...
 
template<typename Type >
auto read ()
 Reads data from attribute, and returns the data and its shape in the form of a hsize_t vector. More...
 
template<typename Type >
void read (Type &buffer)
 Reads data from attribute into a predefined buffer, and returns the data and its shape in the form of a hsize_t vector. User is responsible for providing a buffer which can hold the data and has the correct shape! More...
 
template<typename Type >
void write (Type attribute_data, std::vector< hsize_t > shape={})
 Function for writing data to the attribute. More...
 
template<typename Iter , typename Adaptor >
void write (Iter begin, Iter end, Adaptor adaptor=[](auto &value) { return value;}, [[maybe_unused]] std::vector< hsize_t > shape={})
 Function for writing data to the attribute. More...
 
 HDFAttribute ()=default
 Default constructor, deleted because of reference member. More...
 
 HDFAttribute (const HDFAttribute &other)=default
 Copy constructor. More...
 
 HDFAttribute (HDFAttribute &&other)=default
 Move constructor. More...
 
HDFAttributeoperator= (const HDFAttribute &other)=default
 Copy assignment operator. More...
 
HDFAttributeoperator= (HDFAttribute &&other)=default
 Attribute move assignment operator. More...
 
virtual ~HDFAttribute ()
 Destructor. More...
 
template<HDFCategory cat>
 HDFAttribute (const HDFObject< cat > &object, std::string name)
 Constructor for attribute. More...
 
- Public Member Functions inherited from Utopia::DataIO::HDFObject< HDFCategory::attribute >
void swap (HDFObject &other)
 swap the state of the caller with the state of the argument More...
 
std::string get_path () const
 Get the name or path object. More...
 
auto get_id_object () const
 Get the id object. More...
 
auto get_logger () const
 Get the logger object. More...
 
hid_t get_C_id () const
 Get the C id object. More...
 
auto get_refcount ()
 Get the reference count of object. More...
 
virtual bool is_valid () const
 Check if the object is still valid. More...
 
void close ()
 Close function which takes care of correctly closing the object and managing the reference counter. More...
 
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. More...
 
 HDFObject ()
 Construct HDFObject from the given arguments. More...
 
 HDFObject (HDFObject &&other)
 Construct HDFObject by moving. More...
 
 HDFObject (const HDFObject &other)=default
 Construct HDFObject by copying another object. More...
 
 HDFObject (hid_t id, std::function< herr_t(hid_t) > closing_func, std::string path={})
 Construct HDFObject from the given argument. More...
 
HDFObjectoperator= (const HDFObject &other)
 Copy assignment operator. More...
 
HDFObjectoperator= (HDFObject &&other)
 move assignment operator More...
 
virtual ~HDFObject ()
 Destroy the HDFObject object. Has to be implemented in subclass! More...
 

Private Member Functions

template<typename result_type >
void __create_attribute__ (hsize_t typesize=0)
 private helper function for creation of attribute More...
 
template<typename Type >
herr_t __write_container__ (Type attribute_data)
 Function for writing containers as attribute. More...
 
template<typename Type >
herr_t __write_stringtype__ (Type attribute_data)
 
template<typename Type >
herr_t __write_pointertype__ (Type attribute_data)
 
template<typename Type >
herr_t __write_scalartype__ (Type attribute_data)
 
template<typename Type >
herr_t __read_container__ (Type &buffer)
 
template<typename Type >
auto __read_stringtype__ (Type &buffer)
 
template<typename Type >
auto __read_pointertype__ (Type buffer)
 
template<typename Type >
auto __read_scalartype__ (Type &buffer)
 

Private Attributes

std::vector< hsize_t > _shape
 size of the attributes dataspace More...
 
HDFIdentifier _parent_identifier
 Identifier of the parent object. More...
 
HDFDataspace _dataspace
 Dataspace object that manages topology of data written and read. More...
 
HDFType _type
 type done in the HDFAttribute More...
 

Additional Inherited Members

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

Detailed Description

Class for hdf5 attribute, which can be attached to groups and datasets.

Template Parameters
HDFObjectthe object class

Member Typedef Documentation

◆ Base

alias base class

Constructor & Destructor Documentation

◆ HDFAttribute() [1/4]

Utopia::DataIO::HDFAttribute::HDFAttribute ( )
default

Default constructor, deleted because of reference member.

◆ HDFAttribute() [2/4]

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

Copy constructor.

Parameters
otherThe other

◆ HDFAttribute() [3/4]

Utopia::DataIO::HDFAttribute::HDFAttribute ( HDFAttribute &&  other)
default

Move constructor.

Parameters
otherThe other

◆ ~HDFAttribute()

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

Destructor.

◆ HDFAttribute() [4/4]

template<HDFCategory cat>
Utopia::DataIO::HDFAttribute::HDFAttribute ( const HDFObject< cat > &  object,
std::string  name 
)
inline

Constructor for attribute.

Parameters
objectthe object to create the attribute at
namethe name of the attribute
sizethe size of the attribute if known, else 1

Member Function Documentation

◆ __create_attribute__()

template<typename result_type >
void Utopia::DataIO::HDFAttribute::__create_attribute__ ( hsize_t  typesize = 0)
inlineprivate

private helper function for creation of attribute

Parameters
[in]typesizeThe typesize
Template Parameters
result_typeType of the resulting attribute
Returns
hid_t

◆ __read_container__()

template<typename Type >
herr_t Utopia::DataIO::HDFAttribute::__read_container__ ( Type &  buffer)
inlineprivate

◆ __read_pointertype__()

template<typename Type >
auto Utopia::DataIO::HDFAttribute::__read_pointertype__ ( Type  buffer)
inlineprivate

◆ __read_scalartype__()

template<typename Type >
auto Utopia::DataIO::HDFAttribute::__read_scalartype__ ( Type &  buffer)
inlineprivate

◆ __read_stringtype__()

template<typename Type >
auto Utopia::DataIO::HDFAttribute::__read_stringtype__ ( Type &  buffer)
inlineprivate

◆ __write_container__()

template<typename Type >
herr_t Utopia::DataIO::HDFAttribute::__write_container__ ( Type  attribute_data)
inlineprivate

Function for writing containers as attribute.

Only buffers if necessary, i.e. if non-vector containers or nested containers or containers of strings have to be written.

◆ __write_pointertype__()

template<typename Type >
herr_t Utopia::DataIO::HDFAttribute::__write_pointertype__ ( Type  attribute_data)
inlineprivate

◆ __write_scalartype__()

template<typename Type >
herr_t Utopia::DataIO::HDFAttribute::__write_scalartype__ ( Type  attribute_data)
inlineprivate

◆ __write_stringtype__()

template<typename Type >
herr_t Utopia::DataIO::HDFAttribute::__write_stringtype__ ( Type  attribute_data)
inlineprivate

◆ close()

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

closes the attribute

◆ get_filespace()

HDFDataspace Utopia::DataIO::HDFAttribute::get_filespace ( )
inline

Get the file-dataspace object.

Returns
HDFDataspace

◆ get_parent_id()

HDFIdentifier Utopia::DataIO::HDFAttribute::get_parent_id ( )
inline

Get the hdf5 identifier to which the attribute belongs.

Returns
weak pointer to HDFObject

◆ get_shape()

auto Utopia::DataIO::HDFAttribute::get_shape ( )
inline

Get the shape object.

Returns
std::vector<hsize_t>

◆ get_type()

auto Utopia::DataIO::HDFAttribute::get_type ( )
inline

Get the type object.

Returns
hid_t

◆ open() [1/2]

void Utopia::DataIO::HDFAttribute::open ( const HDFIdentifier parent,
std::string  name 
)
inline

Open a new attribute on HDFObject 'parent' with name 'name'.

Parameters
parentHDFIdentifier to open the attribute on
nameThe attribute's name

◆ open() [2/2]

template<HDFCategory cat>
void Utopia::DataIO::HDFAttribute::open ( const HDFObject< cat > &  parent,
std::string  name 
)
inline

Open a new attribute on HDFObject 'parent' with name 'name'.

Parameters
parentHDFObject to open the attribute on
nameThe attribute's name

◆ operator=() [1/2]

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

Copy assignment operator.

Parameters
otherObject to copy assign from
Returns
HDFAttribute&

◆ operator=() [2/2]

HDFAttribute& Utopia::DataIO::HDFAttribute::operator= ( HDFAttribute &&  other)
default

Attribute move assignment operator.

Parameters
otherObject to move assing from
Returns
HDFAttribute&

◆ read() [1/2]

template<typename Type >
auto Utopia::DataIO::HDFAttribute::read ( )
inline

Reads data from attribute, and returns the data and its shape in the form of a hsize_t vector.

This function has a quirk:

  • N-dimensional data are read into 1d arrays, and the shape has to be used to regain the original layout via index arithmetic.

Depending on the type 'Type', the data will be returned as:

  • if Type is a container other than vector: Will throw runtime_error
  • if Type is a vector or vector of vectors: Will return this, filled with data
  • if Type is a stringtype, i.e. char*, const char*, std::string: Will return std::string
  • if Type is a plain type, will return plain type
  • if Type as a pointer type, it will return a shared_ptr, i.e. Type = double*, return will be std::shared_ptr<double>;
Template Parameters
Typewhich can hold elements in the attribute and which will be returned
Returns
tuple containing (shape, data)

◆ read() [2/2]

template<typename Type >
void Utopia::DataIO::HDFAttribute::read ( Type &  buffer)
inline

Reads data from attribute into a predefined buffer, and returns the data and its shape in the form of a hsize_t vector. User is responsible for providing a buffer which can hold the data and has the correct shape!

Template Parameters
Typewhich can hold elements in the attribute and which will be returned

◆ swap()

void Utopia::DataIO::HDFAttribute::swap ( HDFAttribute other)
inline

Exchange states between caller and argument.

Parameters
other

◆ write() [1/2]

template<typename Iter , typename Adaptor >
void Utopia::DataIO::HDFAttribute::write ( Iter  begin,
Iter  end,
Adaptor  adaptor = [](auto& value) { return value; },
[[maybe_unused] ] std::vector< hsize_t >  shape = {} 
)
inline

Function for writing data to the attribute.

Template Parameters
IterIterator type
AdaptorAdaptor which allows for extraction of a value from compound types, i.e. classes or structs
Parameters
beginStart of iterator range
endEnd of iterator range
adaptorFunction which turns the elements of the iterator into the objects to be written. Should (auto&) argument (simplest), but else must have (typename Iter::value_type&) argument

◆ write() [2/2]

template<typename Type >
void Utopia::DataIO::HDFAttribute::write ( Type  attribute_data,
std::vector< hsize_t >  shape = {} 
)
inline

Function for writing data to the attribute.

Template Parameters
TypeAutomatically determined type of the data to write
Parameters
attribute_dataData to write
shapeLayout of the data, i.e. {20, 50} would indicate a 2d array like int a[20][50]; The parameter has only to be given if the data to be written is given as plain pointers, because the shape cannot be determined automatically then.
typelenIf the elements of the data to be written are arrays of equal length, and the data should be written as 1d attribute, then we can give the length of the arrays in order to speed up the memory allocation and avoid unecessary buffering. This can be useful for grids for instance.

Member Data Documentation

◆ _dataspace

HDFDataspace Utopia::DataIO::HDFAttribute::_dataspace
private

Dataspace object that manages topology of data written and read.

◆ _parent_identifier

HDFIdentifier Utopia::DataIO::HDFAttribute::_parent_identifier
private

Identifier of the parent object.

◆ _shape

std::vector< hsize_t > Utopia::DataIO::HDFAttribute::_shape
private

size of the attributes dataspace

◆ _type

HDFType Utopia::DataIO::HDFAttribute::_type
private

type done in the HDFAttribute


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