2018-04-10 19:32:51 +00:00
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
2022-08-04 13:32:59 +00:00
// version 0.10.2
2018-03-17 03:09:49 +00:00
//
2018-03-18 16:34:09 +00:00
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
2019-03-20 16:41:51 +00:00
// SPDX-License-Identifier: MIT
2023-03-30 15:10:22 +00:00
// Copyright (c) 2016 - 2023 Daniil Goncharov <neargye@gmail.com>.
2018-03-17 03:09:49 +00:00
//
2018-03-24 08:58:08 +00:00
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
2018-03-17 03:09:49 +00:00
//
2018-03-24 08:58:08 +00:00
// The above copyright notice and this permission notice shall be included in all
2018-03-17 03:27:47 +00:00
// copies or substantial portions of the Software.
2018-03-17 03:09:49 +00:00
//
2018-03-24 08:58:08 +00:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2018-03-17 03:27:47 +00:00
// SOFTWARE.
2019-05-28 12:54:10 +00:00
# ifndef NEARGYE_NAMEOF_HPP
# define NEARGYE_NAMEOF_HPP
2018-04-10 18:09:23 +00:00
2020-07-03 13:18:05 +00:00
# define NAMEOF_VERSION_MAJOR 0
2021-01-12 08:39:52 +00:00
# define NAMEOF_VERSION_MINOR 10
2022-08-04 13:32:59 +00:00
# define NAMEOF_VERSION_PATCH 2
2020-07-03 13:18:05 +00:00
2019-04-08 17:55:35 +00:00
# include <array>
2019-10-23 13:45:32 +00:00
# include <cassert>
2019-10-19 11:23:36 +00:00
# include <cstdint>
2018-03-17 05:06:38 +00:00
# include <cstddef>
2019-09-14 22:19:34 +00:00
# include <iosfwd>
2019-10-05 13:39:11 +00:00
# include <iterator>
2019-03-24 11:23:41 +00:00
# include <limits>
2019-10-01 13:43:24 +00:00
# include <type_traits>
# include <utility>
2018-08-26 23:57:25 +00:00
2020-09-01 11:00:59 +00:00
# if !defined(NAMEOF_USING_ALIAS_STRING)
# include <string>
# endif
# if !defined(NAMEOF_USING_ALIAS_STRING_VIEW)
# include <string_view>
# endif
2020-06-03 14:25:30 +00:00
# if __has_include(<cxxabi.h>)
# include <cxxabi.h>
# include <cstdlib>
# endif
2020-06-18 10:34:22 +00:00
# if defined(__clang__)
# pragma clang diagnostic push
2022-08-09 11:45:43 +00:00
# pragma clang diagnostic ignored "-Wunknown-warning-option"
# pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
2020-06-18 10:34:22 +00:00
# elif defined(__GNUC__)
# pragma GCC diagnostic push
2020-07-03 13:18:05 +00:00
# pragma GCC diagnostic ignored "-Wstringop-overflow" // Missing terminating nul 'enum_name_v'.
2020-06-18 10:34:22 +00:00
# elif defined(_MSC_VER)
2019-12-13 15:31:55 +00:00
# pragma warning(push)
2020-06-18 10:34:22 +00:00
# pragma warning(disable : 26495) // Variable 'cstring<N>::chars_' is uninitialized.
2021-01-24 06:39:43 +00:00
# pragma warning(disable : 28020) // Arithmetic overflow: Using operator '-' on a 4 byte value and then casting the result to a 8 byte value.
# pragma warning(disable : 26451) // The expression '0<=_Param_(1)&&_Param_(1)<=1-1' is not true at this call.
2022-06-29 12:40:42 +00:00
# pragma warning(disable : 4514) // Unreferenced inline function has been removed.
2019-12-13 15:31:55 +00:00
# endif
2019-10-02 10:23:54 +00:00
// Checks nameof_type compiler compatibility.
2020-06-03 14:51:05 +00:00
# if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && _MSC_VER >= 1910
2020-02-14 10:28:16 +00:00
# undef NAMEOF_TYPE_SUPPORTED
2019-10-02 10:23:54 +00:00
# define NAMEOF_TYPE_SUPPORTED 1
# endif
2020-06-03 14:51:05 +00:00
// Checks nameof_type_rtti compiler compatibility.
# if defined(__clang__)
# if __has_feature(cxx_rtti)
# undef NAMEOF_TYPE_RTTI_SUPPORTED
# define NAMEOF_TYPE_RTTI_SUPPORTED 1
# endif
# elif defined(__GNUC__)
# if defined(__GXX_RTTI)
# undef NAMEOF_TYPE_RTTI_SUPPORTED
# define NAMEOF_TYPE_RTTI_SUPPORTED 1
# endif
# elif defined(_MSC_VER)
# if defined(_CPPRTTI)
# undef NAMEOF_TYPE_RTTI_SUPPORTED
# define NAMEOF_TYPE_RTTI_SUPPORTED 1
# endif
2020-06-03 14:33:01 +00:00
# endif
2021-04-02 21:06:56 +00:00
// Checks nameof_member compiler compatibility.
2022-07-21 17:07:19 +00:00
# if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
2021-04-02 21:06:56 +00:00
# undef NAMEOF_MEMBER_SUPPORTED
# define NAMEOF_MEMBER_SUPPORTED 1
# endif
2023-03-17 17:39:30 +00:00
// Checks nameof_pointer compiler compatibility.
# if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 7 || defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
# undef NAMEOF_POINTER_SUPPORTED
# define NAMEOF_POINTER_SUPPORTED 1
# endif
2019-10-02 10:23:54 +00:00
// Checks nameof_enum compiler compatibility.
2020-06-03 14:51:05 +00:00
# if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1910
2020-02-14 10:28:16 +00:00
# undef NAMEOF_ENUM_SUPPORTED
2019-10-02 10:23:54 +00:00
# define NAMEOF_ENUM_SUPPORTED 1
# endif
2020-09-01 11:00:59 +00:00
// Checks nameof_enum compiler aliases compatibility.
# if defined(__clang__) && __clang_major__ >= 5 || defined(__GNUC__) && __GNUC__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1920
# undef NAMEOF_ENUM_SUPPORTED_ALIASES
# define NAMEOF_ENUM_SUPPORTED_ALIASES 1
# endif
2019-04-10 12:40:21 +00:00
// 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
2018-04-04 12:19:02 +00:00
namespace nameof {
2018-04-10 18:09:23 +00:00
2020-12-29 18:45:54 +00:00
// If need another string_view type, define the macro NAMEOF_USING_ALIAS_STRING_VIEW.
# if defined(NAMEOF_USING_ALIAS_STRING_VIEW)
NAMEOF_USING_ALIAS_STRING_VIEW
# else
2021-04-28 17:51:50 +00:00
using std : : string_view ;
2020-12-29 18:45:54 +00:00
# endif
// If need another string type, define the macro NAMEOF_USING_ALIAS_STRING.
# if defined(NAMEOF_USING_ALIAS_STRING)
NAMEOF_USING_ALIAS_STRING
# else
2021-04-28 17:51:50 +00:00
using std : : string ;
2020-12-29 18:45:54 +00:00
# endif
namespace customize {
2019-04-28 10:28:20 +00:00
// 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.
2023-03-17 17:39:30 +00:00
// If you need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN and NAMEOF_ENUM_RANGE_MAX.
// If you need another range for specific enum type, add specialization enum_range for necessary enum type.
2019-04-08 17:55:35 +00:00
template < typename E >
2019-10-09 17:48:48 +00:00
struct enum_range {
2020-12-29 18:45:54 +00:00
static_assert ( std : : is_enum_v < E > , " nameof::customize::enum_range requires enum type. " ) ;
2019-10-01 13:43:24 +00:00
inline static constexpr int min = NAMEOF_ENUM_RANGE_MIN ;
inline static constexpr int max = NAMEOF_ENUM_RANGE_MAX ;
2020-12-29 18:45:54 +00:00
static_assert ( max > min , " nameof::customize::enum_range requires max > min. " ) ;
2019-04-08 17:55:35 +00:00
} ;
2019-03-24 11:23:41 +00:00
2019-10-01 13:43:24 +00:00
static_assert ( NAMEOF_ENUM_RANGE_MIN < = 0 , " NAMEOF_ENUM_RANGE_MIN must be less or equals than 0. " ) ;
static_assert ( NAMEOF_ENUM_RANGE_MIN > ( std : : numeric_limits < std : : int16_t > : : min ) ( ) , " NAMEOF_ENUM_RANGE_MIN must be greater than INT16_MIN. " ) ;
2019-07-14 15:49:14 +00:00
2019-10-01 13:43:24 +00:00
static_assert ( NAMEOF_ENUM_RANGE_MAX > 0 , " NAMEOF_ENUM_RANGE_MAX must be greater than 0. " ) ;
static_assert ( NAMEOF_ENUM_RANGE_MAX < ( std : : numeric_limits < std : : int16_t > : : max ) ( ) , " NAMEOF_ENUM_RANGE_MAX must be less than INT16_MAX. " ) ;
2019-07-14 15:49:14 +00:00
2019-10-01 13:43:24 +00:00
static_assert ( NAMEOF_ENUM_RANGE_MAX > NAMEOF_ENUM_RANGE_MIN , " NAMEOF_ENUM_RANGE_MAX must be greater than NAMEOF_ENUM_RANGE_MIN. " ) ;
2019-07-14 16:00:34 +00:00
2023-03-17 17:39:30 +00:00
// If you need custom names for enum, add specialization enum_name for necessary enum type.
2020-12-29 18:45:54 +00:00
template < typename E >
constexpr string_view enum_name ( E ) noexcept {
static_assert ( std : : is_enum_v < E > , " nameof::customize::enum_name requires enum type. " ) ;
2019-10-03 13:25:40 +00:00
2020-12-29 18:45:54 +00:00
return { } ;
}
2019-08-26 16:56:57 +00:00
2023-03-17 17:39:30 +00:00
// If you need custom name for type, add specialization type_name for necessary type.
2020-12-29 18:45:54 +00:00
template < typename T >
constexpr string_view type_name ( ) noexcept {
return { } ;
}
2019-12-12 15:12:42 +00:00
2023-03-17 17:39:30 +00:00
// If you need custom name for member, add specialization member_name for necessary type.
2021-04-02 21:06:56 +00:00
template < auto V >
constexpr string_view member_name ( ) noexcept {
return { } ;
}
2023-03-17 17:39:30 +00:00
// If you need custom name for a pointer, add specialization pointer_name for necessary type.
template < auto V >
constexpr string_view pointer_name ( ) noexcept {
return { } ;
}
2020-12-29 18:45:54 +00:00
} // namespace nameof::customize
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
class [[nodiscard]] cstring {
2019-10-23 13:45:32 +00:00
public :
2020-05-24 12:52:39 +00:00
using value_type = const char ;
2022-08-15 08:38:17 +00:00
using size_type = std : : uint16_t ;
2019-10-23 13:45:32 +00:00
using difference_type = std : : ptrdiff_t ;
2020-05-24 12:52:39 +00:00
using pointer = const char * ;
2019-10-23 13:45:32 +00:00
using const_pointer = const char * ;
2020-05-24 12:52:39 +00:00
using reference = const char & ;
2019-10-23 13:45:32 +00:00
using const_reference = const char & ;
2019-09-14 22:19:34 +00:00
2020-05-24 12:52:39 +00:00
using iterator = const char * ;
2019-10-23 13:45:32 +00:00
using const_iterator = const char * ;
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
using reverse_iterator = std : : reverse_iterator < iterator > ;
using const_reverse_iterator = std : : reverse_iterator < const_iterator > ;
2019-09-14 22:19:34 +00:00
2022-08-15 08:38:17 +00:00
constexpr explicit cstring ( string_view str ) noexcept : cstring { str , std : : make_integer_sequence < std : : uint16_t , N > { } } {
2020-12-29 18:45:54 +00:00
assert ( str . size ( ) > 0 & & str . size ( ) = = N ) ;
2019-12-13 15:31:55 +00:00
}
2019-10-23 13:45:32 +00:00
constexpr cstring ( ) = delete ;
constexpr cstring ( const cstring & ) = default ;
constexpr cstring ( cstring & & ) = default ;
~ cstring ( ) = default ;
2020-06-06 10:41:29 +00:00
cstring & operator = ( const cstring & ) = default ;
2019-10-23 13:45:32 +00:00
2020-06-06 10:41:29 +00:00
cstring & operator = ( cstring & & ) = default ;
2019-10-23 13:45:32 +00:00
2021-03-10 09:39:56 +00:00
[[nodiscard]] constexpr const_pointer data ( ) const noexcept { return chars_ ; }
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr size_type size ( ) const noexcept { return N ; }
[[nodiscard]] constexpr const_iterator begin ( ) const noexcept { return data ( ) ; }
[[nodiscard]] constexpr const_iterator end ( ) const noexcept { return data ( ) + size ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_iterator cbegin ( ) const noexcept { return begin ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_iterator cend ( ) const noexcept { return end ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reverse_iterator rbegin ( ) const noexcept { return end ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reverse_iterator rend ( ) const noexcept { return begin ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reverse_iterator crbegin ( ) const noexcept { return rbegin ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reverse_iterator crend ( ) const noexcept { return rend ( ) ; }
2019-09-14 22:19:34 +00:00
2020-05-24 12:52:39 +00:00
[[nodiscard]] constexpr const_reference operator [ ] ( size_type i ) const noexcept { return assert ( i < size ( ) ) , chars_ [ i ] ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reference front ( ) const noexcept { return chars_ [ 0 ] ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr const_reference back ( ) const noexcept { return chars_ [ N ] ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr size_type length ( ) const noexcept { return size ( ) ; }
2019-09-14 22:19:34 +00:00
2019-10-23 13:45:32 +00:00
[[nodiscard]] constexpr bool empty ( ) const noexcept { return false ; }
2019-09-14 22:19:34 +00:00
2022-05-18 12:26:02 +00:00
[[nodiscard]] constexpr int compare ( string_view str ) const noexcept { return string_view { data ( ) , size ( ) } . compare ( str ) ; }
2019-09-14 22:19:34 +00:00
2019-10-24 11:18:22 +00:00
[[nodiscard]] constexpr const char * c_str ( ) const noexcept { return data ( ) ; }
2019-08-26 16:56:57 +00:00
2020-12-29 18:45:54 +00:00
[[nodiscard]] string str ( ) const { return { begin ( ) , end ( ) } ; }
2019-10-24 15:26:53 +00:00
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr operator string_view ( ) const noexcept { return { data ( ) , size ( ) } ; }
2019-08-26 16:56:57 +00:00
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr explicit operator const_pointer ( ) const noexcept { return data ( ) ; }
[[nodiscard]] explicit operator string ( ) const { return { begin ( ) , end ( ) } ; }
private :
2022-08-15 08:38:17 +00:00
template < std : : uint16_t . . . I >
constexpr cstring ( string_view str , std : : integer_sequence < std : : uint16_t , I . . . > ) noexcept : chars_ { str [ I ] . . . , ' \0 ' } { }
2019-10-24 15:26:53 +00:00
2022-08-15 08:38:17 +00:00
char chars_ [ static_cast < std : : size_t > ( N ) + 1 ] ;
2019-08-26 16:56:57 +00:00
} ;
2022-05-18 12:26:02 +00:00
template < >
class [[nodiscard]] cstring < 0 > {
public :
using value_type = const char ;
2022-08-15 08:38:17 +00:00
using size_type = std : : uint16_t ;
2022-05-18 12:26:02 +00:00
using difference_type = std : : ptrdiff_t ;
using pointer = const char * ;
using const_pointer = const char * ;
using reference = const char & ;
using const_reference = const char & ;
using iterator = const char * ;
using const_iterator = const char * ;
using reverse_iterator = std : : reverse_iterator < iterator > ;
using const_reverse_iterator = std : : reverse_iterator < const_iterator > ;
constexpr explicit cstring ( string_view ) noexcept { }
2022-08-12 17:38:20 +00:00
constexpr cstring ( ) = default ;
2022-05-18 12:26:02 +00:00
constexpr cstring ( const cstring & ) = default ;
constexpr cstring ( cstring & & ) = default ;
~ cstring ( ) = default ;
cstring & operator = ( const cstring & ) = default ;
cstring & operator = ( cstring & & ) = default ;
[[nodiscard]] constexpr const_pointer data ( ) const noexcept { return nullptr ; }
[[nodiscard]] constexpr size_type size ( ) const noexcept { return 0 ; }
2022-05-18 14:31:02 +00:00
[[nodiscard]] constexpr const_iterator begin ( ) const noexcept { return nullptr ; }
[[nodiscard]] constexpr const_iterator end ( ) const noexcept { return nullptr ; }
[[nodiscard]] constexpr const_iterator cbegin ( ) const noexcept { return nullptr ; }
[[nodiscard]] constexpr const_iterator cend ( ) const noexcept { return nullptr ; }
[[nodiscard]] constexpr const_reverse_iterator rbegin ( ) const noexcept { return { } ; }
[[nodiscard]] constexpr const_reverse_iterator rend ( ) const noexcept { return { } ; }
[[nodiscard]] constexpr const_reverse_iterator crbegin ( ) const noexcept { return { } ; }
[[nodiscard]] constexpr const_reverse_iterator crend ( ) const noexcept { return { } ; }
2022-05-18 12:26:02 +00:00
[[nodiscard]] constexpr size_type length ( ) const noexcept { return 0 ; }
[[nodiscard]] constexpr bool empty ( ) const noexcept { return true ; }
[[nodiscard]] constexpr int compare ( string_view str ) const noexcept { return string_view { } . compare ( str ) ; }
[[nodiscard]] constexpr const char * c_str ( ) const noexcept { return nullptr ; }
[[nodiscard]] string str ( ) const { return { } ; }
[[nodiscard]] constexpr operator string_view ( ) const noexcept { return { } ; }
[[nodiscard]] constexpr explicit operator const_pointer ( ) const noexcept { return nullptr ; }
[[nodiscard]] explicit operator string ( ) const { return { } ; }
} ;
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator = = ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) = = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator = = ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) = = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator ! = ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) ! = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator ! = ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) ! = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator > ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) > 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator > ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) > 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator > = ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) > = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator > = ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) > = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator < ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) < 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator < ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) < 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator < = ( const cstring < N > & lhs , string_view rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) < = 0 ;
}
2022-08-15 08:38:17 +00:00
template < std : : uint16_t N >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr bool operator < = ( string_view lhs , const cstring < N > & rhs ) noexcept {
2019-09-14 22:19:34 +00:00
return lhs . compare ( rhs ) < = 0 ;
}
2022-08-15 08:38:17 +00:00
template < typename Char , typename Traits , std : : uint16_t N >
2019-10-03 13:25:40 +00:00
std : : basic_ostream < Char , Traits > & operator < < ( std : : basic_ostream < Char , Traits > & os , const cstring < N > & srt ) {
2020-09-01 11:00:59 +00:00
for ( const auto c : srt ) {
2019-09-14 22:19:34 +00:00
os . put ( c ) ;
}
return os ;
}
2019-10-03 13:25:40 +00:00
namespace detail {
2021-01-12 08:39:52 +00:00
constexpr string_view pretty_name ( string_view name , bool remove_suffix = true ) noexcept {
2019-09-14 22:19:34 +00:00
if ( name . size ( ) > = 1 & & ( name [ 0 ] = = ' " ' | | name [ 0 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // Narrow multibyte string literal.
2019-08-26 16:56:57 +00:00
} else if ( name . size ( ) > = 2 & & name [ 0 ] = = ' R ' & & ( name [ 1 ] = = ' " ' | | name [ 1 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // Raw string literal.
2019-08-26 16:56:57 +00:00
} else if ( name . size ( ) > = 2 & & name [ 0 ] = = ' L ' & & ( name [ 1 ] = = ' " ' | | name [ 1 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // Wide string literal.
2019-08-26 16:56:57 +00:00
} else if ( name . size ( ) > = 2 & & name [ 0 ] = = ' U ' & & ( name [ 1 ] = = ' " ' | | name [ 1 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // UTF-32 encoded string literal.
2019-08-26 16:56:57 +00:00
} else if ( name . size ( ) > = 2 & & name [ 0 ] = = ' u ' & & ( name [ 1 ] = = ' " ' | | name [ 1 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // UTF-16 encoded string literal.
2019-08-26 16:56:57 +00:00
} else if ( name . size ( ) > = 3 & & name [ 0 ] = = ' u ' & & name [ 1 ] = = ' 8 ' & & ( name [ 2 ] = = ' " ' | | name [ 2 ] = = ' \' ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // UTF-8 encoded string literal.
2019-09-14 22:19:34 +00:00
} else if ( name . size ( ) > = 1 & & ( name [ 0 ] > = ' 0 ' & & name [ 0 ] < = ' 9 ' ) ) {
2019-04-11 14:55:29 +00:00
return { } ; // Invalid name.
}
2019-08-26 16:56:57 +00:00
for ( std : : size_t i = name . size ( ) , h = 0 , s = 0 ; i > 0 ; - - i ) {
2019-04-11 14:55:29 +00:00
if ( name [ i - 1 ] = = ' ) ' ) {
+ + h ;
+ + s ;
continue ;
} else if ( name [ i - 1 ] = = ' ( ' ) {
- - h ;
+ + s ;
continue ;
}
if ( h = = 0 ) {
name . remove_suffix ( s ) ;
break ;
} else {
+ + s ;
continue ;
}
}
std : : size_t s = 0 ;
2019-08-26 16:56:57 +00:00
for ( std : : size_t i = name . size ( ) , h = 0 ; i > 0 ; - - i ) {
2019-04-11 14:55:29 +00:00
if ( name [ i - 1 ] = = ' > ' ) {
+ + h ;
+ + s ;
continue ;
} else if ( name [ i - 1 ] = = ' < ' ) {
- - h ;
+ + s ;
continue ;
}
if ( h = = 0 ) {
break ;
} else {
+ + s ;
continue ;
}
}
2019-08-26 16:56:57 +00:00
for ( std : : size_t i = name . size ( ) - s ; i > 0 ; - - i ) {
2019-04-11 14:55:29 +00:00
if ( ! ( ( name [ i - 1 ] > = ' 0 ' & & name [ i - 1 ] < = ' 9 ' ) | |
( name [ i - 1 ] > = ' a ' & & name [ i - 1 ] < = ' z ' ) | |
( name [ i - 1 ] > = ' A ' & & name [ i - 1 ] < = ' Z ' ) | |
( name [ i - 1 ] = = ' _ ' ) ) ) {
name . remove_prefix ( i ) ;
break ;
}
}
2021-01-12 08:39:52 +00:00
if ( remove_suffix ) {
2019-07-21 19:13:32 +00:00
name . remove_suffix ( s ) ;
}
2019-04-11 14:55:29 +00:00
2022-08-13 14:18:52 +00:00
if ( name . size ( ) > 0 & & ( ( name [ 0 ] > = ' a ' & & name [ 0 ] < = ' z ' ) | |
( name [ 0 ] > = ' A ' & & name [ 0 ] < = ' Z ' ) | |
( name [ 0 ] = = ' _ ' ) ) ) {
2019-04-11 14:55:29 +00:00
return name ;
}
2019-07-23 13:50:18 +00:00
return { } ; // Invalid name.
2018-07-13 15:18:52 +00:00
}
2021-03-10 09:39:56 +00:00
template < typename T , std : : size_t N , std : : size_t . . . I >
constexpr std : : array < std : : remove_cv_t < T > , N > to_array ( T ( & a ) [ N ] , std : : index_sequence < I . . . > ) {
return { { a [ I ] . . . } } ;
}
2020-09-01 11:00:59 +00:00
template < typename L , typename R >
constexpr bool cmp_less ( L lhs , R rhs ) noexcept {
static_assert ( std : : is_integral_v < L > & & std : : is_integral_v < R > , " nameof::detail::cmp_less requires integral type. " ) ;
if constexpr ( std : : is_signed_v < L > = = std : : is_signed_v < R > ) {
// If same signedness (both signed or both unsigned).
return lhs < rhs ;
2022-05-18 12:26:02 +00:00
} else if constexpr ( std : : is_same_v < L , bool > ) { // bool special case
2021-06-26 09:03:55 +00:00
return static_cast < R > ( lhs ) < rhs ;
2022-05-18 12:26:02 +00:00
} else if constexpr ( std : : is_same_v < R , bool > ) { // bool special case
2021-06-26 09:03:55 +00:00
return lhs < static_cast < L > ( rhs ) ;
2020-09-01 11:00:59 +00:00
} else if constexpr ( std : : is_signed_v < R > ) {
// If 'right' is negative, then result is 'false', otherwise cast & compare.
return rhs > 0 & & lhs < static_cast < std : : make_unsigned_t < R > > ( rhs ) ;
} else {
// If 'left' is negative, then result is 'true', otherwise cast & compare.
return lhs < 0 | | static_cast < std : : make_unsigned_t < L > > ( lhs ) < rhs ;
}
}
template < typename I >
constexpr I log2 ( I value ) noexcept {
static_assert ( std : : is_integral_v < I > , " nameof::detail::log2 requires integral type. " ) ;
2022-05-18 12:26:02 +00:00
if constexpr ( std : : is_same_v < I , bool > ) { // bool special case
return assert ( false ) , value ;
} else {
auto ret = I { 0 } ;
for ( ; value > I { 1 } ; value > > = I { 1 } , + + ret ) { }
2020-09-01 11:00:59 +00:00
2022-05-18 12:26:02 +00:00
return ret ;
}
2020-09-01 11:00:59 +00:00
}
2020-07-03 13:18:05 +00:00
template < typename T >
struct nameof_enum_supported
# if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT)
: std : : true_type { } ;
# else
: std : : false_type { } ;
# endif
template < typename T , typename R >
using enable_if_enum_t = std : : enable_if_t < std : : is_enum_v < std : : decay_t < T > > , R > ;
template < typename T >
inline constexpr bool is_enum_v = std : : is_enum_v < T > & & std : : is_same_v < T , std : : decay_t < T > > ;
2019-07-13 14:32:33 +00:00
template < typename E , E V >
2019-09-13 14:45:01 +00:00
constexpr auto n ( ) noexcept {
2019-10-01 13:55:37 +00:00
static_assert ( is_enum_v < E > , " nameof::detail::n requires enum type. " ) ;
2020-12-29 18:45:54 +00:00
2022-08-12 17:38:20 +00:00
if constexpr ( nameof_enum_supported < E > : : value ) {
2022-05-18 12:26:02 +00:00
# if defined(__clang__) || defined(__GNUC__)
2020-12-29 18:45:54 +00:00
constexpr auto name = pretty_name ( { __PRETTY_FUNCTION__ , sizeof ( __PRETTY_FUNCTION__ ) - 2 } ) ;
2022-05-18 12:26:02 +00:00
# elif defined(_MSC_VER)
2020-12-29 18:45:54 +00:00
constexpr auto name = pretty_name ( { __FUNCSIG__ , sizeof ( __FUNCSIG__ ) - 17 } ) ;
2019-10-02 10:23:54 +00:00
# else
2022-05-18 12:26:02 +00:00
constexpr auto name = string_view { } ;
2019-07-13 14:32:33 +00:00
# endif
2022-05-18 12:26:02 +00:00
return cstring < name . size ( ) > { name } ;
2020-12-29 18:45:54 +00:00
} else {
2022-08-12 17:38:20 +00:00
return cstring < 0 > { } ;
2020-12-29 18:45:54 +00:00
}
2019-07-13 14:32:33 +00:00
}
2019-08-26 16:56:57 +00:00
template < typename E , E V >
2022-08-12 17:38:20 +00:00
constexpr auto enum_name ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::n requires enum type. " ) ;
[[maybe_unused]] constexpr auto custom_name = customize : : enum_name < E > ( V ) ;
2020-06-06 10:41:29 +00:00
2022-08-12 17:38:20 +00:00
if constexpr ( custom_name . empty ( ) ) {
return n < E , V > ( ) ;
} else {
return cstring < custom_name . size ( ) > { custom_name } ;
}
2020-06-06 10:41:29 +00:00
}
2022-08-12 17:38:20 +00:00
template < typename E , E V >
inline constexpr auto enum_name_v = enum_name < E , V > ( ) ;
2022-08-09 11:28:23 +00:00
template < typename E , auto V >
2022-08-12 17:38:20 +00:00
constexpr bool is_valid ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::is_valid requires enum type. " ) ;
# if defined(__clang__) && __clang_major__ >= 16
// https://reviews.llvm.org/D130058, https://reviews.llvm.org/D131307
constexpr E v = __builtin_bit_cast ( E , V ) ;
[[maybe_unused]] constexpr auto custom_name = customize : : enum_name < E > ( v ) ;
if constexpr ( custom_name . empty ( ) ) {
return n < E , v > ( ) . size ( ) ! = 0 ;
} else {
2022-08-12 17:46:36 +00:00
return custom_name . size ( ) ! = 0 ;
2022-08-12 17:38:20 +00:00
}
# else
return enum_name < E , static_cast < E > ( V ) > ( ) . size ( ) ! = 0 ;
# endif
}
2022-08-09 11:28:23 +00:00
template < typename E , int O , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr U ualue ( std : : size_t i ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::ualue requires enum type. " ) ;
2021-04-22 09:41:25 +00:00
2022-05-18 12:26:02 +00:00
if constexpr ( std : : is_same_v < U , bool > ) { // bool special case
2022-08-09 11:28:23 +00:00
static_assert ( O = = 0 , " nameof::detail::ualue requires valid offset. " ) ;
2022-05-18 12:26:02 +00:00
2022-08-09 11:28:23 +00:00
return static_cast < U > ( i ) ;
2022-05-18 12:26:02 +00:00
} else if constexpr ( IsFlags ) {
2022-08-09 11:28:23 +00:00
return static_cast < U > ( U { 1 } < < static_cast < U > ( static_cast < int > ( i ) + O ) ) ;
2021-04-22 09:41:25 +00:00
} else {
2022-08-09 11:28:23 +00:00
return static_cast < U > ( static_cast < int > ( i ) + O ) ;
2021-04-22 09:41:25 +00:00
}
}
2022-08-09 11:28:23 +00:00
template < typename E , int O , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr E value ( std : : size_t i ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::value requires enum type. " ) ;
return static_cast < E > ( ualue < E , O , IsFlags > ( i ) ) ;
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr int reflected_min ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::reflected_min requires enum type. " ) ;
2019-10-15 11:10:49 +00:00
2020-09-01 11:00:59 +00:00
if constexpr ( IsFlags ) {
return 0 ;
2020-04-18 08:43:45 +00:00
} else {
2020-12-29 18:45:54 +00:00
constexpr auto lhs = customize : : enum_range < E > : : min ;
2020-09-01 11:00:59 +00:00
constexpr auto rhs = ( std : : numeric_limits < U > : : min ) ( ) ;
2021-06-26 09:03:55 +00:00
if constexpr ( cmp_less ( rhs , lhs ) ) {
2020-09-01 11:00:59 +00:00
return lhs ;
2021-06-26 09:03:55 +00:00
} else {
return rhs ;
2020-09-01 11:00:59 +00:00
}
2019-10-15 11:10:49 +00:00
}
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr int reflected_max ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::reflected_max requires enum type. " ) ;
2020-04-30 14:46:46 +00:00
2020-09-01 11:00:59 +00:00
if constexpr ( IsFlags ) {
return std : : numeric_limits < U > : : digits - 1 ;
} else {
2020-12-29 18:45:54 +00:00
constexpr auto lhs = customize : : enum_range < E > : : max ;
2020-09-01 11:00:59 +00:00
constexpr auto rhs = ( std : : numeric_limits < U > : : max ) ( ) ;
2019-10-15 11:10:49 +00:00
2020-09-01 11:00:59 +00:00
if constexpr ( cmp_less ( lhs , rhs ) ) {
return lhs ;
} else {
return rhs ;
}
}
2019-10-15 11:10:49 +00:00
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto reflected_min_v = reflected_min < E , IsFlags > ( ) ;
2019-10-15 11:10:49 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto reflected_max_v = reflected_max < E , IsFlags > ( ) ;
2019-10-15 11:10:49 +00:00
2020-12-29 18:45:54 +00:00
template < std : : size_t N >
2021-03-10 09:39:56 +00:00
constexpr std : : size_t values_count ( const bool ( & valid ) [ N ] ) noexcept {
2020-12-29 18:45:54 +00:00
auto count = std : : size_t { 0 } ;
2021-03-10 09:39:56 +00:00
for ( std : : size_t i = 0 ; i < N ; + + i ) {
2020-12-29 18:45:54 +00:00
if ( valid [ i ] ) {
+ + count ;
}
}
return count ;
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , int Min , std : : size_t . . . I >
constexpr auto values ( std : : index_sequence < I . . . > ) noexcept {
2019-10-15 11:10:49 +00:00
static_assert ( is_enum_v < E > , " nameof::detail::values requires enum type. " ) ;
2022-08-12 17:38:20 +00:00
constexpr bool valid [ sizeof . . . ( I ) ] = { is_valid < E , ualue < E , Min , IsFlags > ( I ) > ( ) . . . } ;
2020-12-29 18:45:54 +00:00
constexpr std : : size_t count = values_count ( valid ) ;
2019-10-15 11:10:49 +00:00
2021-04-22 09:23:54 +00:00
if constexpr ( count > 0 ) {
E values [ count ] = { } ;
for ( std : : size_t i = 0 , v = 0 ; v < count ; + + i ) {
if ( valid [ i ] ) {
values [ v + + ] = value < E , Min , IsFlags > ( i ) ;
}
2019-10-15 11:10:49 +00:00
}
2019-10-01 13:43:24 +00:00
2021-04-22 09:23:54 +00:00
return to_array ( values , std : : make_index_sequence < count > { } ) ;
} else {
return std : : array < E , 0 > { } ;
}
2019-10-01 13:43:24 +00:00
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr auto values ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::values requires enum type. " ) ;
2021-01-12 08:39:52 +00:00
constexpr auto min = reflected_min_v < E , IsFlags > ;
constexpr auto max = reflected_max_v < E , IsFlags > ;
constexpr auto range_size = max - min + 1 ;
2020-09-01 11:00:59 +00:00
static_assert ( range_size > 0 , " nameof::enum_range requires valid size. " ) ;
static_assert ( range_size < ( std : : numeric_limits < std : : uint16_t > : : max ) ( ) , " nameof::enum_range requires valid size. " ) ;
2019-10-01 13:43:24 +00:00
2022-09-02 13:26:22 +00:00
return values < E , IsFlags , min > ( std : : make_index_sequence < range_size > { } ) ;
2020-09-01 11:00:59 +00:00
}
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto values_v = values < E , IsFlags > ( ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false , typename D = std : : decay_t < E > >
using values_t = decltype ( ( values_v < D , IsFlags > ) ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto count_v = values_v < E , IsFlags > . size ( ) ;
2019-07-13 14:32:33 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false , typename U = std : : underlying_type_t < E > >
2021-04-22 09:23:54 +00:00
inline constexpr auto min_v = ( count_v < E , IsFlags > > 0 ) ? static_cast < U > ( values_v < E , IsFlags > . front ( ) ) : U { 0 } ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false , typename U = std : : underlying_type_t < E > >
2021-04-22 09:23:54 +00:00
inline constexpr auto max_v = ( count_v < E , IsFlags > > 0 ) ? static_cast < U > ( values_v < E , IsFlags > . back ( ) ) : U { 0 } ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr std : : size_t range_size ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::range_size requires enum type. " ) ;
constexpr auto max = IsFlags ? log2 ( max_v < E , IsFlags > ) : max_v < E , IsFlags > ;
constexpr auto min = IsFlags ? log2 ( min_v < E , IsFlags > ) : min_v < E , IsFlags > ;
constexpr auto range_size = max - min + U { 1 } ;
static_assert ( range_size > 0 , " nameof::enum_range requires valid size. " ) ;
static_assert ( range_size < ( std : : numeric_limits < std : : uint16_t > : : max ) ( ) , " nameof::enum_range requires valid size. " ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
return static_cast < std : : size_t > ( range_size ) ;
2019-10-01 13:43:24 +00:00
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto range_size_v = range_size < E , IsFlags > ( ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
using index_t = std : : conditional_t < range_size_v < E , IsFlags > < ( std : : numeric_limits < std : : uint8_t > : : max ) ( ) , std : : uint8_t , std : : uint16_t > ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto invalid_index_v = ( std : : numeric_limits < index_t < E , IsFlags > > : : max ) ( ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , std : : size_t . . . I >
constexpr auto indexes ( std : : index_sequence < I . . . > ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::indexes requires enum type. " ) ;
constexpr auto min = IsFlags ? log2 ( min_v < E , IsFlags > ) : min_v < E , IsFlags > ;
[[maybe_unused]] auto i = index_t < E , IsFlags > { 0 } ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
return std : : array < decltype ( i ) , sizeof . . . ( I ) > { { ( is_valid < E , value < E , min , IsFlags > ( I ) > ( ) ? i + + : invalid_index_v < E , IsFlags > ) . . . } } ;
2019-10-01 13:43:24 +00:00
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr auto indexes_v = indexes < E , IsFlags > ( std : : make_index_sequence < range_size_v < E , IsFlags > > { } ) ;
2020-05-23 12:56:46 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
constexpr bool is_sparse ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::is_sparse requires enum type. " ) ;
2019-10-01 13:43:24 +00:00
2020-09-01 11:00:59 +00:00
if constexpr ( IsFlags ) {
return ( sizeof ( const char * ) * range_size_v < E , IsFlags > ) > ( sizeof ( E ) * count_v < E , IsFlags > + sizeof ( const char * ) * count_v < E , IsFlags > ) ;
2019-10-01 13:43:24 +00:00
} else {
2020-09-01 11:00:59 +00:00
return ( sizeof ( const char * ) * range_size_v < E , IsFlags > ) > ( sizeof ( index_t < E > ) * range_size_v < E , IsFlags > + sizeof ( const char * ) * count_v < E , IsFlags > ) ;
2019-10-01 13:43:24 +00:00
}
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline constexpr bool is_sparse_v = is_sparse < E , IsFlags > ( ) ;
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , typename U = std : : underlying_type_t < E > >
[[nodiscard]] constexpr E get_value ( std : : size_t i ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::strings requires enum type. " ) ;
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
if constexpr ( is_sparse_v < E , IsFlags > ) {
return values_v < E , IsFlags > [ i ] ;
} else {
constexpr auto min = IsFlags ? log2 ( min_v < E , IsFlags > ) : min_v < E , IsFlags > ;
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
return value < E , min , IsFlags > ( i ) ;
2020-07-03 13:18:05 +00:00
}
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags , std : : size_t . . . I >
constexpr auto strings ( std : : index_sequence < I . . . > ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::strings requires enum type. " ) ;
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
return std : : array < const char * , sizeof . . . ( I ) > { { enum_name_v < E , get_value < E , IsFlags > ( I ) > . data ( ) . . . } } ;
}
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags >
constexpr auto strings ( ) noexcept {
static_assert ( is_enum_v < E > , " nameof::detail::strings requires enum type. " ) ;
constexpr auto count = is_sparse_v < E , IsFlags > ? count_v < E , IsFlags > : range_size_v < E , IsFlags > ;
2020-07-03 13:18:05 +00:00
2020-09-01 11:00:59 +00:00
return strings < E , IsFlags > ( std : : make_index_sequence < count > { } ) ;
2020-07-03 13:18:05 +00:00
}
2020-09-01 11:00:59 +00:00
template < typename E , bool IsFlags = false >
inline static constexpr auto strings_v = strings < E , IsFlags > ( ) ;
2020-07-03 13:18:05 +00:00
template < typename . . . T >
struct nameof_type_supported
# if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
: std : : true_type { } ;
# else
: std : : false_type { } ;
# endif
2020-12-29 18:45:54 +00:00
template < typename . . . T >
struct nameof_type_rtti_supported
# if defined(NAMEOF_TYPE_RTTI_SUPPORTED) && NAMEOF_TYPE_RTTI_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
: std : : true_type { } ;
# else
: std : : false_type { } ;
# endif
2021-04-02 21:06:56 +00:00
template < typename . . . T >
struct nameof_member_supported
# if defined(NAMEOF_MEMBER_SUPPORTED) && NAMEOF_MEMBER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
: std : : true_type { } ;
# else
: std : : false_type { } ;
# endif
2023-03-17 17:39:30 +00:00
template < typename . . . T >
struct nameof_pointer_supported
# if defined(NAMEOF_POINTER_SUPPORTED) && NAMEOF_POINTER_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
: std : : true_type { } ;
# else
: std : : false_type { } ;
# endif
2020-12-29 18:45:54 +00:00
# if defined(_MSC_VER) && !defined(__clang__)
2020-07-03 13:18:05 +00:00
template < typename T >
struct identity {
using type = T ;
} ;
2020-09-01 11:00:59 +00:00
# else
template < typename T >
using identity = T ;
# endif
2020-07-03 13:18:05 +00:00
template < typename T >
using remove_cvref_t = std : : remove_cv_t < std : : remove_reference_t < T > > ;
2020-12-29 18:45:54 +00:00
template < typename T , typename R >
using enable_if_has_short_name_t = std : : enable_if_t < ! std : : is_array_v < T > & & ! std : : is_pointer_v < T > , R > ;
2019-07-13 14:32:33 +00:00
template < typename . . . T >
2019-09-13 14:45:01 +00:00
constexpr auto n ( ) noexcept {
2020-12-29 18:45:54 +00:00
# if defined(_MSC_VER) && !defined(__clang__)
2022-05-18 12:26:02 +00:00
[[maybe_unused]] constexpr auto custom_name = customize : : type_name < typename T : : type . . . > ( ) ;
2020-12-29 18:45:54 +00:00
# else
2022-05-18 12:26:02 +00:00
[[maybe_unused]] constexpr auto custom_name = customize : : type_name < T . . . > ( ) ;
2020-12-29 18:45:54 +00:00
# endif
2022-05-18 12:26:02 +00:00
if constexpr ( custom_name . empty ( ) & & nameof_type_supported < T . . . > : : value ) {
# if defined(__clang__)
2020-12-29 18:45:54 +00:00
constexpr string_view name { __PRETTY_FUNCTION__ + 31 , sizeof ( __PRETTY_FUNCTION__ ) - 34 } ;
2022-05-18 12:26:02 +00:00
# elif defined(__GNUC__)
2020-12-29 18:45:54 +00:00
constexpr string_view name { __PRETTY_FUNCTION__ + 46 , sizeof ( __PRETTY_FUNCTION__ ) - 49 } ;
2022-05-18 12:26:02 +00:00
# elif defined(_MSC_VER)
2020-12-29 18:45:54 +00:00
constexpr string_view name { __FUNCSIG__ + 63 , sizeof ( __FUNCSIG__ ) - 81 - ( __FUNCSIG__ [ sizeof ( __FUNCSIG__ ) - 19 ] = = ' ' ? 1 : 0 ) } ;
2019-10-02 10:23:54 +00:00
# else
2022-05-18 12:26:02 +00:00
constexpr auto name = string_view { } ;
2019-07-13 14:32:33 +00:00
# endif
2022-05-18 12:26:02 +00:00
return cstring < name . size ( ) > { name } ;
2020-12-29 18:45:54 +00:00
} else {
return cstring < custom_name . size ( ) > { custom_name } ;
}
2019-07-13 14:32:33 +00:00
}
2019-12-02 09:17:48 +00:00
template < typename . . . T >
inline constexpr auto type_name_v = n < T . . . > ( ) ;
2020-06-03 14:25:30 +00:00
# if __has_include(<cxxabi.h>)
2020-12-29 18:45:54 +00:00
template < typename T >
string nameof_type_rtti ( const char * tn ) {
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
const auto dmg = abi : : __cxa_demangle ( tn , nullptr , nullptr , nullptr ) ;
const auto name = string { dmg } ;
2022-07-21 15:55:02 +00:00
free ( dmg ) ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a name. " ) ;
2020-12-29 18:45:54 +00:00
return name ;
}
template < typename T >
string nameof_full_type_rtti ( const char * tn ) {
2022-05-18 18:30:04 +00:00
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
2020-12-29 18:45:54 +00:00
const auto dmg = abi : : __cxa_demangle ( tn , nullptr , nullptr , nullptr ) ;
auto name = string { dmg } ;
2022-07-21 15:55:02 +00:00
free ( dmg ) ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a name. " ) ;
2020-12-29 18:45:54 +00:00
if constexpr ( std : : is_const_v < std : : remove_reference_t < T > > ) {
2022-08-13 14:28:52 +00:00
name = string { " const " } . append ( name ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_volatile_v < std : : remove_reference_t < T > > ) {
2022-08-13 14:28:52 +00:00
name = string { " volatile " } . append ( name ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_lvalue_reference_v < T > ) {
2022-08-13 14:28:52 +00:00
name . append ( 1 , ' & ' ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_rvalue_reference_v < T > ) {
2022-08-13 14:28:52 +00:00
name . append ( " && " ) ;
2020-12-29 18:45:54 +00:00
}
return name ;
}
template < typename T , enable_if_has_short_name_t < T , int > = 0 >
string nameof_short_type_rtti ( const char * tn ) {
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
const auto dmg = abi : : __cxa_demangle ( tn , nullptr , nullptr , nullptr ) ;
2022-08-13 14:28:52 +00:00
const auto pname = pretty_name ( dmg ) ;
const auto name = string { pname . data ( ) , pname . size ( ) } ;
2022-07-21 15:55:02 +00:00
free ( dmg ) ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a short name. " ) ;
2020-07-03 13:18:05 +00:00
2020-12-29 18:45:54 +00:00
return name ;
2020-06-05 10:24:13 +00:00
}
2020-06-03 14:25:30 +00:00
# else
2020-12-29 18:45:54 +00:00
template < typename T >
string nameof_type_rtti ( const char * tn ) noexcept {
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
const auto name = string_view { tn } ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a name. " ) ;
2020-12-29 18:45:54 +00:00
2022-08-14 11:54:27 +00:00
return { name . data ( ) , name . size ( ) } ;
2020-12-29 18:45:54 +00:00
}
template < typename T >
string nameof_full_type_rtti ( const char * tn ) noexcept {
2022-05-18 12:26:02 +00:00
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
2020-12-29 18:45:54 +00:00
auto name = string { tn } ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a name. " ) ;
2020-12-29 18:45:54 +00:00
if constexpr ( std : : is_const_v < std : : remove_reference_t < T > > ) {
2022-08-13 14:28:52 +00:00
name = string { " const " } . append ( name ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_volatile_v < std : : remove_reference_t < T > > ) {
2022-08-13 14:28:52 +00:00
name = string { " volatile " } . append ( name ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_lvalue_reference_v < T > ) {
2022-08-13 14:28:52 +00:00
name . append ( 1 , ' & ' ) ;
2020-12-29 18:45:54 +00:00
}
if constexpr ( std : : is_rvalue_reference_v < T > ) {
2022-08-13 14:28:52 +00:00
name . append ( " && " ) ;
2020-12-29 18:45:54 +00:00
}
return name ;
}
template < typename T , enable_if_has_short_name_t < T , int > = 0 >
string nameof_short_type_rtti ( const char * tn ) noexcept {
static_assert ( nameof_type_rtti_supported < T > : : value , " nameof::nameof_type_rtti unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
const auto name = pretty_name ( tn ) ;
2022-08-14 11:54:27 +00:00
assert ( ! name . empty ( ) & & " Type does not have a short name. " ) ;
2020-12-29 18:45:54 +00:00
2022-08-14 11:54:27 +00:00
return { name . data ( ) , name . size ( ) } ;
2020-06-03 14:25:30 +00:00
}
2020-06-05 10:24:13 +00:00
# endif
2020-06-03 14:25:30 +00:00
2022-07-25 10:07:57 +00:00
template < auto V , auto U = V >
2021-04-02 21:06:56 +00:00
constexpr auto n ( ) noexcept {
2022-05-18 12:26:02 +00:00
[[maybe_unused]] constexpr auto custom_name = customize : : member_name < V > ( ) ;
2021-04-02 21:06:56 +00:00
2022-05-18 12:28:56 +00:00
if constexpr ( custom_name . empty ( ) & & nameof_member_supported < decltype ( V ) > : : value ) {
2022-05-18 12:26:02 +00:00
# if defined(__clang__) || defined(__GNUC__)
2021-04-02 21:06:56 +00:00
constexpr auto name = pretty_name ( { __PRETTY_FUNCTION__ , sizeof ( __PRETTY_FUNCTION__ ) - 2 } ) ;
2022-07-21 17:07:19 +00:00
# elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
constexpr auto name = pretty_name ( { __FUNCSIG__ , sizeof ( __FUNCSIG__ ) - 17 } ) ;
2021-04-02 21:06:56 +00:00
# else
2022-05-18 12:26:02 +00:00
constexpr auto name = string_view { } ;
2021-04-02 21:06:56 +00:00
# endif
2022-05-18 12:26:02 +00:00
return cstring < name . size ( ) > { name } ;
2021-04-02 21:06:56 +00:00
} else {
return cstring < custom_name . size ( ) > { custom_name } ;
}
}
2022-07-21 17:07:19 +00:00
# if defined(__clang__) || defined(__GNUC__)
2021-04-02 21:06:56 +00:00
template < auto V >
inline constexpr auto member_name_v = n < V > ( ) ;
2022-07-21 17:07:19 +00:00
# elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
template < typename From , typename Type >
From get_base_type ( Type From : : * ) ;
template < typename T >
union union_type {
2022-11-13 17:01:31 +00:00
constexpr ~ union_type ( ) { }
2022-07-21 17:07:19 +00:00
char c = { } ;
T f ;
} ;
template < typename T >
2022-12-06 21:24:06 +00:00
struct union_type_holder {
constexpr static union_type < T > value ;
} ;
2022-07-21 17:07:19 +00:00
template < auto V >
constexpr auto get_member_name ( ) noexcept {
if constexpr ( std : : is_member_function_pointer_v < decltype ( V ) > ) {
return n < V > ( ) ;
} else {
2023-03-06 13:02:46 +00:00
constexpr bool is_defined = sizeof ( decltype ( get_base_type ( V ) ) ) ! = 0 ;
2023-03-30 15:10:22 +00:00
static_assert ( is_defined , " nameof::nameof_member member name can use only if the struct is already fully defined. Please use NAMEOF macro, or separate definition and declaration. " ) ;
2023-03-06 13:02:46 +00:00
if constexpr ( is_defined ) {
return n < V , & ( union_type_holder < decltype ( get_base_type ( V ) ) > : : value . f . * V ) > ( ) ;
} else {
return " " ;
}
2022-07-21 17:07:19 +00:00
}
}
template < auto V >
inline constexpr auto member_name_v = get_member_name < V > ( ) ;
# else
template < auto V >
2022-08-12 17:38:20 +00:00
inline constexpr auto member_name_v = cstring < 0 > { } ;
2022-07-21 17:07:19 +00:00
# endif
2021-04-02 21:06:56 +00:00
2023-03-30 15:10:22 +00:00
template < auto U , auto V >
2023-03-17 17:39:30 +00:00
struct is_same : std : : false_type { } ;
2023-03-30 15:10:22 +00:00
template < auto U >
2023-03-17 17:39:30 +00:00
struct is_same < U , U > : std : : true_type { } ;
2023-03-30 15:10:22 +00:00
template < auto P >
2023-03-17 17:39:30 +00:00
constexpr bool is_nullptr_v = is_same < P , static_cast < std : : remove_reference_t < decltype ( P ) > > ( nullptr ) > : : value ;
template < auto V >
constexpr auto p ( ) noexcept {
2023-03-30 15:10:22 +00:00
[[maybe_unused]] constexpr auto custom_name = customize : : pointer_name < V > ( ) . empty ( ) & & is_nullptr_v < V > ? " nullptr " : customize : : pointer_name < V > ( ) ;
2023-03-17 17:39:30 +00:00
if constexpr ( custom_name . empty ( ) & & nameof_pointer_supported < decltype ( V ) > : : value ) {
# if defined(__clang__)
constexpr auto name = pretty_name ( { __PRETTY_FUNCTION__ , sizeof ( __PRETTY_FUNCTION__ ) - 2 } ) ;
# elif defined(__GNUC__)
constexpr bool has_parenthesis = __PRETTY_FUNCTION__ [ sizeof ( __PRETTY_FUNCTION__ ) - 3 ] = = ' ) ' ;
constexpr auto name = pretty_name ( { __PRETTY_FUNCTION__ , sizeof ( __PRETTY_FUNCTION__ ) - 2 - has_parenthesis } ) ;
# elif defined(_MSC_VER) && defined(_MSVC_LANG) && _MSVC_LANG >= 202002L
constexpr auto name = pretty_name ( { __FUNCSIG__ , sizeof ( __FUNCSIG__ ) - 17 } ) ;
# else
constexpr auto name = string_view { } ;
# endif
return cstring < name . size ( ) > { name } ;
} else {
return cstring < custom_name . size ( ) > { custom_name } ;
}
}
template < auto V >
inline constexpr auto pointer_name_v = p < V > ( ) ;
2019-04-10 14:44:41 +00:00
} // namespace nameof::detail
2019-03-21 09:16:06 +00:00
2019-08-26 16:56:57 +00:00
// Checks is nameof_type supported compiler.
2019-08-27 14:54:16 +00:00
inline constexpr bool is_nameof_type_supported = detail : : nameof_type_supported < void > : : value ;
2019-08-26 16:26:28 +00:00
2020-12-29 18:45:54 +00:00
// Checks is nameof_type_rtti supported compiler.
inline constexpr bool is_nameof_type_rtti_supported = detail : : nameof_type_rtti_supported < void > : : value ;
2021-04-02 21:06:56 +00:00
// Checks is nameof_member supported compiler.
inline constexpr bool is_nameof_member_supported = detail : : nameof_member_supported < void > : : value ;
2023-03-17 17:39:30 +00:00
// Checks is nameof_pointer supported compiler.
inline constexpr bool is_nameof_pointer_supported = detail : : nameof_pointer_supported < void > : : value ;
2019-08-26 16:56:57 +00:00
// Checks is nameof_enum supported compiler.
2019-08-27 14:54:16 +00:00
inline constexpr bool is_nameof_enum_supported = detail : : nameof_enum_supported < void > : : value ;
2019-08-26 16:26:28 +00:00
2022-07-26 14:17:36 +00:00
// Obtains name of enum variable.
2019-04-26 05:59:21 +00:00
template < typename E >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr auto nameof_enum ( E value ) noexcept - > detail : : enable_if_enum_t < E , string_view > {
2020-05-27 11:22:29 +00:00
using D = std : : decay_t < E > ;
2020-05-23 12:56:46 +00:00
using U = std : : underlying_type_t < D > ;
2020-05-23 13:06:12 +00:00
static_assert ( detail : : nameof_enum_supported < D > : : value , " nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
2020-05-24 12:52:22 +00:00
static_assert ( detail : : count_v < D > > 0 , " nameof::nameof_enum requires enum implementation and valid max and min. " ) ;
2020-05-23 12:56:46 +00:00
2020-12-29 18:45:54 +00:00
const bool valid = static_cast < U > ( value ) > = static_cast < U > ( detail : : min_v < D > ) & & static_cast < U > ( value ) < = static_cast < U > ( detail : : max_v < D > ) ;
if ( const auto i = static_cast < int > ( value ) - detail : : min_v < D > ; valid ) {
2020-09-01 11:00:59 +00:00
if constexpr ( detail : : is_sparse_v < D > ) {
2020-05-24 12:52:22 +00:00
if ( const auto idx = detail : : indexes_v < D > [ i ] ; idx ! = detail : : invalid_index_v < D > ) {
return detail : : strings_v < D > [ idx ] ;
2020-05-23 12:56:46 +00:00
}
} else {
2022-06-29 12:40:42 +00:00
return detail : : strings_v < D > [ static_cast < std : : size_t > ( i ) ] ;
2020-05-23 12:56:46 +00:00
}
}
return { } ; // Value out of range.
2019-03-24 15:15:14 +00:00
}
2022-07-26 14:17:36 +00:00
// Obtains name of enum variable or default value if enum variable out of range.
template < typename E >
2022-07-26 14:48:58 +00:00
[[nodiscard]] auto nameof_enum_or ( E value , string_view default_value ) - > detail : : enable_if_enum_t < E , string > {
2022-07-26 14:17:36 +00:00
using D = std : : decay_t < E > ;
if ( auto v = nameof_enum < D > ( value ) ; ! v . empty ( ) ) {
return string { v . data ( ) , v . size ( ) } ;
}
return string { default_value . data ( ) , default_value . size ( ) } ;
}
// Obtains name of enum-flags variable.
2020-07-03 13:18:05 +00:00
template < typename E >
2020-12-29 18:45:54 +00:00
[[nodiscard]] auto nameof_enum_flag ( E value ) - > detail : : enable_if_enum_t < E , string > {
2020-07-03 13:18:05 +00:00
using D = std : : decay_t < E > ;
using U = std : : underlying_type_t < D > ;
2020-09-07 17:12:54 +00:00
static_assert ( detail : : nameof_enum_supported < D > : : value , " nameof::nameof_enum_flag unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
static_assert ( detail : : count_v < D , true > > 0 , " nameof::nameof_enum_flag requires enum-flags implementation. " ) ;
2020-09-01 11:00:59 +00:00
constexpr auto size = detail : : is_sparse_v < D , true > ? detail : : count_v < D , true > : detail : : range_size_v < D , true > ;
2020-07-03 13:18:05 +00:00
2020-12-29 18:45:54 +00:00
string name ;
2020-09-01 11:00:59 +00:00
auto check_value = U { 0 } ;
for ( std : : size_t i = 0 ; i < size ; + + i ) {
if ( const auto v = static_cast < U > ( detail : : get_value < D , true > ( i ) ) ; ( static_cast < U > ( value ) & v ) ! = 0 ) {
if ( const auto n = detail : : strings_v < D , true > [ i ] ; n ! = nullptr ) {
check_value | = v ;
2020-07-03 13:18:05 +00:00
if ( ! name . empty ( ) ) {
name . append ( 1 , ' | ' ) ;
}
name . append ( n ) ;
} else {
return { } ; // Value out of range.
}
}
}
2020-12-29 18:45:54 +00:00
const bool valid = check_value ! = 0 & & check_value = = static_cast < U > ( value ) ;
if ( valid ) {
2020-09-01 11:00:59 +00:00
return name ;
}
return { } ; // Invalid value or out of range.
2020-07-03 13:18:05 +00:00
}
2022-07-26 14:17:36 +00:00
// Obtains name of static storage enum variable.
2019-05-03 10:22:18 +00:00
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
template < auto V >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr auto nameof_enum ( ) noexcept - > detail : : enable_if_enum_t < decltype ( V ) , string_view > {
2020-06-03 15:04:09 +00:00
using D = std : : decay_t < decltype ( V ) > ;
static_assert ( detail : : nameof_enum_supported < D > : : value , " nameof::nameof_enum unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
2020-12-29 18:45:54 +00:00
constexpr string_view name = detail : : enum_name_v < D , V > ;
2022-08-14 11:54:27 +00:00
static_assert ( ! name . empty ( ) , " Enum value does not have a name. " ) ;
2019-11-14 12:16:39 +00:00
2019-11-14 15:14:02 +00:00
return name ;
2019-05-03 10:22:18 +00:00
}
2020-12-29 18:45:54 +00:00
// Obtains name of type, reference and cv-qualifiers are ignored.
2019-07-21 19:13:06 +00:00
template < typename T >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr string_view nameof_type ( ) noexcept {
2020-02-14 10:28:16 +00:00
static_assert ( detail : : nameof_type_supported < T > : : value , " nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
using U = detail : : identity < detail : : remove_cvref_t < T > > ;
2020-12-29 18:45:54 +00:00
constexpr string_view name = detail : : type_name_v < U > ;
2022-08-14 11:54:27 +00:00
static_assert ( ! name . empty ( ) , " Type does not have a name. " ) ;
2020-02-14 10:28:16 +00:00
return name ;
2019-08-25 12:32:05 +00:00
}
2020-12-29 18:45:54 +00:00
// Obtains full name of type, with reference and cv-qualifiers.
2019-08-25 12:32:05 +00:00
template < typename T >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr string_view nameof_full_type ( ) noexcept {
2020-02-14 10:28:16 +00:00
static_assert ( detail : : nameof_type_supported < T > : : value , " nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
using U = detail : : identity < T > ;
2020-12-29 18:45:54 +00:00
constexpr string_view name = detail : : type_name_v < U > ;
2022-08-14 11:54:27 +00:00
static_assert ( ! name . empty ( ) , " Type does not have a full name. " ) ;
2020-02-14 10:28:16 +00:00
return name ;
2018-08-26 20:30:16 +00:00
}
2018-08-02 14:51:44 +00:00
2020-12-29 18:45:54 +00:00
// Obtains short name of type.
2020-09-07 17:35:21 +00:00
template < typename T >
2020-12-29 18:45:54 +00:00
[[nodiscard]] constexpr auto nameof_short_type ( ) noexcept - > detail : : enable_if_has_short_name_t < T , string_view > {
2020-09-07 17:35:21 +00:00
static_assert ( detail : : nameof_type_supported < T > : : value , " nameof::nameof_type unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
using U = detail : : identity < detail : : remove_cvref_t < T > > ;
2020-12-29 18:45:54 +00:00
constexpr string_view name = detail : : pretty_name ( detail : : type_name_v < U > ) ;
2022-08-14 11:54:27 +00:00
static_assert ( ! name . empty ( ) , " Type does not have a short name. " ) ;
2020-09-07 17:35:21 +00:00
return name ;
}
2021-04-02 21:06:56 +00:00
// Obtains name of member.
template < auto V >
[[nodiscard]] constexpr auto nameof_member ( ) noexcept - > std : : enable_if_t < std : : is_member_pointer_v < decltype ( V ) > , string_view > {
2023-03-17 17:39:30 +00:00
static_assert ( detail : : nameof_member_supported < decltype ( V ) > : : value , " nameof::nameof_member unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
2021-04-02 21:06:56 +00:00
constexpr string_view name = detail : : member_name_v < V > ;
2022-08-14 11:54:27 +00:00
static_assert ( ! name . empty ( ) , " Member does not have a name. " ) ;
2021-04-02 21:06:56 +00:00
return name ;
}
2023-03-17 17:39:30 +00:00
// Obtains name of a function, a global or class static variable.
template < auto V >
[[nodiscard]] constexpr auto nameof_pointer ( ) noexcept - > std : : enable_if_t < std : : is_pointer_v < decltype ( V ) > , string_view > {
static_assert ( detail : : nameof_pointer_supported < decltype ( V ) > : : value , " nameof::nameof_pointer unsupported compiler (https://github.com/Neargye/nameof#compiler-compatibility). " ) ;
constexpr string_view name = detail : : pointer_name_v < V > ;
static_assert ( ! name . empty ( ) , " Pointer does not have a name. " ) ;
return name ;
}
2018-04-04 16:36:18 +00:00
} // namespace nameof
2018-03-22 14:09:55 +00:00
2020-12-29 18:45:54 +00:00
// Obtains name of variable, function, macro.
2022-08-14 11:54:27 +00:00
# define NAMEOF(...) []() constexpr noexcept { \
: : std : : void_t < decltype ( __VA_ARGS__ ) > ( ) ; \
constexpr auto _name = : : nameof : : detail : : pretty_name ( # __VA_ARGS__ ) ; \
static_assert ( ! _name . empty ( ) , " Expression does not have a name. " ) ; \
constexpr auto _size = _name . size ( ) ; \
constexpr auto _nameof = : : nameof : : cstring < _size > { _name } ; \
2020-12-29 18:45:54 +00:00
return _nameof ; } ( )
// Obtains full name of variable, function, macro.
# define NAMEOF_FULL(...) []() constexpr noexcept { \
2020-06-18 10:34:22 +00:00
: : std : : void_t < decltype ( __VA_ARGS__ ) > ( ) ; \
2020-12-29 18:45:54 +00:00
constexpr auto _name = : : nameof : : detail : : pretty_name ( # __VA_ARGS__ , false ) ; \
2022-08-14 11:54:27 +00:00
static_assert ( ! _name . empty ( ) , " Expression does not have a name. " ) ; \
2020-12-29 18:45:54 +00:00
constexpr auto _size = _name . size ( ) ; \
constexpr auto _nameof_full = : : nameof : : cstring < _size > { _name } ; \
return _nameof_full ; } ( )
// Obtains raw name of variable, function, macro.
2022-08-14 11:54:27 +00:00
# define NAMEOF_RAW(...) []() constexpr noexcept { \
: : std : : void_t < decltype ( __VA_ARGS__ ) > ( ) ; \
constexpr auto _name = : : nameof : : string_view { # __VA_ARGS__ } ; \
static_assert ( ! _name . empty ( ) , " Expression does not have a name. " ) ; \
constexpr auto _size = _name . size ( ) ; \
constexpr auto _nameof_raw = : : nameof : : cstring < _size > { _name } ; \
2020-12-29 18:45:54 +00:00
return _nameof_raw ; } ( )
// Obtains name of enum variable.
2020-05-23 12:56:46 +00:00
# define NAMEOF_ENUM(...) ::nameof::nameof_enum<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
2018-09-01 13:27:49 +00:00
2022-07-26 14:17:36 +00:00
// Obtains name of enum variable or default value if enum variable out of range.
# define NAMEOF_ENUM_OR(...) ::nameof::nameof_enum_or(__VA_ARGS__)
2020-12-29 18:45:54 +00:00
// Obtains name of static storage enum variable.
2019-05-03 10:22:18 +00:00
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
2020-09-07 17:12:54 +00:00
# define NAMEOF_ENUM_CONST(...) ::nameof::nameof_enum<__VA_ARGS__>()
2019-05-03 10:22:18 +00:00
2020-12-29 18:45:54 +00:00
// Obtains name of enum-flags variable.
2020-09-07 17:12:54 +00:00
# define NAMEOF_ENUM_FLAG(...) ::nameof::nameof_enum_flag<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
2020-07-03 13:18:05 +00:00
2020-12-29 18:45:54 +00:00
// Obtains type name, reference and cv-qualifiers are ignored.
2019-04-01 19:07:28 +00:00
# define NAMEOF_TYPE(...) ::nameof::nameof_type<__VA_ARGS__>()
2019-03-22 08:56:01 +00:00
2020-12-29 18:45:54 +00:00
// Obtains full type name, with reference and cv-qualifiers.
2020-09-07 17:12:54 +00:00
# define NAMEOF_FULL_TYPE(...) ::nameof::nameof_full_type<__VA_ARGS__>()
2020-12-29 18:45:54 +00:00
// Obtains short type name.
2020-09-07 17:35:21 +00:00
# define NAMEOF_SHORT_TYPE(...) ::nameof::nameof_short_type<__VA_ARGS__>()
2022-05-18 14:31:02 +00:00
// Obtains type name of expression, reference and cv-qualifiers are ignored.
# define NAMEOF_TYPE_EXPR(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
// Obtains full type name of expression, with reference and cv-qualifiers.
# define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>()
2020-12-29 18:45:54 +00:00
// Obtains short type name of expression.
2020-09-07 17:35:21 +00:00
# define NAMEOF_SHORT_TYPE_EXPR(...) ::nameof::nameof_short_type<decltype(__VA_ARGS__)>()
2020-12-29 18:45:54 +00:00
// Obtains type name, with reference and cv-qualifiers, using RTTI.
2021-01-12 08:39:52 +00:00
# define NAMEOF_TYPE_RTTI(...) ::nameof::detail::nameof_type_rtti<::std::void_t<decltype(__VA_ARGS__)>>(typeid(__VA_ARGS__).name())
2020-12-29 18:45:54 +00:00
// Obtains full type name, using RTTI.
# define NAMEOF_FULL_TYPE_RTTI(...) ::nameof::detail::nameof_full_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
// Obtains short type name, using RTTI.
# define NAMEOF_SHORT_TYPE_RTTI(...) ::nameof::detail::nameof_short_type_rtti<decltype(__VA_ARGS__)>(typeid(__VA_ARGS__).name())
2020-06-03 14:25:30 +00:00
2021-04-02 21:06:56 +00:00
// Obtains name of member.
# define NAMEOF_MEMBER(...) ::nameof::nameof_member<__VA_ARGS__>()
2023-03-17 17:39:30 +00:00
// Obtains name of a function, a global or class static variable.
# define NAMEOF_POINTER(...) ::nameof::nameof_pointer<__VA_ARGS__>()
2020-06-18 10:34:22 +00:00
# if defined(__clang__)
# pragma clang diagnostic pop
# elif defined(__GNUC__)
# pragma GCC diagnostic pop
# elif defined(_MSC_VER)
2019-12-13 15:31:55 +00:00
# pragma warning(pop)
# endif
2019-05-28 12:54:10 +00:00
# endif // NEARGYE_NAMEOF_HPP