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-02 14:51:44 +00:00
|
|
|
#include <ostream>
|
2018-03-17 04:31:59 +00:00
|
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct remove_all_pointers
|
|
|
|
: std::conditional_t<std::is_pointer<T>::value,
|
|
|
|
remove_all_pointers<typename std::remove_pointer<T>::type>,
|
|
|
|
identity<T>> {};
|
|
|
|
class cstring final {
|
|
|
|
const char* str_;
|
|
|
|
std::size_t size_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
constexpr cstring(const char* str, std::size_t size) noexcept : str_(str), size_(size) {}
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
constexpr cstring(const char(&str)[N]) noexcept : str_(str), size_(N - 1) {}
|
|
|
|
|
|
|
|
cstring() = delete;
|
|
|
|
|
|
|
|
cstring(const cstring&) = default;
|
|
|
|
|
|
|
|
cstring(cstring&&) = default;
|
|
|
|
|
|
|
|
cstring& operator=(const cstring&) = default;
|
|
|
|
|
|
|
|
cstring& operator=(cstring&&) = default;
|
|
|
|
|
|
|
|
~cstring() = default;
|
|
|
|
|
|
|
|
inline constexpr std::size_t size() const noexcept { return size_; }
|
|
|
|
|
|
|
|
inline constexpr std::size_t length() const noexcept { return size_; }
|
|
|
|
|
|
|
|
inline constexpr std::size_t max_size() const noexcept { return std::numeric_limits<decltype(size_)>::max(); }
|
|
|
|
|
|
|
|
inline constexpr bool empty() const noexcept { return size_ == 0; }
|
|
|
|
|
|
|
|
inline constexpr const char* begin() const noexcept { return str_; }
|
|
|
|
|
|
|
|
inline constexpr const char* end() const noexcept { return str_ + size_; }
|
|
|
|
|
|
|
|
inline constexpr const char* cbegin() const noexcept { return begin(); }
|
|
|
|
|
|
|
|
inline constexpr const char* cend() const noexcept { return end(); }
|
|
|
|
|
|
|
|
inline constexpr const char& operator[](std::size_t i) const { return str_[i]; }
|
|
|
|
|
|
|
|
inline friend constexpr bool operator==(const cstring& lhs, const cstring& rhs) noexcept;
|
|
|
|
|
|
|
|
inline friend constexpr bool operator!=(const cstring& lhs, const cstring& rhs) noexcept;
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
inline friend constexpr bool operator==(const cstring& lhs, const char(&str)[N]) noexcept;
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
inline friend constexpr bool operator!=(const cstring& lhs, const char(&str)[N]) noexcept;
|
|
|
|
|
|
|
|
inline friend std::ostream& operator<<(std::ostream& os, const cstring& str);
|
|
|
|
|
|
|
|
inline operator std::string() const { return std::string(begin(), end()); }
|
|
|
|
};
|
|
|
|
|
|
|
|
inline constexpr bool operator==(const cstring& lhs, const cstring& rhs) noexcept {
|
|
|
|
if (lhs.size_ != rhs.size_)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < lhs.size_; ++i) {
|
|
|
|
if (lhs.str_[i] != rhs.str_[i])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr bool operator!=(const cstring& lhs, const cstring& rhs) noexcept {
|
|
|
|
return !(lhs == rhs);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
inline constexpr bool operator==(const cstring& lhs, const char(&str)[N]) noexcept {
|
|
|
|
return lhs == cstring{str, N - 1};
|
|
|
|
}
|
|
|
|
|
|
|
|
template <std::size_t N>
|
|
|
|
constexpr bool operator!=(const cstring& lhs, const char(&str)[N]) noexcept {
|
|
|
|
return !(lhs == cstring{str, N - 1});
|
|
|
|
}
|
|
|
|
|
|
|
|
inline std::ostream& operator<<(std::ostream& os, const cstring& str) {
|
|
|
|
os.write(str.begin(), str.size());
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2018-07-13 23:51:17 +00:00
|
|
|
inline constexpr bool IsLexeme(char s) noexcept {
|
2018-08-02 14:51:44 +00:00
|
|
|
return !((s >= '0' && s <= '9') || (s >= 'a' && s <= 'z') ||
|
|
|
|
(s >= 'A' && s <= 'Z') || s == '_');
|
2018-04-02 11:49:15 +00:00
|
|
|
}
|
2018-04-04 16:36:18 +00:00
|
|
|
|
2018-08-02 14:51:44 +00:00
|
|
|
inline constexpr cstring NameofBase(const char* name, std::size_t length) noexcept {
|
|
|
|
if (length == 0)
|
|
|
|
return {name, length};
|
|
|
|
|
|
|
|
for (std::size_t i = length; i > 0; --i) {
|
|
|
|
if (IsLexeme(name[i - 1])) {
|
|
|
|
return {&name[i], length - i};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {name, length};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr cstring NameofFunction(const char* name, std::size_t length, bool with_template_prefix) noexcept {
|
|
|
|
if (length == 0)
|
|
|
|
return {name, length};
|
|
|
|
|
|
|
|
std::size_t h = 0;
|
|
|
|
std::size_t p = 0;
|
|
|
|
for (std::size_t i = length; i > 0; --i) {
|
|
|
|
if (name[i - 1] == '>') {
|
|
|
|
++h;
|
|
|
|
++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name[i - 1] == '<') {
|
|
|
|
--h;
|
|
|
|
++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h != 0) {
|
|
|
|
++p;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsLexeme(name[i - 1]) && h == 0) {
|
|
|
|
return {&name[i], length - i - (with_template_prefix ? 0 : p)};
|
|
|
|
}
|
|
|
|
}
|
2018-04-04 16:36:18 +00:00
|
|
|
|
2018-08-02 14:51:44 +00:00
|
|
|
return {name, length - (with_template_prefix ? 0 : p)};
|
2018-03-17 03:27:47 +00:00
|
|
|
}
|
2018-04-04 16:36:18 +00:00
|
|
|
|
2018-08-02 14:51:44 +00:00
|
|
|
inline constexpr cstring NameofType(const char* name, std::size_t length) noexcept {
|
|
|
|
if (length == 0)
|
|
|
|
return {name, length};
|
|
|
|
|
|
|
|
std::size_t h = 0;
|
|
|
|
for (std::size_t i = length; i > 0; --i) {
|
|
|
|
if (h == 0 && (name[i - 1] == '&' || name[i - 1] == '*')) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name[i - 1] == '>') {
|
|
|
|
++h;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (name[i - 1] == '<') {
|
|
|
|
--h;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (h != 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsLexeme(name[i - 1]) && h == 0) {
|
|
|
|
return {&name[i], length - i};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {name, length};
|
|
|
|
}
|
|
|
|
|
|
|
|
inline constexpr cstring NameofRaw(const char* name, std::size_t length) noexcept {
|
|
|
|
return {name, length};
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace detail
|
|
|
|
|
2018-07-13 15:18:52 +00:00
|
|
|
template <typename T,
|
2018-08-02 14:51:44 +00:00
|
|
|
typename = typename std::enable_if<!std::is_reference<T>::value &&
|
|
|
|
!std::is_void<T>::value>::type>
|
|
|
|
inline constexpr detail::cstring Nameof(const T&, const char* name, std::size_t length, bool with_template_prefix) noexcept {
|
|
|
|
// TODO: conditional expression is constant
|
|
|
|
if (std::is_function<T>::value || std::is_member_function_pointer<T>::value)
|
|
|
|
return detail::NameofFunction(name, length, with_template_prefix);
|
|
|
|
|
|
|
|
return detail::NameofBase(name, length);
|
2018-07-13 15:18:52 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 14:51:44 +00:00
|
|
|
template <typename T,
|
|
|
|
typename = typename std::enable_if<!std::is_enum<T>::value &&
|
|
|
|
!std::is_function<T>::value &&
|
|
|
|
!std::is_member_function_pointer<T>::value>::type>
|
|
|
|
inline constexpr detail::cstring Nameof(T&&, const char*, std::size_t) = delete;
|
|
|
|
|
2018-07-13 15:18:52 +00:00
|
|
|
template <typename T>
|
2018-08-02 14:51:44 +00:00
|
|
|
inline constexpr detail::cstring NameofType() {
|
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
|
|
|
constexpr auto function_name = __PRETTY_FUNCTION__;
|
|
|
|
constexpr auto total_length = sizeof(__PRETTY_FUNCTION__) - 1;
|
|
|
|
constexpr auto prefix_length = sizeof("nameof::detail::cstring nameof::NameofType() [T = ") - 1;
|
|
|
|
constexpr auto suffix_length = sizeof("]") - 1;
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
constexpr auto function_name = __FUNCSIG__;
|
|
|
|
constexpr auto total_length = sizeof(__FUNCSIG__) - 1;
|
|
|
|
constexpr auto prefix_length = sizeof("class nameof::detail::cstring __cdecl nameof::NameofType<") - 1;
|
|
|
|
constexpr auto suffix_length = sizeof(">(void)") - 1;
|
|
|
|
#endif
|
|
|
|
constexpr auto type_name = detail::cstring{function_name + prefix_length, total_length - prefix_length - suffix_length};
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
constexpr auto class_length = sizeof("class") - 1;
|
|
|
|
constexpr auto struct_length = sizeof("struct") - 1;
|
|
|
|
constexpr auto enum_length = sizeof("enum") - 1;
|
|
|
|
|
|
|
|
if (std::is_class<typename detail::remove_all_pointers<typename std::remove_reference<T>::type>::type>::value) {
|
|
|
|
if (type_name[0] == 'c') {
|
|
|
|
if (type_name[class_length] == ' ') {
|
|
|
|
return detail::cstring{type_name.begin() + class_length + 1, type_name.length() - class_length - 1};
|
|
|
|
}
|
|
|
|
return detail::cstring{type_name.begin() + class_length, type_name.length() - class_length};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type_name[0] == 's') {
|
|
|
|
if (type_name[struct_length] == ' ') {
|
|
|
|
return detail::cstring{type_name.begin() + struct_length + 1, type_name.length() - struct_length - 1};
|
|
|
|
}
|
|
|
|
return detail::cstring{type_name.begin() + struct_length, type_name.length() - struct_length};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (std::is_enum<typename detail::remove_all_pointers<typename std::remove_reference<T>::type>::type>::value) {
|
|
|
|
if (type_name[0] == 'e') {
|
|
|
|
if (type_name[enum_length] == ' ') {
|
|
|
|
return detail::cstring{type_name.begin() + enum_length + 1, type_name.length() - enum_length - 1};
|
|
|
|
}
|
|
|
|
return detail::cstring{type_name.begin() + enum_length, type_name.length() - enum_length};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return type_name;
|
|
|
|
}
|
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-04-10 18:09:23 +00:00
|
|
|
#if defined(__GNUC__) || defined(__clang__)
|
2018-08-02 14:51:44 +00:00
|
|
|
// Used to obtain the raw string name of a variable, type, member, function, macros.
|
|
|
|
# define NAMEOF_RAW(name) ::nameof::detail::NameofRaw(#name, ((sizeof(#name) / sizeof(char)) - 1) + (0 * sizeof(void (*)(__typeof__(name)))))
|
2018-04-28 13:04:30 +00:00
|
|
|
#elif defined(_MSC_VER)
|
2018-08-02 14:51:44 +00:00
|
|
|
// Used to obtain the raw string name of a variable, type, member, function, macros.
|
|
|
|
# define NAMEOF_RAW(name) ::nameof::detail::NameofRaw(#name, ((sizeof(#name) / sizeof(char)) - 1) + (0 * sizeof(typeid(name))))
|
2018-04-04 12:19:02 +00:00
|
|
|
#endif
|
2018-07-13 15:18:52 +00:00
|
|
|
|
2018-08-02 14:51:44 +00:00
|
|
|
// Used to obtain the simple (unqualified) string name of a variable, member, function.
|
|
|
|
#define NAMEOF(name) ::nameof::Nameof<decltype(name)>(name, #name, (sizeof(#name) / sizeof(char)) - 1, false)
|
|
|
|
|
|
|
|
#define NAMEOF_T(name) ::nameof::Nameof<decltype(name)>(name, #name, (sizeof(#name) / sizeof(char)) - 1, true)
|
|
|
|
|
|
|
|
// Used to obtain the simple (unqualified) string name of a type.
|
|
|
|
#define NAMEOF_TYPE(name) ::nameof::NameofType<decltype(name)>()
|