Support Map & Set container for C# Generator 77/297177/4
authorjusung son <jusung07.son@samsung.com>
Fri, 11 Aug 2023 07:21:54 +0000 (16:21 +0900)
committerjusung son <jusung07.son@samsung.com>
Fri, 11 Aug 2023 10:30:23 +0000 (19:30 +0900)
This patch supports map and set container types.

Change-Id: I3f8284ad24a321222d5ba8fbb7ab8876f0a3852c
Signed-off-by: jusung son <jusung07.son@samsung.com>
idlc/gen/version2/cs_generator_base.cc
idlc/gen/version2/cs_generator_base.h
idlc/gen/version2/cs_generator_base_cb.h

index b37fd25..f8d4301 100644 (file)
@@ -34,7 +34,7 @@ CsGeneratorBase::CsGeneratorBase(std::shared_ptr<Document> 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("<TYPE>", st.GetID())
                       .Change("<BODY>", [&]() {
@@ -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("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+              .Change("<VALUE_TYPE>", type_string);
+  } else if (type.ToString() == "set") {
+    ret  = ReplaceAll(CB_LIST_SET_WRITE)
+              .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetMetaType()));
+  } else if (type.GetMetaType()->IsEnumType()) {
+    ret  = ReplaceAll(CB_LIST_WRITE)
+              .Change("<VALUE_TYPE>", std::string("int"));
+  } else {
+    ret  = ReplaceAll(CB_LIST_WRITE)
+              .Change("<VALUE_TYPE>", 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("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+              .Change("<TYPE_STRING>", std::string("int"))
+              .Change("<VALUE_TYPE>",  type.GetValueType()->ToString());
+    } else {
+      ret  = ReplaceAll(CB_LIST_MAP_READ)
+              .Change("<KEY_TYPE>", ConvertTypeToString(*type.GetKeyType()))
+              .Change("<TYPE_STRING>", ConvertTypeToString(*type.GetKeyType()))
+              .Change("<VALUE_TYPE>", ConvertTypeToString(*type.GetValueType()));
+    }
+  } else if (type.ToString() == "set") {
+    ret  = ReplaceAll(CB_LIST_SET_READ)
+              .Change("<VALUE_TYPE>", 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("<TYPE_STRING>", std::string("int"))
+              .Change("<LIST_METHOD>", list_method)
+              .Change("<VALUE_TYPE>", type.GetMetaType()->ToString());
+  } else {
+    std::string list_method;
+    if (type.ToString() == "list")
+      list_method = "Last";
+
+    ret  = ReplaceAll(CB_LIST_READ)
+              .Change("<TYPE_STRING>", ConvertTypeToString(*type.GetMetaType()))
+              .Change("<LIST_METHOD>", list_method)
+              .Change("<VALUE_TYPE>", 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>", type_str)
-                      .Change("<BODY>", [&]() {
-                        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("<LENGTH_NAME>", GetLengthName(type))
+                      .Change("<WRITE_LIST>", GenListWrite(type, type_str));
 
-    str += ReplaceAll(CB_LIST_DESERIALIZER)
-               .Change("<TYPE>", type_str)
-               .Change("<BODY>", [&]() {
-                 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>", type_str)
+                      .Change("<LENGTH_NAME>", GetLengthName(type))
+                      .Change("<READ_LIST>", 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<std::string, BaseType> types;
     BaseType bI("int", "int");
index 08dfc32..5032236 100644 (file)
@@ -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<std::string, std::string> type_map_;
index 1cd2189..54b85e7 100644 (file)
@@ -278,17 +278,39 @@ R"__cs_cb(
             }
 )__cs_cb";
 
+constexpr const char CB_LIST_MAP_WRITE[] =
+R"__cs_cb(                    map.Write("key-" + index.ToString(), "<KEY_TYPE>", i.Key);
+                    map.Write("value-" + index.ToString(), "<VALUE_TYPE>", i.Value);)__cs_cb";
+
+constexpr const char CB_LIST_SET_WRITE[] =
+R"__cs_cb(                    map.Write("key-" + index.ToString(), "<VALUE_TYPE>", i);)__cs_cb";
+
+constexpr const char CB_LIST_WRITE[] =
+R"__cs_cb(                    map.Write(index.ToString(), "<VALUE_TYPE>", i);)__cs_cb";
+
+constexpr const char CB_LIST_MAP_READ[] =
+R"__cs_cb(                    map.Read("key-" + index.ToString(), "<KEY_TYPE>", out <KEY_TYPE> key);
+                    map.Read("value-" + index.ToString(), "<TYPE_STRING>", out <VALUE_TYPE> value);
+                    param[key] = value;)__cs_cb";
+
+constexpr const char CB_LIST_SET_READ[] =
+R"__cs_cb(                    map.Read("key-" + index.ToString(), "<VALUE_TYPE>", out <VALUE_TYPE> value);
+                     param.Add(value);)__cs_cb";
+
+constexpr const char CB_LIST_READ[] =
+R"__cs_cb(                    map.Read("key-" + index.ToString(), "<TYPE_STRING>", out <VALUE_TYPE> value);
+                    param.Add<LIST_METHOD>(value);)__cs_cb";
+
 const char CB_LIST_SERIALIZER[] =
 R"__cs_cb(
             private static void Serialize(Parcel h, <TYPE> param)
             {
                 UnitMap map = new UnitMap();
-
-                map.Write("length", "int", param.Count & int.MaxValue);
+                map.Write("<LENGTH_NAME>", "int", param.Count & int.MaxValue);
                 int index = 0;
                 foreach (var i in param)
                 {
-<BODY>
+<WRITE_LIST>
                     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("<LENGTH_NAME>", "int", out int length);
+                for (int index = 0; index < length; index++)
                 {
-<BODY>
+<READ_LIST>
                 }
             }
 )__cs_cb";