From 4379f0f9026a154c02de78e01aafee28142334ce Mon Sep 17 00:00:00 2001 From: neargye Date: Wed, 10 Apr 2019 17:40:21 +0500 Subject: [PATCH] update nameof_enum --- README.md | 39 ++++++++++++++++++++++++--------------- include/nameof.hpp | 20 +++++++++++++++++--- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 866901f..4651ecc 100644 --- a/README.md +++ b/README.md @@ -95,21 +95,6 @@ Header-only C++17 library provides nameof macros and functions to obtain simple * 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 - - enum number { one = 100, two = 200, three = 300 }; - - namespace nameof { - template <> - struct enum_range { - static constexpr int min = 100; - static constexpr int max = 300; - }; - } - ``` - * If you need name with template suffix, use NAMEOF_FULL. ```cpp // 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) -> "&some_class::some_method" ``` +* 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 + ``` + + If need another range for specific enum type, add specialization `enum_range` for necessary enum type. + ```cpp + #include + + enum number { one = 100, two = 200, three = 300 }; + + namespace NAMEOF_enum { + template <> + struct enum_range { + 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. ```cpp enum ShapeKind { diff --git a/include/nameof.hpp b/include/nameof.hpp index 14f5f82..9b4ee82 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -37,14 +37,28 @@ #include #include +// 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 { -// 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 struct enum_range final { static_assert(std::is_enum_v, "nameof::enum_range requires enum type."); - static constexpr int min = std::is_signed_v> ? -256 : 0; - static constexpr int max = 256; + static constexpr int min = std::is_signed_v> ? NAMEOF_ENUM_RANGE_MIN : 0; + static constexpr int max = NAMEOF_ENUM_RANGE_MAX; }; namespace detail {