Fix C Body Generator (Structure part) 66/171266/1
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 28 Feb 2018 03:51:38 +0000 (12:51 +0900)
committerHwankyu Jhun <h.jhun@samsung.com>
Wed, 28 Feb 2018 03:51:38 +0000 (12:51 +0900)
- Uses ReplaceAll method to reduce duplicated codes

Change-Id: I8006a6244d28a92586dc1a6b14d8da2e621e87b7
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
idlc/c_gen/c_body_gen_base.cc
idlc/c_gen/c_body_gen_base.h

index cddd737..c47cbd7 100644 (file)
@@ -53,8 +53,8 @@ void CBodyGeneratorBase::GenStructures(std::ofstream& stream) {
 void CBodyGeneratorBase::GenStructure(std::ofstream& stream,
                                       const Structure& st) {
   GenStructureDeclaration(stream, st);
-  GenStructureParcelTo(stream, st);
-  GenStructureParcelFrom(stream, st);
+  GenStructureParcelSerializer(stream, st);
+  GenStructureParcelDeserializer(stream, st);
   GenStructureConstructor(stream, st);
   GenStructureDestructor(stream, st);
   GenStructureCloner(stream, st);
@@ -91,12 +91,12 @@ void CBodyGeneratorBase::GenStructureDeclaration(std::ofstream& stream,
       );
 }
 
-void CBodyGeneratorBase::GenStructureParcelTo(std::ofstream& stream,
+void CBodyGeneratorBase::GenStructureParcelSerializer(std::ofstream& stream,
                                               const Structure& st) {
   const char block[] =
-      "static void __$$_to(rpc_port_parcel_h parcel, void* data)\n" \
+      "static void __##_to(rpc_port_parcel_h parcel, void* data)\n" \
       "{\n" \
-      "    rpc_port_$$_h h = data;\n" \
+      "    rpc_port_##_h h = data;\n" \
       "\n" \
       "    if (!parcel || !h) {\n" \
       "        LOGE(\"Invalid parameter\");\n" \
@@ -106,13 +106,7 @@ void CBodyGeneratorBase::GenStructureParcelTo(std::ofstream& stream,
       "}\n";
 
   stream << NLine(1);
-  stream << SmartIndent(GenTemplateString(block,
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
+  stream << SmartIndent(GenTemplateString(ReplaceAll(block, "##", st.GetID()),
         [&]()->std::string {
           std::string str;
           for (auto& i : st.GetElements().GetElms()) {
@@ -125,12 +119,12 @@ void CBodyGeneratorBase::GenStructureParcelTo(std::ofstream& stream,
       );
 }
 
-void CBodyGeneratorBase::GenStructureParcelFrom(std::ofstream& stream,
+void CBodyGeneratorBase::GenStructureParcelDeserializer(std::ofstream& stream,
                                                 const Structure& st) {
   const char block[] =
-      "static void __$$_from(rpc_port_parcel_h parcel, void* data)\n" \
+      "static void __##_from(rpc_port_parcel_h parcel, void* data)\n" \
       "{\n" \
-      "    rpc_port_$$_h h = data;\n" \
+      "    rpc_port_##_h h = data;\n" \
       "\n"
       "    if (!parcel || !h) {\n" \
       "        LOGE(\"Invalid parameter\");\n" \
@@ -140,13 +134,7 @@ void CBodyGeneratorBase::GenStructureParcelFrom(std::ofstream& stream,
       "}\n";
 
   stream << NLine(1);
-  stream << SmartIndent(GenTemplateString(block,
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
+  stream << SmartIndent(GenTemplateString(ReplaceAll(block, "##", st.GetID()),
         [&]()->std::string {
           std::string str;
           for (auto& i : st.GetElements().GetElms()) {
@@ -162,23 +150,23 @@ void CBodyGeneratorBase::GenStructureParcelFrom(std::ofstream& stream,
 void CBodyGeneratorBase::GenStructureConstructor(std::ofstream& stream,
                                                  const Structure& st) {
   const char block[] =
-      "int rpc_port_create_$$(rpc_port_$$_h* h)\n" \
+      "int rpc_port_create_##(rpc_port_##_h* h)\n" \
       "{\n" \
-      "    struct $$_s* handle;\n" \
+      "    struct ##_s* handle;\n" \
       "\n" \
       "    if (!h) {\n" \
       "        LOGE(\"Invalid parameter\");\n" \
       "        return -1;\n" \
       "    }\n" \
       "\n" \
-      "    handle = calloc(1, sizeof(struct $$_s));\n" \
+      "    handle = calloc(1, sizeof(struct ##_s));\n" \
       "    if (!handle) {\n" \
       "        LOGE(\"Out of memory\");\n" \
       "        return -1;\n" \
       "    }\n" \
       "\n"
-      "    handle->parcelable.to = __$$_to;\n" \
-      "    handle->parcelable.from = __$$_from;\n" \
+      "    handle->parcelable.to = __##_to;\n" \
+      "    handle->parcelable.from = __##_from;\n" \
       "\n"
       "    *h = handle;\n"
       "\n"
@@ -186,33 +174,13 @@ void CBodyGeneratorBase::GenStructureConstructor(std::ofstream& stream,
       "}\n";
 
   stream << NLine(1);
-  stream << SmartIndent(GenTemplateString(block,
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        }
-        )
-      );
+  stream << SmartIndent(ReplaceAll(block, "##", st.GetID()));
 }
 
 void CBodyGeneratorBase::GenStructureDestructor(std::ofstream& stream,
                                                 const Structure& st) {
   const char block[] =
-      "int rpc_port_destroy_$$(rpc_port_$$_h h)\n" \
+      "int rpc_port_destroy_##(rpc_port_##_h h)\n" \
       "{\n" \
       "    if (!h) {\n" \
       "        LOGE(\"Invalid parameter\");\n" \
@@ -226,13 +194,7 @@ void CBodyGeneratorBase::GenStructureDestructor(std::ofstream& stream,
       "}\n";
 
   stream << NLine(1);
-  stream << SmartIndent(GenTemplateString(block,
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
+  stream << SmartIndent(GenTemplateString(ReplaceAll(block, "##", st.GetID()),
         [&]()->std::string {
           std::string str;
           for (auto& i : st.GetElements().GetElms()) {
@@ -436,18 +398,18 @@ void CBodyGeneratorBase::GenStructureIterator(std::ofstream& stream,
 void CBodyGeneratorBase::GenStructureCloner(std::ofstream& stream,
                                             const Structure& st) {
   const char block[] =
-      "int rpc_port_clone_$$(rpc_port_$$_h h, rpc_port_$$_h* clone)\n" \
+      "int rpc_port_clone_##(rpc_port_##_h h, rpc_port_##_h* clone)\n" \
       "{\n" \
-      "    rpc_port_$$_h handle = NULL;\n" \
+      "    rpc_port_##_h handle = NULL;\n" \
       "\n" \
       "    if (!h || !clone) {\n" \
       "        LOGE(\"Invalid parameter\");\n" \
       "        return -1;\n" \
       "    }\n" \
       "\n" \
-      "    rpc_port_create_$$(&handle);\n" \
+      "    rpc_port_create_##(&handle);\n" \
       "    if (!handle) {\n" \
-      "        LOGE(\"Failed to create $$ handle\");\n" \
+      "        LOGE(\"Failed to create ## handle\");\n" \
       "        return -1;\n" \
       "    }\n" \
       "$$" \
@@ -458,25 +420,7 @@ void CBodyGeneratorBase::GenStructureCloner(std::ofstream& stream,
       "}\n";
 
   stream << NLine(1);
-  stream << SmartIndent(GenTemplateString(block,
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
-        [&]()->std::string {
-          return st.GetID();
-        },
+  stream << SmartIndent(GenTemplateString(ReplaceAll(block, "##", st.GetID()),
         [&]()->std::string {
           std::string str;
           for (auto& i : st.GetElements().GetElms()) {
index b65a505..55d6b0f 100644 (file)
@@ -53,8 +53,8 @@ class CBodyGeneratorBase : public CGeneratorBase {
  private:
   void GenStructure(std::ofstream& stream, const Structure& st);
   void GenStructureDeclaration(std::ofstream& stream, const Structure& st);
-  void GenStructureParcelTo(std::ofstream& stream, const Structure& st);
-  void GenStructureParcelFrom(std::ofstream& stream, const Structure& st);
+  void GenStructureParcelSerializer(std::ofstream& stream, const Structure& st);
+  void GenStructureParcelDeserializer(std::ofstream& stream, const Structure& st);
   void GenStructureConstructor(std::ofstream& stream, const Structure& st);
   void GenStructureDestructor(std::ofstream& stream, const Structure& st);
   void GenStructureSetter(std::ofstream& stream, const Structure& st);