From 302c2c1da0285f0f7df45276f340a4e07df26dea Mon Sep 17 00:00:00 2001 From: jusung son Date: Fri, 11 Aug 2023 16:21:54 +0900 Subject: [PATCH] Support Map & Set container for C# Generator This patch supports map and set container types. Change-Id: I3f8284ad24a321222d5ba8fbb7ab8876f0a3852c Signed-off-by: jusung son --- idlc/gen/version2/cs_generator_base.cc | 162 ++++++++++++++++++++++--------- idlc/gen/version2/cs_generator_base.h | 5 + idlc/gen/version2/cs_generator_base_cb.h | 34 +++++-- 3 files changed, 149 insertions(+), 52 deletions(-) diff --git a/idlc/gen/version2/cs_generator_base.cc b/idlc/gen/version2/cs_generator_base.cc index b37fd25..f8d4301 100644 --- a/idlc/gen/version2/cs_generator_base.cc +++ b/idlc/gen/version2/cs_generator_base.cc @@ -34,7 +34,7 @@ CsGeneratorBase::CsGeneratorBase(std::shared_ptr doc) {"long", "long"}, {"string", "string"}, {"bool", "bool"}, {"list", "LinkedList"}, {"array", "List"}, {"float", "float"}, {"double", "double"}, {"bundle", "Bundle"}, {"void", "void"}, - {"file", "string"}}; + {"file", "string"} , {"set", "HashSet"}, {"map", "Dictionary"}}; parcel_type_map_ = {{"char", "Byte"}, {"int", "Int"}, {"short", "Short"}, {"long", "Long"}, @@ -89,8 +89,11 @@ void CsGeneratorBase::GenStructure(std::ofstream& stream, const Structure& st) { CB_PROPERTY, stream, [&]() -> std::string { return ConvertTypeToString(i->GetType()); }, [&]() -> std::string { return i->GetID(); }); - if (i->GetType().ToString() == "bundle") { - v.push_back(i->GetID() + " = " + "new Bundle()"); + if (i->GetType().ToString() == "bundle" || + i->GetType().ToString() == "map" || + i->GetType().ToString() == "set") { + v.push_back(i->GetID() + " = " + "new " + + ConvertTypeToString(i->GetType()) + "()"); } } @@ -112,6 +115,7 @@ void CsGeneratorBase::GenSerializer(std::ofstream& stream, } std::string CsGeneratorBase::GetSerializer(const Structure& st) { + std::string str(ReplaceAll(CB_SERIALIZER) .Change("", st.GetID()) .Change("", [&]() { @@ -183,55 +187,112 @@ void CsGeneratorBase::AddSerializerList(const BaseType& type, serializer_list_[ConvertTypeToString(type, st_id)] = type; AddSerializerList(*type.GetMetaType(), st_id); + } else if (type.GetKeyType() != nullptr && type.GetValueType() != nullptr) { + if (type.GetValueType()->IsEnumType() && !st_id.empty()) { + BaseType new_type(type.ToString(), "", type.GetUserDefinedType()); + BaseType* new_key = new BaseType(*(type.GetKeyType())); + BaseType* new_value = new BaseType(st_id + "." + + type.GetValueType()->ToString(), "", BaseType::UserType::ENUM); + + new_type.SetKeyType(new_key); + new_type.SetValueType(new_value); + serializer_list_[ConvertTypeToString(type, st_id)] = new_type; + return; + } + serializer_list_[ConvertTypeToString(type, st_id)] = type; + } +} + +std::string CsGeneratorBase::GenListWrite(const BaseType& type, + const std::string& type_str) { + std::string ret; + std::string type_string; + if (type.ToString() == "map") { + if (type.GetValueType()->IsEnumType()) + type_string = "int"; + else + type_string = ConvertTypeToString(*type.GetValueType()); + + ret = ReplaceAll(CB_LIST_MAP_WRITE) + .Change("", ConvertTypeToString(*type.GetKeyType())) + .Change("", type_string); + } else if (type.ToString() == "set") { + ret = ReplaceAll(CB_LIST_SET_WRITE) + .Change("", ConvertTypeToString(*type.GetMetaType())); + } else if (type.GetMetaType()->IsEnumType()) { + ret = ReplaceAll(CB_LIST_WRITE) + .Change("", std::string("int")); + } else { + ret = ReplaceAll(CB_LIST_WRITE) + .Change("", ConvertTypeToString(*type.GetMetaType())); } + + return ret; } +std::string CsGeneratorBase::GenListRead(const BaseType& type, + const std::string& type_str) { + std::string ret; + + if (type.ToString() == "map") { + if (type.GetValueType()->IsEnumType()) { + ret = ReplaceAll(CB_LIST_MAP_READ) + .Change("", ConvertTypeToString(*type.GetKeyType())) + .Change("", std::string("int")) + .Change("", type.GetValueType()->ToString()); + } else { + ret = ReplaceAll(CB_LIST_MAP_READ) + .Change("", ConvertTypeToString(*type.GetKeyType())) + .Change("", ConvertTypeToString(*type.GetKeyType())) + .Change("", ConvertTypeToString(*type.GetValueType())); + } + } else if (type.ToString() == "set") { + ret = ReplaceAll(CB_LIST_SET_READ) + .Change("", ConvertTypeToString(*type.GetMetaType())); + } else if (type.GetMetaType()->IsEnumType()) { + std::string list_method; + if (type.ToString() == "list") + list_method = "Last"; + + ret = ReplaceAll(CB_LIST_READ) + .Change("", std::string("int")) + .Change("", list_method) + .Change("", type.GetMetaType()->ToString()); + } else { + std::string list_method; + if (type.ToString() == "list") + list_method = "Last"; + + ret = ReplaceAll(CB_LIST_READ) + .Change("", ConvertTypeToString(*type.GetMetaType())) + .Change("", list_method) + .Change("", ConvertTypeToString(*type.GetMetaType())); + } + + return ret; +} + +std::string CsGeneratorBase::GetLengthName(const BaseType& type) { + if (type.ToString() == "list") { + return "length"; + } else { + return "size"; + } +} std::string CsGeneratorBase::GetListSerializer(const BaseType& type, const std::string& type_str) { - std::string str(ReplaceAll(CB_LIST_SERIALIZER) + std::string code; + code = ReplaceAll(CB_LIST_SERIALIZER) .Change("", type_str) - .Change("", [&]() { - std::string ret; - auto* ptr = type.GetMetaType(); - if (ptr == nullptr) return ret; - - auto& mt = *ptr; - std::string param_str; - if (mt.GetUserDefinedType() == BaseType::UserType::ENUM) - param_str = "int"; - else - param_str = ConvertTypeToString(mt); - - ret += Tab(5) + "map.Write(index.ToString(), \"" + - param_str + "\", i);" + NLine(1); - return ret; - })); + .Change("", GetLengthName(type)) + .Change("", GenListWrite(type, type_str)); - str += ReplaceAll(CB_LIST_DESERIALIZER) - .Change("", type_str) - .Change("", [&]() { - std::string ret; - auto* ptr = type.GetMetaType(); - if (ptr == nullptr) return ret; - - auto& mt = *ptr; - if (mt.GetUserDefinedType() == BaseType::UserType::ENUM) { - ret += Tab(5) + "map.Read(index.ToString(), \"int\", out " + - mt.ToString() + " p);" + NLine(1); - } else { - ret += Tab(5) + "map.Read(index.ToString(), \"" + - ConvertTypeToString(mt) + - "\", out "+ ConvertTypeToString(mt) - + " p);" + NLine(1); - } - if (type.ToString() == "list") - ret += Tab(5) + "param.AddLast(p);" + NLine(1); - else - ret += Tab(5) + "param.Add(p);" + NLine(1); - return ret; - }); + code += ReplaceAll(CB_LIST_DESERIALIZER) + .Change("", type_str) + .Change("", GetLengthName(type)) + .Change("", GenListRead(type, type_str)); - return str; + return code; } void CsGeneratorBase::GenListSerializer(std::ofstream& stream) { @@ -283,6 +344,12 @@ std::string CsGeneratorBase::ConvertTypeToString(const BaseType& type, return type_map_[type.ToString()] + "<" + ConvertTypeToString(*(type.GetMetaType()), st_id) + ">"; + if (type.GetKeyType() != nullptr && type.GetValueType() != nullptr) { + return type_map_[type.ToString()] + "<" + + ConvertTypeToString(*(type.GetKeyType()), st_id) + ", " + + ConvertTypeToString(*(type.GetValueType()), st_id) + ">"; + } + return type_map_[type.ToString()]; } @@ -498,7 +565,8 @@ std::string CsGeneratorBase::GenUnitMaUintWrite(const Interface& iface, const BaseType& base_type) { std::string ret; - if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr) { + if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr + && base_type.GetKeyType() == nullptr) { ret = "unit.parcel.Write" + ConvertTypeToParcelType(base_type.ToString()) + "(value);"; } else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { @@ -517,7 +585,8 @@ std::string CsGeneratorBase::GenUnitMaUintRead(const Interface& iface, const std::string& type_str) { std::string ret; - if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr) { + if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr + && base_type.GetKeyType() == nullptr) { ret = "value = unit.parcel.Read" + ConvertTypeToParcelType(base_type.ToString()) + "();"; } else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { @@ -552,6 +621,7 @@ void CsGeneratorBase::AddTypeMap( } } } + std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { std::unordered_map types; BaseType bI("int", "int"); diff --git a/idlc/gen/version2/cs_generator_base.h b/idlc/gen/version2/cs_generator_base.h index 08dfc32..5032236 100644 --- a/idlc/gen/version2/cs_generator_base.h +++ b/idlc/gen/version2/cs_generator_base.h @@ -98,6 +98,11 @@ class CsGeneratorBase : public Generator { const std::string& type_str); std::string GenUnitMaUintWrite(const Interface& iface, const BaseType& base_type); + std::string GenListWrite(const BaseType& type, + const std::string& type_str); + std::string GenListRead(const BaseType& type, + const std::string& type_str); + std::string GetLengthName(const BaseType& type); private: std::map type_map_; diff --git a/idlc/gen/version2/cs_generator_base_cb.h b/idlc/gen/version2/cs_generator_base_cb.h index 1cd2189..54b85e7 100644 --- a/idlc/gen/version2/cs_generator_base_cb.h +++ b/idlc/gen/version2/cs_generator_base_cb.h @@ -278,17 +278,39 @@ R"__cs_cb( } )__cs_cb"; +constexpr const char CB_LIST_MAP_WRITE[] = +R"__cs_cb( map.Write("key-" + index.ToString(), "", i.Key); + map.Write("value-" + index.ToString(), "", i.Value);)__cs_cb"; + +constexpr const char CB_LIST_SET_WRITE[] = +R"__cs_cb( map.Write("key-" + index.ToString(), "", i);)__cs_cb"; + +constexpr const char CB_LIST_WRITE[] = +R"__cs_cb( map.Write(index.ToString(), "", i);)__cs_cb"; + +constexpr const char CB_LIST_MAP_READ[] = +R"__cs_cb( map.Read("key-" + index.ToString(), "", out key); + map.Read("value-" + index.ToString(), "", out value); + param[key] = value;)__cs_cb"; + +constexpr const char CB_LIST_SET_READ[] = +R"__cs_cb( map.Read("key-" + index.ToString(), "", out value); + param.Add(value);)__cs_cb"; + +constexpr const char CB_LIST_READ[] = +R"__cs_cb( map.Read("key-" + index.ToString(), "", out value); + param.Add(value);)__cs_cb"; + const char CB_LIST_SERIALIZER[] = R"__cs_cb( private static void Serialize(Parcel h, param) { UnitMap map = new UnitMap(); - - map.Write("length", "int", param.Count & int.MaxValue); + map.Write("", "int", param.Count & int.MaxValue); int index = 0; foreach (var i in param) { - + index++; } map.Serialize(h); @@ -301,10 +323,10 @@ R"__cs_cb( { UnitMap map = new UnitMap(); map.Deserialize(h); - map.Read("length", "int", out int index); - for (int i = 0; index < i; i++) + map.Read("", "int", out int length); + for (int index = 0; index < length; index++) { - + } } )__cs_cb"; -- 2.7.4