1 #include "../icedb/Attribute.hpp" 2 #include "../private/Attribute_impl.hpp" 3 #include "../private/hdf5_load.h" 4 #include "../private/hdf5_supplemental.hpp" 5 #include "../icedb/compat/gsl/gsl_assert" 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));
68 #if ICEDB_H5_HASH5OBJECTATTREXISTS == 1 72 htri_t res = H5Aexists(objid, attributeName.c_str());
74 return (res > 0) ? true :
false;
79 gsl::not_null<const H5::H5Object*>
parent,
const std::string &attributeName)
85 #if ICEDB_H5_HASH5OBJECTATTREXISTS == 1 86 return parent->attrExists(attributeName);
88 hid_t objid = parent->getId();
89 htri_t res = H5Aexists(objid, attributeName.c_str());
91 return (res > 0) ? true :
false;
98 std::set<std::string> anames;
100 for (
int i = 0; i < numAttrs; ++i) {
102 anames.insert(attr.getName());
108 template <
class DataType>
110 template<> constexpr
bool isString<std::string>() {
return true; }
118 template <
class DataType>
120 const std::string &attributeName,
121 std::vector<size_t> &dims,
122 std::vector<DataType> &tdata,
123 gsl::not_null<const H5::H5Object*> obj)
132 dims.resize(1, tdata.size());
149 template <
class DataType>
151 gsl::not_null<const H5::H5Object*>
parent,
152 const std::string &attributeName,
153 std::vector<size_t> &dims,
154 std::vector<DataType> &data)
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"));
163 template <
class DataType>
165 const std::string &attributeName,
166 std::vector<size_t> &dims,
167 std::vector<DataType> &data)
const 173 readAttributeData<DataType>(
parent.get(), attributeName, dims, data);
178 #define INST_READ_ATTR_TYPE(x) \ 179 template void CanHaveAttributes::readAttributeData<x>( \ 180 gsl::not_null<const H5::H5Object*> parent, \ 181 const std::string &attributeName, \ 182 std::vector<size_t> &dims, \ 183 std::vector<x> &data) ; \ 184 template void CanHaveAttributes::readAttributeData<x>( \ 185 const std::string &attributeName,\ 186 std::vector<size_t> &dims, \ 187 std::vector<x> &data) const; 192 template <
class DataType,
class ObjectType>
194 const std::string &attributeName,
195 const std::vector<size_t> &dimensionality,
196 std::shared_ptr<ObjectType> obj,
197 const std::vector<DataType> &data,
198 bool forceArray =
false 202 for (
const auto &s : dimensionality) numElems *= s;
203 Expects(numElems > 0);
205 if (isString<DataType>()) {
207 Expects(dimensionality.size() == 1);
208 Expects(dimensionality[0] == 1);
209 icedb::fs::hdf5::addAttr<DataType, ObjectType>(obj.get(), attributeName.c_str(), data[0]);
212 if ((dimensionality.size() == 1) && !forceArray)
213 icedb::fs::hdf5::addAttrVector<DataType, ObjectType>(obj.get(), attributeName.c_str(), data);
214 else icedb::fs::hdf5::addAttrArray<DataType, ObjectType>(obj.get(), attributeName.c_str(), dimensionality, data);
218 template <
class DataType>
220 const std::string &attributeName,
221 const std::vector<size_t> &dimensionality,
222 const std::vector<DataType> &data)
231 pushData<DataType, H5::H5Object>(attributeName, dimensionality,
parent, data);
234 #define INST_WRITE_ATTR_TYPE(x) \ 235 template void CanHaveAttributes::writeAttributeData( \ 236 const std::string &attributeName, \ 237 const std::vector<size_t> &dimensionality, \ 238 const std::vector<x> &data); virtual std::shared_ptr< H5::H5Object > _getAttributeParent() const override
INST_ATTR(INST_READ_ATTR_TYPE)
void deleteAttribute(const std::string &attributeName)
Delete an attribute, by name, that is attached to this object.
#define INST_READ_ATTR_TYPE(x)
constexpr bool isString()
Used to flag string types for special treatment.
std::set< std::string > getAttributeNames() const
List all attributes attached to this object.
DataContainerType getAttributeGroupingType(gsl::not_null< Container *> obj, gsl::not_null< const char *> attname)
virtual void _setAttributeParent(std::shared_ptr< H5::H5Object > obj) override
virtual ~CanHaveAttributes_impl()
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.
void readAttrVector(gsl::not_null< Container *> obj, gsl::not_null< const char *> attname, std::vector< DataType > &value)
Reads an array (or vector) of objects.
void pushData(const std::string &attributeName, const std::vector< size_t > &dimensionality, std::shared_ptr< ObjectType > obj, const std::vector< DataType > &data, bool forceArray=false)
void readAttr(gsl::not_null< Container *> obj, gsl::not_null< const char *> attname, DataType &value)
Convenient template to read an attribute of a variable.
std::shared_ptr< H5::H5Object > parent
void pullData(const std::string &attributeName, std::vector< size_t > &dims, std::vector< DataType > &tdata, gsl::not_null< const H5::H5Object *> obj)
Template function to pull data from the HDF5 object.
#define INST_WRITE_ATTR_TYPE(x)
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.
void readAttrArray(gsl::not_null< Container *> obj, gsl::not_null< const char *> attname, std::vector< size_t > &dims, std::vector< DataType > &value)
Reads an array (or vector) of objects.
std::type_index getAttributeTypeId(const std::string &attributeName) const
Returns the type of an attribute.
bool doesAttributeExist(const std::string &attributeName) const
Does the object have an attribute with the given name?