HdfAttribute.h 2.7 KB

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