From 2bd7aa50ac4c21888b152c2bfcf321813a7ff8c9 Mon Sep 17 00:00:00 2001 From: 28778 <287781086@qq.com> Date: Tue, 25 Oct 2022 15:18:38 +0800 Subject: [PATCH 1/3] Reflection Code Rename --- engine/source/runtime/core/meta/json.h | 2 +- .../source/runtime/core/meta/meta_example.cpp | 12 +-- .../core/meta/reflection/reflection.cpp | 6 +- .../runtime/core/meta/reflection/reflection.h | 12 +-- .../meta/reflection/reflection_register.cpp | 2 +- .../meta/reflection/reflection_register.h | 4 +- .../core/meta/serializer/serializer.cpp | 80 +++++++++---------- .../runtime/core/meta/serializer/serializer.h | 66 +++++++-------- engine/source/runtime/engine.cpp | 4 +- .../resource/asset_manager/asset_manager.h | 6 +- engine/template/allReflectionFile.mustache | 6 +- engine/template/allSerializer.ipp.mustache | 24 +++--- engine/template/commonReflectionFile.mustache | 20 ++--- .../template/commonSerializerGenFile.mustache | 4 +- 14 files changed, 124 insertions(+), 124 deletions(-) diff --git a/engine/source/runtime/core/meta/json.h b/engine/source/runtime/core/meta/json.h index 91f0f9fba..82876c8b9 100644 --- a/engine/source/runtime/core/meta/json.h +++ b/engine/source/runtime/core/meta/json.h @@ -1,3 +1,3 @@ #pragma once #include "json11.hpp" -using PJson = json11::Json; \ No newline at end of file +using Json = json11::Json; \ No newline at end of file diff --git a/engine/source/runtime/core/meta/meta_example.cpp b/engine/source/runtime/core/meta/meta_example.cpp index a0829c59a..1e56e154d 100644 --- a/engine/source/runtime/core/meta/meta_example.cpp +++ b/engine/source/runtime/core/meta/meta_example.cpp @@ -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); @@ -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 diff --git a/engine/source/runtime/core/meta/reflection/reflection.cpp b/engine/source/runtime/core/meta/reflection/reflection.cpp index 60c281fac..f31e0d614 100644 --- a/engine/source/runtime/core/meta/reflection/reflection.cpp +++ b/engine/source/runtime/core/meta/reflection/reflection.cpp @@ -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); @@ -110,7 +110,7 @@ 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); @@ -118,7 +118,7 @@ namespace Piccolo { return std::get<2>(*iter->second)(instance); } - return PJson(); + return Json(); } std::string TypeMeta::getTypeName() { return m_type_name; } diff --git a/engine/source/runtime/core/meta/reflection/reflection.h b/engine/source/runtime/core/meta/reflection/reflection.h index 0c685c0f5..fa9d9d8bd 100644 --- a/engine/source/runtime/core/meta/reflection/reflection.h +++ b/engine/source/runtime/core/meta/reflection/reflection.h @@ -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) \ @@ -81,13 +81,13 @@ namespace Piccolo typedef std::function GetSizeFunc; typedef std::function GetBoolFunc; - typedef std::function ConstructorWithPJson; - typedef std::function WritePJsonByName; + typedef std::function ConstructorWithJson; + typedef std::function WriteJsonByName; typedef std::function GetBaseClassReflectionInstanceListFunc; typedef std::tuple FieldFunctionTuple; - typedef std::tuple + typedef std::tuple ClassFunctionTuple; typedef std::tuple ArrayFunctionTuple; @@ -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(); diff --git a/engine/source/runtime/core/meta/reflection/reflection_register.cpp b/engine/source/runtime/core/meta/reflection/reflection_register.cpp index d4b6a9858..0b7c7f121 100644 --- a/engine/source/runtime/core/meta/reflection/reflection_register.cpp +++ b/engine/source/runtime/core/meta/reflection/reflection_register.cpp @@ -12,6 +12,6 @@ namespace Piccolo { namespace Reflection { - void TypeMetaRegister::Unregister() { TypeMetaRegisterinterface::unregisterAll(); } + void TypeMetaRegister::metaUnregister() { TypeMetaRegisterinterface::unregisterAll(); } } // namespace Reflection } // namespace Piccolo diff --git a/engine/source/runtime/core/meta/reflection/reflection_register.h b/engine/source/runtime/core/meta/reflection/reflection_register.h index ef45dc1de..63e34faa7 100644 --- a/engine/source/runtime/core/meta/reflection/reflection_register.h +++ b/engine/source/runtime/core/meta/reflection/reflection_register.h @@ -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 \ No newline at end of file diff --git a/engine/source/runtime/core/meta/serializer/serializer.cpp b/engine/source/runtime/core/meta/serializer/serializer.cpp index ed4c09ac6..41cf3ba15 100644 --- a/engine/source/runtime/core/meta/serializer/serializer.cpp +++ b/engine/source/runtime/core/meta/serializer/serializer.cpp @@ -4,96 +4,96 @@ namespace Piccolo { template<> - PJson PSerializer::write(const char& instance) + Json Serializer::write(const char& instance) { - return PJson(instance); + return Json(instance); } template<> - char& PSerializer::read(const PJson& json_context, char& instance) + char& Serializer::read(const Json& json_context, char& instance) { assert(json_context.is_number()); return instance = json_context.number_value(); } template<> - PJson PSerializer::write(const int& instance) + Json Serializer::write(const int& instance) { - return PJson(instance); + return Json(instance); } template<> - int& PSerializer::read(const PJson& json_context, int& instance) + int& Serializer::read(const Json& json_context, int& instance) { assert(json_context.is_number()); return instance = static_cast(json_context.number_value()); } template<> - PJson PSerializer::write(const unsigned int& instance) + Json Serializer::write(const unsigned int& instance) { - return PJson(static_cast(instance)); + return Json(static_cast(instance)); } template<> - unsigned int& PSerializer::read(const PJson& json_context, unsigned int& instance) + unsigned int& Serializer::read(const Json& json_context, unsigned int& instance) { assert(json_context.is_number()); return instance = static_cast(json_context.number_value()); } template<> - PJson PSerializer::write(const float& instance) + Json Serializer::write(const float& instance) { - return PJson(instance); + return Json(instance); } template<> - float& PSerializer::read(const PJson& json_context, float& instance) + float& Serializer::read(const Json& json_context, float& instance) { assert(json_context.is_number()); return instance = static_cast(json_context.number_value()); } template<> - PJson PSerializer::write(const double& instance) + Json Serializer::write(const double& instance) { - return PJson(instance); + return Json(instance); } template<> - double& PSerializer::read(const PJson& json_context, double& instance) + double& Serializer::read(const Json& json_context, double& instance) { assert(json_context.is_number()); return instance = static_cast(json_context.number_value()); } template<> - PJson PSerializer::write(const bool& instance) + Json Serializer::write(const bool& instance) { - return PJson(instance); + return Json(instance); } template<> - bool& PSerializer::read(const PJson& json_context, bool& instance) + bool& Serializer::read(const Json& json_context, bool& instance) { assert(json_context.is_bool()); return instance = json_context.bool_value(); } template<> - PJson PSerializer::write(const std::string& instance) + Json Serializer::write(const std::string& instance) { - return PJson(instance); + return Json(instance); } template<> - std::string& PSerializer::read(const PJson& json_context, std::string& instance) + std::string& Serializer::read(const Json& json_context, std::string& instance) { assert(json_context.is_string()); return instance = json_context.string_value(); } // template<> - // PJson PSerializer::write(const Reflection::object& instance) + // Json Serializer::write(const Reflection::object& instance) //{ - // return PJson::object(); + // return Json::object(); //} // template<> - // Reflection::object& PSerializer::read(const PJson& json_context, Reflection::object& instance) + // Reflection::object& Serializer::read(const Json& json_context, Reflection::object& instance) //{ // return instance; //} @@ -103,47 +103,47 @@ namespace Piccolo ////////////////////////////////// /*template<> - PJson PSerializer::write(const ss& instance) + Json Serializer::write(const ss& instance) { - return PJson(); + return Json(); } template<> - PJson PSerializer::write(const jkj& instance) + Json Serializer::write(const jkj& instance) { - return PJson(); + return Json(); } template<> - PJson PSerializer::write(const test_class& instance) + Json Serializer::write(const test_class& instance) { - PJson::array aa; + Json::array aa; for(auto& item: instance.c) { - aa.emplace_back(PSerializer::write(item)); + aa.emplace_back(Serializer::write(item)); } ss jj; reflection::object* jjj1 = &jj; - auto kkk = PSerializer::write(jjj1); + auto kkk = Serializer::write(jjj1); auto jjj = kkk.dump(); - return PJson::object{ - {"a",PSerializer::write(instance.a)}, - {"b",PSerializer::write(instance.b)}, + return Json::object{ + {"a",Serializer::write(instance.a)}, + {"b",Serializer::write(instance.b)}, {"c",aa} }; } template<> - test_class& PSerializer::read(const PJson& json_context, test_class& instance) + test_class& Serializer::read(const Json& json_context, test_class& instance) { assert(json_context.is_object()); - PSerializer::read(json_context["a"], instance.a); - PSerializer::read(json_context["b"], instance.b); + Serializer::read(json_context["a"], instance.a); + Serializer::read(json_context["b"], instance.b); assert(json_context["c"].is_array()); - PJson::array cc = json_context["c"].array_items(); + Json::array cc = json_context["c"].array_items(); instance.c.resize(cc.size()); for (size_t index=0; index < cc.size();++index) { - PSerializer::read(cc[index], instance.c[index]); + Serializer::read(cc[index], instance.c[index]); } return instance; }*/ diff --git a/engine/source/runtime/core/meta/serializer/serializer.h b/engine/source/runtime/core/meta/serializer/serializer.h index c63645c27..0c62d9a1e 100644 --- a/engine/source/runtime/core/meta/serializer/serializer.h +++ b/engine/source/runtime/core/meta/serializer/serializer.h @@ -9,17 +9,17 @@ namespace Piccolo template inline constexpr bool always_false = false; - class PSerializer + class Serializer { public: template - static PJson writePointer(T* instance) + static Json writePointer(T* instance) { - return PJson::object {{"$typeName", PJson {"*"}}, {"$context", PSerializer::write(*instance)}}; + return Json::object {{"$typeName", Json {"*"}}, {"$context", Serializer::write(*instance)}}; } template - static T*& readPointer(const PJson& json_context, T*& instance) + static T*& readPointer(const Json& json_context, T*& instance) { assert(instance == nullptr); std::string type_name = json_context["$typeName"].string_value(); @@ -32,22 +32,22 @@ namespace Piccolo else { instance = static_cast( - Reflection::TypeMeta::newFromNameAndPJson(type_name, json_context["$context"]).m_instance); + Reflection::TypeMeta::newFromNameAndJson(type_name, json_context["$context"]).m_instance); } return instance; } template - static PJson write(const Reflection::ReflectionPtr& instance) + static Json write(const Reflection::ReflectionPtr& instance) { T* instance_ptr = static_cast(instance.operator->()); std::string type_name = instance.getTypeName(); - return PJson::object {{"$typeName", PJson(type_name)}, + return Json::object {{"$typeName", Json(type_name)}, {"$context", Reflection::TypeMeta::writeByName(type_name, instance_ptr)}}; } template - static T*& read(const PJson& json_context, Reflection::ReflectionPtr& instance) + static T*& read(const Json& json_context, Reflection::ReflectionPtr& instance) { std::string type_name = json_context["$typeName"].string_value(); instance.setTypeName(type_name); @@ -55,7 +55,7 @@ namespace Piccolo } template - static PJson write(const T& instance) + static Json write(const T& instance) { if constexpr (std::is_pointer::value) @@ -64,13 +64,13 @@ namespace Piccolo } else { - static_assert(always_false, "PSerializer::write has not been implemented yet!"); - return PJson(); + static_assert(always_false, "Serializer::write has not been implemented yet!"); + return Json(); } } template - static T& read(const PJson& json_context, T& instance) + static T& read(const Json& json_context, T& instance) { if constexpr (std::is_pointer::value) { @@ -78,7 +78,7 @@ namespace Piccolo } else { - static_assert(always_false, "PSerializer::read has not been implemented yet!"); + static_assert(always_false, "Serializer::read has not been implemented yet!"); return instance; } } @@ -86,44 +86,44 @@ namespace Piccolo // implementation of base types template<> - PJson PSerializer::write(const char& instance); + Json Serializer::write(const char& instance); template<> - char& PSerializer::read(const PJson& json_context, char& instance); + char& Serializer::read(const Json& json_context, char& instance); template<> - PJson PSerializer::write(const int& instance); + Json Serializer::write(const int& instance); template<> - int& PSerializer::read(const PJson& json_context, int& instance); + int& Serializer::read(const Json& json_context, int& instance); template<> - PJson PSerializer::write(const unsigned int& instance); + Json Serializer::write(const unsigned int& instance); template<> - unsigned int& PSerializer::read(const PJson& json_context, unsigned int& instance); + unsigned int& Serializer::read(const Json& json_context, unsigned int& instance); template<> - PJson PSerializer::write(const float& instance); + Json Serializer::write(const float& instance); template<> - float& PSerializer::read(const PJson& json_context, float& instance); + float& Serializer::read(const Json& json_context, float& instance); template<> - PJson PSerializer::write(const double& instance); + Json Serializer::write(const double& instance); template<> - double& PSerializer::read(const PJson& json_context, double& instance); + double& Serializer::read(const Json& json_context, double& instance); template<> - PJson PSerializer::write(const bool& instance); + Json Serializer::write(const bool& instance); template<> - bool& PSerializer::read(const PJson& json_context, bool& instance); + bool& Serializer::read(const Json& json_context, bool& instance); template<> - PJson PSerializer::write(const std::string& instance); + Json Serializer::write(const std::string& instance); template<> - std::string& PSerializer::read(const PJson& json_context, std::string& instance); + std::string& Serializer::read(const Json& json_context, std::string& instance); // template<> - // PJson PSerializer::write(const Reflection::object& instance); + // Json Serializer::write(const Reflection::object& instance); // template<> - // Reflection::object& PSerializer::read(const PJson& json_context, Reflection::object& instance); + // Reflection::object& Serializer::read(const Json& json_context, Reflection::object& instance); //////////////////////////////////// ////sample of generation coder @@ -138,9 +138,9 @@ namespace Piccolo // class ss; // class jkj; // template<> - // PJson PSerializer::write(const ss& instance); + // Json Serializer::write(const ss& instance); // template<> - // PJson PSerializer::write(const jkj& instance); + // Json Serializer::write(const jkj& instance); /*REFLECTION_TYPE(jkj) CLASS(jkj,Fields) @@ -160,9 +160,9 @@ namespace Piccolo ////template of generation coder //////////////////////////////////// // template<> - // PJson PSerializer::write(const test_class& instance); + // Json Serializer::write(const test_class& instance); // template<> - // test_class& PSerializer::read(const PJson& json_context, test_class& instance); + // test_class& Serializer::read(const Json& json_context, test_class& instance); // //////////////////////////////////// diff --git a/engine/source/runtime/engine.cpp b/engine/source/runtime/engine.cpp index 24b88c98f..fbf0a51c1 100644 --- a/engine/source/runtime/engine.cpp +++ b/engine/source/runtime/engine.cpp @@ -19,7 +19,7 @@ namespace Piccolo void PiccoloEngine::startEngine(const std::string& config_file_path) { - Reflection::TypeMetaRegister::Register(); + Reflection::TypeMetaRegister::metaRegister(); g_runtime_global_context.startSystems(config_file_path); @@ -32,7 +32,7 @@ namespace Piccolo g_runtime_global_context.shutdownSystems(); - Reflection::TypeMetaRegister::Unregister(); + Reflection::TypeMetaRegister::metaUnregister(); } void PiccoloEngine::initialize() {} diff --git a/engine/source/runtime/resource/asset_manager/asset_manager.h b/engine/source/runtime/resource/asset_manager/asset_manager.h index 03eb1f828..9a361f450 100644 --- a/engine/source/runtime/resource/asset_manager/asset_manager.h +++ b/engine/source/runtime/resource/asset_manager/asset_manager.h @@ -34,14 +34,14 @@ namespace Piccolo // parse to json object and read to runtime res object std::string error; - auto&& asset_json = PJson::parse(asset_json_text, error); + auto&& asset_json = Json::parse(asset_json_text, error); if (!error.empty()) { LOG_ERROR("parse json file {} failed!", asset_url); return false; } - PSerializer::read(asset_json, out_asset); + Serializer::read(asset_json, out_asset); return true; } @@ -56,7 +56,7 @@ namespace Piccolo } // write to json object and dump to string - auto&& asset_json = PSerializer::write(out_asset); + auto&& asset_json = Serializer::write(out_asset); std::string&& asset_json_text = asset_json.dump(); // write to file diff --git a/engine/template/allReflectionFile.mustache b/engine/template/allReflectionFile.mustache index 972b3b15c..387760411 100644 --- a/engine/template/allReflectionFile.mustache +++ b/engine/template/allReflectionFile.mustache @@ -7,9 +7,9 @@ namespace Piccolo{ namespace Reflection{ - void TypeMetaRegister::Register(){ - {{#class_defines}}TypeWrappersRegister::{{class_name}}(); - {{/class_defines}} + void TypeMetaRegister::metaRegister(){ +{{#class_defines}}TypeWrappersRegister::{{class_name}}(); +{{/class_defines}} } } } \ No newline at end of file diff --git a/engine/template/allSerializer.ipp.mustache b/engine/template/allSerializer.ipp.mustache index 4db60cb61..521d8030c 100644 --- a/engine/template/allSerializer.ipp.mustache +++ b/engine/template/allSerializer.ipp.mustache @@ -5,33 +5,33 @@ namespace Piccolo{ {{#class_defines}} template<> - PJson PSerializer::write(const {{class_name}}& instance){ - PJson::object ret_context; - {{#class_base_class_defines}}auto&& json_context_{{class_base_class_index}} = PSerializer::write(*({{class_base_class_name}}*)&instance); + Json Serializer::write(const {{class_name}}& instance){ + Json::object ret_context; + {{#class_base_class_defines}}auto&& json_context_{{class_base_class_index}} = Serializer::write(*({{class_base_class_name}}*)&instance); assert(json_context_{{class_base_class_index}}.is_object()); auto&& json_context_map_{{class_base_class_index}} = json_context_{{class_base_class_index}}.object_items(); ret_context.insert(json_context_map_{{class_base_class_index}}.begin() , json_context_map_{{class_base_class_index}}.end());{{/class_base_class_defines}} - {{#class_field_defines}}{{#class_field_is_vector}}PJson::array {{class_field_name}}_json; + {{#class_field_defines}}{{#class_field_is_vector}}Json::array {{class_field_name}}_json; for (auto& item : instance.{{class_field_name}}){ - {{class_field_name}}_json.emplace_back(PSerializer::write(item)); + {{class_field_name}}_json.emplace_back(Serializer::write(item)); } ret_context.insert_or_assign("{{class_field_display_name}}",{{class_field_name}}_json);{{/class_field_is_vector}} - {{^class_field_is_vector}}ret_context.insert_or_assign("{{class_field_display_name}}", PSerializer::write(instance.{{class_field_name}}));{{/class_field_is_vector}} + {{^class_field_is_vector}}ret_context.insert_or_assign("{{class_field_display_name}}", Serializer::write(instance.{{class_field_name}}));{{/class_field_is_vector}} {{/class_field_defines}} - return PJson(ret_context); + return Json(ret_context); } template<> - {{class_name}}& PSerializer::read(const PJson& json_context, {{class_name}}& instance){ + {{class_name}}& Serializer::read(const Json& json_context, {{class_name}}& instance){ assert(json_context.is_object()); - {{#class_base_class_defines}}PSerializer::read(json_context,*({{class_base_class_name}}*)&instance);{{/class_base_class_defines}} + {{#class_base_class_defines}}Serializer::read(json_context,*({{class_base_class_name}}*)&instance);{{/class_base_class_defines}} {{#class_field_defines}} if(!json_context["{{class_field_display_name}}"].is_null()){ {{#class_field_is_vector}}assert(json_context["{{class_field_display_name}}"].is_array()); - PJson::array array_{{class_field_name}} = json_context["{{class_field_display_name}}"].array_items(); + Json::array array_{{class_field_name}} = json_context["{{class_field_display_name}}"].array_items(); instance.{{class_field_name}}.resize(array_{{class_field_name}}.size()); for (size_t index=0; index < array_{{class_field_name}}.size();++index){ - PSerializer::read(array_{{class_field_name}}[index], instance.{{class_field_name}}[index]); - }{{/class_field_is_vector}}{{^class_field_is_vector}}PSerializer::read(json_context["{{class_field_display_name}}"], instance.{{class_field_name}});{{/class_field_is_vector}} + Serializer::read(array_{{class_field_name}}[index], instance.{{class_field_name}}[index]); + }{{/class_field_is_vector}}{{^class_field_is_vector}}Serializer::read(json_context["{{class_field_display_name}}"], instance.{{class_field_name}});{{/class_field_is_vector}} }{{/class_field_defines}} return instance; }{{/class_defines}} diff --git a/engine/template/commonReflectionFile.mustache b/engine/template/commonReflectionFile.mustache index b00d0a37b..68984ee62 100644 --- a/engine/template/commonReflectionFile.mustache +++ b/engine/template/commonReflectionFile.mustache @@ -11,13 +11,13 @@ namespace Reflection{ class Type{{class_name}}Operator{ public: static const char* getClassName(){ return "{{class_name}}";} - static void* constructorWithJson(const PJson& json_context){ + static void* constructorWithJson(const Json& json_context){ {{class_name}}* ret_instance= new {{class_name}}; - PSerializer::read(json_context, *ret_instance); + Serializer::read(json_context, *ret_instance); return ret_instance; } - static PJson writeByName(void* instance){ - return PSerializer::write(*({{class_name}}*)instance); + static Json writeByName(void* instance){ + return Serializer::write(*({{class_name}}*)instance); } // base class static int get{{class_name}}BaseClassReflectionInstanceList(ReflectionInstance* &out_list, void* instance){ @@ -64,29 +64,29 @@ namespace Reflection{ }//namespace ArrayReflectionOperator{{/vector_exist}} void TypeWrapperRegister_{{class_name}}(){ - {{#class_field_defines}}FieldFunctionTuple* f_field_function_tuple_{{class_field_name}}=new FieldFunctionTuple( + {{#class_field_defines}}FieldFunctionTuple* field_function_tuple_{{class_field_name}}=new FieldFunctionTuple( &TypeFieldReflectionOparator::Type{{class_name}}Operator::set_{{class_field_name}}, &TypeFieldReflectionOparator::Type{{class_name}}Operator::get_{{class_field_name}}, &TypeFieldReflectionOparator::Type{{class_name}}Operator::getClassName, &TypeFieldReflectionOparator::Type{{class_name}}Operator::getFieldName_{{class_field_name}}, &TypeFieldReflectionOparator::Type{{class_name}}Operator::getFieldTypeName_{{class_field_name}}, &TypeFieldReflectionOparator::Type{{class_name}}Operator::isArray_{{class_field_name}}); - REGISTER_FIELD_TO_MAP("{{class_name}}", f_field_function_tuple_{{class_field_name}}); + REGISTER_FIELD_TO_MAP("{{class_name}}", field_function_tuple_{{class_field_name}}); {{/class_field_defines}} - {{#vector_exist}}{{#vector_defines}}ArrayFunctionTuple* f_array_tuple_{{vector_useful_name}} = new ArrayFunctionTuple( + {{#vector_exist}}{{#vector_defines}}ArrayFunctionTuple* array_tuple_{{vector_useful_name}} = new ArrayFunctionTuple( &ArrayReflectionOperator::Array{{vector_useful_name}}Operator::set, &ArrayReflectionOperator::Array{{vector_useful_name}}Operator::get, &ArrayReflectionOperator::Array{{vector_useful_name}}Operator::getSize, &ArrayReflectionOperator::Array{{vector_useful_name}}Operator::getArrayTypeName, &ArrayReflectionOperator::Array{{vector_useful_name}}Operator::getElementTypeName); - REGISTER_ARRAY_TO_MAP("{{{vector_type_name}}}", f_array_tuple_{{vector_useful_name}}); + REGISTER_ARRAY_TO_MAP("{{{vector_type_name}}}", array_tuple_{{vector_useful_name}}); {{/vector_defines}}{{/vector_exist}} - {{#class_need_register}}ClassFunctionTuple* f_class_function_tuple_{{class_name}}=new ClassFunctionTuple( + {{#class_need_register}}ClassFunctionTuple* class_function_tuple_{{class_name}}=new ClassFunctionTuple( &TypeFieldReflectionOparator::Type{{class_name}}Operator::get{{class_name}}BaseClassReflectionInstanceList, &TypeFieldReflectionOparator::Type{{class_name}}Operator::constructorWithJson, &TypeFieldReflectionOparator::Type{{class_name}}Operator::writeByName); - REGISTER_BASE_CLASS_TO_MAP("{{class_name}}", f_class_function_tuple_{{class_name}}); + REGISTER_BASE_CLASS_TO_MAP("{{class_name}}", class_function_tuple_{{class_name}}); {{/class_need_register}} }{{/class_defines}} namespace TypeWrappersRegister{ diff --git a/engine/template/commonSerializerGenFile.mustache b/engine/template/commonSerializerGenFile.mustache index 84e336030..e43e290f6 100644 --- a/engine/template/commonSerializerGenFile.mustache +++ b/engine/template/commonSerializerGenFile.mustache @@ -5,8 +5,8 @@ namespace Piccolo{ {{#class_defines}}template<> - PJson PSerializer::write(const {{class_name}}& instance); + Json Serializer::write(const {{class_name}}& instance); template<> - {{class_name}}& PSerializer::read(const PJson& json_context, {{class_name}}& instance); + {{class_name}}& Serializer::read(const Json& json_context, {{class_name}}& instance); {{/class_defines}} }//namespace From 23e01369ac00f2b921875362d6f1a450a15a46ec Mon Sep 17 00:00:00 2001 From: kiki Date: Wed, 30 Nov 2022 09:56:07 +0800 Subject: [PATCH 2/3] modify type wrapper register. (#396) * modify type wrapper register. modify type wrapper register. * modify function. modify function. * modify input define. modify input define. * modify input define. modify input define. * Fix linux compile bug. Fix linux compile bug. * use right-value reference. use right-value reference. --- .../parser/generator/reflection_generator.cpp | 19 +++++++++++-------- .../parser/generator/reflection_generator.h | 2 +- .../meta_parser/parser/meta/meta_utils.cpp | 12 +++++++++++- .../meta_parser/parser/meta/meta_utils.h | 5 ++++- engine/template/allReflectionFile.mustache | 4 ++-- engine/template/commonReflectionFile.mustache | 6 ++++-- 6 files changed, 33 insertions(+), 15 deletions(-) diff --git a/engine/source/meta_parser/parser/generator/reflection_generator.cpp b/engine/source/meta_parser/parser/generator/reflection_generator.cpp index 771c96e88..eb2c078d8 100644 --- a/engine/source/meta_parser/parser/generator/reflection_generator.cpp +++ b/engine/source/meta_parser/parser/generator/reflection_generator.cpp @@ -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); @@ -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; @@ -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"); diff --git a/engine/source/meta_parser/parser/generator/reflection_generator.h b/engine/source/meta_parser/parser/generator/reflection_generator.h index 7dd256713..a55973712 100644 --- a/engine/source/meta_parser/parser/generator/reflection_generator.h +++ b/engine/source/meta_parser/parser/generator/reflection_generator.h @@ -17,6 +17,6 @@ namespace Generator private: std::vector m_head_file_list; - std::vector m_type_list; + std::vector m_sourcefile_list; }; } // namespace Generator diff --git a/engine/source/meta_parser/parser/meta/meta_utils.cpp b/engine/source/meta_parser/parser/meta/meta_utils.cpp index 6cce2db9a..e2dda3f22 100644 --- a/engine/source/meta_parser/parser/meta/meta_utils.cpp +++ b/engine/source/meta_parser/parser/meta/meta_utils.cpp @@ -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 diff --git a/engine/source/meta_parser/parser/meta/meta_utils.h b/engine/source/meta_parser/parser/meta/meta_utils.h index 646fae357..a7e696436 100644 --- a/engine/source/meta_parser/parser/meta/meta_utils.h +++ b/engine/source/meta_parser/parser/meta/meta_utils.h @@ -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" \ No newline at end of file +#include "meta_utils.hpp" diff --git a/engine/template/allReflectionFile.mustache b/engine/template/allReflectionFile.mustache index 387760411..21816c638 100644 --- a/engine/template/allReflectionFile.mustache +++ b/engine/template/allReflectionFile.mustache @@ -8,8 +8,8 @@ namespace Piccolo{ namespace Reflection{ void TypeMetaRegister::metaRegister(){ -{{#class_defines}}TypeWrappersRegister::{{class_name}}(); -{{/class_defines}} + {{#sourefile_names}}TypeWrappersRegister::{{sourefile_name_upper_camel_case}}(); + {{/sourefile_names}} } } } \ No newline at end of file diff --git a/engine/template/commonReflectionFile.mustache b/engine/template/commonReflectionFile.mustache index 68984ee62..2579c074a 100644 --- a/engine/template/commonReflectionFile.mustache +++ b/engine/template/commonReflectionFile.mustache @@ -90,9 +90,11 @@ namespace Reflection{ {{/class_need_register}} }{{/class_defines}} namespace TypeWrappersRegister{ - {{#class_defines}} - void {{class_name}}(){ TypeWrapperRegister_{{class_name}}();} + void {{sourefile_name_upper_camel_case}}() + { + {{#class_defines}}TypeWrapperRegister_{{class_name}}(); {{/class_defines}} + } }//namespace TypeWrappersRegister }//namespace Reflection From 36b71737fcc7e680fb10bc862a31a4be152876d4 Mon Sep 17 00:00:00 2001 From: ShenMian <70068015+ShenMian@users.noreply.github.com> Date: Wed, 30 Nov 2022 09:59:01 +0800 Subject: [PATCH 3/3] fix: fix a typo (#394) --- engine/source/runtime/CMakeLists.txt | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/engine/source/runtime/CMakeLists.txt b/engine/source/runtime/CMakeLists.txt index 6391f0aae..3a2fc0533 100644 --- a/engine/source/runtime/CMakeLists.txt +++ b/engine/source/runtime/CMakeLists.txt @@ -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 @@ -75,7 +74,7 @@ target_include_directories( target_include_directories( ${TARGET_NAME} - PUBLIC $ + PUBLIC $ ) target_include_directories(