An open API service indexing awesome lists of open source software.

https://github.com/m-fatah/reflect

A single file header only static reflection library for C++20.
https://github.com/m-fatah/reflect

cplusplus cpp cpp20 gcc header-only msvc no-dependencies reflection single-file static-reflection

Last synced: 28 days ago
JSON representation

A single file header only static reflection library for C++20.

Awesome Lists containing this project

README

          

[![Build status](https://github.com/M-Fatah/reflect/workflows/CI/badge.svg)](https://github.com/M-Fatah/reflect/actions?workflow=CI)

---

## **Introduction:**
Reflect is single file header only library that provides static reflection for C++.

## **API:**
Primitive types, pointers, arrays and enums are supported out of the box.
However automatic generation of reflection info for enums is limited to values between `REFLECT_MIN_ENUM_VALUE` to `REFLECT_MAX_ENUM_VALUE`.

```C++
struct Vector3
{
f32 x, y, z;
};

TYPE_OF(Vector3, x, y, z)

template
struct Foo
{
T t;
};

template
TYPE_OF(Foo, t)

template
struct Bar
{
Foo foo;
R r;
};

template
TYPE_OF((Bar), foo, r)

struct Serializable
{
char a;
bool b;
f32 *c;
const char *d;
void *e;
};

// You can annotate fields with custom annotations that will be stored statically with the type info.
TYPE_OF(Serializable, a, b, c, d, (e, "NoSerialize"))
------------------------------------------------------------------------------
enum Enum
{
A = -3,
B = 8,
c = 7
};

// You can use `type_of();` directly without using `TYPE_OF_ENUM(T, ...)` macro, but it will return type info along enum values in this order {{"A", -3}, {"C", 7}, {"B", 8}}, and will be limited to the range between `REFLECT_MIN_ENUM_VALUE` to `REFLECT_MAX_ENUM_VALUE`.
auto enum_type = type_of();

// However, if `TYPE_OF_ENUM` macro is used, the correct order of definition will be returned.
// {{"A", -3}, {"B", 8}, {"C", 7}}
// And it can work on any enum range.
TYPE_OF_ENUM(Enum, A, B, C)
------------------------------------------------------------------------------
auto t = type_of();
auto t = type_of(T{});
auto n = name_of();
auto k = kind_of();
auto v = value_of(T{});
```

## **Compilers:**
- MSVC: `-std:c++20 -Zc:preprocessor`.
- GCC: `-std=c++2b`.
- Clang (later).

## **Building:**
- Use one of the scripts to build examples and unittest.
- Output is in `build/bin/${CONFIG}/` directory.