mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-13 11:17:06 +07:00
welcome error codes
This commit is contained in:
@@ -27,6 +27,7 @@
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <system_error>
|
||||
#include <tuple>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
@@ -307,23 +308,100 @@ namespace meta_hpp::detail
|
||||
# define META_HPP_TRY try
|
||||
# define META_HPP_CATCH(...) catch ( __VA_ARGS__ )
|
||||
# define META_HPP_RETHROW() throw
|
||||
# define META_HPP_THROW(...) throw ::meta_hpp::detail::exception(__VA_ARGS__)
|
||||
#else
|
||||
# define META_HPP_TRY if ( true )
|
||||
# define META_HPP_CATCH(...) if ( false )
|
||||
# define META_HPP_RETHROW() std::abort()
|
||||
# define META_HPP_THROW(...) std::abort()
|
||||
#endif
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
#if !defined(META_HPP_NO_EXCEPTIONS)
|
||||
class exception final : public std::runtime_error {
|
||||
public:
|
||||
explicit exception(const char* what)
|
||||
: std::runtime_error(what) {}
|
||||
enum class generic_error {
|
||||
no_error,
|
||||
bad_uvalue_cast,
|
||||
bad_argument_cast,
|
||||
bad_instance_cast,
|
||||
};
|
||||
|
||||
class generic_error_category final : public std::error_category {
|
||||
public:
|
||||
~generic_error_category() override = default;
|
||||
|
||||
generic_error_category(generic_error_category&&) = delete;
|
||||
generic_error_category(const generic_error_category&) = delete;
|
||||
|
||||
generic_error_category& operator=(generic_error_category&&) = delete;
|
||||
generic_error_category& operator=(const generic_error_category&) = delete;
|
||||
|
||||
[[nodiscard]] const char* name() const noexcept override {
|
||||
return "generic";
|
||||
}
|
||||
|
||||
[[nodiscard]] std::string message(int ev) const override {
|
||||
switch ( static_cast<generic_error>(ev) ) {
|
||||
case generic_error::no_error:
|
||||
return "no error";
|
||||
case generic_error::bad_uvalue_cast:
|
||||
return "bad uvalue cast";
|
||||
case generic_error::bad_argument_cast:
|
||||
return "bad argument cast";
|
||||
case generic_error::bad_instance_cast:
|
||||
return "bad instance cast";
|
||||
}
|
||||
return "unexpected error code";
|
||||
}
|
||||
|
||||
static const std::error_category& instance() noexcept {
|
||||
static const generic_error_category instance;
|
||||
return instance;
|
||||
}
|
||||
|
||||
private:
|
||||
generic_error_category() = default;
|
||||
};
|
||||
}
|
||||
|
||||
namespace std
|
||||
{
|
||||
template <>
|
||||
struct is_error_code_enum<meta_hpp::detail::generic_error> : true_type {};
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
inline std::error_code make_error_code(generic_error err) noexcept {
|
||||
static_assert(std::is_same_v<int, std::underlying_type_t<generic_error>>);
|
||||
return std::error_code{static_cast<int>(err), generic_error_category::instance()};
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
{
|
||||
class generic_exception final : public std::logic_error {
|
||||
public:
|
||||
explicit generic_exception(generic_error err)
|
||||
: generic_exception{make_error_code(err)} {}
|
||||
|
||||
explicit generic_exception(std::error_code ec)
|
||||
: std::logic_error(ec.message())
|
||||
, error_code_{ec} {}
|
||||
|
||||
[[nodiscard]] const std::error_code& code() const noexcept {
|
||||
return error_code_;
|
||||
}
|
||||
|
||||
private:
|
||||
std::error_code error_code_{};
|
||||
};
|
||||
|
||||
[[noreturn]] inline void throw_generic_exception(generic_error err) {
|
||||
#if !defined(META_HPP_NO_EXCEPTIONS)
|
||||
throw generic_exception{err};
|
||||
#else
|
||||
(void)err;
|
||||
std::abort();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
namespace meta_hpp::detail
|
||||
@@ -1333,9 +1411,8 @@ namespace meta_hpp::detail
|
||||
|
||||
namespace meta_hpp
|
||||
{
|
||||
#if !defined(META_HPP_NO_EXCEPTIONS)
|
||||
using detail::exception;
|
||||
#endif
|
||||
using detail::generic_error;
|
||||
using detail::generic_exception;
|
||||
|
||||
using detail::hashed_string;
|
||||
using detail::memory_buffer;
|
||||
@@ -5558,7 +5635,7 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad argument cast");
|
||||
throw_generic_exception(generic_error::bad_argument_cast);
|
||||
}
|
||||
|
||||
template < typename To >
|
||||
@@ -5614,7 +5691,7 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad argument cast");
|
||||
throw_generic_exception(generic_error::bad_argument_cast);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6068,7 +6145,7 @@ namespace meta_hpp::detail
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad instance cast");
|
||||
throw_generic_exception(generic_error::bad_instance_cast);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8675,7 +8752,7 @@ namespace meta_hpp
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad value cast");
|
||||
throw_generic_exception(generic_error::bad_uvalue_cast);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
@@ -8692,7 +8769,7 @@ namespace meta_hpp
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad value cast");
|
||||
throw_generic_exception(generic_error::bad_uvalue_cast);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
@@ -8709,7 +8786,7 @@ namespace meta_hpp
|
||||
}
|
||||
}
|
||||
|
||||
META_HPP_THROW("bad value cast");
|
||||
throw_generic_exception(generic_error::bad_uvalue_cast);
|
||||
}
|
||||
|
||||
template < typename T >
|
||||
|
||||
Reference in New Issue
Block a user