Exception.h 1.0 KB

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