siplasplas Build Status Build status Documentation GitHub issues Open

A library for C++ reflection and introspection

Features

Reflection metadata

All reflection metadata is processed by DRLParser, a python script that takes input about a proejct (Compilation options, include dirs, etc) and scans the project headers, generating C++ header files with the reflection information of the corresponding input header. All generated code is C++11 compatible.

CMake integration

Users should not worry about DRLParser and its input, a set of cmake scripts is given to simplify reflection in user projects. Just include siplasplas.cmake and invoke configure_siplasplas_reflection() with your target:

add_library(MyLibrary myLib.cpp)

target_include_directories(MyLibrary PUBLIC include/)
target_compile_options(MyLibrary PRIVATE -std=c++11 -Wall)

configure_siplasplas_reflection(MyLibrary)

This will add a custom pre-build target that automatically runs DRLParser and generates reflection metadata headers before building your library.

Static reflection

SIplasplas provides a template-based API to access to static reflection information of user defined types:

// particle.hpp

class Particle
{
public:
    struct Position
    {
        float x, y, z;
    };

    struct Color
    {
        float a, r, g, b;
    };

    enum class State
    {
        Alive,
        Dead
    };

    Position position;
    Color color;
    State state;
};

siplasplas uses a libclang based script to generate C++ code with all the metadata. After running this script, include both the user header and the generated header:

#include <particle.hpp>
#include <reflection/particle.hpp> // Reflection data (generated code)

std::vector<Particle> particles;

std::ostream& operator<<(std::ostream& os, const Particle::Position& position)
{
    using PositionClass = cpp::static_reflection::Class<Particle::Position>;

    os << "{";

    // For each coordinate in the Position class...
    cpp::foreach_type<PositionClass::Fields>([&](auto type)
    {
        using Field = cpp::meta::type_t<decltype(type)>;

        os << Field::spelling() << ": "            // Field name ("x", "y", "z")
           << cpp::invoke(Field::get(), position)  // Field value (Like C++17 invoke with member object ptr)
           << " ";
    });

    return os << "}";
}

std::ostream& operator<<(std::ostream& os, const Particle::Color& color)
{
    using ColorClass = cpp::static_reflection::Class<Particle::Color>;

    os << "{";

    // For each channel in the Color class...
    cpp::foreach_type<ColorClass::Fields>([&](auto type)
    {
        using Field = cpp::meta::type_t<decltype(type)>;

        os << Field::spelling() << ": "             // channel name (r, g, b, ...)
           << cpp::invoke(Field::get(), color)*255  // channel value
           << " ";
    });

    return os << "}";
}

std::ostream& operator<<(std::ostream& os, const Particle::State& state)
{
    // Use static reflection to get the name of the enum value:
    return os << cpp::static_reflection::Enum<Particle::State>::toString(state);
}

int main()
{
    for(const auto& particle : particles)
    {
        std::cout << "position: " << particle.position << std::endl;
        std::cout << "color: " << particle.color << std::endl;
        std::cout << "state: " << particle.state << std::endl;
    }
}

The static reflection API currently supports:

Dynamic reflection

Siplasplas also supports dynamic reflection in the form of a simple entity based component system:

cpp::dynamic_reflection::Runtime runtime = loadDynamicReflection();

// Get dynamic reflection info of the class ::Particle::Position:
cpp::dynamic_reflection::Class&  positionClass = runtime.class_("Particle").class_("Position");

// Manipulate a particle object using dynamic reflection:
Particle particle;
positionCLass.field_("x").get(particle.position) = 42.0f; // particle.position.x = 42

// You can also create objects dynamically:
auto particle2 = runtime.class_("Particle").create();

// Returned objects are dynamically manipulable too:
particle2["color"]["r"] = 0.5f;

The dynamic reflection API can be used to load APIs from external libraries at runtime in a straightforward way:

int main()
{
    cpp::DynamicLibrary lib{"libmylibrary.so"};
    cpp::dynamic_reflection::Runtimeloader loader{lib};
    cpp::dynamic_reflection::Runtime& runtime = loader.runtime();

    auto myObject = runtime.class_("MyClass").create();

    // Invoke MyClass::function with params 1 and "hello":
    myObject("function")(1, std::string("hello!"));
}

### Other features:

Siplasplas offers other features, building blocks for the APIs explained above, including:

Supported compilers

siplasplas has been tested in GCC 5.1/5.2/6.1, Clang 3.6/3.7/3.8, and Visual Studio 2015.

Documentation

Documentation is available here

The documentation is available in Doxygen and Standardese format, each one with multiple versions corresponding to the latest documentation of each siplasplas release and active branch.

Installation

NOTE: siplasplas is a work in progress project subject to changes. We don’t currently provide any kind of API or ABI stability guarantee, nor a production-ready installation process. The following instructions are to build siplasplas from sources.

TL;DR

You can build siplasplas from sources:

$ git clone https://github.com/Manu343726/siplasplas --recursive
$ cd siplasplas
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build .

Or download the bootstrap cmake script and point it to one of the siplasplas releases:

set(SIPLASPLAS_PACKAGE_URL <url to siplasplas release package>)
set(SIPLASPLAS_INSTALL_DRLPARSER_DEPENDENCIES ON) # Download DRLparser deps with pip
set(SIPLASPLAS_LIBCLANG_VERSION 3.8.0) # libclang version
set(SIPLASPLAS_DOWNLOAD_LIBCLANG ON) # Download and configure libclang automatically

include(bootstrap.cmake)

This will download and configure a siplasplas installation in your buildtree. After including bootstrap.cmake, a FindSiplasplas.cmake module is available in your module path to link against the different siplasplas modules:

find_package(Siplasplas)

target_link_libraries(MyLibrary PUBLIC siplasplas-reflection-dynamic)

The module defines one imported library for each siplasplas module. All inter-module dependencies are already solved.

Prerequisites

Dependencies

All siplasplas dependencies are managed automatically through CMake, users don’t have to worry about installing deps. Anyway, here is the list of the thrid party dependencies of siplasplas:

siplasplas also depends on some python modules:

Download and configure the project

Clone the siplasplas repository

$ git clone https://github.com/Manu343726/siplasplas --recursive

Create a build/ directory inside the local repository

$ cd siplasplas
$ mkdir build

Run cmake in the build directory

$ cd build
$ cmake ..

Make sure you installed all the requirements before running cmake, siplasplas configuration may fail if one or more of that requirements is missing.

To build the library, invoke the default build target:

$ cmake --build . # Or just "make" if using Makefiles generator

Configuration

The default cmake invocation will build siplasplas as dynamic libraries (one per module) using the default generator. Also, siplasplas configuration can be modified using some options and variables:

The syntax to pass variables to cmake during configuration is -D<VARIABLE>=<VALUE>, for example:

$ cmake .. -DSIPLASPLAS_VERBOSE_CONFIG=ON

Acknowledgements

Many thanks to:

License

siplasplas project is released under the MIT open source license. This license applies to all C++ sources, CMake scripts, and any other file except explicitly stated.