support "no-exceptions" environment

issue #8
This commit is contained in:
BlackMATov
2020-12-17 01:33:01 +07:00
parent 9b9ce07fe7
commit 639b23480e
2 changed files with 44 additions and 5 deletions

View File

@@ -7,11 +7,17 @@
#pragma once
#include <array>
#include <utility>
#include <cstddef>
#include <cstdlib>
#include <optional>
#include <stdexcept>
#include <string_view>
#include <type_traits>
#include <utility>
#if !defined(__cpp_exceptions) && !defined(__EXCEPTIONS) && !defined(_CPPUNWIND)
# define ENUM_HPP_NO_EXCEPTIONS
#endif
namespace enum_hpp
{
@@ -116,6 +122,15 @@ namespace enum_hpp
namespace enum_hpp::detail
{
inline void throw_exception_with [[noreturn]] (const char* what) {
#ifndef ENUM_HPP_NO_EXCEPTIONS
throw ::enum_hpp::exception(what);
#else
(void)what;
std::abort();
#endif
}
template < typename Enum >
struct ignore_assign final {
Enum value;
@@ -264,7 +279,7 @@ namespace enum_hpp::detail
if ( auto s = to_string(e) ) {\
return *s;\
}\
throw ::enum_hpp::exception(#Enum "_traits::to_string_or_throw(): invalid argument");\
::enum_hpp::detail::throw_exception_with(#Enum "_traits::to_string_or_throw(): invalid argument");\
}\
\
static constexpr std::optional<Enum> from_string(std::string_view name) noexcept {\
@@ -285,7 +300,7 @@ namespace enum_hpp::detail
if ( auto e = from_string(name) ) {\
return *e;\
}\
throw ::enum_hpp::exception(#Enum "_traits::from_string_or_throw(): invalid argument");\
::enum_hpp::detail::throw_exception_with(#Enum "_traits::from_string_or_throw(): invalid argument");\
}\
\
static constexpr std::optional<std::size_t> to_index(Enum e) noexcept {\
@@ -305,7 +320,7 @@ namespace enum_hpp::detail
if ( auto i = to_index(e) ) {\
return *i;\
}\
throw ::enum_hpp::exception(#Enum "_traits::to_index_or_throw(): invalid argument");\
::enum_hpp::detail::throw_exception_with(#Enum "_traits::to_index_or_throw(): invalid argument");\
}\
\
static constexpr std::optional<Enum> from_index(std::size_t index) noexcept {\
@@ -324,7 +339,7 @@ namespace enum_hpp::detail
if ( auto e = from_index(index) ) {\
return *e;\
}\
throw ::enum_hpp::exception(#Enum "_traits::from_index_or_throw(): invalid argument");\
::enum_hpp::detail::throw_exception_with(#Enum "_traits::from_index_or_throw(): invalid argument");\
}\
};

View File

@@ -204,12 +204,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::color_traits::to_string(sn::color(42)));
STATIC_CHECK(sn::color_traits::to_string_or_empty(sn::color(42)) == "");
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::to_string(sn::color::green) == "green");
STATIC_CHECK(enum_hpp::to_string_or_empty(sn::color::green) == "green");
CHECK(enum_hpp::to_string_or_throw(sn::color::green) == "green");
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_string_or_throw(sn::color(42)), enum_hpp::exception);
#endif
}
{
STATIC_CHECK(sn::render::mask_traits::to_string(sn::render::mask::none) == "none");
@@ -242,12 +246,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::numbers_traits::to_string(sn::numbers(100500)));
STATIC_CHECK(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == "");
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::to_string(sn::_180) == "_180");
STATIC_CHECK(enum_hpp::to_string_or_empty(sn::_180) == "_180");
CHECK(enum_hpp::to_string_or_throw(sn::_180) == "_180");
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
}
}
@@ -267,12 +275,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::color_traits::from_string("42"));
STATIC_CHECK(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::from_string<sn::color>("green") == sn::color::green);
STATIC_CHECK(enum_hpp::from_string_or_default("green", sn::color::red) == sn::color::green);
CHECK(enum_hpp::from_string_or_throw<sn::color>("green") == sn::color::green);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::from_string_or_throw<sn::color>("42"), enum_hpp::exception);
#endif
}
{
STATIC_CHECK(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
@@ -308,12 +320,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::color_traits::to_index(sn::color(42)));
STATIC_CHECK(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::to_index(sn::color::green) == 1u);
STATIC_CHECK(enum_hpp::to_index_or_invalid(sn::color::green) == 1u);
CHECK(enum_hpp::to_index_or_throw(sn::color::green) == 1u);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_index_or_throw(sn::color(42)), enum_hpp::exception);
#endif
}
{
STATIC_CHECK(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u);
@@ -346,12 +362,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::numbers_traits::to_index(sn::numbers(100500)));
STATIC_CHECK(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::to_index(sn::_180) == 180u);
STATIC_CHECK(enum_hpp::to_index_or_invalid(sn::_180) == 180u);
CHECK(enum_hpp::to_index_or_throw(sn::_180) == 180u);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
#endif
}
}
@@ -371,12 +391,16 @@ TEST_CASE("enum") {
STATIC_CHECK_FALSE(sn::color_traits::from_index(42));
STATIC_CHECK(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception);
#endif
STATIC_CHECK(enum_hpp::from_index<sn::color>(1) == sn::color::green);
STATIC_CHECK(enum_hpp::from_index_or_default(1, sn::color::red) == sn::color::green);
CHECK(enum_hpp::from_index_or_throw<sn::color>(1) == sn::color::green);
#ifndef ENUM_HPP_NO_EXCEPTIONS
CHECK_THROWS_AS(enum_hpp::from_index_or_throw<sn::color>(42), enum_hpp::exception);
#endif
}
{
STATIC_CHECK(sn::render::mask_traits::from_index(0) == sn::render::mask::none);