Skip to content
forked from mlomb/MetaCPP

C++ Reflection & Serialization using Clang's LibTooling

License

Notifications You must be signed in to change notification settings

tarno0se/MetaCPP

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MetaCPP - Reflection & Serialization

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

Example

You can find the definition of this 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]
  }, {
    "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 very simple if you are using CMake. First clone the repository under your project directory.

Then, inside your CMakeLists.txt add the following:

# If you don't want to compile the CLI and use the precompiled version see MetaPrebuild.cmake
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, fortunately if you use the precompiled version you don't need to. 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.

License

See LICENSE.

About

C++ Reflection & Serialization using Clang's LibTooling

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C++ 86.6%
  • CMake 13.4%