Skip to content

Commit

Permalink
1. add const to_* but to_pb
Browse files Browse the repository at this point in the history
2. using auto const& in range for
  • Loading branch information
171930433 committed Jun 28, 2024
1 parent e27f437 commit b3a4497
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 19 deletions.
33 changes: 18 additions & 15 deletions iguana/dynamic.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ struct base_impl : public base {
}
}

void to_json(std::string& str) override {
void to_json(std::string& str) const override {
if constexpr (ENABLE_FLAG & ENABLE_JSON) {
to_json_adl((iguana_adl_t*)nullptr, *(static_cast<T*>(this)), str);
to_json_adl((iguana_adl_t*)nullptr, *(static_cast<T const*>(this)), str);
}
else {
throw std::runtime_error("Json Disabled");
Expand All @@ -53,9 +53,9 @@ struct base_impl : public base {
}
}

void to_xml(std::string& str) override {
void to_xml(std::string& str) const override {
if constexpr (ENABLE_FLAG & ENABLE_XML) {
to_xml_adl((iguana_adl_t*)nullptr, *(static_cast<T*>(this)), str);
to_xml_adl((iguana_adl_t*)nullptr, *(static_cast<T const*>(this)), str);
}
else {
throw std::runtime_error("Xml Disabled");
Expand All @@ -71,9 +71,9 @@ struct base_impl : public base {
}
}

void to_yaml(std::string& str) override {
void to_yaml(std::string& str) const override {
if constexpr (ENABLE_FLAG & ENABLE_YAML) {
to_yaml_adl((iguana_adl_t*)nullptr, *(static_cast<T*>(this)), str);
to_yaml_adl((iguana_adl_t*)nullptr, *(static_cast<T const*>(this)), str);
}
else {
throw std::runtime_error("Yaml Disabled");
Expand All @@ -89,18 +89,20 @@ struct base_impl : public base {
}
}

iguana::detail::field_info get_field_info(std::string_view name) override {
iguana::detail::field_info get_field_info(
std::string_view name) const override {
static constexpr auto map = iguana::get_members<T>();
iguana::detail::field_info info{};
for (auto& [no, field] : map) {
for (auto const& [no, field] : map) {
if (info.offset > 0) {
break;
}
std::visit(
[&](auto val) {
[&](auto const& val) {
if (val.field_name == name) {
info.offset = member_offset((T*)this, val.member_ptr);
using value_type = typename decltype(val)::value_type;
using value_type =
typename std::remove_reference_t<decltype(val)>::value_type;
#if defined(__clang__) || defined(_MSC_VER) || \
(defined(__GNUC__) && __GNUC__ > 8)
info.type_name = type_string<value_type>();
Expand Down Expand Up @@ -136,17 +138,18 @@ struct base_impl : public base {
static constexpr auto map = iguana::get_members<T>();
std::any result;

for (auto [no, field] : map) {
for (auto const& [no, field] : map) {
if (result.has_value()) {
break;
}
std::visit(
[&](auto val) {
[&](auto const& val) {
if (val.field_name == name) {
using value_type = typename decltype(val)::value_type;
using value_type =
typename std::remove_reference_t<decltype(val)>::value_type;
auto const offset = member_offset((T*)this, val.member_ptr);
auto ptr = (((char*)this) + offset);
result = {*((value_type*)ptr)};
auto ptr = (char*)this + offset;
result = *((value_type*)ptr);
}
},
field);
Expand Down
9 changes: 5 additions & 4 deletions iguana/reflection.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,15 +564,16 @@ struct field_info {
struct base {
virtual void to_pb(std::string &str) {}
virtual void from_pb(std::string_view str) {}
virtual void to_xml(std::string &str) {}
virtual void to_xml(std::string &str) const {}
virtual void from_xml(std::string_view str) {}
virtual void to_json(std::string &str) {}
virtual void to_json(std::string &str) const {}
virtual void from_json(std::string_view str) {}
virtual void to_yaml(std::string &str) {}
virtual void to_yaml(std::string &str) const {}
virtual void from_yaml(std::string_view str) {}
virtual std::vector<std::string_view> get_fields_name() const { return {}; }
virtual std::any get_field_any(std::string_view name) const { return {}; }
virtual iguana::detail::field_info get_field_info(std::string_view name) {
virtual iguana::detail::field_info get_field_info(
std::string_view name) const {
return {};
}

Expand Down
25 changes: 25 additions & 0 deletions test/test_pb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,18 @@ struct numer_st PUBLIC(numer_st) {
};
REFLECTION(numer_st, a, b, c);

struct MyPerson : public iguana::base_impl<MyPerson, iguana::ENABLE_JSON> {
MyPerson() = default;
MyPerson(std::string s, int d) : name(s), age(d) {}
std::string name;
int64_t age;
bool operator==(const MyPerson &other) const {
return name == other.name && age == other.age;
}
};

REFLECTION(MyPerson, name, age);

TEST_CASE("test reflection") {
{
auto t = iguana::create_instance("nest1");
Expand Down Expand Up @@ -286,6 +298,19 @@ TEST_CASE("test reflection") {
std::any_cast<std::variant<int, double>>(mvariant_any);
assert(mvariant == temp_variant);
}
{
// to_json is an const member_function now
MyPerson const p1{"xiaoming", 10};
std::string str;
p1.to_json(str);

// p1.to_pb(str); // compile failed

MyPerson p2;
p2.from_json(str);

assert(p1 == p2);
}
{
auto t = iguana::create_instance("pair_t");
t->set_field_value("x", 12);
Expand Down

0 comments on commit b3a4497

Please sign in to comment.