nameof_module/include/nameof.hpp

498 lines
17 KiB
C++
Raw Normal View History

2018-04-10 19:32:51 +00:00
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
2018-08-02 14:51:44 +00:00
// vesion 0.5.0
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>.
2018-04-02 11:49:15 +00:00
// Copyright (c) 2016, 2018 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>
2018-08-28 15:51:04 +00:00
#include <limits>
2018-08-28 16:45:26 +00:00
#include <stdexcept>
2018-08-06 14:53:58 +00:00
#include <string>
2018-08-02 14:51:44 +00:00
#include <ostream>
2018-03-17 04:31:59 +00:00
2018-08-26 20:30:16 +00:00
#if defined(__cpp_constexpr) && __cpp_constexpr >= 201304L
# define NAMEOF_HAS_CONSTEXPR14 1
2018-08-03 12:34:46 +00:00
#endif
2018-08-27 13:40:23 +00:00
#if (defined(__clang__) || defined(_MSC_VER)) || (defined(__GNUC__) && __GNUC__ >= 5)
2018-09-01 17:08:09 +00:00
# define NAMEOF_HAS_CONSTEXPR 1
# define NAMEOF_CONSTEXPR constexpr
2018-08-26 23:57:25 +00:00
#else
2018-09-01 17:08:09 +00:00
# define NAMEOF_CONSTEXPR inline
2018-08-26 23:57:25 +00:00
#endif
2018-09-01 17:33:26 +00:00
#if !defined(NAMEOF_ENUM_MAX_SEARCH_DEPTH)
# define NAMEOF_ENUM_MAX_SEARCH_DEPTH 64
#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-26 20:30:16 +00:00
namespace nstd {
2018-08-02 14:51:44 +00:00
template <typename T>
struct identity {
using type = T;
};
2018-08-26 20:30:16 +00:00
} // namespace nstd
2018-08-31 14:29:33 +00:00
#if !defined(NAMEOF_HAS_CONSTEXPR14)
2018-08-29 12:21:45 +00:00
constexpr int CharCompare(char lhs, char rhs) {
return (lhs > rhs) ? 1 : ((lhs < rhs) ? -1 : 0);
}
2018-08-31 14:29:33 +00:00
#endif
2018-08-29 12:21:45 +00:00
constexpr int StrCompare(const char* lhs, const char* rhs, std::size_t size) {
2018-08-31 14:29:33 +00:00
#if !defined(_MSC_VER) && __cplusplus >= 201703L
2018-08-29 12:21:45 +00:00
return std::char_traits<char>::compare(lhs, rhs, size);
#elif defined(NAMEOF_HAS_CONSTEXPR14)
2018-08-26 20:30:16 +00:00
for (std::size_t i = 0; i < size; ++i) {
2018-08-29 12:21:45 +00:00
if (lhs[i] > rhs[i]) {
return 1;
}
if (lhs[i] < rhs[i]) {
return -1;
2018-08-26 20:30:16 +00:00
}
}
2018-08-29 12:21:45 +00:00
return 0;
2018-08-26 20:30:16 +00:00
#else
2018-08-29 12:21:45 +00:00
return (size == 0) ? CharCompare(lhs[0], rhs[0])
2018-08-29 14:43:55 +00:00
: (CharCompare(lhs[size - 1], rhs[size - 1]) == 0)
? StrCompare(lhs, rhs, size - 1)
: CharCompare(lhs[size - 1], rhs[size - 1]);
2018-08-26 20:30:16 +00:00
#endif
2018-08-06 14:53:58 +00:00
}
2018-08-26 20:30:16 +00:00
constexpr std::size_t StrLen(const char* str, std::size_t size = 0) {
2018-08-31 14:29:33 +00:00
#if !defined(_MSC_VER) && __cplusplus >= 201703L
2018-08-31 14:52:36 +00:00
static_cast<void>(size);
return std::char_traits<char>::length(str);
2018-08-29 12:21:45 +00:00
#elif defined(NAMEOF_HAS_CONSTEXPR14)
2018-08-26 20:30:16 +00:00
for (; str != nullptr; ++size) {
if (str[size] == '\0') {
return size;
}
}
return size;
#else
return (str[size] == '\0') ? size : StrLen(str, size + 1);
#endif
2018-08-07 16:13:40 +00:00
}
2018-08-28 17:08:53 +00:00
} // namespace detail
2018-08-29 14:43:55 +00:00
// std::string like compile-time const char* string.
2018-08-02 14:51:44 +00:00
class cstring final {
const char* str_;
2018-08-26 20:30:16 +00:00
std::size_t size_;
2018-08-02 14:51:44 +00:00
public:
2018-08-26 20:30:16 +00:00
constexpr cstring(const char* str, std::size_t size, std::size_t prefix = 0, std::size_t suffix = 0) noexcept
2018-08-07 16:13:40 +00:00
: str_{str + prefix},
2018-08-26 20:30:16 +00:00
size_{size - prefix - suffix} {}
2018-08-02 14:51:44 +00:00
2018-08-28 17:08:53 +00:00
constexpr cstring(const char* str) noexcept : cstring{str, detail::StrLen(str), 0, 0} {}
2018-08-02 14:51:44 +00:00
2018-08-31 18:02:22 +00:00
constexpr cstring() noexcept : cstring{nullptr, 0, 0, 0} {}
2018-08-28 15:51:04 +00:00
cstring(const std::string& str) noexcept : cstring{str.data(), str.size(), 0, 0} {}
2018-08-29 12:21:45 +00:00
constexpr cstring(const cstring&) = default;
2018-08-02 14:51:44 +00:00
cstring& operator=(const cstring&) = default;
2018-08-26 20:30:16 +00:00
constexpr std::size_t size() const noexcept { return size_; }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr std::size_t length() const noexcept { return size_; }
2018-08-02 14:51:44 +00:00
2018-08-29 14:43:55 +00:00
constexpr std::size_t max_size() const noexcept {
return (std::numeric_limits<std::size_t>::max)();
}
2018-08-28 15:51:04 +00:00
2018-08-26 20:30:16 +00:00
constexpr bool empty() const noexcept { return size_ == 0; }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char* begin() const noexcept { return str_; }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char* end() const noexcept { return str_ + size_; }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char* cbegin() const noexcept { return begin(); }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char* cend() const noexcept { return end(); }
2018-08-02 14:51:44 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char& operator[](std::size_t i) const { return str_[i]; }
2018-08-02 14:51:44 +00:00
2018-08-31 13:42:59 +00:00
constexpr const char& at(std::size_t i) const {
return (i < size_) ? str_[i]
2018-08-31 18:02:22 +00:00
: (throw std::out_of_range{"cstring::at"}, str_[0]);
2018-08-31 13:42:59 +00:00
}
2018-08-26 20:30:16 +00:00
constexpr const char& front() const { return str_[0]; }
2018-08-03 12:19:51 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char& back() const { return str_[size_ - 1]; }
2018-08-03 12:19:51 +00:00
2018-08-26 20:30:16 +00:00
constexpr const char* data() const noexcept { return str_; }
2018-08-03 12:19:51 +00:00
2018-08-29 14:43:55 +00:00
constexpr cstring remove_prefix(std::size_t n) const {
return {str_ + n, size_ - n};
}
2018-08-03 12:19:51 +00:00
2018-08-29 14:43:55 +00:00
constexpr cstring add_prefix(std::size_t n) const {
return {str_ - n, size_ + n};
}
2018-08-26 20:30:16 +00:00
2018-08-29 14:43:55 +00:00
constexpr cstring remove_suffix(std::size_t n) const {
return {str_, size_ - n};
}
2018-08-03 12:19:51 +00:00
2018-08-29 14:43:55 +00:00
constexpr cstring add_suffix(std::size_t n) const {
return {str_, size_ + n};
}
2018-08-26 20:30:16 +00:00
2018-08-29 14:43:55 +00:00
constexpr cstring substr(std::size_t pos, std::size_t n) const {
return {str_ + pos, n};
}
2018-08-03 12:19:51 +00:00
2018-08-29 12:21:45 +00:00
constexpr int compare(cstring other) const {
2018-08-29 14:43:55 +00:00
return (size_ == other.size_) ? detail::StrCompare(str_, other.str_, size_)
: ((size_ > other.size_) ? 1 : -1);
2018-08-28 15:51:04 +00:00
}
2018-08-28 20:28:06 +00:00
friend constexpr bool operator==(cstring lhs, cstring rhs) {
2018-08-29 12:21:45 +00:00
return lhs.compare(rhs) == 0;
2018-08-03 12:19:51 +00:00
}
2018-08-02 14:51:44 +00:00
2018-08-28 20:28:06 +00:00
friend constexpr bool operator!=(cstring lhs, cstring rhs) {
2018-08-03 12:34:46 +00:00
return !(lhs == rhs);
2018-08-02 14:51:44 +00:00
}
2018-08-28 17:08:53 +00:00
std::string append(cstring s) const {
2018-08-31 18:02:22 +00:00
return std::string{str_, size_}.append(s.str_, s.size_);
2018-08-28 15:51:04 +00:00
}
friend std::string operator+(cstring lhs, cstring rhs) {
2018-09-01 10:24:52 +00:00
return std::string{lhs.str_, lhs.size_} + std::string{rhs.str_, rhs.size_};
2018-08-28 15:51:04 +00:00
}
2018-08-26 23:57:25 +00:00
friend std::ostream& operator<<(std::ostream& os, cstring str) {
2018-08-26 20:30:16 +00:00
os.write(str.str_, str.size_);
2018-08-03 12:34:46 +00:00
return os;
}
2018-08-02 14:51:44 +00:00
2018-08-31 18:02:22 +00:00
operator std::string() const { return std::string{str_, size_}; }
2018-08-03 12:34:46 +00:00
};
2018-08-02 14:51:44 +00:00
2018-08-28 17:08:53 +00:00
namespace detail {
2018-08-26 20:30:16 +00:00
constexpr bool IsLexeme(char s) noexcept {
2018-08-06 11:01:40 +00:00
return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') || (s >= 'A' && s <= 'Z') || s == '_');
2018-08-02 14:51:44 +00:00
}
2018-08-28 14:00:42 +00:00
constexpr bool IsBracket(char s) noexcept {
return s == ')' || s == '}' || s == '>' || s == '(' || s == '{' || s == '<';
}
2018-08-26 20:30:16 +00:00
#if defined(NAMEOF_HAS_CONSTEXPR14)
2018-08-28 20:28:06 +00:00
constexpr cstring NameofPretty(cstring name, bool with_suffix) {
2018-08-28 14:00:42 +00:00
for (std::size_t i = name.size(), h = 0, s = 0; i > 0; --i) {
if (h == 0 && IsLexeme(name[i - 1]) && !IsBracket(name[i - 1])) {
++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) {
name = name.remove_suffix(s);
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) {
if (IsLexeme(name[i - 1])) {
return name.remove_prefix(i).remove_suffix(with_suffix ? 0 : s);
2018-08-06 11:01:40 +00:00
}
2018-08-26 20:30:16 +00:00
}
2018-08-26 23:57:25 +00:00
2018-08-26 20:30:16 +00:00
return name.remove_suffix(with_suffix ? 0 : s);
}
#else
2018-08-28 20:28:06 +00:00
constexpr cstring RemoveSuffix(cstring name, std::size_t h = 0) {
2018-08-28 14:00:42 +00:00
return (h == 0 && IsLexeme(name.back()) && !IsBracket(name.back()))
? RemoveSuffix(name.remove_suffix(1), h)
: (name.back() == ')' || name.back() == '}')
? RemoveSuffix(name.remove_suffix(1), h + 1)
: (name.back() == '(' || name.back() == '{')
? RemoveSuffix(name.remove_suffix(1), h - 1)
2018-08-29 14:43:55 +00:00
: (h == 0) ? name
: RemoveSuffix(name.remove_suffix(1), h);
2018-08-28 14:00:42 +00:00
}
2018-08-28 20:28:06 +00:00
constexpr std::size_t FindSuffix(cstring name, std::size_t h = 0, std::size_t s = 0) {
2018-08-28 14:00:42 +00:00
return (name[name.size() - 1 - s] == '>')
2018-08-27 13:40:23 +00:00
? FindSuffix(name, h + 1, s + 1)
2018-08-28 14:00:42 +00:00
: (name[name.size() - 1 - s] == '<')
2018-08-27 13:40:23 +00:00
? FindSuffix(name, h - 1, s + 1)
2018-08-28 17:08:53 +00:00
: (h == 0) ? s : FindSuffix(name, h, s + 1);
2018-08-26 20:30:16 +00:00
}
2018-08-28 20:28:06 +00:00
constexpr cstring RemovePrefix(cstring name, const std::size_t p = 0) {
2018-08-26 20:30:16 +00:00
return p == name.size() ? name : IsLexeme(name[name.size() - 1 - p])
? name.remove_prefix(name.size() - p)
2018-08-27 13:40:23 +00:00
: RemovePrefix(name, p + 1);
2018-08-26 20:30:16 +00:00
}
2018-09-01 13:27:49 +00:00
constexpr cstring NameofPrettyImpl1(cstring name, std::size_t s, bool with_suffix) {
2018-08-27 13:40:23 +00:00
return RemovePrefix(name.remove_suffix(s)).add_suffix(with_suffix ? s : 0);
2018-08-26 20:30:16 +00:00
}
2018-08-28 20:28:06 +00:00
constexpr cstring NameofPrettyImpl(cstring name, bool with_suffix) {
2018-09-01 13:27:49 +00:00
return NameofPrettyImpl1(name, FindSuffix(name), with_suffix);
2018-08-28 14:00:42 +00:00
}
2018-08-28 20:28:06 +00:00
constexpr cstring NameofPretty(cstring name, bool with_suffix) {
2018-08-28 14:00:42 +00:00
return NameofPrettyImpl(RemoveSuffix(name), with_suffix);
2018-08-26 20:30:16 +00:00
}
#endif
2018-08-27 13:40:23 +00:00
#if defined(_MSC_VER)
2018-08-28 20:28:06 +00:00
constexpr cstring RemoveSpaceSuffix(cstring name) {
2018-08-28 17:08:53 +00:00
return (name.back() == ' ') ? RemoveSpaceSuffix(name.remove_suffix(1)) : name;
2018-08-26 20:30:16 +00:00
}
2018-08-06 11:01:40 +00:00
2018-08-28 20:28:06 +00:00
constexpr cstring RemoveClassPrefix(cstring name) {
2018-08-26 20:30:16 +00:00
return (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"))
: name;
2018-08-02 14:51:44 +00:00
}
2018-08-28 20:28:06 +00:00
constexpr cstring RemoveEnumPrefix(cstring name) {
2018-08-26 20:30:16 +00:00
return (name.size() > sizeof("enum") && name[0] == 'e' && name[1] == 'n' &&
name[2] == 'u' && name[3] == 'm' && name[4] == ' ')
? name.remove_prefix(sizeof("enum"))
: name;
}
2018-08-28 20:28:06 +00:00
constexpr cstring RemoveStructPrefix(cstring name) {
2018-08-26 20:30:16 +00:00
return (name.size() > sizeof("struct") && name[0] == 's' && name[1] == 't' &&
2018-08-29 14:43:55 +00:00
name[2] == 'r' && name[3] == 'u' && name[4] == 'c' &&
name[5] == 't' && name[6] == ' ')
2018-08-26 20:30:16 +00:00
? name.remove_prefix(sizeof("struct"))
: name;
}
2018-08-28 20:28:06 +00:00
constexpr cstring NameofTypePretty(cstring name) {
2018-08-27 13:40:23 +00:00
return RemoveClassPrefix(RemoveStructPrefix(RemoveEnumPrefix(RemoveSpaceSuffix(name))));
2018-08-02 14:51:44 +00:00
}
2018-08-27 13:40:23 +00:00
#elif defined(__clang__) || defined(__GNUC__)
2018-08-28 20:28:06 +00:00
constexpr cstring NameofTypePretty(const char* str, std::size_t size, std::size_t prefix, std::size_t suffix) {
2018-08-28 17:08:53 +00:00
return {str, size, prefix, suffix + ((str[size - suffix - 1] == ' ') ? 1 : 0)};
2018-08-27 13:40:23 +00:00
}
#endif
2018-08-02 14:51:44 +00:00
2018-09-01 13:27:49 +00:00
template <typename T>
2018-09-01 17:08:09 +00:00
NAMEOF_CONSTEXPR int NameofEnumImpl1() {
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
return sizeof(__PRETTY_FUNCTION__) - sizeof("int nameof::detail::NameofEnumImpl1() [T = ") - sizeof("]") + 1;
#elif defined(__GNUC__)
return sizeof(__PRETTY_FUNCTION__) - sizeof("constexpr int nameof::detail::NameofEnumImpl1() [with T = ") - sizeof("]") + 1;
#elif defined(_MSC_VER)
return sizeof(__FUNCSIG__) - sizeof("int __cdecl nameof::detail::NameofEnumImpl1<") - sizeof(">(void)") + 1;
#else
return 0;
#endif
}
template <typename T, T V>
2018-09-01 19:14:11 +00:00
NAMEOF_CONSTEXPR cstring NameofEnumImpl2() {
2018-09-01 13:27:49 +00:00
#if defined(__clang__)
return {__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1,
sizeof("nameof::cstring nameof::detail::NameofEnumImpl2() [T = ") + NameofEnumImpl1<T>() + sizeof("; V = ") - 2,
sizeof("]") - 1};
#elif defined(__GNUC__)
return {__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1,
sizeof("constexpr nameof::cstring nameof::detail::NameofEnumImpl2() [with T = ") + NameofEnumImpl1<T>() + sizeof("; T V = ") - 2,
sizeof("]") - 1};
#elif defined(_MSC_VER)
return {__FUNCSIG__,
sizeof(__FUNCSIG__) - 1,
sizeof("class nameof::cstring __cdecl nameof::detail::NameofEnumImpl2<") + NameofEnumImpl1<T>(),
sizeof(">(void)") - 1};
#else
return {};
#endif
}
2018-09-01 18:16:45 +00:00
template <typename T, int I>
2018-09-01 13:27:49 +00:00
struct NameofEnumImpl {
2018-09-01 19:14:11 +00:00
NAMEOF_CONSTEXPR cstring operator()(int value) const {
2018-09-01 18:16:45 +00:00
return (value - I == 0)
? NameofEnumImpl2<T, T(I)>()
: (value - I == 1)
? NameofEnumImpl2<T, T(I + 1)>()
: (value - I == 2)
? NameofEnumImpl2<T, T(I + 2)>()
: (value - I == 3)
? NameofEnumImpl2<T, T(I + 3)>()
: (value - I == 4)
? NameofEnumImpl2<T, T(I + 4)>()
: (value - I == 5)
? NameofEnumImpl2<T, T(I + 5)>()
: (value - I == 6)
? NameofEnumImpl2<T, T(I + 6)>()
: (value - I == 7)
? NameofEnumImpl2<T, T(I + 7)>()
: NameofEnumImpl<T, I + 8>{}(value);
2018-09-01 13:27:49 +00:00
}
};
template <typename T>
2018-09-01 17:33:26 +00:00
struct NameofEnumImpl<T, NAMEOF_ENUM_MAX_SEARCH_DEPTH> {
2018-09-01 19:14:11 +00:00
NAMEOF_CONSTEXPR cstring operator()(int) const { return {}; }
2018-09-01 17:08:09 +00:00
};
template <typename T>
NAMEOF_CONSTEXPR cstring NameofType() {
2018-08-26 23:57:25 +00:00
#if defined(__clang__)
2018-08-27 13:40:23 +00:00
return NameofTypePretty(
__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1,
2018-08-28 17:08:53 +00:00
sizeof("nameof::cstring nameof::detail::NameofType() [T = nameof::detail::nstd::identity<") - 1,
2018-08-27 13:40:23 +00:00
sizeof(">]") - 1);
2018-08-26 23:57:25 +00:00
#elif defined(__GNUC__)
2018-08-27 13:40:23 +00:00
return NameofTypePretty(
__PRETTY_FUNCTION__,
sizeof(__PRETTY_FUNCTION__) - 1,
2018-09-01 17:08:09 +00:00
# if defined(NAMEOF_HAS_CONSTEXPR)
2018-08-28 17:08:53 +00:00
sizeof("constexpr nameof::cstring nameof::detail::NameofType() [with T = nameof::detail::nstd::identity<") - 1,
2018-08-27 13:40:23 +00:00
# else
2018-08-28 17:08:53 +00:00
sizeof("nameof::cstring nameof::detail::NameofType() [with T = nameof::detail::nstd::identity<") - 1,
2018-08-27 13:40:23 +00:00
# endif
sizeof(">]") - 1);
2018-08-26 23:57:25 +00:00
#elif defined(_MSC_VER)
2018-08-27 13:40:23 +00:00
return NameofTypePretty(
{__FUNCSIG__,
sizeof(__FUNCSIG__) - 1,
2018-08-28 17:08:53 +00:00
sizeof("class nameof::cstring __cdecl nameof::detail::NameofType<struct nameof::detail::nstd::identity<") - 1,
2018-08-28 20:28:06 +00:00
sizeof(">>(void)") - 1});
2018-08-26 23:57:25 +00:00
#else
return {};
#endif
}
2018-08-02 14:51:44 +00:00
2018-08-28 17:08:53 +00:00
} // namespace detail
2018-07-13 15:18:52 +00:00
template <typename T,
2018-08-28 14:00:42 +00:00
typename = typename std::enable_if<!std::is_reference<T>::value>::type>
2018-09-01 11:05:50 +00:00
constexpr cstring Nameof(const char* name, std::size_t size, bool with_suffix = false) {
2018-08-28 17:08:53 +00:00
return detail::NameofPretty({name, size}, with_suffix);
2018-07-13 15:18:52 +00:00
}
2018-09-01 13:27:49 +00:00
template <typename T,
2018-09-01 19:14:11 +00:00
typename = typename std::enable_if<!std::is_reference<T>::value &&
std::is_enum<T>::value>::type>
2018-09-01 17:08:09 +00:00
NAMEOF_CONSTEXPR cstring NameofEnum(T value) {
2018-09-01 16:39:51 +00:00
#if defined(__clang__) || defined(_MSC_VER)
2018-09-01 19:14:14 +00:00
return detail::NameofPretty(
std::is_unsigned<std::underlying_type<T>::type>::value
? detail::NameofEnumImpl<T, 0>{}(static_cast<int>(value))
: detail::NameofEnumImpl<T, -NAMEOF_ENUM_MAX_SEARCH_DEPTH>{}(static_cast<int>(value)),
false);
2018-09-01 16:39:51 +00:00
#elif defined(__GNUC__)
2018-09-01 19:14:14 +00:00
return std::is_unsigned<std::underlying_type<T>::type>::value
? detail::NameofEnumImpl<T, 0>{}(static_cast<int>(value))
: detail::NameofEnumImpl<T, -NAMEOF_ENUM_MAX_SEARCH_DEPTH>{}(static_cast<int>(value));
2018-09-01 16:39:51 +00:00
#else
2018-09-01 19:14:11 +00:00
return {};
2018-09-01 16:39:51 +00:00
#endif
2018-09-01 13:27:49 +00:00
}
2018-08-27 13:40:23 +00:00
template <typename T>
2018-09-01 17:08:09 +00:00
NAMEOF_CONSTEXPR cstring NameofType() {
2018-09-01 19:14:11 +00:00
return true ? detail::NameofType<detail::nstd::identity<T>>()
: detail::NameofType<detail::nstd::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>
constexpr cstring NameofRaw(const char* name, std::size_t size) {
return {name, size};
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
2018-09-01 13:27:49 +00:00
// Used to obtain the simple (unqualified) string name of a variable, member, function, enum, macros.
2018-08-28 17:08:53 +00:00
#define NAMEOF(...) ::nameof::Nameof<decltype(__VA_ARGS__)>(#__VA_ARGS__, (sizeof(#__VA_ARGS__) / sizeof(char)) - 1, false)
2018-09-01 13:27:49 +00:00
// Used to obtain the full string name of a variable, member, function, enum, macros.
2018-08-28 17:08:53 +00:00
#define NAMEOF_FULL(...) ::nameof::Nameof<decltype(__VA_ARGS__)>(#__VA_ARGS__, (sizeof(#__VA_ARGS__) / sizeof(char)) - 1, true)
2018-09-01 13:27:49 +00:00
// Used to obtain the raw string name of a variable, member, function, enum, macros.
2018-08-28 17:08:53 +00:00
#define NAMEOF_RAW(...) ::nameof::NameofRaw<decltype(__VA_ARGS__)>(#__VA_ARGS__, (sizeof(#__VA_ARGS__) / sizeof(char)) - 1)
2018-08-02 15:18:59 +00:00
2018-09-01 13:27:49 +00:00
// Used to obtain the simple (unqualified) string name of a enum variable.
#define NAMEOF_ENUM(...) ::nameof::NameofEnum<decltype(__VA_ARGS__)>(__VA_ARGS__)
2018-08-26 23:57:25 +00:00
// Used to obtain the string name of a type.
2018-08-28 14:00:42 +00:00
#define NAMEOF_TYPE(...) ::nameof::NameofType<decltype(__VA_ARGS__)>()
#define NAMEOF_TYPE_T(...) ::nameof::NameofType<__VA_ARGS__>()