From 110dd44f589f2e9394c5f2a7790ea3a74f2c4d9c Mon Sep 17 00:00:00 2001 From: Neargye Date: Wed, 11 Apr 2018 00:32:51 +0500 Subject: [PATCH] v0.2.2 --- README.md | 58 +++++++++++++++++++++++---------------------- example/example.cpp | 10 ++++---- include/nameof.hpp | 10 ++++++-- test/test.cpp | 5 ++-- 4 files changed, 47 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 478bc89..fcc4a4f 100644 --- a/README.md +++ b/README.md @@ -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" -} +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 \ No newline at end of file +* GCC +* Clang +* MSVC + +## Licensed under the [MIT License](LICENSE) \ No newline at end of file diff --git a/example/example.cpp b/example/example.cpp index 28ced83..70a603b 100644 --- a/example/example.cpp +++ b/example/example.cpp @@ -1,4 +1,4 @@ -// nameof() c++11 example +// nameof c++ example // // Licensed under the MIT License . // Copyright (c) 2018 Daniil Goncharov . @@ -22,6 +22,7 @@ // SOFTWARE. #include + #include #include @@ -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 diff --git a/include/nameof.hpp b/include/nameof.hpp index 4a5cbf5..b6683ba 100644 --- a/include/nameof.hpp +++ b/include/nameof.hpp @@ -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 . // Copyright (c) 2016, 2018 Daniil Goncharov . diff --git a/test/test.cpp b/test/test.cpp index cf49c00..b1734e7 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -1,4 +1,4 @@ -// nameof() c++11 test +// nameof c++ test // // Licensed under the MIT License . // Copyright (c) 2018 Daniil Goncharov . @@ -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);