12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #ifndef CORE_ERROR_HPP
- #define CORE_ERROR_HPP
- #include "core/utils/Check.hpp"
- #include "core/utils/Types.hpp"
- namespace Core {
- struct Error {
- using Code = unsigned int;
- static_assert(sizeof(Code) >= 4, "error code too small");
- Code code;
- constexpr Error(Code c) : code(c) {
- }
- bool check() const {
- return code != 0;
- }
- bool contains(Error e) const {
- return code & e.code;
- }
- bool operator==(Error e) const {
- return code == e.code;
- }
- bool operator!=(Error e) const {
- return !(*this == e);
- }
- Error& operator|=(Error e) {
- code |= e.code;
- return *this;
- }
- Error operator|(Error e) const {
- e |= *this;
- return e;
- }
- };
- namespace ErrorCode {
- static constexpr Error NONE = 0;
- static constexpr Error ERROR = 1 << 0;
- static constexpr Error NEGATIVE_ARGUMENT = 1 << 1;
- static constexpr Error CAPACITY_REACHED = 1 << 2;
- static constexpr Error BLOCKED_STDOUT = 1 << 3;
- static constexpr Error OUT_OF_MEMORY = 1 << 4;
- static constexpr Error INVALID_CHAR = 1 << 5;
- static constexpr Error NOT_FOUND = 1 << 6;
- static constexpr Error INVALID_STATE = 1 << 7;
- static constexpr Error INVALID_INDEX = 1 << 8;
- static constexpr Error INVALID_ARGUMENT = 1 << 9;
- static constexpr Error TIME_NOT_AVAILABLE = 1 << 10;
- static constexpr Error SLEEP_INTERRUPTED = 1 << 11;
- static constexpr Error THREAD_ERROR = 1 << 12;
- static constexpr Error MUTEX_ERROR = 1 << 13;
- static constexpr Error EXISTING_KEY = 1 << 14;
- static constexpr Error CANNOT_OPEN_FILE = 1 << 15;
- static constexpr Error END_OF_FILE = 1 << 16;
- }
- size_t toString(Error e, char* buffer, size_t size);
- inline bool checkError(Error& storage, Error e) {
- return (storage = e).check();
- }
- #define CORE_RETURN_ERROR(checked) \
- { \
- Core::Error error = Core::ErrorCode::NONE; \
- if(checkError(error, checked)) [[unlikely]] { \
- return error; \
- } \
- }
- }
- #endif
|