mirror of
https://github.com/BlackMATov/enum.hpp.git
synced 2025-12-16 22:51:18 +07:00
Merge branch 'refs/heads/dev'
This commit is contained in:
@@ -9,6 +9,14 @@ matrix:
|
|||||||
dist: trusty
|
dist: trusty
|
||||||
addons: { apt: { sources: ubuntu-toolchain-r-test, packages: ["xorg-dev", "g++-8"] } }
|
addons: { apt: { sources: ubuntu-toolchain-r-test, packages: ["xorg-dev", "g++-8"] } }
|
||||||
env: MATRIX_EVAL="CC=gcc-8 && CXX=g++-8"
|
env: MATRIX_EVAL="CC=gcc-8 && CXX=g++-8"
|
||||||
|
- os: linux
|
||||||
|
dist: trusty
|
||||||
|
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-5.0"], packages: ["xorg-dev", "clang-5.0", "g++-7"] } }
|
||||||
|
env: MATRIX_EVAL="CC=clang-5.0 && CXX=clang++-5.0"
|
||||||
|
- os: linux
|
||||||
|
dist: trusty
|
||||||
|
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-6.0"], packages: ["xorg-dev", "clang-6.0", "g++-7"] } }
|
||||||
|
env: MATRIX_EVAL="CC=clang-6.0 && CXX=clang++-6.0"
|
||||||
- os: linux
|
- os: linux
|
||||||
dist: trusty
|
dist: trusty
|
||||||
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-7"], packages: ["xorg-dev", "clang-7", "g++-7"] } }
|
addons: { apt: { sources: ["ubuntu-toolchain-r-test", "llvm-toolchain-trusty-7"], packages: ["xorg-dev", "clang-7", "g++-7"] } }
|
||||||
|
|||||||
244
README.md
244
README.md
@@ -26,7 +26,7 @@
|
|||||||
## Requirements
|
## Requirements
|
||||||
|
|
||||||
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
|
- [gcc](https://www.gnu.org/software/gcc/) **>= 7**
|
||||||
- [clang](https://clang.llvm.org/) **>= 7.0**
|
- [clang](https://clang.llvm.org/) **>= 5.0**
|
||||||
- [msvc](https://visualstudio.microsoft.com/) **>= 2017**
|
- [msvc](https://visualstudio.microsoft.com/) **>= 2017**
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
@@ -63,38 +63,7 @@ enum debug_level : int {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct debug_level_traits {
|
struct debug_level_traits {
|
||||||
using underlying_type = int;
|
/*...*/
|
||||||
static constexpr std::size_t size = 3;
|
|
||||||
|
|
||||||
static constexpr const debug_level values[] = {
|
|
||||||
level_info,
|
|
||||||
level_warning,
|
|
||||||
level_warning
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr const std::string_view names[] = {
|
|
||||||
"level_info",
|
|
||||||
"level_warning",
|
|
||||||
"level_warning"
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr underlying_type to_underlying(debug_level e) noexcept;
|
|
||||||
|
|
||||||
static constexpr std::optional<std::string_view> to_string(debug_level e) noexcept;
|
|
||||||
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
|
|
||||||
static std::string_view to_string_or_throw(Enum e);
|
|
||||||
|
|
||||||
static constexpr std::optional<debug_level> from_string(std::string_view name) noexcept;
|
|
||||||
static constexpr debug_level from_string_or_default(std::string_view name, debug_level def) noexcept;
|
|
||||||
static debug_level from_string_or_throw(std::string_view name);
|
|
||||||
|
|
||||||
static constexpr std::optional<std::size_t> to_index(debug_level e) noexcept;
|
|
||||||
static constexpr std::size_t to_index_or_invalid(debug_level e) noexcept;
|
|
||||||
static std::size_t to_index_or_throw(debug_level e);
|
|
||||||
|
|
||||||
static constexpr std::optional<debug_level> from_index(std::size_t index) noexcept;
|
|
||||||
static constexpr debug_level from_index_or_default(std::size_t index, debug_level def) noexcept;
|
|
||||||
static debug_level from_index_or_throw(std::size_t index);
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -115,40 +84,7 @@ enum class color : unsigned {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct color_traits {
|
struct color_traits {
|
||||||
using underlying_type = unsigned;
|
/*...*/
|
||||||
static constexpr std::size_t size = 4;
|
|
||||||
|
|
||||||
static constexpr const color values[] = {
|
|
||||||
color::red,
|
|
||||||
color::green,
|
|
||||||
color::blue,
|
|
||||||
color::white
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr const std::string_view names[] = {
|
|
||||||
"red",
|
|
||||||
"green",
|
|
||||||
"blue",
|
|
||||||
"white"
|
|
||||||
};
|
|
||||||
|
|
||||||
static constexpr underlying_type to_underlying(color e) noexcept;
|
|
||||||
|
|
||||||
static constexpr std::optional<std::string_view> to_string(color e) noexcept;
|
|
||||||
static constexpr std::string_view to_string_or_empty(Enum e) noexcept;
|
|
||||||
static std::string_view to_string_or_throw(Enum e);
|
|
||||||
|
|
||||||
static constexpr std::optional<color> from_string(std::string_view name) noexcept;
|
|
||||||
static constexpr color from_string_or_default(std::string_view name, color def) noexcept;
|
|
||||||
static color from_string_or_throw(std::string_view name);
|
|
||||||
|
|
||||||
static constexpr std::optional<std::size_t> to_index(color e) noexcept;
|
|
||||||
static constexpr std::size_t to_index_or_invalid(color e) noexcept;
|
|
||||||
static std::size_t to_index_or_throw(color e);
|
|
||||||
|
|
||||||
static constexpr std::optional<color> from_index(std::size_t index) noexcept;
|
|
||||||
static constexpr color from_index_or_default(std::size_t index, color def) noexcept;
|
|
||||||
static color from_index_or_throw(std::size_t index);
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -196,6 +132,180 @@ int main() {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Generic context
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
namespace some_namespace
|
||||||
|
{
|
||||||
|
ENUM_HPP_CLASS_DECL(color, unsigned,
|
||||||
|
(red = 0xFF0000)
|
||||||
|
(green = 0x00FF00)
|
||||||
|
(blue = 0x0000FF)
|
||||||
|
(white = red | green | blue))
|
||||||
|
}
|
||||||
|
|
||||||
|
// register traits in global namespace to generic access
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(color)
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
using color = some_namespace::color;
|
||||||
|
|
||||||
|
// to string
|
||||||
|
static_assert(enum_hpp::to_string(color::red) == "red");
|
||||||
|
|
||||||
|
// from string
|
||||||
|
static_assert(enum_hpp::from_string<color>("red") == color::red);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Adapting external enums
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
namespace external_ns
|
||||||
|
{
|
||||||
|
enum class external_enum : unsigned short {
|
||||||
|
a = 10,
|
||||||
|
b,
|
||||||
|
c = a + b
|
||||||
|
};
|
||||||
|
|
||||||
|
// should be in the same namespace
|
||||||
|
ENUM_HPP_TRAITS_DECL(external_enum,
|
||||||
|
(a)
|
||||||
|
(b)
|
||||||
|
(c))
|
||||||
|
}
|
||||||
|
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(external_ns::external_enum)
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
using ee = external_ns::external_enum;
|
||||||
|
static_assert(enum_hpp::to_string(ee::a) == "a");
|
||||||
|
static_assert(enum_hpp::from_string<ee>("c") == ee::c);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## API
|
||||||
|
|
||||||
|
### Enum traits
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// declare enum
|
||||||
|
ENUM_HPP_DECL(
|
||||||
|
/*enum_name*/,
|
||||||
|
/*underlying_type*/,
|
||||||
|
/*fields*/)
|
||||||
|
|
||||||
|
// declare enum class
|
||||||
|
ENUM_HPP_CLASS_DECL(
|
||||||
|
/*enum_name*/,
|
||||||
|
/*underlying_type*/,
|
||||||
|
/*fields*/)
|
||||||
|
|
||||||
|
// declare only traits
|
||||||
|
ENUM_HPP_TRAITS_DECL(
|
||||||
|
/*enum_name*/,
|
||||||
|
/*fields*/)
|
||||||
|
|
||||||
|
struct /*enum_name*/_traits {
|
||||||
|
using underlying_type = /*underlying_type*/;
|
||||||
|
static constexpr std::size_t size = /*field_count*/;
|
||||||
|
|
||||||
|
static constexpr const std::array</*enum_name*/, /*field_count*/ > values = {
|
||||||
|
/*enum values*/
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr const std::array<std::string_view, /*field_count*/> names = {
|
||||||
|
/*enum names*/
|
||||||
|
};
|
||||||
|
|
||||||
|
static constexpr /*underlying_type*/ to_underlying(/*enum_name*/ e) noexcept;
|
||||||
|
|
||||||
|
static constexpr std::optional<std::string_view> to_string(/*enum_name*/ e) noexcept;
|
||||||
|
static constexpr std::string_view to_string_or_empty(/*enum_name*/ e) noexcept;
|
||||||
|
static std::string_view to_string_or_throw(/*enum_name*/ e);
|
||||||
|
|
||||||
|
static constexpr std::optional</*enum_name*/> from_string(std::string_view name) noexcept;
|
||||||
|
static constexpr /*enum_name*/ from_string_or_default(std::string_view name, /*enum_name*/ def) noexcept;
|
||||||
|
static /*enum_name*/ from_string_or_throw(std::string_view name);
|
||||||
|
|
||||||
|
static constexpr std::optional<std::size_t> to_index(/*enum_name*/ e) noexcept;
|
||||||
|
static constexpr std::size_t to_index_or_invalid(/*enum_name*/ e) noexcept;
|
||||||
|
static std::size_t to_index_or_throw(/*enum_name*/ e);
|
||||||
|
|
||||||
|
static constexpr std::optional</*enum_name*/> from_index(std::size_t index) noexcept;
|
||||||
|
static constexpr /*enum_name*/ from_index_or_default(std::size_t index, /*enum_name*/ def) noexcept;
|
||||||
|
static /*enum_name*/ from_index_or_throw(std::size_t index);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
### Generic functions
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
// should be in global namespace
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(/*declared_enum_name*/)
|
||||||
|
|
||||||
|
namespace enum_hpp
|
||||||
|
{
|
||||||
|
template < typename Enum >
|
||||||
|
using traits_t = typename traits<Enum>::type;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
using underlying_type = typename traits_t<Enum>::underlying_type;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::size_t size() noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr const std::array<Enum, size<Enum>()>& values() noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr const std::array<std::string_view, size<Enum>()>& names() noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr typename traits_t<Enum>::underlying_type to_underlying(Enum e) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<std::string_view> to_string(Enum e) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::string_view to_string_or_empty(Enum e) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
std::string_view to_string_or_throw(Enum e);
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<Enum> from_string(std::string_view name) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
Enum from_string_or_throw(std::string_view name);
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<std::size_t> to_index(Enum e) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::size_t to_index_or_invalid(Enum e) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
std::size_t to_index_or_throw(Enum e);
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<Enum> from_index(std::size_t index) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
Enum from_index_or_throw(std::size_t index);
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
## Alternatives
|
## Alternatives
|
||||||
|
|
||||||
[Better Enums](https://github.com/aantron/better-enums)
|
[Better Enums](https://github.com/aantron/better-enums)
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <array>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
#include <string_view>
|
#include <string_view>
|
||||||
@@ -23,6 +24,98 @@ namespace enum_hpp
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace enum_hpp
|
||||||
|
{
|
||||||
|
template < typename Enum >
|
||||||
|
struct traits;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
using traits_t = typename traits<Enum>::type;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
using underlying_type = typename traits_t<Enum>::underlying_type;
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::size_t size() noexcept {
|
||||||
|
return traits_t<Enum>::size;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr const std::array<Enum, size<Enum>()>& values() noexcept {
|
||||||
|
return traits_t<Enum>::values;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr const std::array<std::string_view, size<Enum>()>& names() noexcept {
|
||||||
|
return traits_t<Enum>::names;
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr typename traits_t<Enum>::underlying_type to_underlying(Enum e) noexcept {
|
||||||
|
return traits_t<Enum>::to_underlying(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<std::string_view> to_string(Enum e) noexcept {
|
||||||
|
return traits_t<Enum>::to_string(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::string_view to_string_or_empty(Enum e) noexcept {
|
||||||
|
return traits_t<Enum>::to_string_or_empty(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
std::string_view to_string_or_throw(Enum e) {
|
||||||
|
return traits_t<Enum>::to_string_or_throw(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<Enum> from_string(std::string_view name) noexcept {
|
||||||
|
return traits_t<Enum>::from_string(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr Enum from_string_or_default(std::string_view name, Enum def) noexcept {
|
||||||
|
return traits_t<Enum>::from_string_or_default(name, def);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
Enum from_string_or_throw(std::string_view name) {
|
||||||
|
return traits_t<Enum>::from_string_or_throw(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<std::size_t> to_index(Enum e) noexcept {
|
||||||
|
return traits_t<Enum>::to_index(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::size_t to_index_or_invalid(Enum e) noexcept {
|
||||||
|
return traits_t<Enum>::to_index_or_invalid(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
std::size_t to_index_or_throw(Enum e) {
|
||||||
|
return traits_t<Enum>::to_index_or_throw(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr std::optional<Enum> from_index(std::size_t index) noexcept {
|
||||||
|
return traits_t<Enum>::from_index(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
constexpr Enum from_index_or_default(std::size_t index, Enum def) noexcept {
|
||||||
|
return traits_t<Enum>::from_index_or_default(index, def);
|
||||||
|
}
|
||||||
|
|
||||||
|
template < typename Enum >
|
||||||
|
Enum from_index_or_throw(std::size_t index) {
|
||||||
|
return traits_t<Enum>::from_index_or_throw(index);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
namespace enum_hpp::detail
|
namespace enum_hpp::detail
|
||||||
{
|
{
|
||||||
template < typename Enum >
|
template < typename Enum >
|
||||||
@@ -118,12 +211,20 @@ namespace enum_hpp::detail
|
|||||||
#define ENUM_HPP_TRAITS_DECL(Enum, Fields)\
|
#define ENUM_HPP_TRAITS_DECL(Enum, Fields)\
|
||||||
struct Enum##_traits {\
|
struct Enum##_traits {\
|
||||||
private:\
|
private:\
|
||||||
enum enum_names_for_this_score_ { ENUM_HPP_GENERATE_ENUM_FIELDS(Fields) };\
|
enum enum_names_for_this_score_ {\
|
||||||
|
ENUM_HPP_GENERATE_ENUM_FIELDS(Fields)\
|
||||||
|
};\
|
||||||
public:\
|
public:\
|
||||||
using underlying_type = std::underlying_type_t<Enum>;\
|
using underlying_type = std::underlying_type_t<Enum>;\
|
||||||
static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\
|
static constexpr std::size_t size = ENUM_HPP_PP_SEQ_SIZE(Fields);\
|
||||||
static constexpr const Enum values[] = { ENUM_HPP_GENERATE_VALUES(Enum, Fields) };\
|
\
|
||||||
static constexpr const std::string_view names[] = { ENUM_HPP_GENERATE_NAMES(Fields) };\
|
static constexpr const std::array<Enum, size> values = {\
|
||||||
|
ENUM_HPP_GENERATE_VALUES(Enum, Fields)\
|
||||||
|
};\
|
||||||
|
\
|
||||||
|
static constexpr const std::array<std::string_view, size> names = {\
|
||||||
|
ENUM_HPP_GENERATE_NAMES(Fields)\
|
||||||
|
};\
|
||||||
public:\
|
public:\
|
||||||
static constexpr underlying_type to_underlying(Enum e) noexcept {\
|
static constexpr underlying_type to_underlying(Enum e) noexcept {\
|
||||||
return static_cast<underlying_type>(e);\
|
return static_cast<underlying_type>(e);\
|
||||||
@@ -213,6 +314,16 @@ namespace enum_hpp::detail
|
|||||||
}\
|
}\
|
||||||
};
|
};
|
||||||
|
|
||||||
|
//
|
||||||
|
// ENUM_HPP_REGISTER_TRAITS
|
||||||
|
//
|
||||||
|
|
||||||
|
#define ENUM_HPP_REGISTER_TRAITS(Enum)\
|
||||||
|
template <>\
|
||||||
|
struct enum_hpp::traits<Enum> {\
|
||||||
|
using type = Enum##_traits;\
|
||||||
|
};
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
//
|
//
|
||||||
// ENUM_HPP_PP
|
// ENUM_HPP_PP
|
||||||
|
|||||||
@@ -21,6 +21,8 @@ namespace
|
|||||||
(white = red | green | blue))
|
(white = red | green | blue))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(color)
|
||||||
|
|
||||||
TEST_CASE("examples") {
|
TEST_CASE("examples") {
|
||||||
SECTION("traits_using") {
|
SECTION("traits_using") {
|
||||||
// size
|
// size
|
||||||
@@ -50,4 +52,12 @@ TEST_CASE("examples") {
|
|||||||
std::cout << n << ",";
|
std::cout << n << ",";
|
||||||
} // stdout: red,green,blue,
|
} // stdout: red,green,blue,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SECTION("generic_context") {
|
||||||
|
// to string
|
||||||
|
static_assert(enum_hpp::to_string(color::red) == "red");
|
||||||
|
|
||||||
|
// from string
|
||||||
|
static_assert(enum_hpp::from_string<color>("red") == color::red);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -53,7 +53,25 @@ namespace some_namespace
|
|||||||
(_181)(_182)(_183)(_184)(_185)(_186)(_187)(_188)(_189)(_190)(_191)(_192)(_193)(_194)(_195)(_196)(_197)(_198)(_199)(_200)
|
(_181)(_182)(_183)(_184)(_185)(_186)(_187)(_188)(_189)(_190)(_191)(_192)(_193)(_194)(_195)(_196)(_197)(_198)(_199)(_200)
|
||||||
(_201)(_202)(_203)(_204)(_205)(_206)(_207)(_208)(_209)(_210)(_211)(_212)(_213)(_214)(_215)(_216)(_217)(_218)(_219)(_220)
|
(_201)(_202)(_203)(_204)(_205)(_206)(_207)(_208)(_209)(_210)(_211)(_212)(_213)(_214)(_215)(_216)(_217)(_218)(_219)(_220)
|
||||||
(_221)(_222)(_223)(_224)(_225)(_226)(_227)(_228)(_229)(_230)(_231)(_232)(_233)(_234)(_235)(_236)(_237)(_238)(_239)(_240))
|
(_221)(_222)(_223)(_224)(_225)(_226)(_227)(_228)(_229)(_230)(_231)(_232)(_233)(_234)(_235)(_236)(_237)(_238)(_239)(_240))
|
||||||
|
|
||||||
|
namespace exns
|
||||||
|
{
|
||||||
|
enum class external_enum : unsigned short {
|
||||||
|
a = 10,
|
||||||
|
b,
|
||||||
|
c = a + b
|
||||||
|
};
|
||||||
|
|
||||||
|
ENUM_HPP_TRAITS_DECL(external_enum,
|
||||||
|
(a)
|
||||||
|
(b)
|
||||||
|
(c))
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(some_namespace::color)
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(some_namespace::numbers)
|
||||||
|
ENUM_HPP_REGISTER_TRAITS(some_namespace::exns::external_enum)
|
||||||
|
|
||||||
TEST_CASE("enum") {
|
TEST_CASE("enum") {
|
||||||
namespace sn = some_namespace;
|
namespace sn = some_namespace;
|
||||||
@@ -64,6 +82,14 @@ TEST_CASE("enum") {
|
|||||||
std::underlying_type_t<sn::color>,
|
std::underlying_type_t<sn::color>,
|
||||||
unsigned>);
|
unsigned>);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(std::is_same_v<
|
||||||
|
sn::color_traits::underlying_type,
|
||||||
|
enum_hpp::underlying_type<sn::color>>);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(std::is_same_v<
|
||||||
|
sn::color_traits,
|
||||||
|
enum_hpp::traits_t<sn::color>>);
|
||||||
|
|
||||||
REQUIRE(enum_to_underlying(sn::color::red) == 2u);
|
REQUIRE(enum_to_underlying(sn::color::red) == 2u);
|
||||||
REQUIRE(enum_to_underlying(sn::color::green) == 3u);
|
REQUIRE(enum_to_underlying(sn::color::green) == 3u);
|
||||||
REQUIRE(enum_to_underlying(sn::color::blue) == 6u);
|
REQUIRE(enum_to_underlying(sn::color::blue) == 6u);
|
||||||
@@ -83,6 +109,14 @@ TEST_CASE("enum") {
|
|||||||
std::underlying_type_t<sn::numbers>,
|
std::underlying_type_t<sn::numbers>,
|
||||||
int>);
|
int>);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(std::is_same_v<
|
||||||
|
sn::numbers_traits::underlying_type,
|
||||||
|
enum_hpp::underlying_type<sn::numbers>>);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(std::is_same_v<
|
||||||
|
sn::numbers_traits,
|
||||||
|
enum_hpp::traits_t<sn::numbers>>);
|
||||||
|
|
||||||
REQUIRE(enum_to_underlying(sn::_0) == 0);
|
REQUIRE(enum_to_underlying(sn::_0) == 0);
|
||||||
REQUIRE(enum_to_underlying(sn::_100) == 100);
|
REQUIRE(enum_to_underlying(sn::_100) == 100);
|
||||||
REQUIRE(enum_to_underlying(sn::_240) == 240);
|
REQUIRE(enum_to_underlying(sn::_240) == 240);
|
||||||
@@ -91,19 +125,29 @@ TEST_CASE("enum") {
|
|||||||
|
|
||||||
SECTION("size") {
|
SECTION("size") {
|
||||||
{
|
{
|
||||||
REQUIRE(sn::color_traits::size == 3u);
|
STATIC_REQUIRE(sn::color_traits::size == 3u);
|
||||||
REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names));
|
STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::names));
|
||||||
REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values));
|
STATIC_REQUIRE(sn::color_traits::size == std::size(sn::color_traits::values));
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::size<sn::color>() == 3u);
|
||||||
|
STATIC_REQUIRE(enum_hpp::traits_t<sn::color>::size == 3u);
|
||||||
|
STATIC_REQUIRE(std::size(enum_hpp::names<sn::color>()) == 3u);
|
||||||
|
STATIC_REQUIRE(std::size(enum_hpp::values<sn::color>()) == 3u);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
REQUIRE(sn::render::mask_traits::size == 4u);
|
STATIC_REQUIRE(sn::render::mask_traits::size == 4u);
|
||||||
REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names));
|
STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::names));
|
||||||
REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values));
|
STATIC_REQUIRE(sn::render::mask_traits::size == std::size(sn::render::mask_traits::values));
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
REQUIRE(sn::numbers_traits::size == 241u);
|
STATIC_REQUIRE(sn::numbers_traits::size == 241u);
|
||||||
REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names));
|
STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::names));
|
||||||
REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values));
|
STATIC_REQUIRE(sn::numbers_traits::size == std::size(sn::numbers_traits::values));
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::size<sn::numbers>() == 241u);
|
||||||
|
STATIC_REQUIRE(enum_hpp::traits_t<sn::numbers>::size == 241u);
|
||||||
|
STATIC_REQUIRE(std::size(enum_hpp::names<sn::numbers>()) == 241u);
|
||||||
|
STATIC_REQUIRE(std::size(enum_hpp::values<sn::numbers>()) == 241u);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -113,6 +157,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green));
|
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green));
|
||||||
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue));
|
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue));
|
||||||
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color(42)) == 42);
|
STATIC_REQUIRE(sn::color_traits::to_underlying(sn::color(42)) == 42);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::red) == enum_to_underlying(sn::color::red));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::green) == enum_to_underlying(sn::color::green));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color::blue) == enum_to_underlying(sn::color::blue));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::color(42)) == 42);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none));
|
STATIC_REQUIRE(sn::render::mask_traits::to_underlying(sn::render::mask::none) == enum_to_underlying(sn::render::mask::none));
|
||||||
@@ -125,6 +174,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180));
|
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_180) == enum_to_underlying(sn::_180));
|
||||||
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240));
|
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::_240) == enum_to_underlying(sn::_240));
|
||||||
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500);
|
STATIC_REQUIRE(sn::numbers_traits::to_underlying(sn::numbers(100500)) == 100500);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_0) == enum_to_underlying(sn::_0));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_180) == enum_to_underlying(sn::_180));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::_240) == enum_to_underlying(sn::_240));
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_underlying(sn::numbers(100500)) == 100500);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -145,6 +199,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42)));
|
STATIC_REQUIRE_FALSE(sn::color_traits::to_string(sn::color(42)));
|
||||||
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == "");
|
STATIC_REQUIRE(sn::color_traits::to_string_or_empty(sn::color(42)) == "");
|
||||||
REQUIRE_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::color_traits::to_string_or_throw(sn::color(42)), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_string(sn::color::green) == "green");
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::color::green) == "green");
|
||||||
|
REQUIRE(enum_hpp::to_string_or_throw(sn::color::green) == "green");
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::color(42)), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none");
|
STATIC_REQUIRE(sn::render::mask_traits::to_string(sn::render::mask::none) == "none");
|
||||||
@@ -178,6 +237,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500)));
|
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_string(sn::numbers(100500)));
|
||||||
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == "");
|
STATIC_REQUIRE(sn::numbers_traits::to_string_or_empty(sn::numbers(100500)) == "");
|
||||||
REQUIRE_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::numbers_traits::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_string(sn::_180) == "_180");
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_string_or_empty(sn::_180) == "_180");
|
||||||
|
REQUIRE(enum_hpp::to_string_or_throw(sn::_180) == "_180");
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::to_string_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,6 +262,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42"));
|
STATIC_REQUIRE_FALSE(sn::color_traits::from_string("42"));
|
||||||
STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red);
|
STATIC_REQUIRE(sn::color_traits::from_string_or_default("42", sn::color::red) == sn::color::red);
|
||||||
REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::color_traits::from_string_or_throw("42"), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_string<sn::color>("green") == sn::color::green);
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_string_or_default("green", sn::color::red) == sn::color::green);
|
||||||
|
REQUIRE(enum_hpp::from_string_or_throw<sn::color>("green") == sn::color::green);
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::from_string_or_throw<sn::color>("42"), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
|
STATIC_REQUIRE(sn::render::mask_traits::from_string("none") == sn::render::mask::none);
|
||||||
@@ -210,6 +279,10 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10);
|
STATIC_REQUIRE(sn::numbers_traits::from_string("_10") == sn::_10);
|
||||||
STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240);
|
STATIC_REQUIRE(sn::numbers_traits::from_string("_240") == sn::_240);
|
||||||
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error"));
|
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_string("error"));
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_string<sn::numbers>("_10") == sn::_10);
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_string<sn::numbers>("_240") == sn::_240);
|
||||||
|
STATIC_REQUIRE_FALSE(enum_hpp::from_string<sn::numbers>("error"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -230,6 +303,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42)));
|
STATIC_REQUIRE_FALSE(sn::color_traits::to_index(sn::color(42)));
|
||||||
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index);
|
STATIC_REQUIRE(sn::color_traits::to_index_or_invalid(sn::color(42)) == enum_hpp::invalid_index);
|
||||||
REQUIRE_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::color_traits::to_index_or_throw(sn::color(42)), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_index(sn::color::green) == 1u);
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::color::green) == 1u);
|
||||||
|
REQUIRE(enum_hpp::to_index_or_throw(sn::color::green) == 1u);
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::color(42)), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u);
|
STATIC_REQUIRE(sn::render::mask_traits::to_index(sn::render::mask::none) == 0u);
|
||||||
@@ -263,6 +341,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500)));
|
STATIC_REQUIRE_FALSE(sn::numbers_traits::to_index(sn::numbers(100500)));
|
||||||
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index);
|
STATIC_REQUIRE(sn::numbers_traits::to_index_or_invalid(sn::numbers(100500)) == enum_hpp::invalid_index);
|
||||||
REQUIRE_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::numbers_traits::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_index(sn::_180) == 180u);
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_index_or_invalid(sn::_180) == 180u);
|
||||||
|
REQUIRE(enum_hpp::to_index_or_throw(sn::_180) == 180u);
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::to_index_or_throw(sn::numbers(100500)), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -283,6 +366,11 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42));
|
STATIC_REQUIRE_FALSE(sn::color_traits::from_index(42));
|
||||||
STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red);
|
STATIC_REQUIRE(sn::color_traits::from_index_or_default(42, sn::color::red) == sn::color::red);
|
||||||
REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception);
|
REQUIRE_THROWS_AS(sn::color_traits::from_index_or_throw(42), enum_hpp::exception);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_index<sn::color>(1) == sn::color::green);
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_index_or_default(1, sn::color::red) == sn::color::green);
|
||||||
|
REQUIRE(enum_hpp::from_index_or_throw<sn::color>(1) == sn::color::green);
|
||||||
|
REQUIRE_THROWS_AS(enum_hpp::from_index_or_throw<sn::color>(42), enum_hpp::exception);
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none);
|
STATIC_REQUIRE(sn::render::mask_traits::from_index(0) == sn::render::mask::none);
|
||||||
@@ -295,6 +383,25 @@ TEST_CASE("enum") {
|
|||||||
STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10);
|
STATIC_REQUIRE(sn::numbers_traits::from_index(10) == sn::_10);
|
||||||
STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240);
|
STATIC_REQUIRE(sn::numbers_traits::from_index(240) == sn::_240);
|
||||||
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500));
|
STATIC_REQUIRE_FALSE(sn::numbers_traits::from_index(100500));
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_index<sn::numbers>(10) == sn::_10);
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_index<sn::numbers>(240) == sn::_240);
|
||||||
|
STATIC_REQUIRE_FALSE(enum_hpp::from_index<sn::numbers>(100500));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("external_enum") {
|
||||||
|
using ee = some_namespace::exns::external_enum;
|
||||||
|
STATIC_REQUIRE(std::is_same_v<enum_hpp::underlying_type<ee>, unsigned short>);
|
||||||
|
STATIC_REQUIRE(enum_hpp::size<ee>() == 3);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::names<ee>()[0] == "a");
|
||||||
|
STATIC_REQUIRE(enum_hpp::names<ee>()[2] == "c");
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::values<ee>()[0] == ee::a);
|
||||||
|
STATIC_REQUIRE(enum_hpp::values<ee>()[2] == ee::c);
|
||||||
|
|
||||||
|
STATIC_REQUIRE(enum_hpp::to_string(ee::c) == "c");
|
||||||
|
STATIC_REQUIRE(enum_hpp::from_string<ee>("b") == ee::b);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user