Skip to content

HustLion/USRefl

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

USRefl

Ubpa Static Reflection

repo-size tag license

Feature

  • the best C++ static reflection libliray in the whole world
  • tiny, handy, elegant
  • basic
    • (non-static / static) member variable
    • (non-static / static) member function
  • attribute
  • enum
    • string <-> key
    • static dispatch
  • template
  • inheritance
    • inherit field (member variable, member function)
    • iterate bases recursively

Example

#include <USRefl.h>
#include <iostream>
using namespace Ubpa::USRefl;
using namespace std;

struct [[size(8)]] Point {
  [[not_serialize]]
  float x;
  [[info("hello")]]
  float y;
};

template<>
struct Type<Point> {
  static constexpr std::string_view name = "Point";
  using type = Point;

  static constexpr FieldList fields = {
    Field{"x", &Point::x, AttrList{ Attr{ "not_serialize" } }},
    Field{"y", &Point::y, AttrList{ Attr{ "info", "hello" } }}
  };

  static constexpr AttrList attrs = {
    Attr{ "size", 8 }
  };
};

int main() {
  Point p{ 1,2 };

  Type<Point>::fields.ForEach([](auto field) {
    cout << field.name << endl;
    field.attrs.ForEach([](auto attr) {
      cout << attr.name << endl;
      if constexpr (attr.has_value)
        cout << ": " << attr.value << endl;
    });
  });

  static_assert(Type<Point>::fields.Contains("x"));

  Type<Point>::attrs.ForEach([](auto attr) {
    cout << "name   : " << attr.name << endl;
    if constexpr (!attr.has_value)
      cout << "value : " << attr.value << endl;
  });

  ForEachVarOf(p, [](auto&& var) {
    cout << var << endl;
  });
}

other example

About

Ubpa static reflection

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • C++ 93.4%
  • CMake 6.6%