add new use cases
This commit is contained in:
parent
cb1644b16c
commit
7f68aa68ae
3 changed files with 29 additions and 0 deletions
23
README.md
23
README.md
|
@ -42,6 +42,13 @@ NAMEOF_VAR(someVar) -> "someVar"
|
|||
NAMEOF_VAR(someVar.SomeField) -> "SomeField"
|
||||
```
|
||||
|
||||
* Name of enum
|
||||
|
||||
```cpp
|
||||
NAMEOF(SomeEnum::RED) -> "RED"
|
||||
NAMEOF(SomeEnum::GREEN) -> "GREEN"
|
||||
```
|
||||
|
||||
* Name of type
|
||||
|
||||
```cpp
|
||||
|
@ -79,6 +86,22 @@ void f(char* s) {
|
|||
}
|
||||
```
|
||||
|
||||
* Serialization, for example json:
|
||||
|
||||
```cpp
|
||||
void to_json(json& j, const person& p) {
|
||||
j = json{{NAMEOF(p.name), p.name},
|
||||
{NAMEOF(p.address), p.address},
|
||||
{NAMEOF(p.age), p.age}};
|
||||
}
|
||||
|
||||
void from_json(const json& j, person& p) {
|
||||
p.name = j.at(NAMEOF(p.name));
|
||||
p.address = j.at(NAMEOF(p.address));
|
||||
p.age = j.at(NAMEOF(p.age));
|
||||
}
|
||||
```
|
||||
|
||||
* Logging
|
||||
|
||||
```cpp
|
||||
|
|
|
@ -45,6 +45,8 @@ struct Long {
|
|||
|
||||
int someVar = 0;
|
||||
|
||||
enum class Color { RED, GREEN, BLUE };
|
||||
|
||||
void TestCase1() {
|
||||
SomeStruct someVar{1};
|
||||
Long otherVar{2};
|
||||
|
@ -56,6 +58,7 @@ void TestCase1() {
|
|||
std::cout << constexpr_work_fine << std::endl; // intValue
|
||||
|
||||
std::cout << NAMEOF(someVar) << std::endl; // someVar
|
||||
std::cout << NAMEOF(Color::RED) << std::endl; // RED
|
||||
std::cout << NAMEOF(someVar.SomeField) << std::endl; // SomeField
|
||||
std::cout << NAMEOF((&someVar)->SomeField) << std::endl; // SomeField
|
||||
std::cout << NAMEOF(::someVar) << std::endl; // someVar
|
||||
|
|
|
@ -49,6 +49,8 @@ struct Long {
|
|||
|
||||
int someVar = 0;
|
||||
|
||||
enum class Color { RED, GREEN, BLUE };
|
||||
|
||||
TEST_CASE("NAMEOF") {
|
||||
SomeStruct someVar{1};
|
||||
Long otherVar{2};
|
||||
|
@ -63,6 +65,7 @@ TEST_CASE("NAMEOF") {
|
|||
|
||||
SECTION("NAMEOF") {
|
||||
REQUIRE(std::strcmp(NAMEOF(someVar), "someVar") == 0);
|
||||
REQUIRE(std::strcmp(NAMEOF(Color::RED), "RED") == 0);
|
||||
REQUIRE(std::strcmp(NAMEOF(someVar.SomeField), "SomeField") == 0);
|
||||
REQUIRE(std::strcmp(NAMEOF((&someVar)->SomeField), "SomeField") == 0);
|
||||
REQUIRE(std::strcmp(NAMEOF(::someVar), "someVar") == 0);
|
||||
|
|
Loading…
Reference in a new issue