You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

59 lines
1.4 KiB

  1. #ifndef STORM_EXCEPTIONS_BASEEXCEPTION_H_
  2. #define STORM_EXCEPTIONS_BASEEXCEPTION_H_
  3. #include <exception>
  4. #include <sstream>
  5. namespace storm {
  6. namespace exceptions {
  7. template<typename E>
  8. class BaseException : public std::exception {
  9. public:
  10. BaseException() : exception() {}
  11. BaseException(const BaseException& cp)
  12. : exception(cp), stream(cp.stream.str()) {
  13. }
  14. BaseException(const char* cstr) {
  15. stream << cstr;
  16. }
  17. ~BaseException() throw() { }
  18. template<class T>
  19. E& operator<<(const T& var) {
  20. this->stream << var;
  21. return * dynamic_cast<E*>(this);
  22. }
  23. virtual const char* what() const throw() {
  24. std::string errorString = this->stream.str();
  25. char* result = new char[errorString.size() + 1];
  26. result[errorString.size()] = '\0';
  27. std::copy(errorString.begin(), errorString.end(), result);
  28. return result;
  29. }
  30. private:
  31. std::stringstream stream;
  32. };
  33. } // namespace exceptions
  34. } // namespace storm
  35. /* Macro to generate descendant exception classes.
  36. * As all classes are nearly the same, this makes changing common features much easier.
  37. */
  38. #define STORM_EXCEPTION_DEFINE_NEW(exception_name) class exception_name : public BaseException<exception_name> { \
  39. public: \
  40. exception_name() : BaseException() { \
  41. } \
  42. exception_name(const char* cstr) : BaseException(cstr) { \
  43. } \
  44. exception_name(const exception_name& cp) : BaseException(cp) { \
  45. } \
  46. };
  47. #endif // STORM_EXCEPTIONS_BASEEXCEPTION_H_