HdfItem.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //
  2. // Created by patrik on 11.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFITEM_H
  5. #define HDF4CPP_HDFITEM_H
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfAttribute.h>
  8. #include <iostream>
  9. #include <map>
  10. #include <memory>
  11. #include <vector>
  12. #include <hdf/hdf.h>
  13. class HdfItemBase {
  14. public:
  15. HdfItemBase(int32 id) : id(id) {}
  16. virtual ~HdfItemBase() {}
  17. virtual bool isValid() const { return id != FAIL; }
  18. virtual Type getType() const = 0;
  19. virtual int32 getId() const = 0;
  20. virtual std::string getName() const = 0;
  21. virtual std::vector<int32> getDims() = 0;
  22. virtual intn size() const = 0;
  23. template <class T> bool read(std::vector<T> &dest) {
  24. intn length = size();
  25. if(length != FAIL) {
  26. auto it = typeSizeMap.find(getDataType());
  27. if(it != typeSizeMap.end()) {
  28. if(it->second != sizeof(T)) {
  29. throw std::runtime_error("HDF4CPP: type size missmatch");
  30. }
  31. } else {
  32. throw std::runtime_error("HDF4CPP: hdf data set type not supported");
  33. }
  34. dest.resize(length);
  35. return read(dest.data());
  36. } else {
  37. return false;
  38. }
  39. }
  40. virtual HdfAttribute getAttribute(const std::string& name) = 0;
  41. protected:
  42. int32 id;
  43. virtual bool read(void *dest) = 0;
  44. virtual int32 getDataType() const = 0;
  45. };
  46. class HdfDatasetItem : public HdfItemBase {
  47. public:
  48. HdfDatasetItem(int32 id);
  49. ~HdfDatasetItem();
  50. Type getType() const;
  51. int32 getId() const;
  52. std::string getName() const;
  53. std::vector<int32> getDims();
  54. intn size() const;
  55. HdfAttribute getAttribute(const std::string& name);
  56. private:
  57. intn _size;
  58. int32 dataType;
  59. std::string name;
  60. bool read(void *dest);
  61. int32 getDataType() const;
  62. };
  63. class HdfGroupItem : public HdfItemBase {
  64. public:
  65. HdfGroupItem(int32 id);
  66. ~HdfGroupItem();
  67. Type getType() const;
  68. int32 getId() const;
  69. std::string getName() const;
  70. std::vector<int32> getDims();
  71. intn size() const;
  72. HdfAttribute getAttribute(const std::string& name);
  73. private:
  74. std::string name;
  75. bool read(void *dest);
  76. int32 getDataType() const;
  77. };
  78. class HdfItem {
  79. public:
  80. HdfItem(HdfItemBase *item, int32 sId, int32 vId) : item(item), sId(sId), vId(vId) {}
  81. HdfItem(const HdfItem& item) = delete;
  82. HdfItem(HdfItem&& item);
  83. bool isValid() const;
  84. Type getType() const;
  85. std::string getName() const;
  86. std::vector<int32> getDims();
  87. intn size() const;
  88. HdfAttribute getAttribute(const std::string& name);
  89. template <class T> bool read(std::vector<T> &dest) {
  90. if(isValid()) {
  91. return item->read(dest);
  92. } else {
  93. return false;
  94. }
  95. }
  96. class Iterator;
  97. Iterator begin() const;
  98. Iterator end() const;
  99. private:
  100. std::unique_ptr<HdfItemBase> item;
  101. int32 sId;
  102. int32 vId;
  103. };
  104. class HdfItem::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  105. public:
  106. Iterator(int32 sId, int32 vId, int32 key, int32 index) : sId(sId), vId(vId), key(key), index(index) {}
  107. bool operator!=(const Iterator& it) { return index != it.index; }
  108. bool operator==(const Iterator& it) { return index == it.index; }
  109. // bool operator<(const Iterator& it) { return index < it.index; }
  110. // bool operator>(const Iterator& it) { return index > it.index; }
  111. // bool operator>=(const Iterator& it) { return index >= it.index; }
  112. // bool operator<=(const Iterator& it) { return index <= it.index; }
  113. Iterator& operator++() {
  114. ++index;
  115. return *this;
  116. }
  117. Iterator operator++(int) {
  118. Iterator it(*this);
  119. ++index;
  120. return it;
  121. }
  122. Iterator& operator--() {
  123. --index;
  124. return *this;
  125. }
  126. Iterator operator--(int) {
  127. Iterator it(*this);
  128. --index;
  129. return it;
  130. }
  131. HdfItem operator*() {
  132. int32 tag, ref;
  133. if(Vgettagref(key, index, &tag, &ref) == FAIL) {
  134. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  135. }
  136. if(Visvs(key, ref)) {
  137. throw std::runtime_error("HDF4CPP: vdata not supported yet");
  138. } else if(Visvg(key, ref)) {
  139. int32 id = Vattach(vId, ref, "r");
  140. return HdfItem(new HdfGroupItem(id), sId, vId);
  141. } else {
  142. int32 id = SDselect(sId, SDreftoindex(sId, ref));
  143. return HdfItem(new HdfDatasetItem(id), sId, vId);
  144. }
  145. }
  146. private:
  147. int32 sId;
  148. int32 vId;
  149. int32 key;
  150. int32 index;
  151. };
  152. #endif //HDF4CPP_HDFITEM_H