Skip to content

Commit

Permalink
JSONSerialization
Browse files Browse the repository at this point in the history
  • Loading branch information
mlomb committed Feb 6, 2018
1 parent e1bf291 commit 1268cc5
Show file tree
Hide file tree
Showing 26 changed files with 930 additions and 85 deletions.
2 changes: 0 additions & 2 deletions .gitattributes

This file was deleted.

3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[submodule "External/Mustache"]
path = External/Mustache
url = https://github.com/kainjow/Mustache.git
[submodule "External/rapidjson"]
path = External/rapidjson
url = https://github.com/Tencent/rapidjson.git
2 changes: 1 addition & 1 deletion Examples/Generation/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ add_executable(Example-Generation main.cpp)
target_link_libraries(Example-Generation MetaCPP)

include(../../CMake/MetaPrebuild.cmake)
meta_generate(Example-Generation "test.hpp" "Generated.hpp" "Generated.cpp")
meta_generate(Example-Generation "test.hpp" "Generated.hpp" "Generated.cpp" "")
52 changes: 50 additions & 2 deletions Examples/Generation/main.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
#include <MetaCPP/Storage.hpp>
#include <iostream>

#include "test.hpp"

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

int main() {
metacpp::generated::Load(metacpp::Runtime::GetStorage());

metacpp::Storage* storage = metacpp::Runtime::GetStorage();
//storage->dump();

Entity e;
e.name = "Tank";
e.health = 199;

e.m_Position.x = 3.3;
e.m_Position.y = 4.4;
e.m_Velocity.x = -4;
e.m_Velocity.y = -2;

Target* t = new Target();
t->attack = true;
t->targetPosition.x = -99;
t->targetPosition.y = 99;

t->vics = { {1,2},{3,4},{5,6} };
t->tgts = { t,t,t };

e.m_TargetA = t;
e.m_TargetB = t;

e.m_TargetA->target = t;

e.m_B = new Base();
e.m_D = new Derived();

e.list = { 5, 6, 7 };
e.two_dim = { {0,1,2},{3,4,5},{6,7,8} };

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

std::string json = serializer.Serialize(&e, true);
std::cout << json.length() << std::endl;
std::cout << json << std::endl;

std::cout << "---------------------------------------------" << std::endl;

Entity* e2 = serializer.DeSerialize<Entity>(json);
std::cout << serializer.Serialize(e2, true) << std::endl;

system("pause");

return 0;
}
101 changes: 38 additions & 63 deletions Examples/Generation/test.hpp
Original file line number Diff line number Diff line change
@@ -1,81 +1,56 @@
// This is a dumb file

//#include <vector>
#include <string>
#include <vector>

namespace N5 {
enum AnEnum {
AnEnumElement,
AnotherEnumElement,
YetAnotherEnumElement
};
}

namespace N1 {
struct Point {
struct WhyNot {
protected:
int whynotint;
};
double x, y;
WhyNot wn;
int WhyNot;
};
}
template<typename T>
struct Vector {
T x, y;
};

typedef Vector<int> Vectori;
typedef Vector<float> Vectorf;

class Base {
protected:
int a;
double b;
N5::AnEnum daenum;
struct Target {
public:
float method(short c) {
return a + c;
Target() {

}

bool attack;
Vectorf targetPosition;

Target* target;
std::vector<Vectori> vics;
std::vector<Target*> tgts;
};

namespace N4 {
template<typename T>
class ExampleT {
public:
ExampleT() {};
class Base {
public:
virtual ~Base() {};

private:
int a;
T var;
char c;
};
}
int base_int = 3;
};

class Derived : public Base {
public:
long method(int d) {
return a + b + d;
}
protected:
N1::Point derived_point;
char c;
int e, f;
int derived_int = 4;
};

N4::ExampleT<float> specified3;
N4::ExampleT<double> specified4;
N4::ExampleT<int> specified5;
N4::ExampleT<N1::Point> specified6;
struct Entity {
std::string name;
int health;

//std::vector<int> vecints;
};
Vectorf m_Position;
Vectori m_Velocity;

namespace N2 {
typedef N4::ExampleT<int> ExampleTSpecified;
typedef int defint;
}
std::vector<int> list;
std::vector<std::vector<int>> two_dim;

namespace N3 {
class Specified2 : public N4::ExampleT<N1::Point> {
public:
int jj;
N2::defint kk;
N4::ExampleT<Derived> test;
};
Target* m_TargetA;
Target* m_TargetB;
Target** m_Targets;

}
Base* m_B;
Base* m_D;
};
1 change: 1 addition & 0 deletions External/rapidjson
Submodule rapidjson added at daabb8
66 changes: 61 additions & 5 deletions MetaCPP-CLI/src/ASTScraper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,23 @@ namespace metacpp {
const clang::Type* cType = cxxRecordDecl->getTypeForDecl();
metacpp::Type* type = ScrapeType(cType);

if (type)
if (type) {
const clang::CXXRecordDecl* typeCxxRecordDecl = cType->getAsCXXRecordDecl();

type->setAccess(TransformAccess(cxxRecordDecl->getAccess()));
type->setHasDefaultConstructor(!typeCxxRecordDecl->hasUserProvidedDefaultConstructor() && typeCxxRecordDecl->needsImplicitDefaultConstructor());

// methods
for (auto it = cxxRecordDecl->method_begin(); it != cxxRecordDecl->method_end(); it++) {
clang::CXXMethodDecl* method = *it;
if (method != 0) {
ScrapeMethodDecl(method, type);
}
}

if (typeCxxRecordDecl->isAbstract())
type->setHasDefaultConstructor(false);
}

return type;
}
Expand All @@ -90,7 +105,7 @@ namespace metacpp {

const clang::TemplateArgumentList& args = templateDecl->getTemplateArgs();

for (int i = 0; i < args.size(); i++) {
for (int i = 0; i < (int)args.size(); i++) {
const clang::TemplateArgument& arg = args[i];
if (i)
typeName += ",";
Expand Down Expand Up @@ -168,16 +183,26 @@ namespace metacpp {

type->setSize(m_Context->getTypeSize(cType) / 8);
type->setKind(kind);
type->setHasDefaultConstructor(cType->getTypeClass() == clang::Type::TypeClass::Builtin && qualifiedName.getName() != "void");
type->setPolymorphic(false);

if (auto cxxRecordDecl = cType->getAsCXXRecordDecl()) {
type->setPolymorphic(cxxRecordDecl->isPolymorphic());

// Parse base classes
for (auto it = cxxRecordDecl->bases_begin(); it != cxxRecordDecl->bases_end(); it++)
{
type->addBaseType(ResolveQualType(it->getType()), TransformAccess(it->getAccessSpecifier()));
}
QualifiedType* base_qtype = ResolveQualType(it->getType());
type->addBaseType(base_qtype, TransformAccess(it->getAccessSpecifier()));

if (it == cxxRecordDecl->bases_begin()) {
m_Storage->getType(base_qtype->getTypeID())->addDerivedType(typeId);
}
}

// Scrape annotations
std::vector<std::string> annotations = ScrapeAnnotations(cxxRecordDecl);

}

for (auto qt : templateArgs)
Expand Down Expand Up @@ -207,6 +232,36 @@ namespace metacpp {
parent->addField(field);
}

void ASTScraper::ScrapeMethodDecl(const clang::CXXMethodDecl* cxxMethodDecl, Type* parent)
{
auto constructor = clang::dyn_cast<clang::CXXConstructorDecl>(cxxMethodDecl);
auto destructor = clang::dyn_cast<clang::CXXDestructorDecl>(cxxMethodDecl);

if (constructor && constructor->isDefaultConstructor() && constructor->getAccess() == clang::AccessSpecifier::AS_public) {
parent->setHasDefaultConstructor(true);
}

metacpp::QualifiedName qualifiedName = ResolveQualifiedName(cxxMethodDecl->getQualifiedNameAsString());

Method* method = new Method(qualifiedName);

method->setOwner(parent->getID());


//cxxMethodDecl->dumpColor();

//std::cout << cxxMethodDecl->isUserProvided() << cxxMethodDecl->isUsualDeallocationFunction() << cxxMethodDecl->isCopyAssignmentOperator() << cxxMethodDecl->isMoveAssignmentOperator() << std::endl;
//std::cout << cxxMethodDecl->isDefaulted() << std::endl;

// params
for (auto it = cxxMethodDecl->param_begin(); it != cxxMethodDecl->param_end(); it++) {
clang::ParmVarDecl* param = *it;

}

parent->addMethod(method);
}

QualifiedType* ASTScraper::ResolveQualType(clang::QualType qualType)
{
MakeCanonical(qualType);
Expand All @@ -221,7 +276,7 @@ namespace metacpp {
}
else if (auto ref = clang::dyn_cast<clang::ReferenceType>(qualType.split().Ty)) {
qualifiedType->setQualifierOperator(QualifierOperator::REFERENCE);
qualType = ptr->getPointeeType();
qualType = ref->getPointeeType();
} else
qualifiedType->setQualifierOperator(QualifierOperator::VALUE);

Expand Down Expand Up @@ -270,6 +325,7 @@ namespace metacpp {
case clang::AccessSpecifier::AS_protected:
return AccessSpecifier::PROTECTED;
case clang::AccessSpecifier::AS_private:
default:
return AccessSpecifier::PRIVATE;
}
}
Expand Down
1 change: 1 addition & 0 deletions MetaCPP-CLI/src/ASTScraper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace metacpp {
std::vector<QualifiedType*> ResolveCXXRecordTemplate(const clang::CXXRecordDecl* cxxRecordDecl, QualifiedName& qualifiedName);
Type* ScrapeType(const clang::Type* cType);
void ScrapeFieldDecl(const clang::FieldDecl* fieldDecl, Type* parent);
void ScrapeMethodDecl(const clang::CXXMethodDecl* cxxMethodDecl, Type* parent);

QualifiedType* ResolveQualType(clang::QualType qualType);
std::vector<std::string> ScrapeAnnotations(const clang::Decl* decl);
Expand Down
2 changes: 1 addition & 1 deletion MetaCPP/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ file(GLOB_RECURSE MetaCPP_SRC ${MetaCPP_INC_DIR}/MetaCPP/*.hpp

add_library(MetaCPP ${MetaCPP_SRC})

target_include_directories(MetaCPP PUBLIC ${MetaCPP_INC_DIR} ../External/Mustache)
target_include_directories(MetaCPP PUBLIC ${MetaCPP_INC_DIR} ../External/Mustache ../External/rapidjson/include)
10 changes: 10 additions & 0 deletions MetaCPP/include/MetaCPP/Container.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef METACPP_CONTAINER_HPP
#define METACPP_CONTAINER_HPP

namespace metacpp {
class Container {

};
}

#endif
13 changes: 11 additions & 2 deletions MetaCPP/include/MetaCPP/Field.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ namespace metacpp {
void setOffset(const size_t bytes);

template<typename T>
T* get(void* object) const;
T* get(const void* object) const;
template<typename T>
void set(const T& value, const void* object) const;

mustache::data asMustache() const override;
private:
Expand All @@ -35,10 +37,17 @@ namespace metacpp {
};

template<typename T>
inline T* Field::get(void* object) const
inline T* Field::get(const void* object) const
{
return reinterpret_cast<T*>((size_t)object + m_OffsetInBytes);
}

template<typename T>
inline void Field::set(const T& value, const void* object) const
{
T& obj_value = *reinterpret_cast<T*>((size_t)object + m_OffsetInBytes);
obj_value = value;
}
}

#endif
Loading

0 comments on commit 1268cc5

Please sign in to comment.