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

48 lines
1.2 KiB
C++

#ifndef BWIDGETS_EXCEPTION_HPP
#define BWIDGETS_EXCEPTION_HPP
#include <any>
#include <exception>
#include <functional>
#include <map>
#include <basic_widgets/core/type/concepts.hpp>
namespace bwidgets
{
struct BaseException : std::exception
{
const char* file;
const char* func;
const int line;
const char* what;
BaseException(const char* file, const char* func, int l, const char* w)
: file(file), func(func), line(l), what(w)
{}
};
template<typename T>
concept Exception = std::derived_from<T, BaseException>;
template<Exception E, typename T>
static inline auto ptr_or_throw(T* ptr, const char* file, const char* func,
const int l, const char* w = nullptr) -> T*
{
if (ptr == nullptr) throw E(file, func, l, w);
return ptr;
}
template<Exception E, typename T>
static inline auto success_or_throw(
T code, const char* file, const char* func, const int l, const char* w = nullptr,
std::function<bool(const T&)> success = [](const T& code) { return code == 0; })
-> T
{
if (!success(code)) throw E(file, func, l, w);
return code;
}
}
#endif