nameof_module/include/nameof.hpp

269 lines
11 KiB
C++
Raw Normal View History

2018-04-10 19:32:51 +00:00
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
2019-04-18 17:00:59 +00:00
// vesion 0.8.1
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
// Copyright (c) 2016, 2018 - 2019 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.
2018-03-17 04:31:59 +00:00
#pragma once
2018-04-10 18:09:23 +00:00
2019-04-08 17:55:35 +00:00
#include <array>
2018-03-17 05:06:38 +00:00
#include <cstddef>
2019-03-24 11:23:41 +00:00
#include <limits>
2019-04-08 17:55:35 +00:00
#include <type_traits>
2019-03-20 16:41:51 +00:00
#include <string_view>
2018-08-26 23:57:25 +00:00
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-10 12:40:21 +00:00
// Enum value must be in range [-NAMEOF_ENUM_RANGE_MAX, NAMEOF_ENUM_RANGE_MIN]. By default NAMEOF_ENUM_RANGE_MIN = -128, NAMEOF_ENUM_RANGE_MAX = 128.
// If need another range for all enum types by default, redefine the macro NAMEOF_ENUM_RANGE_MAX and NAMEOF_ENUM_RANGE_MIN.
// If need another range for specific enum type, add specialization enum_range for necessary enum type.
2019-04-08 17:55:35 +00:00
template <typename E>
struct enum_range final {
static_assert(std::is_enum_v<E>, "nameof::enum_range requires enum type.");
2019-04-10 12:40:21 +00:00
static constexpr int min = std::is_signed_v<std::underlying_type_t<E>> ? NAMEOF_ENUM_RANGE_MIN : 0;
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
2018-04-04 12:19:02 +00:00
namespace detail {
2018-04-04 16:36:18 +00:00
2018-08-02 14:51:44 +00:00
template <typename T>
2019-03-22 10:09:13 +00:00
struct identity final {
2018-08-02 14:51:44 +00:00
using type = T;
};
2019-04-10 12:13:51 +00:00
template <typename... T>
2019-03-24 15:15:14 +00:00
[[nodiscard]] constexpr std::string_view nameof_type_impl() noexcept {
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
2019-04-10 12:13:51 +00:00
constexpr auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = <nameof::detail::identity<") - 1;
constexpr auto suffix = sizeof(">>]") - 1;
2019-04-11 18:16:36 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__ + prefix, sizeof(__PRETTY_FUNCTION__) - prefix - suffix - 1};
2018-09-01 13:27:49 +00:00
#elif defined(__GNUC__)
2019-04-10 12:13:51 +00:00
constexpr auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = {nameof::detail::identity<") - 1;
constexpr auto suffix = sizeof(">}; std::string_view = std::basic_string_view<char>]") - 1;
2019-04-11 18:16:36 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__ + prefix, sizeof(__PRETTY_FUNCTION__) - prefix - suffix - 1};
2018-09-01 13:27:49 +00:00
#elif defined(_MSC_VER)
2019-03-24 15:15:14 +00:00
constexpr auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_type_impl<struct nameof::detail::identity<") - 1;
constexpr auto suffix = sizeof(">>(void) noexcept") - 1;
2019-04-11 18:16:36 +00:00
constexpr std::string_view name{__FUNCSIG__ + prefix, sizeof(__FUNCSIG__) - prefix - suffix - 1};
2018-09-01 13:27:49 +00:00
#else
return {}; // Unsupported compiler.
2018-09-01 13:27:49 +00:00
#endif
#if defined(__clang__) || defined(__GNUC__) || defined(_MSC_VER)
2019-04-11 18:16:36 +00:00
return name.substr(0, name.length() - (name.back() == ' ' ? 1 : 0));
#endif
2018-09-01 13:27:49 +00:00
}
2019-04-01 19:29:11 +00:00
template <typename E, E V>
2019-04-08 17:55:35 +00:00
[[nodiscard]] constexpr std::string_view enum_name_impl() noexcept {
static_assert(std::is_enum_v<E>, "nameof::enum_name_impl requires enum type.");
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
2019-04-11 14:55:29 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__};
2019-03-23 12:17:50 +00:00
constexpr auto suffix = sizeof("]") - 1;
2019-03-26 07:45:00 +00:00
#elif defined(__GNUC__) && __GNUC__ >= 9
2019-04-11 14:55:29 +00:00
constexpr std::string_view name{__PRETTY_FUNCTION__};
2019-03-26 07:03:53 +00:00
constexpr auto suffix = sizeof("; std::string_view = std::basic_string_view<char>]") - 1;
2018-09-01 13:27:49 +00:00
#elif defined(_MSC_VER)
2019-04-11 14:55:29 +00:00
constexpr std::string_view name{__FUNCSIG__};
2019-03-23 12:17:50 +00:00
constexpr auto suffix = sizeof(">(void) noexcept") - 1;
2018-09-01 13:27:49 +00:00
#else
return {}; // Unsupported compiler.
#endif
2019-03-26 07:45:00 +00:00
#if defined(__clang__) || (defined(__GNUC__) && __GNUC__ >= 9) || defined(_MSC_VER)
2019-04-11 14:55:29 +00:00
constexpr auto prefix = name.find_last_of(" :,-)", name.length() - suffix) + 1;
2019-04-08 17:55:35 +00:00
2019-04-11 18:16:36 +00:00
if constexpr (name[prefix] >= '0' && name[prefix] <= '9') {
2019-04-11 14:55:29 +00:00
return {}; // Value does not have name.
2019-04-11 18:16:36 +00:00
} else {
return name.substr(prefix, name.length() - prefix - suffix);
2019-04-11 14:55:29 +00:00
}
2018-09-01 13:27:49 +00:00
#endif
}
2019-04-08 18:06:25 +00:00
template <typename E, int O, int... I>
2019-04-14 14:35:07 +00:00
[[nodiscard]] constexpr decltype(auto) enum_names_impl(std::integer_sequence<int, I...>) noexcept {
2019-04-08 21:52:00 +00:00
static_assert(std::is_enum_v<E>, "nameof::detail::enum_strings_impl requires enum type.");
2019-04-14 12:33:16 +00:00
constexpr std::array<std::string_view, sizeof...(I)> names{{enum_name_impl<E, static_cast<E>(I + O)>()...}};
2019-03-24 11:23:41 +00:00
2019-04-14 12:33:16 +00:00
return names;
2019-04-08 17:55:35 +00:00
}
2018-09-01 13:27:49 +00:00
2019-04-14 13:16:24 +00:00
template <typename T>
struct check final {
using type = void;
};
template <typename T>
using check_t = typename check<T>::type;
2019-04-02 15:42:47 +00:00
template <typename T>
2019-04-01 19:07:28 +00:00
[[nodiscard]] constexpr std::string_view nameof_impl(std::string_view name, bool with_template_suffix) noexcept {
2019-04-14 14:35:07 +00:00
static_assert(std::is_void_v<T>, "nameof::detail::nameof_impl requires void type.");
2019-04-11 14:55:29 +00:00
if (name.length() >= 1 && (name.front() == '"' || name.front() == '\'')) {
return {}; // Narrow multibyte string literal.
} else if (name.length() >= 2 && name[0] == 'R' && (name[1] == '"' || name[1] == '\'')) {
return {}; // Raw string literal.
} else if (name.length() >= 2 && name[0] == 'L' && (name[1] == '"' || name[1] == '\'')) {
return {}; // Wide string literal.
} else if (name.length() >= 2 && name[0] == 'U' && (name[1] == '"' || name[1] == '\'')) {
return {}; // UTF-32 encoded string literal.
} else if (name.length() >= 2 && name[0] == 'u' && (name[1] == '"' || name[1] == '\'')) {
return {}; // UTF-16 encoded string literal.
} else if (name.length() >= 3 && name[0] == 'u' && name[1] == '8' && (name[2] == '"' || name[2] == '\'')) {
return {}; // UTF-8 encoded string literal.
} else if (name.length() >= 1 && (name.front() >= '0' && name.front() <= '9')) {
return {}; // Invalid name.
}
2019-04-11 18:16:36 +00:00
for (std::size_t i = name.length(), 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-04-11 18:16:36 +00:00
for (std::size_t i = name.length(), 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-04-11 18:16:36 +00:00
for (std::size_t i = name.length() - 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;
}
}
name.remove_suffix(with_template_suffix ? 0 : s);
if (name.length() > 0 && ((name.front() >= 'a' && name.front() <= 'z') ||
(name.front() >= 'A' && name.front() <= 'Z') ||
(name.front() == '_'))) {
return name;
} else {
return {}; // Invalid name.
}
2018-07-13 15:18:52 +00:00
}
2019-04-02 15:42:47 +00:00
template <typename T>
2019-03-22 10:09:13 +00:00
[[nodiscard]] constexpr std::string_view nameof_raw_impl(std::string_view name) noexcept {
2019-04-14 14:35:07 +00:00
static_assert(std::is_void_v<T>, "nameof::detail::nameof_raw_impl requires void type.");
2019-03-21 09:16:06 +00:00
return name;
}
2019-04-10 14:44:41 +00:00
} // namespace nameof::detail
2019-03-21 09:16:06 +00:00
2019-04-08 18:02:45 +00:00
// Obtains simple (unqualified) string enum name of enum variable.
2019-04-23 13:10:21 +00:00
template <typename E, typename = std::enable_if_t<std::is_enum_v<std::decay_t<E>>>>
2019-04-08 21:52:00 +00:00
[[nodiscard]] constexpr std::string_view nameof_enum(E value) noexcept {
2019-04-23 13:10:21 +00:00
using D = std::decay_t<E>;
2019-04-08 18:06:25 +00:00
static_assert(std::is_enum_v<D>, "nameof::nameof_enum requires enum type.");
2019-04-14 14:35:07 +00:00
static_assert(enum_range<D>::max > enum_range<D>::min, "nameof::enum_range requires max > min.");
2019-04-23 13:10:21 +00:00
using U = std::underlying_type_t<D>;
2019-04-14 14:35:07 +00:00
constexpr int max = enum_range<D>::max < (std::numeric_limits<U>::max)() ? enum_range<D>::max : (std::numeric_limits<U>::max)();
constexpr int min = enum_range<D>::min > (std::numeric_limits<U>::min)() ? enum_range<D>::min : (std::numeric_limits<U>::min)();
constexpr auto range = std::make_integer_sequence<int, max - min + 1>{};
constexpr auto names = detail::enum_names_impl<D, min>(range);
2019-04-08 18:06:25 +00:00
2019-04-25 16:57:07 +00:00
if (int i = static_cast<int>(value) - min; i >= 0 && static_cast<std::size_t>(i) < names.size()) {
2019-04-14 14:35:07 +00:00
return names[i];
} else {
return {}; // Value out of range.
}
2019-03-24 15:15:14 +00:00
}
2019-04-08 18:02:45 +00:00
// Obtains string name of type.
2019-03-20 16:41:51 +00:00
template <typename T>
2019-03-22 10:09:13 +00:00
[[nodiscard]] constexpr std::string_view nameof_type() noexcept {
2019-03-20 16:41:51 +00:00
return detail::nameof_type_impl<detail::identity<T>>();
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-04-05 12:19:18 +00:00
// NAMEOF obtains simple (unqualified) string name of variable, function, enum, macro.
2019-04-14 13:16:24 +00:00
#define NAMEOF(...) ::nameof::detail::nameof_impl<::nameof::detail::check_t<decltype(__VA_ARGS__)>>(#__VA_ARGS__, false)
2019-03-20 16:41:51 +00:00
2019-04-08 18:02:45 +00:00
// Obtains simple (unqualified) full (with template suffix) string name of variable, function, enum, macro.
2019-04-14 13:16:24 +00:00
#define NAMEOF_FULL(...) ::nameof::detail::nameof_impl<::nameof::detail::check_t<decltype(__VA_ARGS__)>>(#__VA_ARGS__, true)
2019-03-20 16:41:51 +00:00
2019-04-08 18:02:45 +00:00
// Obtains raw string name of variable, function, enum, macro.
2019-04-14 13:16:24 +00:00
#define NAMEOF_RAW(...) ::nameof::detail::nameof_raw_impl<::nameof::detail::check_t<decltype(__VA_ARGS__)>>(#__VA_ARGS__)
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.
2019-03-20 16:41:51 +00:00
#define NAMEOF_ENUM(...) ::nameof::nameof_enum<decltype(__VA_ARGS__)>(__VA_ARGS__)
2018-09-01 13:27:49 +00:00
2019-04-08 18:02:45 +00:00
// Obtains string name of type.
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-04-08 18:02:45 +00:00
// Obtains string name of variable type.
2019-04-01 19:07:28 +00:00
#define NAMEOF_VAR_TYPE(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()