HdfAttribute.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFATTRIBUTE_H
  5. #define HDF4CPP_HDFATTRIBUTE_H
  6. #include <hdf4cpp/HdfException.h>
  7. #include <hdf4cpp/HdfObject.h>
  8. #include <hdf/hdf.h>
  9. #include <hdf/mfhdf.h>
  10. #include <string>
  11. #include <vector>
  12. #include <map>
  13. #include <memory>
  14. namespace hdf4cpp {
  15. class HdfAttributeBase : public HdfObject {
  16. public:
  17. HdfAttributeBase(int32 id, int32 index, Type type) : HdfObject(type, ATTRIBUTE), id(id), index(index) {
  18. if(id == FAIL || index == FAIL) {
  19. raiseException(INVALID_ID);
  20. }
  21. }
  22. virtual ~HdfAttributeBase() {}
  23. virtual intn size() const = 0;
  24. template <class T> void get(std::vector<T> &dest) {
  25. intn length = size();
  26. auto it = typeSizeMap.find(getDataType());
  27. if (it != typeSizeMap.end()) {
  28. if ((size_t) it->second != sizeof(T)) {
  29. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  30. }
  31. dest.resize(length);
  32. get(dest.data());
  33. } else {
  34. raiseException(INVALID_DATA_TYPE);
  35. }
  36. }
  37. protected:
  38. int32 id;
  39. int32 index;
  40. virtual void 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. intn size() const;
  47. private:
  48. intn _size;
  49. int32 dataType;
  50. void get(void *dest);
  51. int32 getDataType() const;
  52. };
  53. class HdfGroupAttribute : public HdfAttributeBase {
  54. public:
  55. HdfGroupAttribute(int32 id, const std::string& name);
  56. intn size() const;
  57. private:
  58. intn _size;
  59. int32 dataType;
  60. void get(void *dest);
  61. int32 getDataType() const;
  62. };
  63. class HdfDataAttribute : public HdfAttributeBase {
  64. public:
  65. HdfDataAttribute(int32 id, const std::string& name);
  66. intn size() const;
  67. private:
  68. intn _size;
  69. int32 dataType;
  70. void get(void *dest);
  71. int32 getDataType() const;
  72. };
  73. class HdfAttribute : public HdfObject {
  74. public:
  75. HdfAttribute(HdfAttributeBase *attribute) : HdfObject(attribute->getType(), attribute->getClassType()), attribute(attribute) {}
  76. HdfAttribute(const HdfAttribute&) = delete;
  77. HdfAttribute(HdfAttribute&& attr);
  78. HdfAttribute& operator=(const HdfAttribute& attribute) = delete;
  79. HdfAttribute& operator=(HdfAttribute&& attribute);
  80. Type getType() const;
  81. intn size() const;
  82. template <class T> void get(std::vector<T> &dest) {
  83. attribute->get(dest);
  84. }
  85. private:
  86. std::unique_ptr<HdfAttributeBase> attribute;
  87. };
  88. }
  89. #endif //HDF4CPP_HDFATTRIBUTE_H