nameof_module/include/nameof.hpp

278 lines
9.7 KiB
C++
Raw Normal View History

2018-04-10 19:32:51 +00:00
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
2019-03-21 09:16:18 +00:00
// vesion 0.6.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
2018-03-17 05:06:38 +00:00
#include <cstddef>
2018-07-13 15:18:52 +00:00
#include <type_traits>
2019-03-20 16:41:51 +00:00
#include <string_view>
2018-08-26 23:57:25 +00:00
2018-09-01 17:33:26 +00:00
#if !defined(NAMEOF_ENUM_MAX_SEARCH_DEPTH)
2019-03-21 06:56:59 +00:00
# define NAMEOF_ENUM_MAX_SEARCH_DEPTH 256
2018-09-01 17:33:26 +00:00
#endif
2018-04-04 12:19:02 +00:00
namespace nameof {
2018-04-10 18:09:23 +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>
struct identity {
using type = T;
};
2019-03-22 08:56:01 +00:00
constexpr bool is_name_char(char s) noexcept {
return (s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_';
2018-08-02 14:51:44 +00:00
}
2019-03-22 08:56:01 +00:00
constexpr bool is_bracket_char(char s) noexcept {
2018-08-28 14:00:42 +00:00
return s == ')' || s == '}' || s == '>' || s == '(' || s == '{' || s == '<';
}
2019-03-21 09:16:06 +00:00
constexpr std::string_view pretty_name(std::string_view name, bool with_suffix) noexcept {
2018-08-28 14:00:42 +00:00
for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) {
2019-03-22 08:56:01 +00:00
if (h == 0 && !is_name_char(name[i - 1]) && !is_bracket_char(name[i - 1])) {
2018-08-28 14:00:42 +00:00
++s;
continue;
}
if (name[i - 1] == ')' || name[i - 1] == '}') {
++h;
++s;
continue;
} else if (name[i - 1] == '(' || name[i - 1] == '{') {
--h;
++s;
continue;
}
if (h == 0) {
2019-03-20 16:41:51 +00:00
name.remove_suffix(s);
2018-08-28 14:00:42 +00:00
break;
} else {
++s;
continue;
}
}
2018-08-26 20:30:16 +00:00
std::size_t s = 0;
for (std::size_t i = name.size(), h = 0; i > 0; --i) {
2018-08-28 14:00:42 +00:00
if (name[i - 1] == '>') {
2018-08-26 20:30:16 +00:00
++h;
++s;
continue;
2018-08-28 14:00:42 +00:00
} else if (name[i - 1] == '<') {
2018-08-26 20:30:16 +00:00
--h;
++s;
continue;
}
if (h == 0) {
break;
2018-08-26 23:57:25 +00:00
} else {
++s;
continue;
2018-08-02 14:51:44 +00:00
}
}
2018-08-26 20:30:16 +00:00
for (std::size_t i = name.size() - s; i > 0; --i) {
2019-03-22 08:56:01 +00:00
if (!is_name_char(name[i - 1])) {
2019-03-20 16:41:51 +00:00
name.remove_prefix(i);
break;
2018-08-06 11:01:40 +00:00
}
2018-08-26 20:30:16 +00:00
}
2019-03-20 16:41:51 +00:00
name.remove_suffix(with_suffix ? 0 : s);
2018-08-26 23:57:25 +00:00
2019-03-20 16:41:51 +00:00
return name;
2018-08-26 20:30:16 +00:00
}
2019-03-22 08:56:01 +00:00
template <typename E>
2019-03-21 09:16:06 +00:00
constexpr int nameof_enum_impl_() noexcept {
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
2019-03-22 08:56:01 +00:00
return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::nameof_enum_impl_() [E = ") - sizeof("]") + 1;
2018-09-01 13:27:49 +00:00
#elif defined(__GNUC__)
2019-03-22 08:56:01 +00:00
return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::nameof_enum_impl_() [with E = ") - sizeof("]") + 1;
2018-09-01 13:27:49 +00:00
#elif defined(_MSC_VER)
2019-03-21 09:16:06 +00:00
return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::nameof_enum_impl_<") - sizeof(">(void) noexcept") + 1;
2018-09-01 13:27:49 +00:00
#else
return 0;
#endif
}
2019-03-22 08:56:01 +00:00
template <typename E, E V>
2019-03-21 09:16:06 +00:00
constexpr std::string_view nameof_enum_impl() noexcept {
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
2019-03-20 16:41:51 +00:00
const auto str = __PRETTY_FUNCTION__;
const auto size = sizeof(__PRETTY_FUNCTION__) - 1;
2019-03-22 08:56:01 +00:00
const auto prefix = sizeof("std::string_view nameof::detail::nameof_enum_impl() [E = ") + nameof_enum_impl_<E>() + sizeof("; V = ") - 2;
2019-03-20 16:41:51 +00:00
const auto suffix = sizeof("]") - 1;
2019-03-21 09:16:06 +00:00
return detail::pretty_name({str + prefix, size - prefix - suffix}, false);
2018-09-01 13:27:49 +00:00
#elif defined(__GNUC__)
2019-03-20 16:41:51 +00:00
const auto str = __PRETTY_FUNCTION__;
const auto size = sizeof(__PRETTY_FUNCTION__) - 1;
2019-03-22 08:56:01 +00:00
const auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_enum_impl() [with E = ") + nameof_enum_impl_<E>() + sizeof("; V = ");
2019-03-20 16:41:51 +00:00
const auto suffix = sizeof("; std::string_view = std::basic_string_view<char>]") - 1;
return {str + prefix, size - prefix - suffix};
2018-09-01 13:27:49 +00:00
#elif defined(_MSC_VER)
2019-03-20 16:41:51 +00:00
const auto str = __FUNCSIG__;
const auto size = sizeof(__FUNCSIG__) - 1;
2019-03-22 08:56:01 +00:00
const auto prefix = sizeof("class std::basic_string_view<char,struct std::char_traits<char> > __cdecl nameof::detail::nameof_enum_impl<") + nameof_enum_impl_<E>();
2019-03-21 09:16:06 +00:00
const auto suffix = sizeof(">(void) noexcept") - 1;
return detail::pretty_name({str + prefix, size - prefix - suffix}, false);
2018-09-01 13:27:49 +00:00
#else
return {};
#endif
}
2019-03-22 08:56:01 +00:00
template <typename E, int V>
2019-03-20 16:41:51 +00:00
struct nameof_enum_t {
2019-03-21 09:16:06 +00:00
constexpr std::string_view operator()(int value) const noexcept {
2019-03-22 08:56:01 +00:00
switch (value - V) {
2019-03-21 06:56:59 +00:00
case 0:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V}>();
2019-03-21 06:56:59 +00:00
case 1:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 1}>();
2019-03-21 06:56:59 +00:00
case 2:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 2}>();
2019-03-21 06:56:59 +00:00
case 3:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 3}>();
2019-03-21 06:56:59 +00:00
case 4:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 4}>();
2019-03-21 06:56:59 +00:00
case 5:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 5}>();
2019-03-21 06:56:59 +00:00
case 6:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 6}>();
2019-03-21 06:56:59 +00:00
case 7:
2019-03-22 08:56:01 +00:00
return nameof_enum_impl<E, E{V + 7}>();
2019-03-21 06:56:59 +00:00
default:
2019-03-22 08:56:01 +00:00
return nameof_enum_t<E, V + 8>{}(value);
2019-03-21 06:56:59 +00:00
}
2018-09-01 13:27:49 +00:00
}
};
2019-03-22 08:56:01 +00:00
template <typename E>
struct nameof_enum_t<E, NAMEOF_ENUM_MAX_SEARCH_DEPTH> {
2019-03-21 09:16:06 +00:00
constexpr std::string_view operator()(int) const noexcept {
2019-03-22 08:56:01 +00:00
return {"nameof_enum::out_of_range"};
2019-03-21 06:56:59 +00:00
}
2018-09-01 17:08:09 +00:00
};
2019-03-21 09:16:06 +00:00
constexpr std::string_view nameof_type_impl_(std::string_view name) noexcept {
2019-03-20 16:41:51 +00:00
#if defined(_MSC_VER)
if (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' && name[2] == 'u' && name[3] == 'm' && name[4] == ' ') {
name.remove_prefix(sizeof("enum"));
}
if (name.size() > sizeof("class") && name[0] == 'c' && name[1] == 'l' && name[2] == 'a' && name[3] == 's' && name[4] == 's' && name[5] == ' ') {
name.remove_prefix(sizeof("class"));
}
if (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' && name[2] == 'r' && name[3] == 'u' && name[4] == 'c' && name[5] == 't' && name[6] == ' ') {
name.remove_prefix(sizeof("struct"));
}
#endif
while (name.back() == ' ') {
name.remove_suffix(1);
}
return name;
}
2018-09-01 17:08:09 +00:00
template <typename T>
2019-03-21 09:16:06 +00:00
constexpr std::string_view nameof_type_impl() noexcept {
2018-08-26 23:57:25 +00:00
#if defined(__clang__)
2019-03-20 16:41:51 +00:00
const auto str = __PRETTY_FUNCTION__;
const auto size = sizeof(__PRETTY_FUNCTION__) - 1;
const auto prefix = sizeof("std::string_view nameof::detail::nameof_type_impl() [T = nameof::detail::identity<") - 1;
const auto suffix = sizeof(">]") - 1;
return nameof_type_impl_({str + prefix, size - prefix - suffix});
2018-08-26 23:57:25 +00:00
#elif defined(__GNUC__)
2019-03-20 16:41:51 +00:00
const auto str = __PRETTY_FUNCTION__;
const auto size = sizeof(__PRETTY_FUNCTION__) - 1;
const auto prefix = sizeof("constexpr std::string_view nameof::detail::nameof_type_impl() [with T = nameof::detail::identity<") - 1;
const auto suffix = sizeof(">; std::string_view = std::basic_string_view<char>]") - 1;
return nameof_type_impl_({str + prefix, size - prefix - suffix});
2018-08-26 23:57:25 +00:00
#elif defined(_MSC_VER)
2019-03-20 16:41:51 +00:00
const auto str = __FUNCSIG__;
const auto size = sizeof(__FUNCSIG__) - 1;
const 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;
2019-03-21 09:16:06 +00:00
const auto suffix = sizeof(">>(void) noexcept") - 1;
2019-03-20 16:41:51 +00:00
return nameof_type_impl_({str + prefix, size - prefix - suffix});
2018-08-26 23:57:25 +00:00
#else
return {};
#endif
}
2018-08-02 14:51:44 +00:00
2019-03-22 08:56:01 +00:00
template <typename T, typename = std::enable_if_t<!std::is_reference_v<T>>>
constexpr std::string_view nameof_impl(std::string_view name, bool with_suffix) noexcept {
2019-03-20 16:41:51 +00:00
return detail::pretty_name(name, with_suffix);
2018-07-13 15:18:52 +00:00
}
2019-03-21 09:16:06 +00:00
template <typename T>
constexpr std::string_view nameof_raw_impl(std::string_view name) noexcept {
return name;
}
} // namespace detail
2019-03-22 08:56:01 +00:00
template <typename T, typename = std::enable_if_t<std::is_enum_v<std::decay_t<T>>>>
2019-03-21 09:16:06 +00:00
constexpr std::string_view nameof_enum(T value) noexcept {
2019-03-22 08:56:01 +00:00
constexpr auto s = std::is_signed_v<std::underlying_type_t<std::decay_t<T>>>;
return detail::nameof_enum_t<std::decay_t<T>, s ? -NAMEOF_ENUM_MAX_SEARCH_DEPTH : 0>{}(static_cast<int>(value));
2018-09-01 13:27:49 +00:00
}
2019-03-20 16:41:51 +00:00
template <typename T>
2019-03-21 09:16:06 +00:00
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-09-01 11:05:50 +00:00
template <typename T>
2019-03-21 09:16:06 +00:00
constexpr std::string_view nameof_type(T) noexcept {
return nameof_type<T>();
2018-08-02 14:51:44 +00:00
}
2018-07-13 15:18:52 +00:00
2018-04-04 16:36:18 +00:00
} // namespace nameof
2018-03-22 14:09:55 +00:00
2019-03-22 08:56:01 +00:00
// Used to obtain the simple (unqualified) string name of variable, function, enum, macro.
2019-03-21 09:16:06 +00:00
#define NAMEOF(...) ::nameof::detail::nameof_impl<decltype(__VA_ARGS__)>(#__VA_ARGS__, false)
2019-03-20 16:41:51 +00:00
2019-03-22 08:56:01 +00:00
// Used to obtain the full string name of variable, function, enum, macro.
2019-03-21 09:16:06 +00:00
#define NAMEOF_FULL(...) ::nameof::detail::nameof_impl<decltype(__VA_ARGS__)>(#__VA_ARGS__, true)
2019-03-20 16:41:51 +00:00
2019-03-22 08:56:01 +00:00
// Used to obtain the raw string name of variable, function, enum, macro.
2019-03-21 09:16:06 +00:00
#define NAMEOF_RAW(...) ::nameof::detail::nameof_raw_impl<decltype(__VA_ARGS__)>(#__VA_ARGS__)
2018-08-02 15:18:59 +00:00
2019-03-22 08:56:01 +00:00
// Used to obtain the simple (unqualified) string 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-03-22 08:56:01 +00:00
// Used to obtain the string name of variable type.
2019-03-20 16:41:51 +00:00
#define NAMEOF_TYPE(...) ::nameof::nameof_type<decltype(__VA_ARGS__)>()
2019-03-22 08:56:01 +00:00
// Used to obtain the string name of type.
2019-03-20 16:41:51 +00:00
#define NAMEOF_TYPE_T(...) ::nameof::nameof_type<__VA_ARGS__>()