HdfObject.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Created by patrik on 23.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFOBJECT_H
  5. #define HDF4CPP_HDFOBJECT_H
  6. #include <hdf4cpp/HdfDefines.h>
  7. #include <hdf4cpp/HdfException.h>
  8. #include <vector>
  9. #include <functional>
  10. #include <memory>
  11. namespace hdf4cpp {
  12. /// The HdfObject class is the base class of all the HdfObjects.
  13. class HdfObject {
  14. protected:
  15. /// This class is responsible to call the end access functions
  16. class HdfDestroyer {
  17. public:
  18. HdfDestroyer(const std::function<int32 (int32)>& endFunction, int32 id) : endFunction(endFunction), id(id) {}
  19. ~HdfDestroyer() {
  20. endFunction(id);
  21. }
  22. private:
  23. std::function<int32 (int32)> endFunction;
  24. int32 id;
  25. };
  26. /// A chain of destroyers.
  27. /// If an HdfObject creates a new one, then gives forward their chain.
  28. /// If all the HdfObjects would be destroyed, which holds a reference of the destroyer,
  29. /// then the destructor of the destroyer will call the end access function.
  30. class HdfDestroyerChain {
  31. public:
  32. HdfDestroyerChain() {}
  33. HdfDestroyerChain(const HdfDestroyerChain& other) : chain(other.chain) {}
  34. void pushBack(HdfDestroyer * destroyer) {
  35. chain.push_back(std::shared_ptr<HdfDestroyer>(destroyer));
  36. }
  37. private:
  38. std::vector<std::shared_ptr<HdfDestroyer> > chain;
  39. };
  40. public:
  41. /// \returns the type of the object
  42. virtual Type getType() const { return type; }
  43. /// \returns the class type of the object
  44. virtual ClassType getClassType() const { return classType; }
  45. protected:
  46. HdfObject(const Type& type, const ClassType& classType, const HdfDestroyerChain& chain = HdfDestroyerChain()) : type(type),
  47. classType(classType),
  48. chain(chain) {}
  49. HdfObject(const HdfObject *object) : type(object->getType()), classType(object->getClassType()), chain(object->chain) {}
  50. virtual void setType(const Type& type) { this->type = type; }
  51. virtual void setClassType(const ClassType& classType) { this->classType = classType; }
  52. /// Throws an HdfException by its type
  53. virtual void raiseException(const ExceptionType& exceptionType) const {
  54. throw HdfException(type, classType, exceptionType);
  55. }
  56. /// Thorws an HdfException by its message, the type will be OTHER.
  57. virtual void raiseException(const std::string& message) const {
  58. throw HdfException(type, classType, message);
  59. }
  60. private:
  61. /// The type of the object.
  62. Type type;
  63. /// The class type of the object.
  64. ClassType classType;
  65. protected:
  66. /// The destroyer chain of the object
  67. HdfDestroyerChain chain;
  68. };
  69. }
  70. #endif //HDF4CPP_HDFOBJECT_H