Exception.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #ifndef ANKI_UTIL_EXCEPTION_H
  2. #define ANKI_UTIL_EXCEPTION_H
  3. #include "anki/Config.h"
  4. #include "anki/util/StdTypes.h"
  5. #include <exception>
  6. #include <string>
  7. namespace anki {
  8. /// Mother of all AnKi exceptions.
  9. ///
  10. /// Custom exception that takes file, line and function that throw it. Throw
  11. /// it using the ANKI_EXCEPTION macro
  12. class Exception: public std::exception
  13. {
  14. public:
  15. /// Constructor
  16. explicit Exception(const char* error, const char* file = "unknown",
  17. I line = -1, const char* func = "unknown");
  18. /// Constructor 2
  19. explicit Exception(const std::string& error, const char* file = "unknown",
  20. I line = -1, const char* func = "unknown");
  21. /// Copy constructor
  22. Exception(const Exception& e);
  23. /// Destructor. Do nothing
  24. ~Exception() throw()
  25. {}
  26. /// For re-throws
  27. /// Usage:
  28. /// @code
  29. /// catch(std::exception& e) {
  30. /// throw Exception("message", ...) << e;
  31. /// }
  32. /// @endcode
  33. Exception operator<<(const std::exception& e) const;
  34. /// Implements std::exception::what()
  35. const char* what() const throw()
  36. {
  37. return err.c_str();
  38. }
  39. private:
  40. std::string err;
  41. /// Synthesize the error string
  42. static std::string synthErr(const char* error, const char* file,
  43. I line, const char* func);
  44. };
  45. } // end namespace anki
  46. /// Macro for easy throwing
  47. #define ANKI_EXCEPTION(x) \
  48. Exception(std::string() + x, ANKI_FILE, __LINE__, ANKI_FUNC)
  49. #endif