Lluvia
error.h
Go to the documentation of this file.
1 
8 #ifndef LLUVIA_CORE_ERROR_H_
9 #define LLUVIA_CORE_ERROR_H_
10 
12 
13 #include <array>
14 #include <string>
15 #include <system_error>
16 #include <tuple>
17 
18 namespace ll {
19 
23 enum class ErrorCode : ll::enum_t {
36  KeyNotFound,
41  IOError,
44  SessionLost,
48  VulkanError,
50 };
51 
52 namespace impl {
53 
57  constexpr const std::array<std::tuple<const char*, ll::ErrorCode>, 26> ErrorCodeStrings {{std::make_tuple("EnumConversionFailed", ll::ErrorCode::EnumConversionFailed),
58  std::make_tuple("MemoryMapFailed", ll::ErrorCode::MemoryMapFailed),
59  std::make_tuple("ObjectAllocationError", ll::ErrorCode::ObjectAllocationError),
60  std::make_tuple("PortBindingError", ll::ErrorCode::PortBindingError),
61  std::make_tuple("InvalidShaderFunctionName", ll::ErrorCode::InvalidShaderFunctionName),
62  std::make_tuple("InvalidShaderProgram", ll::ErrorCode::InvalidShaderProgram),
63  std::make_tuple("BufferCopyError", ll::ErrorCode::BufferCopyError),
64  std::make_tuple("ProgramCompilationError", ll::ErrorCode::ProgramCompilationError),
65  std::make_tuple("InvalidLocalShape", ll::ErrorCode::InvalidLocalShape),
66  std::make_tuple("InvalidGridShape", ll::ErrorCode::InvalidGridShape),
67  std::make_tuple("BadEnumCasting", ll::ErrorCode::BadEnumCasting),
68  std::make_tuple("PhysicalDevicesNotFound", ll::ErrorCode::PhysicalDevicesNotFound),
69  std::make_tuple("KeyNotFound", ll::ErrorCode::KeyNotFound),
70  std::make_tuple("MemoryCreationError", ll::ErrorCode::MemoryCreationError),
71  std::make_tuple("InvalidNodeState", ll::ErrorCode::InvalidNodeState),
72  std::make_tuple("InterpreterError", ll::ErrorCode::InterpreterError),
73  std::make_tuple("PushConstantError", ll::ErrorCode::PushConstantError),
74  std::make_tuple("IOError", ll::ErrorCode::IOError),
75  std::make_tuple("InvalidArgument", ll::ErrorCode::InvalidArgument),
76  std::make_tuple("InstanceCreationError", ll::ErrorCode::InstanceCreationError),
77  std::make_tuple("SessionLost", ll::ErrorCode::SessionLost),
78  std::make_tuple("LayerNotFound", ll::ErrorCode::LayerNotFound),
79  std::make_tuple("ExtensionNotFound", ll::ErrorCode::ExtensionNotFound),
80  std::make_tuple("PipelineCreationError", ll::ErrorCode::PipelineCreationError),
81  std::make_tuple("VulkanError", ll::ErrorCode::VulkanError),
82  std::make_tuple("InvalidParameterType", ll::ErrorCode::InvalidParameterType)}};
83 
84 } // namespace impl
85 
95 template <typename T = std::string>
96 inline T errorCodeToString(ll::ErrorCode&& value) noexcept
97 {
98  return impl::enumToString<ll::ErrorCode, ll::impl::ErrorCodeStrings.size(), ll::impl::ErrorCodeStrings>(std::forward<ll::ErrorCode>(value));
99 }
100 
104 class ErrorCategory : public std::error_category {
105 
106 public:
112  static ErrorCategory& getInstance() noexcept;
113 
119  virtual const char* name() const noexcept override { return "ll::ErrorCode"; }
120 
128  virtual std::string message(int ev) const override { return errorCodeToString(static_cast<ll::ErrorCode>(ev)); }
129 };
130 
138 inline std::error_code createErrorCode(ll::ErrorCode errorCode)
139 {
140  return std::error_code {static_cast<int>(errorCode), ErrorCategory::getInstance()};
141 }
142 
152 template <typename T>
153 void throwSystemErrorIf(bool condition, ll::ErrorCode errorCode, T&& msg)
154 {
155 
156  static_assert(std::is_convertible<T, std::string>(), "T must be a string-like type");
157 
158  if (condition) {
159  throw std::system_error(createErrorCode(errorCode), msg);
160  }
161 }
162 
172 template <typename T>
173 void throwSystemError(ll::ErrorCode errorCode, T&& msg)
174 {
175 
176  static_assert(std::is_convertible<T, std::string>(), "T must be a string-like type");
177  throw std::system_error(createErrorCode(errorCode), msg);
178 }
179 
180 } // namespace ll
181 
182 #endif // LLUVIA_CORE_ERROR_H_
Class for error code category.
Definition: error.h:104
static ErrorCategory & getInstance() noexcept
Gets a singleton instance.
virtual const char * name() const noexcept override
Gets the error category name.
Definition: error.h:119
virtual std::string message(int ev) const override
Converts from an error code to a string message.
Definition: error.h:128
Enums.
constexpr const std::array< std::tuple< const char *, ll::ErrorCode >, 26 > ErrorCodeStrings
Definition: error.h:57
std::string enumToString(E &&value) noexcept
Definition: enums.h:146
Definition: Buffer.h:28
T errorCodeToString(ll::ErrorCode &&value) noexcept
Converts from ll::ErrorCode enum value to std::string.
Definition: error.h:96
void throwSystemErrorIf(bool condition, ll::ErrorCode errorCode, T &&msg)
Throws a std::system_error exception if the condition passed is true.
Definition: error.h:153
void throwSystemError(ll::ErrorCode errorCode, T &&msg)
Throws a std::system_error exception with error code and message.
Definition: error.h:173
std::error_code createErrorCode(ll::ErrorCode errorCode)
Creates an error code.
Definition: error.h:138
uint32_t enum_t
Definition: enums.h:23
ErrorCode
Error codes.
Definition: error.h:23
@ InvalidShaderFunctionName