HdfFile.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // Created by patrik on 10.08.17.
  3. //
  4. #ifndef GRASP_SEGMENTER_HDFFILE_H
  5. #define GRASP_SEGMENTER_HDFFILE_H
  6. #include <string>
  7. #include <vector>
  8. #include <hdf4cpp/HdfItem.h>
  9. #include <hdf4cpp/HdfAttribute.h>
  10. namespace hdf4cpp {
  11. class HdfFile {
  12. public:
  13. HdfFile(const std::string& path);
  14. HdfFile(const HdfFile& file) = delete;
  15. HdfFile(HdfFile&& file);
  16. HdfFile& operator=(const HdfFile& file) = delete;
  17. HdfFile& operator=(HdfFile&& file);
  18. ~HdfFile();
  19. bool isValid() const;
  20. explicit operator bool() const { return isValid(); }
  21. HdfItem get(const std::string& name) const;
  22. std::vector<HdfItem> getAll(const std::string& name) const;
  23. HdfAttribute getAttribute(const std::string& name);
  24. class Iterator;
  25. Iterator begin() const;
  26. Iterator end() const;
  27. private:
  28. void destroy();
  29. int32 getDatasetId(const std::string& name) const;
  30. int32 getGroupId(const std::string& name) const;
  31. int32 getDataId(const std::string& name) const;
  32. std::vector<int32> getDatasetIds(const std::string& name) const;
  33. std::vector<int32> getGroupIds(const std::string& name) const;
  34. std::vector<int32> getDataIds(const std::string& name) const;
  35. int32 sId;
  36. int32 vId;
  37. std::vector<std::pair<int32, Type> > loneRefs;
  38. };
  39. class HdfFile::Iterator : public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  40. public:
  41. Iterator(const HdfFile* file, int32 index) : file(file), index(index) {}
  42. bool operator!=(const Iterator& it) { return index != it.index; }
  43. bool operator==(const Iterator& it) { return index == it.index; }
  44. Iterator& operator++() {
  45. ++index;
  46. return *this;
  47. }
  48. Iterator operator++(int) {
  49. Iterator it(*this);
  50. ++index;
  51. return it;
  52. }
  53. Iterator& operator--() {
  54. --index;
  55. return *this;
  56. }
  57. Iterator operator--(int) {
  58. Iterator it(*this);
  59. --index;
  60. return it;
  61. }
  62. HdfItem operator*() {
  63. if(index < 0 || index >= file->loneRefs.size()) {
  64. throw std::runtime_error("HDF4CPP: cannot access invalid item");
  65. }
  66. int32 ref = file->loneRefs[index].first;
  67. switch(file->loneRefs[index].second) {
  68. case VGROUP: {
  69. int32 id = Vattach(file->vId, ref, "r");
  70. return HdfItem(new HdfGroupItem(id), file->sId, file->vId);
  71. }
  72. case VDATA: {
  73. int32 id = VSattach(file->vId, ref, "r");
  74. return HdfItem(new HdfDataItem(id), file->sId, file->vId);
  75. }
  76. default: {
  77. return HdfItem(nullptr, file->sId, file->vId);
  78. }
  79. }
  80. }
  81. private:
  82. const HdfFile *file;
  83. int32 index;
  84. };
  85. }
  86. #endif //GRASP_SEGMENTER_HDFFILE_H