Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dynamic reflection #285

Merged
merged 10 commits into from
May 28, 2024
Merged

dynamic reflection #285

merged 10 commits into from
May 28, 2024

Conversation

qicosmos
Copy link
Owner

@qicosmos qicosmos commented May 26, 2024

动态反射,支持根据名称创建对象,根据字段名去设置字段的值,获取所有的字段名等等。

struct pair_t : public iguana::pb_base_impl<pair_t> {
  pair_t() = default;
  pair_t(int a, int b) : x(a), y(b) {}
  int x;
  int y;
};
REFLECTION(pair_t, x, y);

  {
    auto t = iguana::create_instance("pair_t");
    t->set_field_value("x", 12);
    t->set_field_value("y", 24);
    auto &r0 = t->get_field_value<int>("x");
    CHECK(r0 == 12);
    auto &r = t->get_field_value<int>("y");
    CHECK(r == 24);
    
    std::vector<std::string> all_fields_name = t->get_fields_name();

    std::string str;
    t->to_pb(str);

    pair_t t1;
    t1.from_pb(str);

    pair_t *st = dynamic_cast<pair_t *>(t.get());
    CHECK(st->x == t1.x);
    CHECK(st->y == t1.y);
  }

@codecov-commenter
Copy link

Codecov Report

Attention: Patch coverage is 68.57143% with 22 lines in your changes are missing coverage. Please review.

Project coverage is 47.25%. Comparing base (c5e96a7) to head (1a2f7de).
Report is 102 commits behind head on master.

Files Patch % Lines
iguana/reflection.hpp 53.12% 15 Missing ⚠️
iguana/pb_util.hpp 58.82% 7 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #285      +/-   ##
==========================================
+ Coverage   45.19%   47.25%   +2.05%     
==========================================
  Files          46       52       +6     
  Lines        5677     6137     +460     
==========================================
+ Hits         2566     2900     +334     
- Misses       3111     3237     +126     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@qicosmos qicosmos merged commit 90e9f4a into master May 28, 2024
21 checks passed
@qicosmos qicosmos deleted the reflection branch May 28, 2024 06:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants