update readme
This commit is contained in:
parent
532860bc52
commit
9597796775
1 changed files with 31 additions and 41 deletions
72
README.md
72
README.md
|
@ -16,62 +16,52 @@ master |[![Build Status](https://travis-ci.org/Neargye/nameof.svg?branch=master)
|
||||||
Used to obtain the simple name of a variable, type, member, function, macros.
|
Used to obtain the simple name of a variable, type, member, function, macros.
|
||||||
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.
|
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.
|
||||||
|
|
||||||
A nameof macros expression has this form:
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
std::cout << NAMEOF(person.address.zip_code) << std::endl; // prints "zip_code"
|
|
||||||
```
|
|
||||||
|
|
||||||
## Features
|
## Features
|
||||||
|
|
||||||
* Simple name
|
|
||||||
* C++11
|
* C++11
|
||||||
* Header-only
|
* Header-only
|
||||||
* Dependency-free
|
* Dependency-free
|
||||||
* Compile-time
|
* Compile-time
|
||||||
* Compilation check
|
* Compilation check
|
||||||
|
* Name of variable
|
||||||
|
* Name of member
|
||||||
|
* Name of function
|
||||||
|
* Name of emum
|
||||||
|
* Name of macrose
|
||||||
|
* Name of type
|
||||||
|
|
||||||
## [Examples](example/example.cpp)
|
## [Examples](example/example.cpp)
|
||||||
|
|
||||||
* Name of a variable, member or function and etc
|
* A nameof macros expression has this form
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
// Name of variable
|
||||||
NAMEOF(somevar) -> "somevar"
|
NAMEOF(somevar) -> "somevar"
|
||||||
NAMEOF(somevar.somefield) -> "somefield"
|
// Name of member
|
||||||
NAMEOF(SomeMethod) -> "SomeMethod"
|
NAMEOF(person.address.zip_code) -> "zip_code"
|
||||||
```
|
// Name of function
|
||||||
|
NAMEOF(SomeMethod<int, float>) -> "SomeMethod"
|
||||||
* Name of enum
|
NAMEOF_FULL(SomeMethod<int, float>) -> "SomeMethod4<int, float>"
|
||||||
|
// Name of enum
|
||||||
```cpp
|
|
||||||
NAMEOF(SomeEnum::RED) -> "RED"
|
NAMEOF(SomeEnum::RED) -> "RED"
|
||||||
NAMEOF(SomeEnum::GREEN) -> "GREEN"
|
// Name of type
|
||||||
```
|
NAMEOF_TYPE(SomeEnum::RED) -> "SomeEnum"
|
||||||
|
NAMEOF_TYPE_T(int) -> "int"
|
||||||
* Name of type
|
// Name of macros
|
||||||
|
|
||||||
```cpp
|
|
||||||
NAMEOF(volatile const int) -> "volatile const int int"
|
|
||||||
NAMEOF(std::string) -> "string"
|
|
||||||
```
|
|
||||||
|
|
||||||
* Name of macros
|
|
||||||
|
|
||||||
```cpp
|
|
||||||
NAMEOF(__LINE__) -> "__LINE__"
|
NAMEOF(__LINE__) -> "__LINE__"
|
||||||
NAMEOF(__FILE__) -> "__FILE__"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
* Constexpr
|
* Constexpr
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
constexpr auto constexpr_work_fine = NAMEOF(somevar); -> "somevar"
|
constexpr auto cx = NAMEOF(somevar); -> "somevar"
|
||||||
|
static_assert(cx == "somevar", "Wrong name!");
|
||||||
```
|
```
|
||||||
|
|
||||||
* Compilation check
|
* Compilation check
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
NAMEOF(std::stringgg) -> error namespace "std" has no member "stringgg"
|
NAMEOF_TYPE_T(std::stringgg) -> error namespace "std" has no member "stringgg"
|
||||||
```
|
```
|
||||||
|
|
||||||
* Validate parameters
|
* Validate parameters
|
||||||
|
@ -79,7 +69,7 @@ NAMEOF(std::stringgg) -> error namespace "std" has no member "stringgg"
|
||||||
```cpp
|
```cpp
|
||||||
void f(char* s) {
|
void f(char* s) {
|
||||||
if (s == nullptr)
|
if (s == nullptr)
|
||||||
throw std::invalid_argument(NAMEOF(s));
|
throw std::invalid_argument(NAMEOF(s).apend("with type = ").apend(NAMEOF_TYPE(s)));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -93,17 +83,18 @@ void to_json(json& j, const person& p) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void from_json(const json& j, person& p) {
|
void from_json(const json& j, person& p) {
|
||||||
p.name = j.at(NAMEOF(p.name));
|
p.name = j.at(NAMEOF(p.name));
|
||||||
p.address = j.at(NAMEOF(p.address));
|
p.address = j.at(NAMEOF(p.address));
|
||||||
p.age = j.at(NAMEOF(p.age));
|
p.age = j.at(NAMEOF(p.age));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
* Logging
|
* Logging
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
|
template <typename T>
|
||||||
void f() {
|
void f() {
|
||||||
Log(NAMEOF(f), " method entry");
|
Log(NAMEOF(f) + " method entry with T = " + NAMEOF_TYPE_T(T));
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -111,19 +102,18 @@ void f() {
|
||||||
|
|
||||||
* The argument expression identifies a code definition, but it is never evaluated.
|
* The argument expression identifies a code definition, but it is never evaluated.
|
||||||
|
|
||||||
* If you need fully-qualified name, use NAMEOF_FULL().
|
* If you need raw fully-qualified name, use NAMEOF_RAW.
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
NAMEOF_FULL(somevar.somefield) -> "somevar.somefield"
|
NAMEOF_RAW(somevar.somefield) -> "somevar.somefield"
|
||||||
NAMEOF_FULL(&SomeStruct::SomeMethod) -> "&SomeStruct::SomeMethod"
|
NAMEOF_RAW(&SomeStruct::SomeMethod) -> "&SomeStruct::SomeMethod"
|
||||||
NAMEOF_FULL(std::string) -> "std::string"
|
|
||||||
```
|
```
|
||||||
|
|
||||||
* Spaces and Tabs ignored
|
* Spaces and Tabs ignored
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
NAMEOF( std::string ) -> "string"
|
NAMEOF( somevar ) -> "somevar"
|
||||||
NAMEOF( std::string ) -> "string"
|
NAMEOF( somevar ) -> "somevar"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Integration
|
## Integration
|
||||||
|
|
Loading…
Reference in a new issue