update nameof_enum

This commit is contained in:
neargye 2019-04-10 17:40:21 +05:00
parent e01cf351d5
commit 4379f0f902
2 changed files with 41 additions and 18 deletions

View file

@ -95,21 +95,6 @@ Header-only C++17 library provides nameof macros and functions to obtain simple
* Nameof type returns compiler-specific type name. * Nameof type returns compiler-specific type name.
* Enum value must be in range `[-256, 256]`. If you need another range, add specialization enum_range for necessary enum type.
```cpp
#include <nameof.hpp>
enum number { one = 100, two = 200, three = 300 };
namespace nameof {
template <>
struct enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
}
```
* If you need name with template suffix, use NAMEOF_FULL. * If you need name with template suffix, use NAMEOF_FULL.
```cpp ```cpp
// Name of function. // Name of function.
@ -125,6 +110,30 @@ Header-only C++17 library provides nameof macros and functions to obtain simple
NAMEOF_RAW(&some_class::some_method<int>) -> "&some_class::some_method<int>" NAMEOF_RAW(&some_class::some_method<int>) -> "&some_class::some_method<int>"
``` ```
* Enum value must be in range `[NAMEOF_ENUM_RANGE_MIN, NAMEOF_ENUM_RANGE_MAX]`. By default `NAMEOF_ENUM_RANGE_MIN = -128`, `NAMEOF_ENUM_RANGE_MAX = 128`.
If need another range for all enum types by default, redefine the macro `NAMEOF_ENUM_RANGE_MIN` and `NAMEOF_ENUM_RANGE_MAX`.
```cpp
#define NAMEOF_ENUM_RANGE_MIN 0
#define NAMEOF_ENUM_RANGE_MAX 256
#include <NAMEOF_enum.hpp>
```
If need another range for specific enum type, add specialization `enum_range` for necessary enum type.
```cpp
#include <nameof.hpp>
enum number { one = 100, two = 200, three = 300 };
namespace NAMEOF_enum {
template <>
struct enum_range<number> {
static constexpr int min = 100;
static constexpr int max = 300;
};
}
```
* Nameof enum obtains the first defined value enums, and won't work if value are aliased. * Nameof enum obtains the first defined value enums, and won't work if value are aliased.
```cpp ```cpp
enum ShapeKind { enum ShapeKind {

View file

@ -37,14 +37,28 @@
#include <type_traits> #include <type_traits>
#include <string_view> #include <string_view>
// Enum value must be greater or equals than NAMEOF_ENUM_RANGE_MIN. By default NAMEOF_ENUM_RANGE_MIN = -128.
// If need another min range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN.
#if !defined(NAMEOF_ENUM_RANGE_MIN)
# define NAMEOF_ENUM_RANGE_MIN -128
#endif
// Enum value must be less or equals than NAMEOF_ENUM_RANGE_MAX. By default NAMEOF_ENUM_RANGE_MAX = 128.
// If need another max range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX.
#if !defined(NAMEOF_ENUM_RANGE_MAX)
# define NAMEOF_ENUM_RANGE_MAX 128
#endif
namespace nameof { namespace nameof {
// Enum value must be in range [-256, 256]. If you need another range, add specialization enum_range for necessary enum type. // Enum value must be in range [-NAMEOF_ENUM_RANGE_MAX, NAMEOF_ENUM_RANGE_MIN]. By default NAMEOF_ENUM_RANGE_MIN = -128, NAMEOF_ENUM_RANGE_MAX = 128.
// If need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX and NAMEOF_ENUM_RANGE_MIN.
// If need another range for specific enum type, add specialization enum_range for necessary enum type.
template <typename E> template <typename E>
struct enum_range final { struct enum_range final {
static_assert(std::is_enum_v<E>, "nameof::enum_range requires enum type."); static_assert(std::is_enum_v<E>, "nameof::enum_range requires enum type.");
static constexpr int min = std::is_signed_v<std::underlying_type_t<E>> ? -256 : 0; static constexpr int min = std::is_signed_v<std::underlying_type_t<E>> ? NAMEOF_ENUM_RANGE_MIN : 0;
static constexpr int max = 256; static constexpr int max = NAMEOF_ENUM_RANGE_MAX;
}; };
namespace detail { namespace detail {