All Projects → Ubpa → Usrefl

Ubpa / Usrefl

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

Programming Languages

cpp
1120 projects
reflection
70 projects

Projects that are alternatives of or similar to Usrefl

perses
Language-agnostic program reducer.
Stars: ✭ 57 (-80.14%)
Mutual labels:  gcc, clang
rebuild
Zero-dependency, reproducible build environments
Stars: ✭ 48 (-83.28%)
Mutual labels:  gcc, clang
C-Cpp-Coverage-for-CLion
Get coverage data in CLion using gcov or llvm-cov
Stars: ✭ 37 (-87.11%)
Mutual labels:  gcc, clang
Sol2
Sol3 (sol2 v3.0) - a C++ <-> Lua API wrapper with advanced features and top notch performance - is here, and it's great! Documentation:
Stars: ✭ 2,791 (+872.47%)
Mutual labels:  clang, gcc
cpp-compiler-options
Compilation options for different versions of Clang, GCC and MSVC. Provided a generator and different file formats (cmake, xmake, meson, premake5, bjam/b2, ...)
Stars: ✭ 19 (-93.38%)
Mutual labels:  gcc, clang
xcross
"Zero Setup" cross-compilation for C/C++. Supports numerous architectures, build systems, C standard libraries, vcpkg, and Conan.
Stars: ✭ 29 (-89.9%)
Mutual labels:  gcc, clang
c-compiler-security
Security-related flags and options for C compilers
Stars: ✭ 125 (-56.45%)
Mutual labels:  gcc, clang
Libosmscout
Libosmscout is a C++ library for offline map rendering, routing and location lookup based on OpenStreetMap data
Stars: ✭ 159 (-44.6%)
Mutual labels:  clang, gcc
lldbg
A lightweight native GUI for LLDB.
Stars: ✭ 83 (-71.08%)
Mutual labels:  gcc, clang
cdetect
🔬 Detect which compiler and compiler version a Linux executable (in the ELF format) was compiled with
Stars: ✭ 23 (-91.99%)
Mutual labels:  gcc, clang
Cmake Scripts
A selection of useful scripts for use in CMake projects, include code coverage, sanitizers, and dependency graph generation.
Stars: ✭ 202 (-29.62%)
Mutual labels:  clang, gcc
rooki
A stupid simple script runner supporting c, c++, rust, haskell and virtually anything
Stars: ✭ 26 (-90.94%)
Mutual labels:  gcc, clang
Fixed point
C++ Binary Fixed-Point Arithmetic
Stars: ✭ 199 (-30.66%)
Mutual labels:  clang, gcc
FrameOfReference
C++ library to pack and unpack vectors of integers having a small range of values using a technique called Frame of Reference
Stars: ✭ 36 (-87.46%)
Mutual labels:  gcc, clang
Polymcu
An open framework for micro-controller software
Stars: ✭ 173 (-39.72%)
Mutual labels:  clang, gcc
minilib
A c standard system library with a focus on size, headeronly, "singlefile", intended for static linking. 187 Bytes for "Hello World"(regular elf), compiled with the standard gcc toolchain.
Stars: ✭ 29 (-89.9%)
Mutual labels:  gcc, clang
Arm Cmake Toolchains
CMake toolchain configurations for ARM
Stars: ✭ 148 (-48.43%)
Mutual labels:  clang, gcc
C
Compile and execute C "scripts" in one go!
Stars: ✭ 1,920 (+568.99%)
Mutual labels:  clang, gcc
ci playground
Playground for Cloud CI development for C++
Stars: ✭ 23 (-91.99%)
Mutual labels:  gcc, clang
hmg
💝 My personal Gentoo/Linux configuration backup files
Stars: ✭ 16 (-94.43%)
Mutual labels:  gcc, clang

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

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].