2021-08-06 14:22:56 +02:00
|
|
|
#ifndef BWIDGETS_SDL_ERROR_HPP
|
|
|
|
#define BWIDGETS_SDL_ERROR_HPP
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
#include <basic_widgets/core/type/exception.hpp>
|
|
|
|
|
|
|
|
extern "C" {
|
2021-07-30 20:41:47 +02:00
|
|
|
auto SDL_GetError() -> const char*;
|
2021-07-18 18:26:12 +02:00
|
|
|
}
|
|
|
|
|
2021-08-06 14:22:56 +02:00
|
|
|
namespace bwidgets
|
2021-07-18 18:26:12 +02:00
|
|
|
{
|
|
|
|
struct SDLError final : BaseException
|
|
|
|
{
|
|
|
|
SDLError(const char* file, const char* func, const int l,
|
2021-07-29 16:06:03 +02:00
|
|
|
const char* w = nullptr)
|
|
|
|
: BaseException(file, func, l, w)
|
|
|
|
{}
|
2021-07-18 18:26:12 +02:00
|
|
|
|
|
|
|
template<typename T>
|
2021-08-06 14:22:56 +02:00
|
|
|
[[nodiscard]] static inline auto ptr_or_throw(T* ptr, const char* file,
|
|
|
|
const char* func, const int l,
|
|
|
|
const char* w = nullptr) -> T*
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
2021-08-06 14:22:56 +02:00
|
|
|
if (!ptr) {
|
|
|
|
if (!w) w = SDL_GetError();
|
2021-07-30 20:41:47 +02:00
|
|
|
throw SDLError(file, func, l, w);
|
2021-07-18 18:26:12 +02:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2021-07-30 20:41:47 +02:00
|
|
|
static inline auto success_or_throw(int code, const char* file, const char* func,
|
|
|
|
const int l, const char* w = nullptr) -> int
|
2021-07-29 16:06:03 +02:00
|
|
|
{
|
2021-07-18 18:26:12 +02:00
|
|
|
if (code < 0) {
|
|
|
|
if (w == nullptr) w = SDL_GetError();
|
|
|
|
throw SDLError(file, func, l, w);
|
|
|
|
}
|
|
|
|
return code;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|