HdfAttribute.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. namespace hdf4cpp {
  13. class HdfAttributeBase {
  14. public:
  15. HdfAttributeBase(int32 id, int32 index) : id(id), index(index) {}
  16. virtual ~HdfAttributeBase() {}
  17. virtual bool isValid() const { return index != FAIL; }
  18. virtual Type getType() const = 0;
  19. virtual intn size() const = 0;
  20. template <class T> bool get(std::vector<T> &dest) {
  21. intn length = size();
  22. if(length != FAIL) {
  23. auto it = typeSizeMap.find(getDataType());
  24. if(it != typeSizeMap.end()) {
  25. if(it->second != sizeof(T)) {
  26. throw std::runtime_error("HDF4CPP: type size missmatch");
  27. }
  28. } else {
  29. throw std::runtime_error("HDF4CPP: hdf data set type not supported");
  30. }
  31. dest.resize(length);
  32. return get(dest.data());
  33. } else {
  34. return false;
  35. }
  36. }
  37. protected:
  38. int32 id;
  39. int32 index;
  40. virtual bool get(void *dest) = 0;
  41. virtual int32 getDataType() const = 0;
  42. };
  43. class HdfDatasetAttribute : public HdfAttributeBase {
  44. public:
  45. HdfDatasetAttribute(int32 id, const std::string& name);
  46. Type getType() const;
  47. intn size() const;
  48. private:
  49. intn _size;
  50. int32 dataType;
  51. bool get(void *dest);
  52. int32 getDataType() const;
  53. };
  54. class HdfGroupAttribute : public HdfAttributeBase {
  55. public:
  56. HdfGroupAttribute(int32 id, const std::string& name);
  57. Type getType() const;
  58. intn size() const;
  59. private:
  60. intn _size;
  61. int32 dataType;
  62. bool get(void *dest);
  63. int32 getDataType() const;
  64. };
  65. class HdfDataAttribute : public HdfAttributeBase {
  66. public:
  67. HdfDataAttribute(int32 id, const std::string& name);
  68. Type getType() const;
  69. intn size() const;
  70. private:
  71. intn _size;
  72. int32 dataType;
  73. bool get(void *dest);
  74. int32 getDataType() const;
  75. };
  76. class HdfAttribute {
  77. public:
  78. HdfAttribute(HdfAttributeBase *attribute) : attribute(attribute) {}
  79. HdfAttribute(const HdfAttribute&) = delete;
  80. HdfAttribute(HdfAttribute&& attribute);
  81. bool isValid() const;
  82. Type getType() const;
  83. intn size() const;
  84. template <class T> bool get(std::vector<T> &dest) {
  85. if(isValid()) {
  86. return attribute->get(dest);
  87. } else {
  88. return false;
  89. }
  90. }
  91. private:
  92. std::unique_ptr<HdfAttributeBase> attribute;
  93. };
  94. }
  95. #endif //HDF4CPP_HDFATTRIBUTE_H