Line data Source code
1 : // Copyright (c) 2025 Milton McDonald
2 : // This source code is licensed under the MIT License. See LICENSE file in the
3 : // project root for details.
4 :
5 : #include "error.h"
6 :
7 : #include <utility>
8 :
9 : using ::graphics_engine::types::ErrorCode;
10 : using enum ::graphics_engine::types::ErrorCode;
11 :
12 : using std::error_category;
13 : using std::error_code;
14 : using std::string;
15 : using std::to_underlying;
16 :
17 : namespace graphics_engine::error {
18 :
19 : class ErrorCategory : public error_category {
20 : public:
21 2 : [[nodiscard]] auto name() const noexcept -> const char* override {
22 2 : return "graphics_engine::error";
23 : }
24 :
25 3 : [[nodiscard]] auto message(int condition) const -> string override {
26 3 : constexpr int expectedCount = 12;
27 3 : static_assert(to_underlying(kNumErrorCodes) == expectedCount,
28 : "Update the switch statement below!");
29 :
30 3 : switch (static_cast<ErrorCode>(condition)) {
31 1 : default:
32 1 : assert(false); // If we get here, add a new case to the switch
33 1 : [[fallthrough]];
34 1 : case kGladLoadGL:
35 1 : return "glad failed to load OpenGL.";
36 0 : case kGLError:
37 0 : return "OpenGL Error";
38 0 : case kGLErrorInvalidOperation:
39 0 : return "OpenGL Error: Invalid Operation.";
40 0 : case kGLErrorInvalidValue:
41 0 : return "OpenGL Error: Invalid Value.";
42 0 : case kGLErrorOutOfMemory:
43 0 : return "OpenGL Error: Out of Memory.";
44 0 : case kShaderError:
45 0 : return "Shader Error.";
46 1 : case kStbErrorLoad:
47 1 : return "Stb Error: Failed to load file.";
48 1 : case kStbErrorWritePng:
49 1 : return "Stb Error: Failed to write png file.";
50 : }
51 : }
52 : };
53 :
54 3 : auto GetErrorCategory() -> const ErrorCategory& {
55 3 : static ErrorCategory instance;
56 3 : return instance;
57 : }
58 :
59 4 : auto CheckGLError() -> void { assert(glGetError() == GL_NO_ERROR); }
60 :
61 3 : auto MakeErrorCode(ErrorCode code) -> error_code {
62 3 : return {to_underlying(code), GetErrorCategory()};
63 : }
64 :
65 : } // namespace graphics_engine::error
|