nameof_module/include/nameof.hpp

710 lines
26 KiB
C++
Raw Normal View History

2018-04-10 19:32:51 +00:00
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
2019-12-30 14:34:00 +00:00
// version 0.9.3
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
2020-02-25 10:53:42 +00:00
// Copyright (c) 2016, 2018 - 2020 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
2019-04-08 17:55:35 +00:00
#include <array>
#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-03-20 16:41:51 +00:00
#include <string_view>
2019-10-01 13:43:24 +00:00
#include <type_traits>
#include <utility>
2018-08-26 23:57:25 +00:00
2020-06-03 14:25:30 +00:00
#if __has_include(<cxxabi.h>)
#include <cxxabi.h>
#include <cstdlib>
#include <string>
#endif
2019-12-13 15:31:55 +00:00
#if defined(_MSC_VER)
# pragma warning(push)
# pragma warning(disable : 26495) // Variable 'nameof::cstring<N>::chars_' is uninitialized.
#endif
// 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
# 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
// 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
# define NAMEOF_ENUM_SUPPORTED 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
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.
2019-04-28 09:15:18 +00:00
// If need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MIN and NAMEOF_ENUM_RANGE_MAX.
2019-04-10 12:40:21 +00:00
// If 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 {
2019-04-08 17:55:35 +00:00
static_assert(std::is_enum_v<E>, "nameof::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;
2019-04-25 16:57:00 +00:00
static_assert(max > min, "nameof::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
2019-08-26 16:56:57 +00:00
template <std::size_t N>
class [[nodiscard]] cstring {
2019-10-10 11:15:42 +00:00
static_assert(N > 0, "nameof::cstring requires size greater than 0.");
2019-10-03 13:25:40 +00:00
std::array<char, N + 1> chars_;
2019-08-26 16:56:57 +00:00
2019-12-13 15:31:55 +00:00
template <std::size_t... I>
constexpr cstring(std::string_view str, std::index_sequence<I...>) noexcept : chars_{{str[I]..., '\0'}} {}
2019-12-12 15:12:42 +00:00
public:
2020-05-24 12:52:39 +00:00
using value_type = const char;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
2020-05-24 12:52:39 +00:00
using pointer = const char*;
using const_pointer = const char*;
2020-05-24 12:52:39 +00:00
using reference = const char&;
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*;
using const_iterator = const char*;
2019-09-14 22:19:34 +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
2019-12-13 15:31:55 +00:00
constexpr explicit cstring(std::string_view str) noexcept : cstring{str, std::make_index_sequence<N>{}} {
assert(str.size() == N);
}
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;
2020-06-06 10:41:29 +00:00
cstring& operator=(cstring&&) = default;
[[nodiscard]] constexpr const_pointer data() const noexcept { return chars_.data(); }
[[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
[[nodiscard]] constexpr const_iterator cbegin() const noexcept { return begin(); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_iterator cend() const noexcept { return end(); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_reverse_iterator rbegin() const noexcept { return end(); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_reverse_iterator rend() const noexcept { return begin(); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_reverse_iterator crbegin() const noexcept { return rbegin(); }
2019-09-14 22:19:34 +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
2020-05-24 12:52:39 +00:00
[[nodiscard]] constexpr const_reference at(size_type i) const { return assert(i < size()), chars_.at(i); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_reference front() const noexcept { return chars_[0]; }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr const_reference back() const noexcept { return chars_[N]; }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr size_type length() const noexcept { return size(); }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr bool empty() const noexcept { return false; }
2019-09-14 22:19:34 +00:00
[[nodiscard]] constexpr int compare(std::string_view str) const noexcept {
2019-09-14 22:19:34 +00:00
return std::string_view{data(), size()}.compare(str);
}
[[nodiscard]] constexpr const char* c_str() const noexcept { return data(); }
2019-08-26 16:56:57 +00:00
2020-01-15 10:42:14 +00:00
template <typename Char = char, typename Traits = std::char_traits<Char>, typename Allocator = std::allocator<Char>>
2019-10-25 10:33:53 +00:00
[[nodiscard]] std::basic_string<Char, Traits, Allocator> str() const { return {begin(), end()}; }
[[nodiscard]] constexpr operator std::string_view() const noexcept { return {data(), size()}; }
2019-08-26 16:56:57 +00:00
[[nodiscard]] constexpr explicit operator const char*() const noexcept { return data(); }
2020-01-15 10:42:14 +00:00
template <typename Char = char, typename Traits = std::char_traits<Char>, typename Allocator = std::allocator<Char>>
[[nodiscard]] explicit operator std::basic_string<Char, Traits, Allocator>() const { return {begin(), end()}; }
2019-08-26 16:56:57 +00:00
};
2019-09-14 22:19:34 +00:00
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator==(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) == 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator==(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) == 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator!=(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) != 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator!=(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) != 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator>(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) > 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator>(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) > 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator>=(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) >= 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator>=(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) >= 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator<(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) < 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator<(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) < 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator<=(const cstring<N>& lhs, std::string_view rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) <= 0;
}
template <std::size_t N>
2019-10-03 13:25:40 +00:00
[[nodiscard]] constexpr bool operator<=(std::string_view lhs, const cstring<N>& rhs) noexcept {
2019-09-14 22:19:34 +00:00
return lhs.compare(rhs) <= 0;
}
template <typename Char, typename Traits, std::size_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-01-15 10:42:14 +00:00
for (const auto c : std::string_view{srt}) {
2019-09-14 22:19:34 +00:00
os.put(c);
}
return os;
}
2019-10-03 13:25:40 +00:00
namespace detail {
template <typename T>
2019-10-09 17:48:48 +00:00
struct identity {
2019-10-03 13:25:40 +00:00
using type = T;
};
template <typename... T>
2019-10-09 17:48:48 +00:00
struct nameof_type_supported
#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED || defined(NAMEOF_TYPE_NO_CHECK_SUPPORT)
2019-10-03 13:25:40 +00:00
: std::true_type {};
#else
: std::false_type {};
#endif
template <typename T>
2019-10-09 17:48:48 +00:00
struct nameof_enum_supported
#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED || defined(NAMEOF_ENUM_NO_CHECK_SUPPORT)
2019-10-03 13:25:40 +00:00
: std::true_type {};
#else
: std::false_type {};
#endif
2019-04-14 13:16:24 +00:00
template <typename T>
2019-07-23 13:50:18 +00:00
using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<T>>;
template <typename T, typename R>
2020-05-27 11:22:29 +00:00
using enable_if_enum_t = std::enable_if_t<std::is_enum_v<std::decay_t<T>>, R>;
2019-04-14 13:16:24 +00:00
2019-10-03 13:25:40 +00:00
template <typename T>
inline constexpr bool is_enum_v = std::is_enum_v<T> && std::is_same_v<T, std::decay_t<T>>;
2019-10-01 13:55:37 +00:00
2019-09-14 22:19:34 +00:00
constexpr std::string_view pretty_name(std::string_view name, bool remove_template_suffix = true) noexcept {
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;
}
}
2019-07-21 19:13:32 +00:00
if (remove_template_suffix) {
name.remove_suffix(s);
}
2019-04-11 14:55:29 +00:00
2019-08-26 16:56:57 +00:00
if (name.size() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
(name.front() >= 'A' && name.front() <= 'Z') ||
(name.front() == '_'))) {
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
}
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.");
#if defined(NAMEOF_ENUM_SUPPORTED) && NAMEOF_ENUM_SUPPORTED
2020-01-31 09:08:22 +00:00
# if defined(__clang__) || defined(__GNUC__)
2019-09-14 22:19:34 +00:00
constexpr auto name = pretty_name({__PRETTY_FUNCTION__, sizeof(__PRETTY_FUNCTION__) - 2});
# elif defined(_MSC_VER)
2019-09-14 22:19:34 +00:00
constexpr auto name = pretty_name({__FUNCSIG__, sizeof(__FUNCSIG__) - 17});
# endif
2019-11-14 15:23:57 +00:00
if constexpr (name.size() > 0) {
return cstring<name.size()>{name};
} else {
return std::string_view{};
}
#else
return std::string_view{}; // Unsupported compiler.
2019-07-13 14:32:33 +00:00
#endif
}
2019-08-26 16:56:57 +00:00
template <typename E, E V>
2019-10-01 13:43:24 +00:00
inline constexpr auto enum_name_v = n<E, V>();
2020-06-06 10:41:29 +00:00
template <typename E, auto V>
constexpr bool is_valid() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::is_valid requires enum type.");
return n<E, static_cast<E>(V)>().size() != 0;
}
2019-10-15 11:10:49 +00:00
template <typename L, typename R>
2020-04-18 08:43:45 +00:00
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.");
2019-10-15 11:10:49 +00:00
2020-04-18 08:43:45 +00:00
if constexpr (std::is_signed_v<L> == std::is_signed_v<R>) {
2019-10-15 11:10:49 +00:00
// If same signedness (both signed or both unsigned).
return lhs < rhs;
2020-04-18 08:43:45 +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;
2019-10-15 11:10:49 +00:00
}
}
2020-04-30 14:46:46 +00:00
template <typename E, int Min, int Max>
constexpr std::size_t range_size() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::range_size requires enum type.");
constexpr auto size = Max - Min + 1;
static_assert(size > 0, "nameof::enum_range requires valid size.");
static_assert(size < (std::numeric_limits<std::uint16_t>::max)(), "nameof::enum_range requires valid size.");
return static_cast<std::size_t>(size);
}
2019-10-19 11:23:36 +00:00
template <typename E>
constexpr int reflected_min() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::reflected_min requires enum type.");
constexpr auto lhs = enum_range<E>::min;
static_assert(lhs > (std::numeric_limits<std::int16_t>::min)(), "nameof::enum_range requires min must be greater than INT16_MIN.");
constexpr auto rhs = (std::numeric_limits<std::underlying_type_t<E>>::min)();
2019-10-15 11:10:49 +00:00
2020-04-18 08:43:45 +00:00
return cmp_less(lhs, rhs) ? rhs : lhs;
2019-10-15 11:10:49 +00:00
}
2019-10-19 11:23:36 +00:00
template <typename E>
constexpr int reflected_max() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::reflected_max requires enum type.");
constexpr auto lhs = enum_range<E>::max;
static_assert(lhs < (std::numeric_limits<std::int16_t>::max)(), "nameof::enum_range requires max must be less than INT16_MAX.");
constexpr auto rhs = (std::numeric_limits<std::underlying_type_t<E>>::max)();
2019-10-15 11:10:49 +00:00
2020-04-18 08:43:45 +00:00
return cmp_less(lhs, rhs) ? lhs : rhs;
2019-10-15 11:10:49 +00:00
}
2019-10-01 13:43:24 +00:00
template <typename E>
2019-10-19 11:23:36 +00:00
inline constexpr int reflected_min_v = reflected_min<E>();
2019-10-01 13:43:24 +00:00
template <typename E>
2019-10-19 11:23:36 +00:00
inline constexpr int reflected_max_v = reflected_max<E>();
2019-10-01 13:43:24 +00:00
template <typename E, int... I>
2019-10-15 11:10:49 +00:00
constexpr auto values(std::integer_sequence<int, I...>) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::values requires enum type.");
2020-06-06 10:41:29 +00:00
constexpr std::array<bool, sizeof...(I)> valid{{is_valid<E, I + reflected_min_v<E>>()...}};
2019-10-19 11:23:36 +00:00
constexpr int count = ((valid[I] ? 1 : 0) + ...);
2019-10-15 11:10:49 +00:00
std::array<E, count> values{};
2019-10-19 11:23:36 +00:00
for (int i = 0, v = 0; v < count; ++i) {
2019-10-15 11:10:49 +00:00
if (valid[i]) {
2019-10-19 11:23:36 +00:00
values[v++] = static_cast<E>(i + reflected_min_v<E>);
2019-10-15 11:10:49 +00:00
}
}
2019-10-01 13:43:24 +00:00
2019-10-15 11:10:49 +00:00
return values;
2019-10-01 13:43:24 +00:00
}
2019-10-15 11:10:49 +00:00
template <typename E>
2020-04-30 14:46:46 +00:00
inline constexpr auto values_v = values<E>(std::make_integer_sequence<int, range_size<E, reflected_min_v<E>, reflected_max_v<E>>()>{});
2019-10-01 13:43:24 +00:00
2019-10-15 11:10:49 +00:00
template <typename E>
inline constexpr std::size_t count_v = values_v<E>.size();
2019-10-01 13:43:24 +00:00
template <typename E>
2020-06-06 10:41:29 +00:00
inline constexpr int min_v = static_cast<int>(values_v<E>.front());
2019-10-01 13:43:24 +00:00
template <typename E>
2020-06-06 10:41:29 +00:00
inline constexpr int max_v = static_cast<int>(values_v<E>.back());
2019-10-01 13:43:24 +00:00
template <typename E>
2020-04-30 14:46:46 +00:00
inline constexpr std::size_t range_size_v = range_size<E, min_v<E>, max_v<E>>();
2019-07-13 14:32:33 +00:00
2019-10-01 13:43:24 +00:00
template <typename E>
2019-10-15 11:10:49 +00:00
using index_t = std::conditional_t<range_size_v<E> < (std::numeric_limits<std::uint8_t>::max)(), std::uint8_t, std::uint16_t>;
2019-10-01 13:43:24 +00:00
template <typename E>
inline constexpr auto invalid_index_v = (std::numeric_limits<index_t<E>>::max)();
template <typename E, int... I>
constexpr auto indexes(std::integer_sequence<int, I...>) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::indexes requires enum type.");
2020-06-06 10:41:29 +00:00
[[maybe_unused]] index_t<E> i = 0;
2019-10-01 13:43:24 +00:00
2020-06-06 10:41:29 +00:00
return std::array<index_t<E>, sizeof...(I)>{{(is_valid<E, I + min_v<E>>() ? i++ : invalid_index_v<E>)...}};
2019-10-01 13:43:24 +00:00
}
template <typename E>
inline constexpr auto indexes_v = indexes<E>(std::make_integer_sequence<int, range_size_v<E>>{});
2019-10-01 13:43:24 +00:00
template <typename E, int... I>
constexpr auto strings(std::integer_sequence<int, I...>) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
2019-10-15 11:10:49 +00:00
return std::array<const char*, sizeof...(I)>{{enum_name_v<E, static_cast<E>(I + min_v<E>)>.data()...}};
2019-10-01 13:43:24 +00:00
}
template <typename E, std::size_t... I>
constexpr auto strings(std::index_sequence<I...>) noexcept {
static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
2019-10-15 11:10:49 +00:00
return std::array<const char*, sizeof...(I)>{{enum_name_v<E, values_v<E>[I]>.data()...}};
2019-10-01 13:43:24 +00:00
}
template <typename E>
inline constexpr bool sparsity_v = (sizeof(const char*) * range_size_v<E>) > (sizeof(index_t<E>) * range_size_v<E> + sizeof(const char*) * count_v<E>);
2019-10-01 13:43:24 +00:00
template <typename E>
constexpr auto strings() noexcept {
static_assert(is_enum_v<E>, "nameof::detail::strings requires enum type.");
if constexpr (sparsity_v<E>) {
2019-10-15 11:10:49 +00:00
return strings<E>(std::make_index_sequence<count_v<E>>{});
2019-10-01 13:43:24 +00:00
} else {
2019-10-15 11:10:49 +00:00
return strings<E>(std::make_integer_sequence<int, range_size_v<E>>{});
2019-10-01 13:43:24 +00:00
}
}
template <typename E>
inline static constexpr auto strings_v = strings<E>();
2019-10-01 13:43:24 +00:00
2019-07-13 14:32:33 +00:00
template <typename... T>
2019-09-13 14:45:01 +00:00
constexpr auto n() noexcept {
#if defined(NAMEOF_TYPE_SUPPORTED) && NAMEOF_TYPE_SUPPORTED
# if defined(__clang__)
2019-08-26 16:56:57 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__ + 31, sizeof(__PRETTY_FUNCTION__) - 34};
# elif defined(__GNUC__)
2019-08-26 16:56:57 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__ + 46, sizeof(__PRETTY_FUNCTION__) - 49};
# elif defined(_MSC_VER)
2019-08-26 16:56:57 +00:00
constexpr std::string_view name{__FUNCSIG__ + 63, sizeof(__FUNCSIG__) - 81 - (__FUNCSIG__[sizeof(__FUNCSIG__) - 19] == ' ' ? 1 : 0)};
# endif
2019-10-10 11:15:42 +00:00
2019-10-03 13:25:40 +00:00
return cstring<name.size()>{name};
#else
return std::string_view{}; // Unsupported compiler.
2019-07-13 14:32:33 +00:00
#endif
}
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-06-05 10:24:13 +00:00
inline std::string demangle(const char* tn) {
2020-06-03 14:25:30 +00:00
auto dmg = abi::__cxa_demangle(tn, nullptr, nullptr, nullptr);
auto r = std::string{dmg != nullptr ? dmg : tn};
std::free(dmg);
return r;
2020-06-05 10:24:13 +00:00
}
2020-06-03 14:25:30 +00:00
#else
2020-06-05 10:24:13 +00:00
constexpr std::string_view demangle(const char* tn) noexcept {
2020-06-03 14:25:30 +00:00
return std::string_view{tn};
}
2020-06-05 10:24:13 +00:00
#endif
2020-06-03 14:25:30 +00:00
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: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-04-08 18:02:45 +00:00
// Obtains simple (unqualified) string enum name of enum variable.
2019-04-26 05:59:21 +00:00
template <typename E>
2019-09-12 09:41:16 +00:00
[[nodiscard]] constexpr auto nameof_enum(E value) noexcept -> detail::enable_if_enum_t<E, std::string_view> {
2020-05-27 11:22:29 +00:00
using D = std::decay_t<E>;
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-24 12:52:22 +00:00
if (const auto i = static_cast<int>(value) - detail::min_v<D>; static_cast<U>(value) >= static_cast<U>(detail::min_v<D>) &&
static_cast<U>(value) <= static_cast<U>(detail::max_v<D>)) {
if constexpr (detail::sparsity_v<D>) {
if (const auto idx = detail::indexes_v<D>[i]; idx != detail::invalid_index_v<D>) {
return detail::strings_v<D>[idx];
}
} else {
2020-05-24 12:52:22 +00:00
return detail::strings_v<D>[i];
}
}
return {}; // Value out of range.
2019-03-24 15:15:14 +00:00
}
2019-05-03 10:22:18 +00:00
// Obtains simple (unqualified) string enum name of static storage enum variable.
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
template <auto V>
2019-11-18 07:48:51 +00:00
[[nodiscard]] constexpr auto nameof_enum() noexcept -> detail::enable_if_enum_t<decltype(V), std::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).");
constexpr std::string_view name = detail::enum_name_v<D, V>;
2019-11-14 12:16:39 +00:00
static_assert(name.size() > 0, "Enum value does not have a name.");
2019-11-14 15:14:02 +00:00
return name;
2019-05-03 10:22:18 +00:00
}
2019-08-26 16:56:57 +00:00
// Obtains string name of type, reference and cv-qualifiers are ignored.
2019-07-21 19:13:06 +00:00
template <typename T>
2019-12-02 09:17:48 +00:00
[[nodiscard]] constexpr std::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).");
2019-09-14 22:19:34 +00:00
#if defined(_MSC_VER)
2020-02-14 10:28:16 +00:00
using U = detail::identity<detail::remove_cvref_t<T>>;
2019-09-14 22:19:34 +00:00
#else
2020-02-14 10:28:16 +00:00
using U = detail::remove_cvref_t<T>;
2019-09-14 22:19:34 +00:00
#endif
2020-02-14 10:28:16 +00:00
constexpr std::string_view name = detail::type_name_v<U>;
static_assert(name.size() > 0, "Type does not have a name.");
return name;
2019-08-25 12:32:05 +00:00
}
2019-08-26 16:56:57 +00:00
// Obtains string name of full type, with reference and cv-qualifiers.
2019-08-25 12:32:05 +00:00
template <typename T>
2019-12-02 09:17:48 +00:00
[[nodiscard]] constexpr std::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).");
2019-09-14 22:19:34 +00:00
#if defined(_MSC_VER)
2020-02-14 10:28:16 +00:00
using U = detail::identity<T>;
2019-09-14 22:19:34 +00:00
#else
2020-02-14 10:28:16 +00:00
using U = T;
2019-09-14 22:19:34 +00:00
#endif
2020-02-14 10:28:16 +00:00
constexpr std::string_view name = detail::type_name_v<U>;
static_assert(name.size() > 0, "Type does not have a name.");
return name;
2018-08-26 20:30:16 +00:00
}
2018-08-02 14:51:44 +00:00
2018-04-04 16:36:18 +00:00
} // namespace nameof
2018-03-22 14:09:55 +00:00
2019-09-14 22:19:34 +00:00
// Obtains simple (unqualified) string name of variable, function, macro.
#define NAMEOF(...) []() constexpr noexcept { \
::std::void_t<decltype(__VA_ARGS__)>(); \
constexpr auto name = ::nameof::detail::pretty_name(#__VA_ARGS__, true); \
2019-11-14 12:16:39 +00:00
static_assert(name.size() > 0, "Expression does not have a name."); \
2019-09-14 22:19:34 +00:00
constexpr auto size = name.size(); \
constexpr auto nameof = ::nameof::cstring<size>{name}; \
return nameof; }()
2019-09-14 22:19:34 +00:00
// Obtains simple (unqualified) full (with template suffix) string name of variable, function, macro.
#define NAMEOF_FULL(...) []() constexpr noexcept { \
::std::void_t<decltype(__VA_ARGS__)>(); \
constexpr auto name = ::nameof::detail::pretty_name(#__VA_ARGS__, false); \
2019-11-14 12:16:39 +00:00
static_assert(name.size() > 0, "Expression does not have a name."); \
2019-09-14 22:19:34 +00:00
constexpr auto size = name.size(); \
constexpr auto nameof_full = ::nameof::cstring<size>{name}; \
return nameof_full; }()
2019-09-14 22:19:34 +00:00
// Obtains raw string name of variable, function, macro.
2019-11-14 12:16:39 +00:00
#define NAMEOF_RAW(...) []() constexpr noexcept { \
::std::void_t<decltype(__VA_ARGS__)>(); \
constexpr auto name = ::std::string_view{#__VA_ARGS__}; \
static_assert(name.size() > 0, "Expression does not have a name."); \
constexpr auto size = name.size(); \
constexpr auto nameof_raw = ::nameof::cstring<size>{name}; \
return nameof_raw; }()
2019-04-02 15:42:47 +00:00
2019-04-08 18:02:45 +00:00
// Obtains simple (unqualified) string enum name of enum variable.
#define NAMEOF_ENUM(...) ::nameof::nameof_enum<::std::decay_t<decltype(__VA_ARGS__)>>(__VA_ARGS__)
2018-09-01 13:27:49 +00:00
2019-05-03 10:22:18 +00:00
// Obtains simple (unqualified) string enum name of static storage enum variable.
// This version is much lighter on the compile times and is not restricted to the enum_range limitation.
#define NAMEOF_CONST_ENUM(...) ::nameof::nameof_enum<__VA_ARGS__>()
2019-07-21 19:13:06 +00:00
// Obtains string name of type, 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
2019-07-21 19:13:06 +00:00
// Obtains string name of full type, with reference and cv-qualifiers.
#define NAMEOF_FULL_TYPE(...) ::nameof::nameof_full_type<__VA_ARGS__>()
// Obtains string name type of expression, reference and cv-qualifiers are ignored.
#define NAMEOF_TYPE_EXPR(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
// Obtains string name full type of expression, with reference and cv-qualifiers.
#define NAMEOF_FULL_TYPE_EXPR(...) ::nameof::nameof_full_type<decltype(__VA_ARGS__)>()
2019-05-01 14:44:25 +00:00
2020-06-03 14:25:30 +00:00
// Obtains string name of type using RTTI.
2020-06-05 10:24:13 +00:00
#define NAMEOF_TYPE_RTTI(...) ::nameof::detail::demangle(typeid(__VA_ARGS__).name())
2020-06-03 14:25:30 +00:00
2019-12-13 15:31:55 +00:00
#if defined(_MSC_VER)
# pragma warning(pop)
#endif
2019-05-28 12:54:10 +00:00
#endif // NEARGYE_NAMEOF_HPP