项目作者: mlomb

项目描述 :
C++ Reflection & Serialization using Clang's LibTooling
高级语言: C++
项目地址: git://github.com/mlomb/MetaCPP.git
创建时间: 2017-11-26T00:16:23Z
项目社区:https://github.com/mlomb/MetaCPP

开源协议:Apache License 2.0

下载


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.

  1. // load the auto-generated metadata
  2. metacpp::Storage* storage = metacpp::Runtime::GetStorage();
  3. metacpp::generated::Load(storage);
  4. Player* player = new Player();
  5. player->health = 255;
  6. player->position = { 5, 5 };
  7. player->velocity = { 1, 1 };
  8. player->name = "mlomb";
  9. Monster* monster = new Monster();
  10. monster->health = 255;
  11. monster->position = { 10, 10 };
  12. monster->velocity = { -1, -1 };
  13. monster->scary_factor = 42.123;
  14. Map map;
  15. map.entities = { player, monster };
  16. map.magic_numbers = { 4, 2 };
  17. map.map = {
  18. { 1, 2, 3 },
  19. { 4, 5, 6 },
  20. { 7, 8, 9 },
  21. };
  22. metacpp::JsonSerializer serializer = metacpp::JsonSerializer(storage);
  23. // serialize
  24. std::string json = serializer.Serialize(&map, true /* pretty print */);
  25. std::cout << json << std::endl;
  26. // deserialize
  27. Map* deserialized_map = serializer.DeSerialize<Map>(json);

The code above spits out a JSON like this:

  1. [{
  2. "magic_numbers": [4, 2],
  3. "map": [[1, 2, 3], [4, 5, 6], [7, 8, 9]],
  4. "entities": [1, 2, null, ...]
  5. }, {
  6. "reflection_class": "Player",
  7. "health": 255,
  8. "position": {
  9. "x": 5.0,
  10. "y": 5.0
  11. },
  12. "velocity": {
  13. "x": 1.0,
  14. "y": 1.0
  15. },
  16. "name": "mlomb"
  17. }, {
  18. "reflection_class": "Monster",
  19. "health": 255,
  20. "position": {
  21. "x": 10.0,
  22. "y": 10.0
  23. },
  24. "velocity": {
  25. "x": -1.0,
  26. "y": -1.0
  27. },
  28. "scary_factor": 42.12300109863281
  29. }]

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:

  1. add_subdirectory(MetaCPP/MetaCPP-CLI)
  2. ...
  3. # MetaCPP automatic reflection
  4. include(MetaCPP/CMake/MetaPrebuild.cmake)
  5. 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:

  1. #include <MetaCPP/Runtime.hpp>
  2. int main() {
  3. metacpp::Storage* storage = metacpp::Runtime::GetStorage();
  4. metacpp::generated::Load(storage);
  5. ...
  6. }

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.