HdfObject.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 <functional>
  8. #include <memory>
  9. #include <vector>
  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)
  18. : endFunction(endFunction)
  19. , id(id) {
  20. }
  21. ~HdfDestroyer() {
  22. endFunction(id);
  23. }
  24. private:
  25. std::function<int32(int32)> endFunction;
  26. int32 id;
  27. };
  28. /// A chain of destroyers.
  29. /// If an HdfObject creates a new one, then gives forward their chain.
  30. /// If all the HdfObjects would be destroyed, which holds a reference of the destroyer,
  31. /// then the destructor of the destroyer will call the end access function.
  32. class HdfDestroyerChain {
  33. public:
  34. HdfDestroyerChain() {
  35. }
  36. HdfDestroyerChain(const HdfDestroyerChain &other)
  37. : chain(other.chain) {
  38. }
  39. void pushBack(HdfDestroyer *destroyer) {
  40. chain.push_back(std::shared_ptr<HdfDestroyer>(destroyer));
  41. }
  42. private:
  43. std::vector<std::shared_ptr<HdfDestroyer>> chain;
  44. };
  45. public:
  46. /// \returns the type of the object
  47. virtual Type getType() const {
  48. return type;
  49. }
  50. /// \returns the class type of the object
  51. virtual ClassType getClassType() const {
  52. return classType;
  53. }
  54. protected:
  55. HdfObject(const Type &type, const ClassType &classType, const HdfDestroyerChain &chain = HdfDestroyerChain())
  56. : type(type)
  57. , classType(classType)
  58. , chain(chain) {
  59. }
  60. HdfObject(const HdfObject *object)
  61. : type(object->getType())
  62. , classType(object->getClassType())
  63. , chain(object->chain) {
  64. }
  65. virtual void setType(const Type &type) {
  66. this->type = type;
  67. }
  68. virtual void setClassType(const ClassType &classType) {
  69. this->classType = classType;
  70. }
  71. /// Throws an HdfException by its type
  72. NORETURN void raiseException(const ExceptionType &exceptionType) const {
  73. throw HdfException(type, classType, exceptionType);
  74. }
  75. /// Thorws an HdfException by its message, the type will be OTHER.
  76. NORETURN void raiseException(const std::string &message) const {
  77. throw HdfException(type, classType, message);
  78. }
  79. private:
  80. /// The type of the object.
  81. Type type;
  82. /// The class type of the object.
  83. ClassType classType;
  84. protected:
  85. /// The destroyer chain of the object
  86. HdfDestroyerChain chain;
  87. };
  88. }
  89. #endif // HDF4CPP_HDFOBJECT_H