This commit is contained in:
Neargye 2018-04-11 00:32:51 +05:00 committed by terik23
parent bb8cf28a6d
commit 110dd44f58
4 changed files with 47 additions and 36 deletions

View file

@ -1,11 +1,20 @@
# nameof() c++11
# nameof c++
C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) operator in [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)).
```text
_ _ __ _____
| \ | | / _| / ____|_ _
| \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
| . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
| |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
|_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
```
Branch | Linux/OSX | Windows
-------|-----------|---------
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)
C++ alternative to [nameof](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/nameof) operator in [C#](https://en.wikipedia.org/wiki/C_Sharp_(programming_language)).
Used to obtain the simple name of a variable, type, member or function and etc.
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,16 +33,14 @@ std::cout << NAMEOF(person.address.zip_code) << std::endl; // prints "zip_code"
* Compile-time
* Compilation check
## [Example & Key Use Cases](https://github.com/Neargye/nameof/blob/master/example/example.cpp)
## [Example](example/example.cpp) & Key Use Cases
* Name of a variable, member or function and etc
```cpp
NAMEOF(someVar) -> "someVar"
NAMEOF(someVar.SomeField) -> "SomeField"
NAMEOF(someVar.SomeMethod1()) -> "SomeMethod1()"
NAMEOF(&SomeStruct::SomeMethod2) -> "SomeMethod2"
NAMEOF(somevar) -> "somevar"
NAMEOF(somevar.somefield) -> "somefield"
NAMEOF(SomeMethod) -> "SomeMethod"
```
* Name of enum
@ -46,28 +53,20 @@ NAMEOF(SomeEnum::GREEN) -> "GREEN"
* Name of type
```cpp
NAMEOF(int[]) -> "int[]"
NAMEOF(volatile const int) -> "volatile const int int"
NAMEOF(std::string) -> "string"
```
* Constexpr
```cpp
void f() {
int i;
constexpr auto constexpr_work_fine = NAMEOF(i); -> "i"
}
constexpr auto constexpr_work_fine = NAMEOF(somevar); -> "somevar"
```
* Compilation check
```cpp
void f() {
int i;
NAMEOF(i); -> "i"
NAMEOF(iii); -> error identifier "iii" is undefined
NAMEOF(std::stringgg) -> error namespace "std" has no member "stringgg"
}
```
* Validate parameters
@ -105,21 +104,24 @@ void f() {
## Remarks
* The argument expression identifies a code definition, but it is never evaluated.
* If you need to get the fully-qualified name, you could use the NAMEOF_FULL().
```cpp
NAMEOF_FULL(someVar.SomeField) -> "someVar.SomeField"
NAMEOF_FULL(&SomeStruct::SomeMethod2) -> "&SomeStruct::SomeMethod2"
NAMEOF_FULL(somevar.somefield) -> "somevar.somefield"
NAMEOF_FULL(&SomeStruct::SomeMethod) -> "&SomeStruct::SomeMethod"
NAMEOF_FULL(std::string) -> "std::string"
```
* By default nameof work with RTTI, but can work without RTTI. If compiling without RTTI, you need use the NAMEOF_TYPE() for get name of type.
## Integration
```cpp
NAMEOF_TYPE(int[]) -> "int[]"
NAMEOF_TYPE(std::string) -> "string"
You need to add the single required file [nameof.hpp](include/nameof.hpp), and the necessary switches to enable C++11.
NAMEOF_TYPE_FULL(std::string) -> "std::string"
```
## Compiler compatibility
## License MIT
* GCC
* Clang
* MSVC
## Licensed under the [MIT License](LICENSE)

View file

@ -1,4 +1,4 @@
// nameof() c++11 example
// nameof c++ example
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2018 Daniil Goncharov <neargye@gmail.com>.
@ -22,6 +22,7 @@
// SOFTWARE.
#include <nameof.hpp>
#include <iostream>
#include <stdexcept>
@ -65,11 +66,12 @@ void TestCase1() {
std::cout << NAMEOF(Color::RED) << std::endl; // RED
std::cout << NAMEOF(somevar) << std::endl; // somevar
std::cout << NAMEOF(somevar.somefield) << std::endl; // somefield
std::cout << NAMEOF((&somevar)->somefield) << std::endl; // somefield
std::cout << NAMEOF(::somevar) << std::endl; // somevar
std::cout << NAMEOF(&somevar) << std::endl; // somevar
std::cout << NAMEOF(somevar.somefield) << std::endl; // somefield
std::cout << NAMEOF((&somevar)->somefield) << std::endl; // somefield
std::cout << NAMEOF(othervar.ll.field) << std::endl; // field
std::cout << NAMEOF(ptrvar) << std::endl; // ptrvar
@ -90,7 +92,7 @@ void TestCase1() {
std::cout << NAMEOF(int[]) << std::endl; // int[]
std::cout << NAMEOF(SomeStruct) << std::endl; // SomeStruct
std::cout << NAMEOF(Long::LL) << std::endl; // LL
std::cout << NAMEOF(volatile const int) << std::endl; // const volatile int
std::cout << NAMEOF(volatile const int) << std::endl; // volatile const int
std::cout << NAMEOF_FULL(somevar.somefield) << std::endl; // somevar.somefield
std::cout << NAMEOF_FULL(&SomeStruct::SomeMethod2) << std::endl; // &SomeStruct::SomeMethod2

View file

@ -1,5 +1,11 @@
// nameof() c++11 https://github.com/Neargye/nameof
// Vesion 0.2.1
// _ _ __ _____
// | \ | | / _| / ____|_ _
// | \| | __ _ _ __ ___ ___ ___ | |_ | | _| |_ _| |_
// | . ` |/ _` | '_ ` _ \ / _ \/ _ \| _| | | |_ _|_ _|
// | |\ | (_| | | | | | | __/ (_) | | | |____|_| |_|
// |_| \_|\__,_|_| |_| |_|\___|\___/|_| \_____|
// https://github.com/Neargye/nameof
// vesion 0.2.2
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2016, 2018 Daniil Goncharov <neargye@gmail.com>.

View file

@ -1,4 +1,4 @@
// nameof() c++11 test
// nameof c++ test
//
// Licensed under the MIT License <http://opensource.org/licenses/MIT>.
// Copyright (c) 2018 Daniil Goncharov <neargye@gmail.com>.
@ -91,9 +91,10 @@ TEST_CASE("NAMEOF") {
SECTION("NAMEOF_VARIABLE") {
REQUIRE(std::strcmp(NAMEOF(somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF(&somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF(::somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF(somevar.somefield), "somefield") == 0);
REQUIRE(std::strcmp(NAMEOF((&somevar)->somefield), "somefield") == 0);
REQUIRE(std::strcmp(NAMEOF(::somevar), "somevar") == 0);
REQUIRE(std::strcmp(NAMEOF(othervar.ll.field), "field") == 0);