Skip to content

Commit

Permalink
Merge branch 'pr/3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomb committed Oct 26, 2020
2 parents a8ea256 + 0258311 commit f370225
Show file tree
Hide file tree
Showing 36 changed files with 1,527 additions and 1,153 deletions.
44 changes: 44 additions & 0 deletions .github/workflows/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: CMake

on: [push, pull_request]

env:
BUILD_TYPE: Release
CXX: clang++
CC: clang

jobs:
build:
runs-on: ubuntu-20.04

steps:
- name: Download Libtooling
uses: wei/wget@v1
with:
args: https://github.com/llvm/llvm-project/releases/download/llvmorg-11.0.0/clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz
- name: Extract Libtooling
run: |
mkdir ${{runner.workspace}}/llvm
tar -xf clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04.tar.xz -C ${{runner.workspace}}/llvm
echo "${{runner.workspace}}/llvm/clang+llvm-11.0.0-x86_64-linux-gnu-ubuntu-20.04/bin" >> $GITHUB_PATH
- uses: actions/checkout@v2
with:
submodules: 'true'

- name: Create build directory
run: cmake -E make_directory ${{runner.workspace}}/build

- name: Generate project
working-directory: ${{runner.workspace}}/build
run: cmake $GITHUB_WORKSPACE/Example -DCMAKE_BUILD_TYPE=$BUILD_TYPE

- name: Build
working-directory: ${{runner.workspace}}/build
shell: bash
run: cmake --build . --config $BUILD_TYPE

- name: Run Example
working-directory: ${{runner.workspace}}/build
shell: bash
run: ./Example
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ Builds/
*.out
*.app
*.ps1

.idea
cmake-build-debug
cmake-build-release
12 changes: 10 additions & 2 deletions CMake/MetaPrebuild.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,21 @@ macro(meta_generate PROJECT_NAME IN_SOURCE OUT_HEADER OUT_SOURCE ADDITIONAL_FLAG

set(CMD "${META_CPP_EXE} \"${IN_SOURCE_PATH}\" -out-header \"${OUT_HEADER_PATH}\" -out-source \"${OUT_SOURCE_PATH}\" ${FLAGS}")
separate_arguments(CMD)

# Useful for debugging
message("MetaCPP Command: ${CMD}")

add_custom_command(
TARGET ${PROJECT_NAME}
PRE_BUILD
OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/MetaCPP"
COMMAND ${CMD}
WORKING_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}"
)

add_custom_target(
ProduceMetaCPP ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/MetaCPP"
)

set_source_files_properties(${OUT_HEADER_PATH} ${OUT_SOURCE_PATH} PROPERTIES GENERATED TRUE)

endmacro()
26 changes: 25 additions & 1 deletion Example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
cmake_minimum_required (VERSION 2.8)
project(MetaCPP-Example)

set (CMAKE_CXX_STANDARD 17)

add_subdirectory(../MetaCPP-CLI MetaCPP-CLI)

add_executable(Example main.cpp objects.hpp)
Expand All @@ -9,4 +11,26 @@ target_link_libraries(Example MetaCPP)

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

if(NOT DEFINED META_GENERATE_FLAGS)
set(META_GENERATE_FLAGS "")
endif()
if(DEFINED CLANG_INCLUDE_DIR)
set(META_GENERATE_FLAGS "${META_GENERATE_FLAGS} --flag -I${CLANG_INCLUDE_DIR}")
else()
# Fire all weapons
set(CLANG_INCLUDE_CANDIDATES "/usr/local/lib/clang/12.0.0/include")
set(CLANG_INCLUDE_CANDIDATES "${CLANG_INCLUDE_CANDIDATES};/usr/lib/llvm-12/lib/clang/12.0.0/include")
set(CLANG_INCLUDE_CANDIDATES "${CLANG_INCLUDE_CANDIDATES};/usr/local/lib/clang/11.0.0/include")
set(CLANG_INCLUDE_CANDIDATES "${CLANG_INCLUDE_CANDIDATES};/usr/lib/llvm-11/lib/clang/11.0.0/include")
set(CLANG_INCLUDE_CANDIDATES "${CLANG_INCLUDE_CANDIDATES};/usr/local/lib/clang/10.0.0/include")
set(CLANG_INCLUDE_CANDIDATES "${CLANG_INCLUDE_CANDIDATES};/usr/lib/llvm-10/lib/clang/10.0.0/include")
foreach(CLANG_INCLUDE_CANDIDATE ${CLANG_INCLUDE_CANDIDATES})
if(EXISTS ${CLANG_INCLUDE_CANDIDATE})
set(META_GENERATE_FLAGS "${META_GENERATE_FLAGS} --flag -I${CLANG_INCLUDE_CANDIDATE}")
break()
endif()
endforeach()
endif()

meta_generate(Example "objects.hpp" "Generated.hpp" "Generated.cpp" "${META_GENERATE_FLAGS}")
103 changes: 55 additions & 48 deletions Example/main.cpp
Original file line number Diff line number Diff line change
@@ -1,49 +1,56 @@
#include <iostream>

#include "objects.hpp"

#include <MetaCPP/Storage.hpp>
#include <MetaCPP/Runtime.hpp>
#include <MetaCPP/JsonSerializer.hpp>

int main() {
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);

// serialize again and compare the jsons
if (serializer.Serialize(deserialized_map, true) == json){
std::cout << "The serialization was successful!" << std::endl;
}

return 0;
#include <iostream>

#include "objects.hpp"

#include <MetaCPP/Storage.hpp>
#include <MetaCPP/Runtime.hpp>
#include <MetaCPP/JsonSerializer.hpp>

int main() {
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 << "Original: " << json << std::endl;

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

// serialize again and compare the jsons
std::string reserialized = serializer.Serialize(deserialized_map, true);
if (reserialized == json) {
std::cout << "The serialization was successful!" << std::endl;
return 0;
}
else {
std::cout << "The serialization did not match!" << std::endl;
std::cout << "Reserialized: " << reserialized << std::endl;
return 1;
}
}
76 changes: 43 additions & 33 deletions Example/objects.hpp
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
#include <string>
#include <vector>

template<typename T>
struct Vector {
T x, y;
};

class Entity {
public:
virtual ~Entity() {};

int health;
Vector<float> position;
Vector<float> velocity;
};

class Player : public Entity {
public:
std::string name;
};

class Monster : public Entity {
public:
float scary_factor;
};

class Map {
public:
std::vector<int> magic_numbers;
std::vector<std::vector<int>> map;
std::vector<Entity*> entities;
};
#include <string>
#include <vector>
#include <array>

// TODO: Support alignment.
#pragma pack(push, 1)
template<typename T>
struct Vector {
T x, y;
};

class Entity {
public:
virtual ~Entity() {};

int health;
Vector<float> position;
Vector<float> velocity;
};

class Player : public Entity {
public:
std::string name;

void Attack(Entity* entity) {
--entity->health;
}
};

class Monster : public Entity {
public:
float scary_factor;
};

class Map {
public:
std::vector<int> magic_numbers;
std::vector<std::vector<int>> map;
//std::vector<Entity*> entities;
std::array<Entity*, 16> entities;
};

#pragma pack(pop)
Loading

0 comments on commit f370225

Please sign in to comment.