This commit is contained in:
Terik23 2018-04-02 16:49:15 +05:00 committed by Neargye
parent 348a90a566
commit f6f479d7d1
5 changed files with 19 additions and 17 deletions

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2016 - 2018 Daniil Goncharov
Copyright (c) 2016, 2018 Daniil Goncharov
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -4,7 +4,7 @@ C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/langu
Linux/OSX | Windows
-----------|---------
[![Build Status](https://travis-ci.org/Terik23/nameof.svg?branch=master)](https://travis-ci.org/Terik23/nameof)|[![Build status](https://ci.appveyor.com/api/projects/status/4tyl8lee10ckw10k/branch/master?svg=true)](https://ci.appveyor.com/project/Terik23/nameof/branch/master)
[![Build Status](https://travis-ci.org/Neargye/nameof.svg?branch=master)](https://travis-ci.org/Neargye/nameof)|[![Build status](https://ci.appveyor.com/api/projects/status/yq5fk0d9mwljbubt/branch/master?svg=true)](https://ci.appveyor.com/project/Neargye/nameof/branch/master)
Used to obtain the simple name of a variable, type, function.
Before, you had to use string literals to refer to definitions, which is brittle when renaming code elements because tools do not know to check these string literals.
@ -24,7 +24,7 @@ std::cout << NAMEOF(person.address.zip_code) << std::endl; // prints "zip_code"
* Compile-time
* Compilation check
## [Example & Key Use Cases](https://github.com/Terik23/nameof/blob/master/example/example.cpp)
## [Example & Key Use Cases](https://github.com/Neargye/nameof/blob/master/example/example.cpp)
* Name of a variable, function and etc

View file

@ -80,6 +80,7 @@ void TestCase1() {
std::cout << NAMEOF_TYPE(int[]) << std::endl; // int[]
std::cout << NAMEOF_TYPE(SomeStruct) << std::endl; // SomeStruct
std::cout << NAMEOF_TYPE(Long::LL) << std::endl; // LL
std::cout << NAMEOF_TYPE(const volatile int) << std::endl; // const volatile int
std::cout << NAMEOF_FULL(someVar.SomeField) << std::endl; // someVar.SomeField
std::cout << NAMEOF_FULL(&SomeStruct::SomeMethod2) << std::endl; // &SomeStruct::SomeMethod2

View file

@ -1,8 +1,8 @@
// nameof() c++11 https://github.com/Terik23/nameof
// Vesion 0.1.4
// nameof() c++11 https://github.com/Neargye/nameof
// Vesion 0.1.5
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2016 - 2018 Daniil Goncharov <neargye@gmail.com>.
// Copyright (c) 2016, 2018 Daniil Goncharov <neargye@gmail.com>.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
@ -27,14 +27,13 @@
namespace nameof {
template <typename T, size_t N>
inline constexpr const char* Nameof(const char(&name)[N], const size_t length = N) {
return length == 0 ? name
: (name[length - 1] == ' ' || name[length - 1] == '.' ||
name[length - 1] == '>' || name[length - 1] == ':' ||
name[length - 1] == '&' || name[length - 1] == '*' ||
name[length - 1] == '+' || name[length - 1] == '~' ||
name[length - 1] == '-' || name[length - 1] == '!')
inline constexpr bool IsLexeme(const char s) {
return (s == '.' || s == '>' || s == ':' || s == '&' || s == '*' ||
s == '+' || s == '~' || s == '-' || s == '!');
}
template <typename T, std::size_t N>
inline constexpr const char* Nameof(const char(&name)[N], const std::size_t length = N) {
return length == 0 ? name : IsLexeme(name[length - 1])
? &name[length]
: Nameof<T>(name, length - 1);
}

View file

@ -116,6 +116,7 @@ TEST_CASE("NAMEOF_TYPE") {
SECTION("NAMEOF_TYPE") {
REQUIRE(std::strcmp(NAMEOF_TYPE(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(const volatile int[]), "const volatile int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(std::string), "string") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE(Long::LL), "LL") == 0);
@ -163,7 +164,8 @@ TEST_CASE("NAMEOF_FULL") {
TEST_CASE("NAMEOF_TYPE_FULL") {
SECTION("NAMEOF_TYPE_FULL") {
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int[]), "int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(int), "int") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(const volatile int[]), "const volatile int[]") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(Long::LL), "Long::LL") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(std::string), "std::string") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(SomeStruct), "SomeStruct") == 0);
REQUIRE(std::strcmp(NAMEOF_TYPE_FULL(Long::LL), "Long::LL") == 0);