bwidgets/inc/basic_widgets/core/type/sdl_error.hpp

44 lines
1.2 KiB
C++

#ifndef BWIDGETS_SDL_ERROR_HPP
#define BWIDGETS_SDL_ERROR_HPP
#include <basic_widgets/core/type/exception.hpp>
extern "C" {
auto SDL_GetError() -> const char*;
}
namespace bwidgets
{
struct SDLError final : BaseException
{
SDLError(const char* file, const char* func, const int l,
const char* w = nullptr)
: BaseException(file, func, l, w)
{}
template<typename T>
[[nodiscard]] static inline auto ptr_or_throw(T* ptr, const char* file,
const char* func, const int l,
const char* w = nullptr) -> T*
{
if (!ptr) {
if (!w) w = SDL_GetError();
throw SDLError(file, func, l, w);
}
return ptr;
}
static inline auto success_or_throw(int code, const char* file, const char* func,
const int l, const char* w = nullptr) -> int
{
if (code < 0) {
if (w == nullptr) w = SDL_GetError();
throw SDLError(file, func, l, w);
}
return code;
}
};
}
#endif