exception.h 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. //
  2. // exception.h
  3. // graphics
  4. //
  5. // Created by Sam Jaffe on 7/6/19.
  6. // Copyright © 2019 Sam Jaffe. All rights reserved.
  7. //
  8. #pragma once
  9. #include <stdexcept>
  10. #include <string>
  11. namespace graphics {
  12. struct file_read_error : std::runtime_error {
  13. file_read_error(std::string const & file)
  14. : std::runtime_error("Unable to read file " + file) {}
  15. file_read_error(std::string const & type, std::string const & file)
  16. : std::runtime_error("Unable to read " + type + " file " + file) {}
  17. };
  18. struct unknown_texture_format : std::runtime_error {
  19. unknown_texture_format(int components)
  20. : std::runtime_error("Invalid number of components provided: " +
  21. std::to_string(components)) {}
  22. };
  23. template <typename E> struct unmapped_enum : std::logic_error {
  24. // TODO (sjaffe): This is just an int cast, which isn't all that helpful.
  25. // Instead, one might use https://github.com/Neargye/magic_enum
  26. // or wait for C++ to implement that sort of thing through metaclasses, etc.
  27. unmapped_enum(E en)
  28. : std::logic_error("Unknown " + type_name + std::to_string((int)en)) {}
  29. unmapped_enum(std::string const & str)
  30. : std::logic_error("Unknown " + type_name + str) {}
  31. static std::string const type_name;
  32. };
  33. template <typename E>
  34. std::string const unmapped_enum<E>::type_name{typeid(E).name()};
  35. }