/USRefl

Header-only, tiny (99 lines) and powerful C++17 static reflection library.

Primary LanguageC++MIT LicenseMIT


 __    __       _______..______       _______  _______  __      
|  |  |  |     /       ||   _  \     |   ____||   ____||  |     
|  |  |  |    |   (----`|  |_)  |    |  |__   |  |__   |  |     
|  |  |  |     \   \    |      /     |   __|  |   __|  |  |     
|  `--'  | .----)   |   |  |\  \----.|  |____ |  |     |  `----.
 \______/  |_______/    | _| `._____||_______||__|     |_______|
                                                                

repo-size tag license compiler explorer

⭐ Star us on GitHub — it helps!

USRefl

Ubpa Static Reflection

Header-only, tiny (99 lines) and powerful C++17 static reflection library.

Feature

  • header-only, tiny (99 lines) and powerful (USRefl_99.h)
  • noninvasive
  • basic
    • (non-static / static) member variable
    • (non-static / static) member function
  • attribute
  • enum
    • string <-> enumerator
    • static dispatch
  • template
  • inheritance
    • diamond inheritance
    • iterate bases recursively
    • virtual inheritance
  • parser

Example

run it online : compiler explorer

#include <USRefl/USRefl.h>
#include <iostream>

struct Point {
  float x;
  float y;
};

template<>
struct Ubpa::USRefl::TypeInfo<Point> : TypeInfoBase<Point> {
#ifdef UBPA_USREFL_NOT_USE_NAMEOF
  static constexpr char name[6] = "Point";
#endif
  static constexpr AttrList attrs = {};
  static constexpr FieldList fields = {
    Field {TSTR("x"), &Type::x},
    Field {TSTR("y"), &Type::y},
  };
};

int main() {
  Point p{ 1.f, 2.f };
  Ubpa::USRefl::TypeInfo<Point>::ForEachVarOf(p, [](auto field, auto&& var) {
    std::cout << field.name << ": " << var << std::endl;
  });
}

result is

x : 1
y : 2

other example

Documentation

Integration

You can choose one of the following three methods

  • method 0: add required file USRefl_99.h
  • method 1: directly use include/USRefl/
  • method 2: cmake install, find package(USRefl REQUIRED) to get imported target Ubpa::USRefl_core

Compiler compatibility

  • Clang/LLVM >= 6
  • GCC >= 7.3
  • MSVC++ >= 19.20 / VS 2019 + (not fully support virtual inheritance because of a MSVC++ bug)

Licensing

You can copy and paste the license summary from below.

MIT License

Copyright (c) 2020 Ubpa

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.