HdfFile.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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/HdfObject.h>
  9. #include <hdf4cpp/HdfItem.h>
  10. #include <hdf4cpp/HdfAttribute.h>
  11. #include <hdf4cpp/HdfDestroyer.h>
  12. namespace hdf4cpp {
  13. class HdfFile : public HdfObject {
  14. public:
  15. HdfFile(const std::string& path);
  16. HdfFile(const HdfFile& file) = delete;
  17. HdfFile(HdfFile&& file);
  18. HdfFile& operator=(const HdfFile& file) = delete;
  19. HdfFile& operator=(HdfFile&& file);
  20. ~HdfFile();
  21. int32 getSId() const;
  22. int32 getVId() const;
  23. HdfItem get(const std::string& name) const;
  24. std::vector<HdfItem> getAll(const std::string& name) const;
  25. HdfAttribute getAttribute(const std::string& name);
  26. class Iterator;
  27. Iterator begin() const;
  28. Iterator end() const;
  29. private:
  30. int32 getDatasetId(const std::string& name) const;
  31. int32 getGroupId(const std::string& name) const;
  32. int32 getDataId(const std::string& name) const;
  33. std::vector<int32> getDatasetIds(const std::string& name) const;
  34. std::vector<int32> getGroupIds(const std::string& name) const;
  35. std::vector<int32> getDataIds(const std::string& name) const;
  36. int32 sId;
  37. int32 vId;
  38. std::vector<std::pair<int32, Type> > loneRefs;
  39. };
  40. class HdfFile::Iterator : public HdfObject, public std::iterator<std::bidirectional_iterator_tag, HdfItem> {
  41. public:
  42. Iterator(const HdfFile* file, int32 index, const HdfDestroyerChain& chain) : HdfObject(HFILE, ITERATOR, chain),
  43. file(file),
  44. index(index) {}
  45. bool operator!=(const Iterator& it) { return index != it.index; }
  46. bool operator==(const Iterator& it) { return index == it.index; }
  47. Iterator& operator++() {
  48. ++index;
  49. return *this;
  50. }
  51. Iterator operator++(int) {
  52. Iterator it(*this);
  53. ++index;
  54. return it;
  55. }
  56. Iterator& operator--() {
  57. --index;
  58. return *this;
  59. }
  60. Iterator operator--(int) {
  61. Iterator it(*this);
  62. --index;
  63. return it;
  64. }
  65. HdfItem operator*() {
  66. if(index < 0 || index >= (int) file->loneRefs.size()) {
  67. raiseException(OUT_OF_RANGE);
  68. }
  69. int32 ref = file->loneRefs[index].first;
  70. switch(file->loneRefs[index].second) {
  71. case VGROUP: {
  72. int32 id = Vattach(file->vId, ref, "r");
  73. return HdfItem(new HdfGroupItem(id, chain), file->sId, file->vId);
  74. }
  75. case VDATA: {
  76. int32 id = VSattach(file->vId, ref, "r");
  77. return HdfItem(new HdfDataItem(id, chain), file->sId, file->vId);
  78. }
  79. default: {
  80. raiseException(OUT_OF_RANGE);
  81. }
  82. }
  83. }
  84. private:
  85. const HdfFile *file;
  86. int32 index;
  87. };
  88. }
  89. #endif //GRASP_SEGMENTER_HDFFILE_H