Giter Club home page Giter Club logo

usrefl's Introduction


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

repo-size tag license compiler explorer

⭐ Star us on GitHub — it helps!

USRefl

Ubpa Static Reflection

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

Feature

  • header-only, tiny (99 lines) and powerful (USRefl_99.h (MSVC & GCC), USRefl_99_clang.h (Clang))
  • 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

How to use

run it online : compiler explorer

Suppose you need to reflect struct Vec

struct Vec {
  float x;
  float y;
  float norm() const { return std::sqrt(x*x + y*y); }
};

Manual registration

template<>
struct Ubpa::USRefl::TypeInfo<Vec> :
  TypeInfoBase<Vec>
{
  static constexpr AttrList attrs = {};
  static constexpr FieldList fields = {
    Field {TSTR("x")   , &Type::x   },
    Field {TSTR("y")   , &Type::y   },
    Field {TSTR("norm"), &Type::norm},
  };
};

if you use 99-lines version, you need to add a line

static constexpr std::string_view name = "...";

Iterate over members

TypeInfo<Vec>::fields.ForEach([](const auto& field) {
  std::cout << field.name << std::endl;
});

Set/get variables

std::invoke(TypeInfo<Vec>::fields.Find(TSTR("y")).value, v) = 4.f;
std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v) = 3.f;
std::cout
  << "x: "
  << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("x")).value, v)
  << std::endl;

Invoke Methods

std::cout
  << "norm: "
  << std::invoke(TypeInfo<Vec>::fields.Find(TSTR("norm")).value, v)
  << std::endl;

Iterate over variables

TypeInfo<Vec>::ForEachVarOf(v, [](const auto& field, const auto& var) {
  std::cout << field.name << ": " << var << std::endl;
});

other example

Integration

You can choose one of the following two methods

  • method 0: add the required file
  • method 1: cmake install, use find package(USRefl REQUIRED) to get imported target Ubpa::USRefl_core

Compiler compatibility

  • Clang/LLVM >= 10
  • GCC >= 10
  • MSVC >= 1926 (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.

usrefl's People

Contributors

ubpa avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

usrefl's Issues

compile error using VS2019

tried compiling the example, and got these errors, I am using c++lastest :
TryReflCpp.zip

1>------ Build started: Project: TryReflCpp, Configuration: Debug x64 ------
1>TryReflCpp.cpp
1>C:\try\cpp\TryReflCpp\USRefl.h(21,22): error C2760: syntax error: unexpected token ')', expected 'expression'
1>C:\try\cpp\TryReflCpp\USRefl.h(28,36): error C2760: syntax error: unexpected token ')', expected 'expression'
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(8,10): warning C5030: attribute 'size' is not recognized
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(9,7): warning C5030: attribute 'not_serialize' is not recognized
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(11,7): warning C5030: attribute 'info' is not recognized
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(40,30): error C2653: 'Type': is not a class or namespace name
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(40,38): error C2903: 'Type': symbol is neither a class template nor a function template
1>C:\try\cpp\TryReflCpp\TryReflCpp.cpp(40,32): error C2065: 'fields': undeclared identifier
1>Done building project "TryReflCpp.vcxproj" -- FAILED.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.