Skip to content

Commit

Permalink
Merge branch 'BoomingTech:main' into 1st
Browse files Browse the repository at this point in the history
  • Loading branch information
liangzai12 authored Nov 30, 2022
2 parents 7705b4f + 36b7173 commit 282d42e
Show file tree
Hide file tree
Showing 19 changed files with 159 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ namespace Generator
auto relativeDir = fs::path(path).filename().replace_extension("reflection.gen.h").string();
return m_out_path + "/" + relativeDir;
}

int ReflectionGenerator::generate(std::string path, SchemaMoudle schema)
{
static const std::string vector_prefix = "std::vector<";

std::string file_path = processFileName(path);

Mustache::data mustache_data;
Mustache::data include_headfiles(Mustache::data::type::list);
Mustache::data class_defines(Mustache::data::type::list);
Expand Down Expand Up @@ -101,14 +103,15 @@ namespace Generator

mustache_data.set("class_defines", class_defines);
mustache_data.set("include_headfiles", include_headfiles);

std::string tmp = Utils::convertNameToUpperCamelCase(fs::path(path).stem().string(), "_");
mustache_data.set("sourefile_name_upper_camel_case", tmp);

std::string render_string =
TemplateManager::getInstance()->renderByTemplate("commonReflectionFile", mustache_data);
Utils::saveFile(render_string, file_path);

for (auto class_item : class_names)
{
m_type_list.emplace_back(class_item.first);
}
m_sourcefile_list.emplace_back(tmp);

m_head_file_list.emplace_back(Utils::makeRelativePath(m_root_path, file_path).string());
return 0;
Expand All @@ -117,18 +120,18 @@ namespace Generator
{
Mustache::data mustache_data;
Mustache::data include_headfiles = Mustache::data::type::list;
Mustache::data class_defines = Mustache::data::type::list;
Mustache::data sourefile_names = Mustache::data::type::list;

for (auto& head_file : m_head_file_list)
{
include_headfiles.push_back(Mustache::data("headfile_name", head_file));
}
for (auto& class_name : m_type_list)
for (auto& sourefile_name_upper_camel_case : m_sourcefile_list)
{
class_defines.push_back(Mustache::data("class_name", class_name));
sourefile_names.push_back(Mustache::data("sourefile_name_upper_camel_case", sourefile_name_upper_camel_case));
}
mustache_data.set("include_headfiles", include_headfiles);
mustache_data.set("class_defines", class_defines);
mustache_data.set("sourefile_names", sourefile_names);
std::string render_string =
TemplateManager::getInstance()->renderByTemplate("allReflectionFile", mustache_data);
Utils::saveFile(render_string, m_out_path + "/all_reflection.h");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ namespace Generator

private:
std::vector<std::string> m_head_file_list;
std::vector<std::string> m_type_list;
std::vector<std::string> m_sourcefile_list;
};
} // namespace Generator
12 changes: 11 additions & 1 deletion engine/source/meta_parser/parser/meta/meta_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,5 +322,15 @@ namespace Utils
out_string.append(out_sub_string[out_sub_string.size() - 1]);
return 0;
}

std::string convertNameToUpperCamelCase(const std::string& name, std::string pat)
{
std::string ret_string;
auto&& name_spilts = split(name, pat);
for (auto& split_string : name_spilts)
{
split_string[0] = toupper(split_string[0]);
ret_string.append(split_string);
}
return ret_string;
}
} // namespace Utils
5 changes: 4 additions & 1 deletion engine/source/meta_parser/parser/meta/meta_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ namespace Utils
void replaceAll(std::string& resource_str, std::string sub_str, std::string new_str);

unsigned long formatPathString(const std::string& path_string, std::string& out_string);

std::string convertNameToUpperCamelCase(const std::string& name, std::string pat);

} // namespace Utils

#include "meta_utils.hpp"
#include "meta_utils.hpp"
9 changes: 4 additions & 5 deletions engine/source/runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ set(TARGET_NAME PiccoloRuntime)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

set(json_inlcude ${THIRD_PARTY_DIR}/json11)
set(json_lib_dir ${THIRD_PARTY_DIR}/lib/json11)
set(JSON_INCLUDE ${THIRD_PARTY_DIR}/json11)

add_library(json11 ${json_inlcude}/json11.cpp)
set_target_properties(json11 PROPERTIES FOLDER "ThirdParty/json11" )
add_library(json11 ${JSON_INCLUDE}/json11.cpp)
set_target_properties(json11 PROPERTIES FOLDER "ThirdParty/json11")
# ---- Add source files ----

# Note: globbing sources is considered bad practice as CMake's generators may not detect new files
Expand Down Expand Up @@ -76,7 +75,7 @@ target_include_directories(

target_include_directories(
${TARGET_NAME}
PUBLIC $<BUILD_INTERFACE:${json_inlcude}>
PUBLIC $<BUILD_INTERFACE:${JSON_INCLUDE}>
)

target_include_directories(
Expand Down
2 changes: 1 addition & 1 deletion engine/source/runtime/core/meta/json.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#pragma once
#include "json11.hpp"
using PJson = json11::Json;
using Json = json11::Json;
12 changes: 6 additions & 6 deletions engine/source/runtime/core/meta/meta_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ namespace Piccolo
// serializer & deserializer

// write Test1_in (object) to Test1_json_in (json)
auto test1_json_in = PSerializer::write(test1_in);
auto test1_json_in = Serializer::write(test1_in);

std::string test1_context = test1_json_in.dump();

// read Test1_context (json) to Test1_out (object)
std::string err;

auto&& Test1_json = PJson::parse(test1_context, err);
PSerializer::read(Test1_json, test1_out);
auto&& Test1_json = Json::parse(test1_context, err);
Serializer::read(Test1_json, test1_out);
LOG_INFO(test1_context);

auto Test2_json_in = PSerializer::write(test2_in);
auto Test2_json_in = Serializer::write(test2_in);
std::string test2_context = Test2_json_in.dump();

std::fstream out_put("out.txt", std::ios::out);
Expand All @@ -46,8 +46,8 @@ namespace Piccolo
out_put.close();

Test2 test2_out;
auto&& test2_json = PJson::parse(test2_context, err);
PSerializer::read(test2_json, test2_out);
auto&& test2_json = Json::parse(test2_context, err);
Serializer::read(test2_json, test2_out);
LOG_INFO(test2_context.c_str());

// reflection
Expand Down
6 changes: 3 additions & 3 deletions engine/source/runtime/core/meta/reflection/reflection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ namespace Piccolo
return false;
}

ReflectionInstance TypeMeta::newFromNameAndPJson(std::string type_name, const PJson& json_context)
ReflectionInstance TypeMeta::newFromNameAndJson(std::string type_name, const Json& json_context)
{
auto iter = m_class_map.find(type_name);

Expand All @@ -110,15 +110,15 @@ namespace Piccolo
return ReflectionInstance();
}

PJson TypeMeta::writeByName(std::string type_name, void* instance)
Json TypeMeta::writeByName(std::string type_name, void* instance)
{
auto iter = m_class_map.find(type_name);

if (iter != m_class_map.end())
{
return std::get<2>(*iter->second)(instance);
}
return PJson();
return Json();
}

std::string TypeMeta::getTypeName() { return m_type_name; }
Expand Down
12 changes: 6 additions & 6 deletions engine/source/runtime/core/meta/reflection/reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace Piccolo

#define REFLECTION_BODY(class_name) \
friend class Reflection::TypeFieldReflectionOparator::Type##class_name##Operator; \
friend class PSerializer;
friend class Serializer;
// public: virtual std::string getTypeName() override {return #class_name;}

#define REFLECTION_TYPE(class_name) \
Expand Down Expand Up @@ -81,13 +81,13 @@ namespace Piccolo
typedef std::function<int(void*)> GetSizeFunc;
typedef std::function<bool()> GetBoolFunc;

typedef std::function<void*(const PJson&)> ConstructorWithPJson;
typedef std::function<PJson(void*)> WritePJsonByName;
typedef std::function<void*(const Json&)> ConstructorWithJson;
typedef std::function<Json(void*)> WriteJsonByName;
typedef std::function<int(Reflection::ReflectionInstance*&, void*)> GetBaseClassReflectionInstanceListFunc;

typedef std::tuple<SetFuncion, GetFuncion, GetNameFuncion, GetNameFuncion, GetNameFuncion, GetBoolFunc>
FieldFunctionTuple;
typedef std::tuple<GetBaseClassReflectionInstanceListFunc, ConstructorWithPJson, WritePJsonByName>
typedef std::tuple<GetBaseClassReflectionInstanceListFunc, ConstructorWithJson, WriteJsonByName>
ClassFunctionTuple;
typedef std::tuple<SetArrayFunc, GetArrayFunc, GetSizeFunc, GetNameFuncion, GetNameFuncion> ArrayFunctionTuple;

Expand Down Expand Up @@ -116,8 +116,8 @@ namespace Piccolo
static TypeMeta newMetaFromName(std::string type_name);

static bool newArrayAccessorFromName(std::string array_type_name, ArrayAccessor& accessor);
static ReflectionInstance newFromNameAndPJson(std::string type_name, const PJson& json_context);
static PJson writeByName(std::string type_name, void* instance);
static ReflectionInstance newFromNameAndJson(std::string type_name, const Json& json_context);
static Json writeByName(std::string type_name, void* instance);

std::string getTypeName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ namespace Piccolo
{
namespace Reflection
{
void TypeMetaRegister::Unregister() { TypeMetaRegisterinterface::unregisterAll(); }
void TypeMetaRegister::metaUnregister() { TypeMetaRegisterinterface::unregisterAll(); }
} // namespace Reflection
} // namespace Piccolo
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ namespace Piccolo
class TypeMetaRegister
{
public:
static void Register();
static void Unregister();
static void metaRegister();
static void metaUnregister();
};
} // namespace Reflection
} // namespace Piccolo
Loading

0 comments on commit 282d42e

Please sign in to comment.