HdfFile.h 2.5 KB

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