HdfAttribute.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /// \copyright Copyright (c) Catalysts GmbH
  2. /// \author Patrik Kovacs, Catalysts GmbH
  3. #ifndef HDF4CPP_HDFATTRIBUTE_H
  4. #define HDF4CPP_HDFATTRIBUTE_H
  5. #include <hdfi.h>
  6. #include <hdf4cpp/HdfItem.h>
  7. #include <hdf4cpp/HdfObject.h>
  8. namespace hdf4cpp {
  9. /// Represents an hdf attribute
  10. class HdfAttribute : public HdfObject {
  11. private:
  12. /// The base class of the attribute classes
  13. class HdfAttributeBase : public HdfObject {
  14. public:
  15. HdfAttributeBase(int32 id, int32 index, Type type, const HdfDestroyerChain &chain)
  16. : HdfObject(type, ATTRIBUTE, chain)
  17. , id(id)
  18. , index(index) {
  19. if (id == FAIL || index == FAIL) {
  20. raiseException(INVALID_ID);
  21. }
  22. }
  23. virtual ~HdfAttributeBase() {
  24. }
  25. /// \returns The number of existing data in the attribute
  26. virtual intn size() const = 0;
  27. /// \returns The data type number of the data held by the attribute
  28. virtual int32 getDataType() const = 0;
  29. /// Reads the data from the attribute
  30. /// \param dest The destination vector
  31. virtual void get(void *dest) = 0;
  32. protected:
  33. int32 id;
  34. int32 index;
  35. };
  36. /// Attribute class for the SData attributes
  37. class HdfDatasetAttribute : public HdfAttributeBase {
  38. public:
  39. HdfDatasetAttribute(int32 id, const std::string &name, const HdfDestroyerChain &chain);
  40. intn size() const;
  41. private:
  42. intn _size;
  43. int32 dataType;
  44. void get(void *dest);
  45. int32 getDataType() const;
  46. };
  47. /// Attribute class for the VGroup attributes
  48. class HdfGroupAttribute : public HdfAttributeBase {
  49. public:
  50. HdfGroupAttribute(int32 id, const std::string &name, const HdfDestroyerChain &chain);
  51. intn size() const;
  52. private:
  53. intn _size;
  54. int32 dataType;
  55. void get(void *dest);
  56. int32 getDataType() const;
  57. };
  58. /// Attribute class for the VData attributes
  59. class HdfDataAttribute : public HdfAttributeBase {
  60. public:
  61. HdfDataAttribute(int32 id, const std::string &name, const HdfDestroyerChain &chain);
  62. intn size() const;
  63. private:
  64. intn _size;
  65. int32 dataType;
  66. void get(void *dest);
  67. int32 getDataType() const;
  68. };
  69. public:
  70. HdfAttribute(const HdfAttribute &) = delete;
  71. HdfAttribute(HdfAttribute &&attr);
  72. HdfAttribute &operator=(const HdfAttribute &attribute) = delete;
  73. HdfAttribute &operator=(HdfAttribute &&attribute);
  74. /// \returns the number of elements of the attribute data
  75. intn size() const;
  76. /// Reads the data from the attribute
  77. /// \param dest the vector in which the data will be stored
  78. template <class T> void get(std::vector<T> &dest) {
  79. intn length = size();
  80. auto it = typeSizeMap.find(getDataType());
  81. if (it != typeSizeMap.end()) {
  82. if ((size_t)it->second != sizeof(T)) {
  83. raiseException(BUFFER_SIZE_NOT_ENOUGH);
  84. }
  85. dest.resize(length);
  86. getInternal(dest.data());
  87. } else {
  88. raiseException(INVALID_DATA_TYPE);
  89. }
  90. }
  91. friend HdfAttribute HdfFile::getAttribute(const std::string &name) const;
  92. friend HdfAttribute HdfItem::HdfDatasetItem::getAttribute(const std::string &name) const;
  93. friend HdfAttribute HdfItem::HdfGroupItem::getAttribute(const std::string &name) const;
  94. friend HdfAttribute HdfItem::HdfDataItem::getAttribute(const std::string &name) const;
  95. private:
  96. HdfAttribute(HdfAttributeBase *attribute);
  97. /// An internal get function
  98. /// \param dest The destination buffer
  99. void getInternal(void *dest);
  100. int32 getDataType() const;
  101. /// Holds an attribute object address
  102. std::unique_ptr<HdfAttributeBase> attribute;
  103. };
  104. }
  105. #endif //HDF4CPP_HDFATTRIBUTE_H