Skip to content

Commit

Permalink
Merge pull request #293 from qicosmos/field_name
Browse files Browse the repository at this point in the history
  • Loading branch information
qicosmos authored Jul 9, 2024
2 parents 3795450 + f48fe36 commit 53becba
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 8 deletions.
6 changes: 6 additions & 0 deletions iguana/enum_reflection.hpp → iguana/field_reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ inline constexpr std::string_view enum_string() {
return s1;
}

template <auto field>
inline constexpr std::string_view field_string() {
constexpr std::string_view raw_name = enum_string<field>();
return raw_name.substr(raw_name.rfind(":") + 1);
}

#if defined(__clang__) && (__clang_major__ >= 17)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wenum-constexpr-conversion"
Expand Down
2 changes: 1 addition & 1 deletion iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

#include "detail/string_stream.hpp"
#include "detail/traits.hpp"
#include "enum_reflection.hpp"
#include "field_reflection.hpp"
#include "frozen/string.h"
#include "frozen/unordered_map.h"

Expand Down
2 changes: 1 addition & 1 deletion iguana/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
#include "define.h"
#include "detail/charconv.h"
#include "detail/utf.hpp"
#include "enum_reflection.hpp"
#include "error_code.h"
#include "field_reflection.hpp"
#include "reflection.hpp"

namespace iguana {
Expand Down
34 changes: 28 additions & 6 deletions test/test_util.cpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
#include <iguana/enum_reflection.hpp>
#include <iguana/field_reflection.hpp>
#include <iostream>

#define DOCTEST_CONFIG_IMPLEMENT
#include "doctest.h"

#if defined(__clang__) || defined(_MSC_VER) || \
#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
class message {};

namespace ns {
class message {};
} // namespace ns
} // namespace ns

namespace ns::ns2 {
class message {};
} // namespace ns::ns2
} // namespace ns::ns2

TEST_CASE("test type string") {
static_assert(iguana::type_string<message>() == "message");
Expand All @@ -41,12 +41,12 @@ enum Size { small, large };
namespace ns {
enum class Color { red, black };
enum Size { small, large };
} // namespace ns
} // namespace ns

namespace ns::ns2 {
enum class Color { red, black };
enum Size { small, large };
} // namespace ns::ns2
} // namespace ns::ns2

TEST_CASE("test enum string") {
static_assert(iguana::enum_string<Color::red>() == "Color::red");
Expand Down Expand Up @@ -83,6 +83,28 @@ TEST_CASE("test enum string") {
std::cout << s5 << "\n";
CHECK(s5 == "ns::ns2::large");
}

struct sub {
int id;
};

struct person {
ns::ns2::Color color;
int id;
sub s;
std::string str;
};

TEST_CASE("test field string") {
constexpr auto field_name1 = iguana::field_string<&person::color>();
constexpr auto field_name2 = iguana::field_string<&person::id>();
constexpr auto field_name3 = iguana::field_string<&person::s>();
constexpr auto field_name4 = iguana::field_string<&person::str>();
CHECK(field_name1 == "color");
CHECK(field_name2 == "id");
CHECK(field_name3 == "s");
CHECK(field_name4 == "str");
}
#endif

DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4007)
Expand Down

0 comments on commit 53becba

Please sign in to comment.