From 7f68aa68ae5324f104e2c4df9af3b9e0905e8988 Mon Sep 17 00:00:00 2001 From: Terik23 Date: Tue, 27 Mar 2018 19:00:06 +0500 Subject: [PATCH] add new use cases --- README.md | 23 +++++++++++++++++++++++ example/example.cpp | 3 +++ test/test.cpp | 3 +++ 3 files changed, 29 insertions(+) diff --git a/README.md b/README.md index 67fc36d..c7ef739 100644 --- a/README.md +++ b/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 diff --git a/example/example.cpp b/example/example.cpp index a38e314..82b5f6e 100644 --- a/example/example.cpp +++ b/example/example.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 diff --git a/test/test.cpp b/test/test.cpp index afefca8..a7a1246 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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);