add new use cases

This commit is contained in:
Terik23 2018-03-27 19:00:06 +05:00
parent cb1644b16c
commit 7f68aa68ae
3 changed files with 29 additions and 0 deletions

View file

@ -42,6 +42,13 @@ NAMEOF_VAR(someVar) -> "someVar"
NAMEOF_VAR(someVar.SomeField) -> "SomeField" NAMEOF_VAR(someVar.SomeField) -> "SomeField"
``` ```
* Name of enum
```cpp
NAMEOF(SomeEnum::RED) -> "RED"
NAMEOF(SomeEnum::GREEN) -> "GREEN"
```
* Name of type * Name of type
```cpp ```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 * Logging
```cpp ```cpp

View file

@ -45,6 +45,8 @@ struct Long {
int someVar = 0; int someVar = 0;
enum class Color { RED, GREEN, BLUE };
void TestCase1() { void TestCase1() {
SomeStruct someVar{1}; SomeStruct someVar{1};
Long otherVar{2}; Long otherVar{2};
@ -56,6 +58,7 @@ void TestCase1() {
std::cout << constexpr_work_fine << std::endl; // intValue std::cout << constexpr_work_fine << std::endl; // intValue
std::cout << NAMEOF(someVar) << std::endl; // someVar 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)->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

View file

@ -49,6 +49,8 @@ struct Long {
int someVar = 0; int someVar = 0;
enum class Color { RED, GREEN, BLUE };
TEST_CASE("NAMEOF") { TEST_CASE("NAMEOF") {
SomeStruct someVar{1}; SomeStruct someVar{1};
Long otherVar{2}; Long otherVar{2};
@ -63,6 +65,7 @@ TEST_CASE("NAMEOF") {
SECTION("NAMEOF") { SECTION("NAMEOF") {
REQUIRE(std::strcmp(NAMEOF(someVar), "someVar") == 0); 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)->SomeField), "SomeField") == 0); REQUIRE(std::strcmp(NAMEOF((&someVar)->SomeField), "SomeField") == 0);
REQUIRE(std::strcmp(NAMEOF(::someVar), "someVar") == 0); REQUIRE(std::strcmp(NAMEOF(::someVar), "someVar") == 0);