• Stars
    star
    255
  • Rank 159,729 (Top 4 %)
  • Language
    C++
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A work in progress minimal C++ static reflection API and codegen tool.

tinyrefl pipeline status

A work in progress minimal C++ static reflection API and codegen tool.

Features

  • Reflection of classes and enums:

    • Class and enum names
    • User declared constructors
    • Public member functions
    • Public Member variables
    • Enumeration values
  • User defined attributes on all reflected entities

  • C++14 static reflection API: Convert enum values to strings, get the set of member functions of a class, check for attributes, visit object members...

    // foo.h
    
    namespace mylib {
    class [[interesting]] Foo {
        std::string str;
        void f() {}
        int i;
    
        enum class Enum
        {
            A, B, C
        };
    };
    }
    
    // main.cpp
    
    #include <tinyrefl/api.hpp>
    #include "foo.h"
    #include "foo.h.tinyrefl"
    
    int main()
    {
        static_assert(tinyrefl::has_attribute<mylib::Foo>("interesting"),
            "Wow, I thought your API was interesting...");
    
        static_assert(tinyrefl::metadata<mylib::Foo::Enum>().get_value(mylib::Foo::Enum::A).underlying_value() == 0,
            "Great, it seems to work");
    
        static_assert(tinyrefl::metadata<mylib::Foo::Enum>().get_value(mylib::Foo::Enum::A).name() == "A",
            "Once upon a time there was a proposal to make static_assert() accept constexpr strings as message");
    
        mylib::Foo foo{"hello", 42};
    
        tinyrefl::visit_object(foo, [](const std::string& name, auto depth, const auto& member, CTTI_STATIC_VALUE(tinyrefl::entity::MEMBER_VARIABLE))
        {
            std::cout << "Foo::" << name << ": " << member << "\n";
        });
    
        using base_classes = typename tinyrefl::metadata<mylib::Foo>::base_classes;
    }

    See the api.cpp example for a full example of usage.

  • API agnostic code generation: The tinyrefl-tool codegen tool included in the project is completely independent from the reflection API, generating macro based C/C++ code to allow users to write their own reflection API:

    #include <boost/fusion/adapted/struct/adapt_struct.hpp>
    
    // Write boost-adapt-struct compatible API:
    #define TINYREFL_STRING(...) # __VA_ARGS__
    #define TINYREFL_VALUE(...) __VA_ARGS__
    #define TINYREFL_SEQUENCE(...) __VA_ARGS__
    #define TINYREFL_TYPE(name, fullname) fullname
    #define TINYREFL_MEMBER(name, fullname, type, pointer) ((typename std::result_of<decltype(pointer), type>::type), (pointer))
    #define TINYREFL_REFLECT_CLASS(type, bases, members, classes, enums) BOOST_FUSION_ADAPT_STRUCT(type, members)
    
    #include "foo.h"
    #include "foo.h.tinyrefl"

    See examples/ for more backend examples.

  • CMake integration: Both the provided reflection API and codegen tool can be integrated in CMake based projects. To enable reflection, the library provides the tinyrefl_tool() function:

    # Include tinyrefl and its tooling
    add_subdirectory(thirdparty/tinyrefl)
    include(${TINYREFL_SOURCE_DIR}/tool/driver.cmake)
    
    # Declare your library
    add_library(mylib foo.h foo.cpp)
    
    # Enable code generation for foo.h of your library
    # The tool will be built and run before building your library
    tinyrefl_tool(TARGET mylib HEADERS foo.h)
  • Cross compilation supported: The CMake setup of tinyrefl detects whether your proyect is being cross compiled with a custom CMake toolchain file, and configures itself accordingly. No changes are needed in the tool integration as shown in the previous example.

Compiler support

The library has been testes against GCC >= 5.x and Clang >= 5.x.y. MSVC is not supported, but I have plans to make it work in the future. See the CI build matrix for details.

Documentation

Docs available in the project wiki.

Acknowledgements

Many thanks to:

  • Jonathan "foonathan" Müller, for his awesome cppast library, feedback, and quick bugfixing.
  • My team lead at Bytech, for letting me integrate this for Qt QML C++ models (More about this soon).

License

This project is released under the MIT license. See LICENSE file for details.

Feedback and bug reporting

Feel free to open an issue in this repository. You can also send a mail to the project desk account incoming+Manu343726/[email protected].

More Repositories

1

ctti

Compile Time Type Information for C++
C++
572
star
2

siplasplas

A library for C++ reflection and introspection
C++
195
star
3

Turbo

C++11 metaprogramming library
C++
110
star
4

snail

Continuation-ready container algorithms from STL algorithms
C++
60
star
5

unittest

C++ unit testing and mocking made easy
C++
55
star
6

Polyop

Overridable universal operator overloading for C++14
C++
20
star
7

gcc-concepts-bugs

A repository to host C++ Concepts Lite TS examples, to help with GCC bug reporting
C++
15
star
8

spacevim

My vim SpaceVim-based config
Vim Script
15
star
9

coroutine

A simple stackful coroutine implementation in C++11
C++
10
star
10

libexecstream

CMake-ready libexecstream library
C++
8
star
11

strings

Examples of string implementation techniques in C++11/14
C++
8
star
12

dotfiles

My personal dotfiles repository
Shell
8
star
13

cppascii

Code snippets, ejemplos, utilidades, etc
C++
7
star
14

clang-conan-packages

conan.io recipes for llvm and clang libraries
Python
7
star
15

meetingcpp2016

Slides and other stuff from my meetingcpp 2016 talk
CMake
5
star
16

cpp-dod-tests

Performance tests for an encapsulation-enabled Data Oriented Design implementation
Assembly
5
star
17

cppcon2019

Materials of CppCon 2019 talk "Next Generation Unit Testing Using Static Reflection"
5
star
18

meetingcpp2018

Slides from "C++, QML, and static reflection" talk at Meeting C++ 2018
4
star
19

tiny-metaprogramming-library

A practical example of high-level metaprogramming as a growing up library
C++
4
star
20

raytracer

My implementation of "Raytracing In One Weekend" by Peter Shirley
C++
3
star
21

Cpp11CustomLogClass

C++11 custom Log class. Code example of a response to stackoverflow question: http://stackoverflow.com/questions/17595957/operator-overloading-in-c-for-logging-purposes/
Shell
3
star
22

traces

Type safe Tracy API
C++
3
star
23

telegram-reactive

Example C++ telegram bot using a reactive API
C++
3
star
24

rainbow

C++
2
star
25

uftrace-docker

Docker image for uftracer C/C++ profiler
Dockerfile
2
star
26

C--

The sandbox where we show our increasingly depressing C++ skills
C++
2
star
27

ProyectoOmegauhm

Web de consulta y recursos centralizados para los alumnos de FDI
CSS
2
star
28

TemplateMetaprogramming101

Scripts, utilities, and examples for my Modern C++ Template Metaprogramming workshops
C++
1
star
29

worm

C++
1
star
30

TTL

C++11 Tuple Template Library
C++
1
star
31

xml

The neXt Metaprogramming Library
1
star
32

WALLE

Prácticas de Tecnología de la Programación
Java
1
star
33

PracticasIA

Java
1
star
34

efsw

conan.io package for the Entropia Filesystem Watcher library
Python
1
star
35

cmake

CMake
1
star
36

biicode-posts

C
1
star
37

cucaracha

An emulator for a CPU of my own
Go
1
star
38

TerminalWorkspace

My personal zsh, tmux, and vim configurations. No guarantees, works for me at least.
Shell
1
star