GithubHelp home page GithubHelp logo

mlomb / metacpp Goto Github PK

View Code? Open in Web Editor NEW
49.0 5.0 11.0 118 KB

C++ Reflection & Serialization using Clang's LibTooling

License: Apache License 2.0

CMake 12.46% C++ 87.54%
reflection cpp clang meta serialization clang-libtooling metadata

metacpp's Introduction

MetaCPP - Reflection & Serialization

The objective is to generate reflection information and enable serialization/deserialization with the least or -zero- manual code possible.

Note that this project is not production ready, there are many bugs and lacks proper testing.

Example

You can find the definition of the following objects in Example/objects.hpp.

// load the auto-generated metadata
metacpp::Storage* storage = metacpp::Runtime::GetStorage();
metacpp::generated::Load(storage);

Player* player = new Player();
player->health = 255;
player->position = { 5, 5 };
player->velocity = { 1, 1 };
player->name = "mlomb";

Monster* monster = new Monster();
monster->health = 255;
monster->position = { 10, 10 };
monster->velocity = { -1, -1 };
monster->scary_factor = 42.123;

Map map;
map.entities = { player, monster };

map.magic_numbers = { 4, 2 };

map.map = {
        { 1, 2, 3 },
        { 4, 5, 6 },
        { 7, 8, 9 },
};

metacpp::JsonSerializer serializer = metacpp::JsonSerializer(storage);

// serialize
std::string json = serializer.Serialize(&map, true /* pretty print */);

std::cout << json << std::endl;

// deserialize
Map* deserialized_map = serializer.DeSerialize<Map>(json);

The code above spits out a JSON like this:

[{
    "magic_numbers": [4, 2],
    "map": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
    "entities": [1, 2, null, ...]
}, {
    "reflection_class": "Player",
    "health": 255,
    "position": {
        "x": 5.0,
        "y": 5.0
    },
    "velocity": {
        "x": 1.0,
        "y": 1.0
    },
    "name": "mlomb"
}, {
    "reflection_class": "Monster",
    "health": 255,
    "position": {
        "x": 10.0,
        "y": 10.0
    },
    "velocity": {
        "x": -1.0,
        "y": -1.0
    },
    "scary_factor": 42.12300109863281
}]

You can find the full code for this example here.

Setup

You can set it up using CMake. First clone the repository under your project directory.

Then, inside your CMakeLists.txt add the following:

add_subdirectory(MetaCPP/MetaCPP-CLI)

...

# MetaCPP automatic reflection
include(MetaCPP/CMake/MetaPrebuild.cmake)
meta_generate(YOUR_TARGET "FileToReflect.hpp" "Generated.hpp" "Generated.cpp" "")

Next and last, include the following lines at the begining of your application to load all the metadata:

#include <MetaCPP/Runtime.hpp>

int main() {
  metacpp::Storage* storage = metacpp::Runtime::GetStorage();
  metacpp::generated::Load(storage);
 
  ...
}

Dependencies

MetaCPP relies on Clang's LibTooling to generate all the metadata.
These are the dependencies:

Mustache and rapidjson are included as submodules so don't forget to clone with --recursive.

If you need to compile the CLI make sure to have installed Clang's Libtooling. In Windows you should have in PATH the Clang and LLVM binaries.

Known issues

  • Arrays of pointers are broken (#pr-3)

License

See LICENSE.

metacpp's People

Contributors

hexlord avatar mlomb 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

Watchers

 avatar  avatar  avatar  avatar  avatar

metacpp's Issues

Clang12 & ninja cmake support

Expected behaviour: clang 12 ninja build works correctly
Actual behaviour: it does not compile
So I had to make these (Hexlord@34172ad) changes to make it compile under linux with latest clang (I build clang from sources). Feel free to pull them if you agree.

Note that for ninja to work the generated files must reside in some target, otherwise a rule is expected to make them.

I also fixed delete void* in your code, it is not correct (maybe it is on MSVS, as part of some extension I know nothing of), so I added destructor support, but I used c++17 features to call destructor with qualified name (otherwise I would need to pass non-qual name to ~T() for your mustache, which I had no intention of.

And thanks for this great tool! I came from metareflection, as yours already supports templates, I hope to expand it for my use-case, I will try to share useful bits.

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.