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