HdfAttribute.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFATTRIBUTE_H
  5. #define HDF4CPP_HDFATTRIBUTE_H
  6. #include <hdf/hdf.h>
  7. #include <hdf/mfhdf.h>
  8. #include <string>
  9. #include <vector>
  10. #include <map>
  11. #include <memory>
  12. class HdfAttributeBase {
  13. public:
  14. HdfAttributeBase(int32 id, int32 index) : id(id), index(index) {}
  15. virtual ~HdfAttributeBase() {}
  16. virtual bool isValid() const { return index != FAIL; }
  17. virtual Type getType() const = 0;
  18. virtual intn size() const = 0;
  19. template <class T> bool get(std::vector<T> &dest) {
  20. intn length = size();
  21. if(length != FAIL) {
  22. auto it = typeSizeMap.find(getDataType());
  23. if(it != typeSizeMap.end()) {
  24. if(it->second != sizeof(T)) {
  25. throw std::runtime_error("HDF4CPP: type size missmatch");
  26. }
  27. } else {
  28. throw std::runtime_error("HDF4CPP: hdf data set type not supported");
  29. }
  30. dest.resize(length);
  31. return get(dest.data());
  32. } else {
  33. return false;
  34. }
  35. }
  36. protected:
  37. int32 id;
  38. int32 index;
  39. virtual bool get(void *dest) = 0;
  40. virtual int32 getDataType() const = 0;
  41. };
  42. class HdfDatasetAttribute : public HdfAttributeBase {
  43. public:
  44. HdfDatasetAttribute(int32 id, const std::string& name);
  45. Type getType() const;
  46. intn size() const;
  47. private:
  48. intn _size;
  49. int32 dataType;
  50. bool get(void *dest);
  51. int32 getDataType() const;
  52. };
  53. class HdfGroupAttribute : public HdfAttributeBase {
  54. public:
  55. HdfGroupAttribute(int32 id, const std::string& name);
  56. Type getType() const;
  57. intn size() const;
  58. private:
  59. intn _size;
  60. int32 dataType;
  61. bool get(void *dest);
  62. int32 getDataType() const;
  63. };
  64. class HdfAttribute {
  65. public:
  66. HdfAttribute(HdfAttributeBase *attribute) : attribute(attribute) {}
  67. HdfAttribute(const HdfAttribute&) = delete;
  68. HdfAttribute(HdfAttribute&& attribute);
  69. bool isValid() const;
  70. Type getType() const;
  71. intn size() const;
  72. template <class T> bool get(std::vector<T> &dest) {
  73. if(isValid()) {
  74. return attribute->get(dest);
  75. } else {
  76. return false;
  77. }
  78. }
  79. private:
  80. std::unique_ptr<HdfAttributeBase> attribute;
  81. };
  82. #endif //HDF4CPP_HDFATTRIBUTE_H