icedb  version 0.5.1
Snow particle scattering database API
Public Member Functions | Static Public Member Functions | Protected Member Functions | Private Member Functions | List of all members
icedb::Attributes::CanHaveAttributes Class Referenceabstract

This is a virtual base class for objects that can have attributes. This includes tables, groups and HDF5 files. More...

#include <Attribute.hpp>

Inheritance diagram for icedb::Attributes::CanHaveAttributes:
Inheritance graph
[legend]

Public Member Functions

 ~CanHaveAttributes ()
 
bool doesAttributeExist (const std::string &attributeName) const
 Does the object have an attribute with the given name? More...
 
std::type_index getAttributeTypeId (const std::string &attributeName) const
 Returns the type of an attribute. More...
 
template<class Type >
bool isAttributeOfType (const std::string &attributeName) const
 Is the type of the attribute "Type"? More...
 
std::set< std::string > getAttributeNames () const
 List all attributes attached to this object. More...
 
void deleteAttribute (const std::string &attributeName)
 Delete an attribute, by name, that is attached to this object. More...
 
template<class DataType >
void readAttributeData (const std::string &attributeName, std::vector< size_t > &dimensions, std::vector< DataType > &data) const
 Function to read the data from an attribute using the provided input vectors. More...
 
template<class DataType >
void writeAttributeData (const std::string &attributeName, const std::vector< size_t > &dimensionas, const std::vector< DataType > &data)
 Function to write an attribute to an object, with the provided raw data. More...
 
template<class DataType >
Attribute< DataType > readAttribute (const std::string &attributeName) const
 Convenience function to read an attribute's data and return an Attribute object. More...
 
template<class DataType >
void writeAttribute (const Attribute< DataType > &attribute)
 Convenience function to write an Attribute to an object. More...
 
template<class DataType >
void writeAttribute (const std::string &name, std::initializer_list< size_t > dims, std::initializer_list< DataType > data)
 Convenience function to write a small Attribute to an object, using initializer lists. More...
 

Static Public Member Functions

static bool doesAttributeExist (gsl::not_null< const H5::H5Object *> parent, const std::string &attributeName)
 Does an object (parent) have an attribute with the given name? More...
 
static std::type_index getAttributeTypeId (gsl::not_null< const H5::H5Object *> parent, const std::string &attributeName)
 Returns the type of an attribute. More...
 
template<class DataType >
static void readAttributeData (gsl::not_null< const H5::H5Object *> parent, const std::string &attributeName, std::vector< size_t > &dimensions, std::vector< DataType > &data)
 Function to read the data from an attribute. More...
 
template<class DataType >
static Attribute< DataType > readAttribute (gsl::not_null< const H5::H5Object *> obj, const std::string &attributeName)
 Convenience function to read an attribute's data and return an Attribute object. More...
 

Protected Member Functions

 CanHaveAttributes ()
 
virtual void _setAttributeParent (std::shared_ptr< H5::H5Object > obj)=0
 
virtual std::shared_ptr< H5::H5Object > _getAttributeParent () const =0
 

Private Member Functions

bool valid () const
 

Detailed Description

This is a virtual base class for objects that can have attributes. This includes tables, groups and HDF5 files.

Definition at line 61 of file Attribute.hpp.

Constructor & Destructor Documentation

◆ CanHaveAttributes()

icedb::Attributes::CanHaveAttributes::CanHaveAttributes ( )
protected

Definition at line 11 of file Attributes.cpp.

11 {}

◆ ~CanHaveAttributes()

icedb::Attributes::CanHaveAttributes::~CanHaveAttributes ( )

Definition at line 12 of file Attributes.cpp.

12 {}

Member Function Documentation

◆ _getAttributeParent()

virtual std::shared_ptr<H5::H5Object> icedb::Attributes::CanHaveAttributes::_getAttributeParent ( ) const
protectedpure virtual

◆ _setAttributeParent()

virtual void icedb::Attributes::CanHaveAttributes::_setAttributeParent ( std::shared_ptr< H5::H5Object >  obj)
protectedpure virtual

◆ deleteAttribute()

void icedb::Attributes::CanHaveAttributes::deleteAttribute ( const std::string &  attributeName)

Delete an attribute, by name, that is attached to this object.

Exceptions
ifthe attribute does not exist.

Definition at line 55 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), and valid().

Referenced by writeAttributeData().

56  {
57  Expects(valid());
58  _getAttributeParent()->removeAttr(attributeName);
59  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ doesAttributeExist() [1/2]

bool icedb::Attributes::CanHaveAttributes::doesAttributeExist ( const std::string &  attributeName) const

Does the object have an attribute with the given name?

Note
Despite the documentation in HDF5 1.8, the attrExists function does not, in fact, exist! Tested on HDF5 1.8.5-patch1 on CentOS6, GCC 7. Docs state that this is a method of H5Object.

Definition at line 61 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), and valid().

Referenced by icedb::Shapes::Shape::isShape(), icedb::Shapes::Shape::isValid(), readAttributeData(), and writeAttributeData().

62  {
67  Expects(valid());
68 #if ICEDB_H5_HASH5OBJECTATTREXISTS == 1
69  return _getAttributeParent()->attrExists(attributeName);
70 #else
71  hid_t objid = _getAttributeParent()->getId();
72  htri_t res = H5Aexists(objid, attributeName.c_str());
73  if (res < 0) throw;
74  return (res > 0) ? true : false;
75 #endif
76  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ doesAttributeExist() [2/2]

bool icedb::Attributes::CanHaveAttributes::doesAttributeExist ( gsl::not_null< const H5::H5Object *>  parent,
const std::string &  attributeName 
)
static

Does an object (parent) have an attribute with the given name?

Note
Despite the documentation in HDF5 1.8, the attrExists function does not, in fact, exist! Tested on HDF5 1.8.5-patch1 on CentOS6, GCC 7. Docs state that this is a method of H5Object.

Definition at line 78 of file Attributes.cpp.

80  {
85 #if ICEDB_H5_HASH5OBJECTATTREXISTS == 1
86  return parent->attrExists(attributeName);
87 #else
88  hid_t objid = parent->getId();
89  htri_t res = H5Aexists(objid, attributeName.c_str());
90  if (res < 0) throw;
91  return (res > 0) ? true : false;
92 #endif
93  }

◆ getAttributeNames()

std::set< std::string > icedb::Attributes::CanHaveAttributes::getAttributeNames ( ) const

List all attributes attached to this object.

Definition at line 95 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), and valid().

95  {
96  Expects(valid());
97  //icedb::fs::hdf5::
98  std::set<std::string> anames;
99  int numAttrs = _getAttributeParent()->getNumAttrs();
100  for (int i = 0; i < numAttrs; ++i) {
101  H5::Attribute attr = _getAttributeParent()->openAttribute(i);
102  anames.insert(attr.getName());
103  }
104  return anames;
105  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
Here is the call graph for this function:

◆ getAttributeTypeId() [1/2]

std::type_index icedb::Attributes::CanHaveAttributes::getAttributeTypeId ( const std::string &  attributeName) const

Returns the type of an attribute.

Exceptions
ifthe attribute does not exist.

Definition at line 50 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), and icedb::Attributes::CanHaveAttributes_impl::parent.

Referenced by icedb::Shapes::Shape::isShape(), and icedb::Shapes::Shape::isValid().

50  {
51  auto parent = _getAttributeParent();
52  return getAttributeTypeId(parent.get(), attributeName);
53  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
std::type_index getAttributeTypeId(const std::string &attributeName) const
Returns the type of an attribute.
Definition: Attributes.cpp:50
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getAttributeTypeId() [2/2]

std::type_index icedb::Attributes::CanHaveAttributes::getAttributeTypeId ( gsl::not_null< const H5::H5Object *>  parent,
const std::string &  attributeName 
)
static

Returns the type of an attribute.

Exceptions
ifthe attribute does not exist.

Definition at line 34 of file Attributes.cpp.

34  {
35  if (fs::hdf5::isType<uint64_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(uint64_t));
36  if (fs::hdf5::isType<int64_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(int64_t));
37  if (fs::hdf5::isType<uint32_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(uint32_t));
38  if (fs::hdf5::isType<int32_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(int32_t));
39  if (fs::hdf5::isType<uint16_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(uint16_t));
40  if (fs::hdf5::isType<int16_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(int16_t));
41  if (fs::hdf5::isType<uint8_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(uint8_t));
42  if (fs::hdf5::isType<int8_t, H5::H5Object>(parent.get(), attributeName)) return (typeid(int8_t));
43  if (fs::hdf5::isType<double, H5::H5Object>(parent.get(), attributeName)) return (typeid(double));
44  if (fs::hdf5::isType<float, H5::H5Object>(parent.get(), attributeName)) return (typeid(float));
45  if (fs::hdf5::isType<char, H5::H5Object>(parent.get(), attributeName)) return (typeid(char));
46  if (fs::hdf5::isType<std::string, H5::H5Object>(parent.get(), attributeName)) return (typeid(std::string));
47  throw;
48  }

◆ isAttributeOfType()

template<class Type >
bool icedb::Attributes::CanHaveAttributes::isAttributeOfType ( const std::string &  attributeName) const
inline

Is the type of the attribute "Type"?

Exceptions
ifthe attribute does not exist.

Definition at line 81 of file Attribute.hpp.

References icedb::Attributes::Attribute< DataType >::data.

81  {
82  std::type_index atype = getAttributeTypeId(attributeName);
83  if (atype == typeid(Type)) return true;
84  return false;
85  }
std::type_index getAttributeTypeId(const std::string &attributeName) const
Returns the type of an attribute.
Definition: Attributes.cpp:50

◆ readAttribute() [1/2]

template<class DataType >
static Attribute<DataType> icedb::Attributes::CanHaveAttributes::readAttribute ( gsl::not_null< const H5::H5Object *>  obj,
const std::string &  attributeName 
)
inlinestatic

Convenience function to read an attribute's data and return an Attribute object.

Exceptions
ifthe base object does not exist
ifthe attribute has a mismatched DataType
ifthe attribute does not exist
Parameters
objis the containing object
DataTypeis the type of the data
attributeNameis the name of the attribute

Definition at line 138 of file Attribute.hpp.

References icedb::Attributes::Attribute< DataType >::data, and icedb::Attributes::Attribute< DataType >::dimensionality.

140  {
141  Attribute<DataType> res(attributeName);
142  readAttributeData(obj, attributeName, res.dimensionality, res.data);
143  return res;
144  }
static void readAttributeData(gsl::not_null< const H5::H5Object *> parent, const std::string &attributeName, std::vector< size_t > &dimensions, std::vector< DataType > &data)
Function to read the data from an attribute.
Definition: Attributes.cpp:150

◆ readAttribute() [2/2]

template<class DataType >
Attribute<DataType> icedb::Attributes::CanHaveAttributes::readAttribute ( const std::string &  attributeName) const
inline

Convenience function to read an attribute's data and return an Attribute object.

Exceptions
ifthe attribute has a mismatched DataType
ifthe attribute does not exist
Parameters
DataTypeis the type of the data
attributeNameis the name of the attribute

Definition at line 150 of file Attribute.hpp.

References icedb::Attributes::Attribute< DataType >::data, and icedb::Attributes::Attribute< DataType >::dimensionality.

150  {
151  Attribute<DataType> res(attributeName);
152  readAttributeData(attributeName, res.dimensionality, res.data);
153  return res;
154  }
static void readAttributeData(gsl::not_null< const H5::H5Object *> parent, const std::string &attributeName, std::vector< size_t > &dimensions, std::vector< DataType > &data)
Function to read the data from an attribute.
Definition: Attributes.cpp:150

◆ readAttributeData() [1/2]

template<class DataType >
void icedb::Attributes::CanHaveAttributes::readAttributeData ( gsl::not_null< const H5::H5Object *>  parent,
const std::string &  attributeName,
std::vector< size_t > &  dimensions,
std::vector< DataType > &  data 
)
static

Function to read the data from an attribute.

Exceptions
ifthe attribute does not exist
ifthe parent is invalid
Parameters
DataTypeis the type of the data to be read
Exceptions
ifthere is a type mismatch between the actual data's type and the type expected in the function call.
Parameters
parentis the container of the attribute
attributeNameis the name of the attribute
dimensionsare the returned dimensions of the attribute.
dataare the returned attribute data. The mapping of dimensions and data follows the regular C convention (in contrast to the Fortran one).

Definition at line 150 of file Attributes.cpp.

References doesAttributeExist().

155  {
156  Expects(doesAttributeExist(parent,attributeName));
157 
158  if (icedb::fs::hdf5::isType<DataType, H5::H5Object>(parent.get(), attributeName))
159  pullData<DataType>(attributeName, dims, data, parent.get());
160  else throw(std::invalid_argument("Unhandled data type"));
161  }
bool doesAttributeExist(const std::string &attributeName) const
Does the object have an attribute with the given name?
Definition: Attributes.cpp:61
Here is the call graph for this function:

◆ readAttributeData() [2/2]

template<class DataType >
void icedb::Attributes::CanHaveAttributes::readAttributeData ( const std::string &  attributeName,
std::vector< size_t > &  dimensions,
std::vector< DataType > &  data 
) const

Function to read the data from an attribute using the provided input vectors.

Exceptions
ifthe attribute does not exist
Parameters
DataTypeis the type of the data to be read
Exceptions
ifthere is a type mismatch between the actual data's type and the type expected in the function call.
Parameters
attributeNameis the name of the attribute
dimensionsare the returned dimensions of the attribute.
dataare the returned attribute data. The mapping of dimensions and data follows the regular C convention (in contrast to the Fortran one).

Definition at line 164 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), doesAttributeExist(), icedb::Attributes::CanHaveAttributes_impl::parent, and valid().

168  {
169  Expects(valid());
170  Expects(doesAttributeExist(attributeName));
171  auto parent = _getAttributeParent();
172 
173  readAttributeData<DataType>(parent.get(), attributeName, dims, data);
174  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
bool doesAttributeExist(const std::string &attributeName) const
Does the object have an attribute with the given name?
Definition: Attributes.cpp:61
Here is the call graph for this function:

◆ valid()

bool icedb::Attributes::CanHaveAttributes::valid ( ) const
private

Definition at line 19 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent().

Referenced by deleteAttribute(), doesAttributeExist(), getAttributeNames(), readAttributeData(), and writeAttributeData().

19  {
20  if (!_getAttributeParent()) return false;
21  return true;
22  }
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
Here is the call graph for this function:
Here is the caller graph for this function:

◆ writeAttribute() [1/2]

template<class DataType >
void icedb::Attributes::CanHaveAttributes::writeAttribute ( const Attribute< DataType > &  attribute)
inline

Convenience function to write an Attribute to an object.

Exceptions
ifthe attribute is somehow invalid (usually from not giving it a proper name).
Parameters
DataTypeis the type of the data
isan Attribute<DataType> object

Definition at line 160 of file Attribute.hpp.

References icedb::Attributes::Attribute< DataType >::data, icedb::Attributes::Attribute< DataType >::dimensionality, and icedb::Attributes::Attribute< DataType >::name.

160  {
161  size_t sz = 1;
162  for (const auto &d : attribute.dimensionality) sz *= d;
163  Expects(attribute.data.size() == sz);
164 
165  writeAttributeData(attribute.name, attribute.dimensionality, attribute.data);
166  }
void writeAttributeData(const std::string &attributeName, const std::vector< size_t > &dimensionas, const std::vector< DataType > &data)
Function to write an attribute to an object, with the provided raw data.
Definition: Attributes.cpp:219

◆ writeAttribute() [2/2]

template<class DataType >
void icedb::Attributes::CanHaveAttributes::writeAttribute ( const std::string &  name,
std::initializer_list< size_t >  dims,
std::initializer_list< DataType >  data 
)
inline

Convenience function to write a small Attribute to an object, using initializer lists.

Example: grpTest1->writeAttribute<uint64_t>("TestInt5", {1}, {65536});

Parameters
nameis the attribute's name
dimsare the dimensions
dataare the data
Exceptions
ifthe name is invalid

Definition at line 175 of file Attribute.hpp.

178  {
179  Attribute<DataType> attr(name, dims, data);
180  writeAttribute(attr);
181  }
void writeAttribute(const Attribute< DataType > &attribute)
Convenience function to write an Attribute to an object.
Definition: Attribute.hpp:160

◆ writeAttributeData()

template<class DataType >
void icedb::Attributes::CanHaveAttributes::writeAttributeData ( const std::string &  attributeName,
const std::vector< size_t > &  dimensionas,
const std::vector< DataType > &  data 
)

Function to write an attribute to an object, with the provided raw data.

Exceptions
ifthe base object is read-only
Parameters
DataTypeis the type of the data to be read
attributeNameis the name of the attribute
dimensionsare the dimensions of the attribute.
dataare the attribute data. The mapping of dimensions and data follows the regular C convention (in contrast to the Fortran one).

Definition at line 219 of file Attributes.cpp.

References icedb::Attributes::CanHaveAttributes_impl::_getAttributeParent(), deleteAttribute(), doesAttributeExist(), icedb::Attributes::CanHaveAttributes_impl::parent, and valid().

223  {
224  Expects(valid());
225  if (doesAttributeExist(attributeName)) deleteAttribute(attributeName);
226  auto parent = _getAttributeParent();
227  // Need to copy from the variant structure into an array of the exact data type
228 
229  //if (icedb::fs::hdf5::isType<DataType, H5::H5Object>(parent.get(), attributeName))
230  //pullData<DataType>(attributeName, dims, data, parent.get());
231  pushData<DataType, H5::H5Object>(attributeName, dimensionality, parent, data);
232  }
void deleteAttribute(const std::string &attributeName)
Delete an attribute, by name, that is attached to this object.
Definition: Attributes.cpp:55
virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const =0
bool doesAttributeExist(const std::string &attributeName) const
Does the object have an attribute with the given name?
Definition: Attributes.cpp:61
Here is the call graph for this function:

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