HdfObject.h 2.9 KB

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