FOSS-VG/src/lib/error.h++

74 lines
1.9 KiB
C++

// Copyright 2022, FOSS-VG Developers and Contributers
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, version 3.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// version 3 along with this program.
// If not, see https://www.gnu.org/licenses/agpl-3.0.en.html
#pragma once
#include <cstdint>
template <typename T>
struct ErrorOr {
bool isError;
uint8_t errorCode;
T value;
ErrorOr<T>();
ErrorOr<T>(T);
ErrorOr<T>(bool);
ErrorOr<T>(bool, uint8_t);
ErrorOr<T>(bool, uint8_t, T);
};
template <typename T>
ErrorOr<T>::ErrorOr() {
this->isError = false;
this->errorCode = 0;
}
template <typename T>
ErrorOr<T>::ErrorOr(T value) {
this->isError = false;
this->errorCode = 0;
this->value = value;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError) {
this->isError = isError;
this->errorCode = 0;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode) {
this->isError = isError;
this->errorCode = errorCode;
}
template <typename T>
ErrorOr<T>::ErrorOr(bool isError, uint8_t errorCode, T value) {
this->isError = isError;
this->errorCode = errorCode;
this->value = value;
}
namespace ErrorCodes {
// These are all arbitrary values used to assign error codes to different
// kinds of errors.
// Using them is optional as ErrorOr<> accepts any uint8_t value as
// error code, but they are useful for readability.
// IndexOutOfRangeException equivalent
const uint8_t RANGE_ERROR = 1;
}