HdfObject.h 3.6 KB

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