icedb  version 0.5.1
Snow particle scattering database API
Attribute.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <memory>
3 #include <set>
4 #include <string>
5 #include <vector>
6 #include "compat/gsl/gsl_assert"
7 #include "compat/gsl/gsl"
8 #include "Data_Types.hpp"
9 
10 
11 namespace icedb {
13  namespace Attributes {
23  template <class DataType> class Attribute {
24  protected:
25  Attribute() : Attribute("", {}, {}) {}
26  public:
27  typedef DataType type;
30  std::vector<DataType> data;
32  std::string name;
34  std::vector<size_t> dimensionality;
36  inline bool isArray() const {
37  if (dimensionality.size() > 1) return true;
38  return false;
39  }
40 
42  Attribute(const std::string &name) : Attribute(name, {}, {}) {}
44  Attribute(const std::string &name, DataType val)
45  : Attribute(name, { 1 }, { val }) {}
47  Attribute(const std::string &name, std::initializer_list<size_t> dims, std::initializer_list<DataType> data)
48  : name{ name }, dimensionality{ dims }, data{ data }
49  {
50  static_assert(icedb::Data_Types::Is_Valid_Data_Type<DataType>() == true,
51  "Attributes must be a valid data type");
52 
53  //size_t sz = 1;
54  //for (const auto &d : dims) sz *= d;
55  //Expects(data.size() == sz);
56  }
57  };
58 
62  bool valid() const;
63  protected:
65  virtual void _setAttributeParent(std::shared_ptr<H5::H5Object> obj) = 0;
66  virtual std::shared_ptr<H5::H5Object> _getAttributeParent() const = 0;
67  public:
70  bool doesAttributeExist(const std::string &attributeName) const;
72  static bool doesAttributeExist(gsl::not_null<const H5::H5Object*> parent, const std::string &attributeName);
75  std::type_index getAttributeTypeId(const std::string &attributeName) const;
78  static std::type_index getAttributeTypeId(gsl::not_null<const H5::H5Object*> parent, const std::string &attributeName);
81  template<class Type> bool isAttributeOfType(const std::string &attributeName) const {
82  std::type_index atype = getAttributeTypeId(attributeName);
83  if (atype == typeid(Type)) return true;
84  return false;
85  }
86 
88  std::set<std::string> getAttributeNames() const;
91  void deleteAttribute(const std::string &attributeName);
92 
103  template <class DataType> static void readAttributeData(
104  gsl::not_null<const H5::H5Object*> parent,
105  const std::string &attributeName,
106  std::vector<size_t> &dimensions,
107  std::vector<DataType> &data);
116  template <class DataType> void readAttributeData(
117  const std::string &attributeName,
118  std::vector<size_t> &dimensions,
119  std::vector<DataType> &data) const;
126  template <class DataType> void writeAttributeData(
127  const std::string &attributeName,
128  const std::vector<size_t> &dimensionas,
129  const std::vector<DataType> &data);
130 
138  template <class DataType> static Attribute<DataType> readAttribute(
139  gsl::not_null<const H5::H5Object*> obj, const std::string &attributeName)
140  {
141  Attribute<DataType> res(attributeName);
142  readAttributeData(obj, attributeName, res.dimensionality, res.data);
143  return res;
144  }
150  template<class DataType> Attribute<DataType> readAttribute(const std::string &attributeName) const {
151  Attribute<DataType> res(attributeName);
152  readAttributeData(attributeName, res.dimensionality, res.data);
153  return res;
154  }
155 
160  template<class DataType> void writeAttribute(const Attribute<DataType> &attribute) {
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  }
175  template<class DataType> void writeAttribute(
176  const std::string &name,
177  std::initializer_list<size_t> dims,
178  std::initializer_list<DataType> data) {
179  Attribute<DataType> attr(name, dims, data);
180  writeAttribute(attr);
181  }
182 
183 
184  };
185 
186  }
187 }
Attribute(const std::string &name, std::initializer_list< size_t > dims, std::initializer_list< DataType > data)
Create a multi-dimensional attribute with pre-populated data.
Definition: Attribute.hpp:47
std::vector< size_t > dimensionality
The attribute&#39;s dimensional span. NetCDF only accepts one-dimensional attributes. ...
Definition: Attribute.hpp:34
void writeAttribute(const Attribute< DataType > &attribute)
Convenience function to write an Attribute to an object.
Definition: Attribute.hpp:160
Attribute(const std::string &name, DataType val)
Create a zero-dimensional attribute with pre-populated data.
Definition: Attribute.hpp:44
std::string name
The name of the attribute.
Definition: Attribute.hpp:32
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.
Definition: Attribute.hpp:175
static Attribute< DataType > readAttribute(gsl::not_null< const H5::H5Object *> obj, const std::string &attributeName)
Convenience function to read an attribute&#39;s data and return an Attribute object.
Definition: Attribute.hpp:138
std::vector< DataType > data
Definition: Attribute.hpp:30
This is a virtual base class for objects that can have attributes. This includes tables, groups and HDF5 files.
Definition: Attribute.hpp:61
bool isAttributeOfType(const std::string &attributeName) const
Is the type of the attribute "Type"?
Definition: Attribute.hpp:81
Attribute< DataType > readAttribute(const std::string &attributeName) const
Convenience function to read an attribute&#39;s data and return an Attribute object.
Definition: Attribute.hpp:150
This class defines an attribute.
Definition: Attribute.hpp:23
bool isArray() const
Does this attribute span more than one dimension?
Definition: Attribute.hpp:36
Attribute(const std::string &name)
Create an empty attribute.
Definition: Attribute.hpp:42