HdfException.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. //
  2. // Created by patrik on 22.08.17.
  3. //
  4. #ifndef HDF4CPP_HDFEXCEPTION_H
  5. #define HDF4CPP_HDFEXCEPTION_H
  6. #include <hdf4cpp/HdfDefines.h>
  7. namespace hdf4cpp {
  8. const std::string exceptionMessagePrefix = "HDF4CPP: ";
  9. const std::map<ExceptionType, std::string> exceptionTypeMap = {
  10. {INVALID_ID, "cannot construct object, the id is invalid"},
  11. {INVALID_OPERATION, "operation not supported for this type"},
  12. {INVALID_NAME, "invalid item name"},
  13. {OUT_OF_RANGE, "out of range, cannot access element (usually thrown when an iterator points to an inaccessible address)"},
  14. {BUFFER_SIZE_NOT_ENOUGH, "not enough buffer size (usually thrown when the type of the vector in which we read the data is too small)"},
  15. {BUFFER_SIZE_NOT_DIVISIBLE, "buffer cannot be splited up (usually thrown when the size of the readable data is not divisible by the type size of the vector"},
  16. {INVALID_RANGES, "the given ranges are invalids (usually thrown when the ranges is negative or it is bigger than the dimension of tha data)"},
  17. {STATUS_RETURN_FAIL, "hdf routine failed"},
  18. {INVALID_DATA_TYPE, "the type of the data in the hdf item is not supported"},
  19. {OTHER, "exception thrown"},
  20. };
  21. class HdfException : public std::exception {
  22. public:
  23. HdfException(const Type& type, const ClassType& classType, const std::string& message) : type(type),
  24. classType(classType),
  25. exceptionType(OTHER),
  26. message(exceptionMessagePrefix + message) {}
  27. HdfException(const Type& type, const ClassType& classType, const ExceptionType& exceptionType) : type(type),
  28. classType(classType),
  29. exceptionType(exceptionType),
  30. message(exceptionMessagePrefix + exceptionTypeMap.at(exceptionType)) {}
  31. Type getType() const noexcept;
  32. ClassType getClassType() const noexcept;
  33. ExceptionType getExceptionType() const noexcept;
  34. std::string getMessage() const noexcept;
  35. const char *what() const noexcept {
  36. return message.data();
  37. }
  38. private:
  39. Type type;
  40. ClassType classType;
  41. ExceptionType exceptionType;
  42. std::string message;
  43. };
  44. }
  45. #endif //HDF4CPP_HDFEXCEPTION_H