Refactor c++ generator (struct part) 82/170182/9
authorJunghoon Park <jh9216.park@samsung.com>
Wed, 14 Feb 2018 04:46:36 +0000 (13:46 +0900)
committerJunghoon Park <jh9216.park@samsung.com>
Tue, 20 Feb 2018 13:39:33 +0000 (22:39 +0900)
Change-Id: Ic959a3034da42abf86ae27adde5a86afeede1a30
Signed-off-by: Junghoon Park <jh9216.park@samsung.com>
idlc/cpp_gen/cpp_gen_base.cc
idlc/cpp_gen/cpp_gen_base.h
idlc/cpp_gen/cpp_proxy_body_gen.cc
idlc/cpp_gen/cpp_proxy_header_gen.cc
idlc/cpp_gen/cpp_stub_body_gen.cc
idlc/cpp_gen/cpp_stub_header_gen.cc
idlc/generator.h

index 611f897..feae307 100644 (file)
@@ -27,7 +27,7 @@ CppGeneratorBase::CppGeneratorBase(std::shared_ptr<Document> doc)
       {"char", "char"}, {"int", "int"}, {"short", "short"},
       {"long", "long"}, {"string", "std::string"}, {"bool", "bool"},
       {"list", "std::list"}, {"float","float"}, {"double", "double"},
-      {"bundle", "bundle*"}, {"void", "void"}
+      {"bundle", "Bundle"}, {"void", "void"}, {"array", "std::vector"}
   };
 
   parcel_type_map_ = {
@@ -44,6 +44,49 @@ CppGeneratorBase::CppGeneratorBase(std::shared_ptr<Document> doc)
 }
 
 void CppGeneratorBase::GenStructuresForHeader(std::ofstream& stream) {
+ const char* cls = R"__cls_bundle(class Bundle final {
+ public:
+  Bundle() {
+    raw_ = bundle_create();
+  }
+
+  Bundle(bundle* b) {
+    raw_ = b;
+  }
+
+  ~Bundle() {
+    if (raw_)
+      bundle_free(raw_);
+  }
+
+  Bundle(Bundle&& b) : raw_(b.raw_) {
+    b.raw_ = nullptr;
+  }
+
+  Bundle& operator = (Bundle&& b) {
+    raw_ = b.raw_;
+    b.raw_ = nullptr;
+    return *this;
+  }
+
+  Bundle(const Bundle& b) : raw_(bundle_dup(b.GetHandle())) {}
+
+  Bundle& operator = (const Bundle& b) {
+    raw_ = bundle_dup(b.GetHandle());
+    return *this;
+  }
+
+  bundle* GetHandle() const {
+    return raw_;
+  }
+
+ private:
+  bundle* raw_;
+};
+
+)__cls_bundle";
+
+  stream << cls;
   for (auto& i : GetDocument().GetBlocks()) {
     if (i->GetType() != Block::TYPE_STRUCTURE)
       continue;
@@ -57,13 +100,11 @@ void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, const Struct
   std::vector<std::string> p;
   std::vector<std::string> v;
   std::vector<std::string> lv;
-  const char ctor[] = "    $$();\n" \
-                      "    $$($$);\n";
-  const char dtor[] = "    ~$$();\n";
-  const char setget[] = "$$";
+  const char ctor[] = "  $$();\n" \
+                      "  $$($$);\n";
   const char variable[] = "$$\n";
 
-  stream << "  class " << st.GetID() << " final ";
+  stream << "class " << st.GetID() << " final ";
 
   for (auto& i : st.GetElements().GetElms()) {
     if (i->GetType().GetMetaType() != nullptr) {
@@ -79,8 +120,8 @@ void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, const Struct
     }
   }
 
-  GenBrace(stream, TAB_SIZE, [&]() {
-    stream << Tab(1) << "public:" << NLine(1);
+  GenBrace(stream, 0, [&]() {
+    stream << public:" << NLine(1);
     GenTemplate(ctor, stream,
       [&]()->std::string {
         return st.GetID();
@@ -100,63 +141,87 @@ void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, const Struct
       }
     );
 
-    GenTemplate(dtor, stream,
-      [&]()->std::string {
-        return st.GetID();
-      }
-    );
-
     stream << NLine(1);
+    for (auto& i : st.GetElements().GetElms()) {
+      GenSetter(stream, *i);
+      GenGetter(stream, *i);
+      stream << NLine(1);
+    }
 
-    GenTemplate(setget, stream,
-      [&]()->std::string {
-        std::string str;
-        for (auto& i : st.GetElements().GetElms()) {
-          if (i->GetType().IsUserDefinedType()) {
-            str += Tab(2) + "void Set" + i->GetID() + "("
-                   + ConvertTypeToString(i->GetType()) + " " + i->GetID() + ")";
-            str += "{ " + i->GetID() + "_ = " + i->GetID() + "; " + "}";
-            str += NLine(1);
-            str += Tab(2) + ConvertTypeToString(i->GetType()) + "& Get" + i->GetID()
-                   + "() const " + "{ return *" + i->GetID() + "_ }";
-          } else if (i->GetType().GetMetaType() != nullptr) {
-            str += Tab(2) + "void Set" + i->GetID() + "("
-                   + ConvertTypeToString(i->GetType()) + " " + i->GetID() + ")";
-            str += "{ " + i->GetID() + "_ = " + i->GetID() + "; " + "}";
-            str += NLine(1);
-            str += Tab(2) + ConvertTypeToString(i->GetType()) + "& Get" + i->GetID()
-                   + "() const " + "{ return " + i->GetID() + "_ }";
-          }
-          else {
-            str += Tab(2) + "void Set" + i->GetID() + "("
-                   + ConvertTypeToString(i->GetType()) + " " + i->GetID() + ")";
-            str += "{ " + i->GetID() + "_ = " + i->GetID() + "; " + "}";
-            str += NLine(1);
-            str += Tab(2) + ConvertTypeToString(i->GetType()) + " Get" + i->GetID()
-                   + "() const " + "{ return " + i->GetID() + "_ }";
-          }
-
-          str += NLine(1);
-        }
-        return str;
-      }
-    );
-
-    stream << NLine(1);
-    stream << Tab(1) << "private:";
+    stream << " private:";
     GenTemplate(variable, stream,
       [&]()->std::string {
         std::string str;
         for (auto& i : v) {
-            str += "\n" + Tab(2) + i + "_;";
+            str += "\n" + Tab(1) + i + "_;";
         }
         for (auto& i : lv) {
-            str += "\n" + Tab(2) + i + "_;";
+            str += "\n" + Tab(1) + i + "_;";
         }
         return str;
       }
     );
-  }, false);
+  }, false, false);
+  stream << ";" << NLine(1);
+}
+
+void CppGeneratorBase::GenSetter(std::ofstream& stream, const Element& ele) {
+  const char setter[] =
+      "void Set$$($$ $$) {\n" \
+      "  $$_ = $$;\n" \
+      "}\n";
+
+  GenTemplate(AddIndent(TAB_SIZE, setter, true), stream,
+    [&]()->std::string {
+      return ele.GetID();
+    },
+    [&]()->std::string {
+      return ConvertTypeToString(ele.GetType());
+    },
+    [&]()->std::string {
+      return ele.GetID();
+    },
+    [&]()->std::string {
+      return ele.GetID();
+    },
+    [&]()->std::string {
+      if(ele.GetType().IsUserDefinedType() ||
+        ele.GetType().GetMetaType() != nullptr ||
+        ele.GetType().ToString() == "string" ||
+        ele.GetType().ToString() == "bundle") {
+        return "std::move(" + ele.GetID() + ")";
+      }
+
+      return ele.GetID();
+    }
+  );
+  stream << NLine(1);
+}
+
+void CppGeneratorBase::GenGetter(std::ofstream& stream, const Element& ele) {
+  const char getter[] =
+      "$$ Get$$() const {\n" \
+      "  return $$_;\n" \
+      "}\n";
+
+  GenTemplate(AddIndent(TAB_SIZE, getter, true), stream,
+    [&]()->std::string {
+      if(ele.GetType().IsUserDefinedType() ||
+        ele.GetType().GetMetaType() != nullptr ||
+        ele.GetType().ToString() == "string" ||
+        ele.GetType().ToString() == "bundle") {
+        return "const " + ConvertTypeToString(ele.GetType()) + "&";
+      }
+
+      return ConvertTypeToString(ele.GetType());
+    },
+    [&]()->std::string {
+      return ele.GetID();
+    },
+    [&]()->std::string {
+      return ele.GetID();
+    }
+  );
 }
 
 void CppGeneratorBase::GenStructuresForBody(std::ofstream& stream) {
@@ -171,10 +236,9 @@ void CppGeneratorBase::GenStructuresForBody(std::ofstream& stream) {
 
 void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, const Structure& st) {
   std::vector<std::pair<std::string, std::string>> v;
-  const char ctor[] = "  $$::$$() {}\n" \
-                      "  $$::$$($$)\n" \
+  const char ctor[] = "$$::$$() {}\n\n" \
+                      "$$::$$($$)\n" \
                       "    : $$ {}";
-  const char dtor[] = "  $$::~$$() $$";
 
   for (auto& i : st.GetElements().GetElms()) {
     if (i->GetType().GetMetaType() == nullptr) {
@@ -217,10 +281,7 @@ void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, const Structur
     [&]()->std::string {
       std::string str;
       for (auto& i : v) {
-        if (i.first == "std::string")
-          str += i.second + "_(std::move(" + i.second + "))";
-        else
-          str += i.second + "_(" + i.second + ")";
+        str += i.second + "_(std::move(" + i.second + "))";
 
         if (i != v.back())
           str += ", ";
@@ -228,29 +289,7 @@ void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, const Structur
       return str;
     }
   );
-
-  stream <<  NLine(1);
-  GenTemplate(dtor, stream,
-    [&]()->std::string {
-      return st.GetID();
-    },
-    [&]()->std::string {
-      return st.GetID();
-    },
-    [&]()->std::string {
-      std::string str;
-      str += "{" + NLine(1);
-      for (auto& i : v) {
-        if (i.first == "bundle*") {
-          str += Tab(2) + "if (" + i.second + "_) {" + NLine(1);
-          str += Tab(3) + "bundle_free(" + i.second + "_);" + NLine(1);
-          str += Tab(2) + "}" + NLine(1);
-        }
-      }
-      str += Tab(1) + "}" + NLine(1);
-      return str;
-    }
-  );
+  stream <<  NLine(2);
 }
 
 void CppGeneratorBase::GenSerializer(std::ofstream& stream) {
@@ -259,52 +298,60 @@ void CppGeneratorBase::GenSerializer(std::ofstream& stream) {
       continue;
     Structure& st = static_cast<Structure&>(*i);
     GenSerializer(stream, st);
-    stream << std::endl;
+    stream << NLine(1);
   }
 }
 
-void CppGeneratorBase::GenSerializer(std::ofstream& stream, const Structure& st) {
+void CppGeneratorBase::GenPrototype(std::ofstream& stream) {
+  for (auto& i : GetDocument().GetBlocks()) {
+    if (i->GetType() != Block::TYPE_STRUCTURE)
+      continue;
+    Structure& st = static_cast<Structure&>(*i);
+    GenSerializer(stream, st, true);
+    GenDeSerializer(stream, st, true);
+  }
+  GenListSerializer(stream, true);
+  stream << NLine(1);
+}
+
+void CppGeneratorBase::GenSerializer(std::ofstream& stream, const Structure& st,
+                                     bool proto) {
   const char parcel_str[] = "rpc_port_parcel_h";
 
-  stream << Tab(1) << parcel_str << " operater << ("
-         << parcel_str << " h, const " << st.GetID() << "& param) ";
-  GenBrace(stream, TAB_SIZE, [&]() {
+  stream << parcel_str << " operator << ("
+         << parcel_str << " h, const " << st.GetID() << "& param)";
+  if (proto) {
+    stream << ";" << NLine(1);
+    return;
+  }
+
+  stream << " ";
+  GenBrace(stream, 0, [&]() {
     for (auto& i : st.GetElements().GetElms()) {
-       if (i->GetType().ToString() == "list") {
-        stream << Tab(2) << "rpc_port_parcel_write_array_count(h, param."
-               << i->GetID() << "_.size());" << NLine(1);
-        stream << Tab(2) << "for (auto& i : param." << i->GetID() << "_) ";
-        GenBrace(stream, TAB_SIZE * 2, [&] {
-          stream << Tab(3) << "h << *i;" << NLine(1);
-        }, false);
-      } else if (i->GetType().ToString() == "string") {
-        stream << Tab(2) << "rpc_port_parcel_write_"
+      if (i->GetType().ToString() == "string") {
+        stream << Tab(1) << "rpc_port_parcel_write_"
                << parcel_type_map_[i->GetType().ToString()]
-               << "(h, &" << i->GetID() << "_.c_str());"
+               << "(h, param.Get" << i->GetID() << "().c_str());"
                << NLine(1);
       } else if (i->GetType().ToString() == "bundle") {
-        stream << Tab(2) << "if (param." << i->GetID() << "_) ";
-        GenBrace(stream, TAB_SIZE * 2, [&] {
-          stream << Tab(3) << "rpc_port_parcel_write_bundle(h, param."
-                 << i->GetType().ToString() << "_);" << NLine(1);
-        }, false);
-        stream << Tab(2) << "else ";
-        GenBrace(stream, TAB_SIZE * 2, [&] {
-          stream << Tab(3) << "bundle* b = bundle_create();" << NLine(1)
-                 << Tab(3) << "rpc_port_parcel_write_bundle(h, b);" << NLine(1)
-                 << Tab(3) << "bundle_free(b);" << NLine(1);
-        }, false);
+        stream << Tab(1) << "rpc_port_parcel_write_bundle(h, param.Get"
+               << i->GetID() << "().GetHandle());" << NLine(1);
+      } else if (i->GetType().GetMetaType() ||
+                 i->GetType().IsUserDefinedType()) {
+        stream << Tab(1) << "h << param.Get" << i->GetID() << "();"
+               << NLine(1);
       } else {
-        stream << Tab(2) << "rpc_port_parcel_write_"
+        stream << Tab(1) << "rpc_port_parcel_write_"
                << parcel_type_map_[i->GetType().ToString()]
-               << "(h, param." << i->GetID() << "_);"
+               << "(h, param.Get" << i->GetID() << "());"
                << NLine(1);
       }
+      stream << NLine(1);
     }
+    stream << Tab(1) << "return h;" << NLine(1);
   }, false);
 }
 
-
 void CppGeneratorBase::GenDeSerializer(std::ofstream& stream) {
   for (auto& i : GetDocument().GetBlocks()) {
     if (i->GetType() != Block::TYPE_STRUCTURE)
@@ -315,60 +362,65 @@ void CppGeneratorBase::GenDeSerializer(std::ofstream& stream) {
   }
 }
 
-void CppGeneratorBase::GenDeSerializer(std::ofstream& stream, const Structure& st) {
+void CppGeneratorBase::GenDeSerializer(std::ofstream& stream,
+                                       const Structure& st, bool proto) {
   const char parcel_str[] = "rpc_port_parcel_h";
 
-  stream << Tab(1) << parcel_str << " operater >> ("
-         << parcel_str << " h, " << st.GetID() << "& param) ";
-
-  GenBrace(stream, TAB_SIZE, [&]() {
-    for (auto& i : st.GetElements().GetElms()) {
-      if (i->GetType().ToString() == "string") {
-        stream << Tab(2) << "char* " << i->GetID() << " = nullptr;";
-        stream << NLine(1);
-      } else if (i->GetType().ToString() == "list") {
-        stream << Tab(2) << "int l = 0;";
-        stream << NLine(1);
-      }
-    }
-
-    stream << NLine(1);
+  stream << parcel_str << " operator >> ("
+         << parcel_str << " h, " << st.GetID() << "& param)";
+  if (proto) {
+    stream << ";" << NLine(1);
+    return;
+  }
 
+  stream << " ";
+  GenBrace(stream, 0, [&]() {
     for (auto& i : st.GetElements().GetElms()) {
       if (i->GetType().ToString() == "string") {
-        stream << Tab(2) << "rpc_port_parcel_read_"
+        stream << Tab(1) << "char* " << i->GetID() << " = nullptr;" << NLine(1);
+        stream << Tab(1) << "rpc_port_parcel_read_"
+               << parcel_type_map_[i->GetType().ToString()]
+               << "(h, &" << i->GetID() << ");"
+               << NLine(1);
+        stream << Tab(1) << "param.Set" << i->GetID() << "(" << i->GetID() << ");"
+               << NLine(1);
+        stream << Tab(1) << "free(" << i->GetID() << ");"
+               << NLine(1);
+      } else if (i->GetType().ToString() == "bundle") {
+        stream << Tab(1) << "bundle* " << i->GetID() << " = nullptr;" << NLine(1);
+        stream << Tab(1) << "rpc_port_parcel_read_"
                << parcel_type_map_[i->GetType().ToString()]
                << "(h, &" << i->GetID() << ");"
                << NLine(1);
-        stream << Tab(2) << "param." << i->GetID() << "_ = " << i->GetID() << ";"
+        stream << Tab(1) << "param.Set" << i->GetID() << "(Bundle(" << i->GetID() << "));"
+               << NLine(1);
+      } else if (i->GetType().GetMetaType() != nullptr ||
+                 i->GetType().IsUserDefinedType()) {
+        stream << Tab(1) << ConvertTypeToString(i->GetType())
+               << " " << i->GetID() << ";" << NLine(1);
+        stream << Tab(1) << "h >> " << i->GetID() << ";"
                << NLine(1);
-        stream << Tab(2) << "free(" << i->GetID() << ");"
+        stream << Tab(1) << "param.Set" << i->GetID() << "(std::move(" << i->GetID() << "));"
                << NLine(1);
-      } else if (i->GetType().ToString() == "list") {
-        stream << Tab(2) << "for (int i = 0; i < l; i++) ";
-        GenBrace(stream, TAB_SIZE * 2, [&] {
-          stream << Tab(3) << i->GetType().GetMetaType()->ToString() << "* value = new "
-                 << i->GetType().GetMetaType()->ToString() << "();"
-                 << NLine(1);
-          stream << NLine(1);
-          stream << Tab(3) << "h >> *value;"
-                 << NLine(1);
-          stream << Tab(3) << "param." << i->GetID() << "_.emplace_back(value);"
-                 << NLine(1);
-        }, false);
       } else {
-        stream << Tab(2) << "rpc_port_parcel_read_"
+        stream << Tab(1) << ConvertTypeToString(i->GetType())
+               << " " << i->GetID() << ";" << NLine(1);
+        stream << Tab(1) << "rpc_port_parcel_read_"
                << parcel_type_map_[i->GetType().ToString()]
-               << "(h, &param." << i->GetID() << "_);"
+               << "(h, &" << i->GetID() << ");"
+               << NLine(1);
+        stream << Tab(1) << "param.Set" << i->GetID() << "(" << i->GetID() << ");"
                << NLine(1);
       }
+      stream << NLine(1);
     }
+    stream << Tab(1) << "return h;" << NLine(1);
   }, false);
 }
 
 std::string CppGeneratorBase::ConvertTypeToString(const BaseType& type) {
   if (type.IsUserDefinedType())
-    return "std::unique_ptr<" + type.ToString() + ">";
+    return type.ToString();
 
   if (type.GetMetaType() != nullptr)
     return type_map_[type.ToString()] + "<" +
@@ -389,4 +441,96 @@ std::string CppGeneratorBase::NLine(int cnt) {
   return t;
 }
 
+void CppGeneratorBase::AddSerializerList(const BaseType& type) {
+  if (type.GetMetaType() != nullptr) {
+    serializer_list_[ConvertTypeToString(type)] = &type;
+    AddSerializerList(*type.GetMetaType());
+  }
+}
+
+void CppGeneratorBase::GenListSerializer(std::ofstream& stream,
+                                         const BaseType& type, bool proto) {
+  stream << "rpc_port_parcel_h operator << (rpc_port_parcel_h h, const "
+         << ConvertTypeToString(type) << "& c)";
+
+  if (proto) {
+    stream << ";" << NLine(1);
+    stream << "rpc_port_parcel_h operator >> (rpc_port_parcel_h h, "
+           << ConvertTypeToString(type) << "& c);" << NLine(1);
+    return;
+  }
+
+  stream << " ";
+  GenBrace(stream, 0, [&]() {
+    stream << Tab(1)
+           << "rpc_port_parcel_write_array_count(h, c.size());"
+           << NLine(1);
+    stream << Tab(1) << "for (auto& i : c) ";
+    GenBrace(stream, TAB_SIZE, [&]() {
+      auto& mt = *type.GetMetaType();
+      if (!mt.IsUserDefinedType() && mt.GetMetaType() == nullptr) {
+        stream << Tab(2) << "rpc_port_parcel_write_"
+                         << parcel_type_map_[mt.ToString()]
+                         << "(h, i);" << NLine(1);
+      } else {
+        stream << Tab(2) << "h << i;" << NLine(1);
+      }
+    }, false);
+    stream << Tab(1) << "return h;" << NLine(1);
+  }, false);
+  stream << NLine(1);
+
+  stream << "rpc_port_parcel_h operator >> (rpc_port_parcel_h h, "
+         << ConvertTypeToString(type) << "& c) ";
+  GenBrace(stream, 0, [&]() {
+    stream << Tab(1) << "int l = 0;" << NLine(1);
+    stream << Tab(1)
+           << "rpc_port_parcel_read_array_count(h, &l);" << NLine(1);
+    stream << Tab(1) << "for (int i = 0; i < l; i++) ";
+    GenBrace(stream, TAB_SIZE, [&]() {
+      auto& mt = *type.GetMetaType();
+      if (!mt.IsUserDefinedType() && mt.GetMetaType() == nullptr) {
+        stream << Tab(2) << ConvertTypeToString(mt) << " v;" << NLine(1);
+        stream << Tab(2) << "rpc_port_parcel_read_"
+                         << parcel_type_map_[mt.ToString()]
+                         << "(h, &v);" << NLine(1);
+      } else {
+        stream << Tab(2) << ConvertTypeToString(mt) << " v;" << NLine(1);
+        stream << Tab(2) << "h >> v;" << NLine(1);
+      }
+      stream << Tab(2) << "c.push_back(std::move(v));" << NLine(1);
+    }, false);
+    stream << Tab(1) << "return h;" << NLine(1);
+  }, false);
+  stream << NLine(1);
+}
+
+void CppGeneratorBase::GenListSerializer(std::ofstream& stream, bool proto) {
+  serializer_list_.clear();
+  for (auto& i : GetDocument().GetBlocks()) {
+    if (i->GetType() == Block::TYPE_STRUCTURE) {
+      const Structure& st = static_cast<const Structure&>(*i);
+      for (auto& j : st.GetElements().GetElms()) {
+        auto& t = j->GetType();
+        AddSerializerList(t);
+      }
+    } else if (i->GetType() == Block::TYPE_INTERFACE) {
+      const Interface& iface = static_cast<const Interface&>(*i);
+      for (auto& j : iface.GetDeclarations().GetDecls()) {
+        auto& t = j->GetType();
+        AddSerializerList(t);
+        for (auto& k : j->GetParameters().GetParams()) {
+          auto& t1 = k->GetParameterType().GetBaseType();
+          AddSerializerList(t1);
+        }
+      }
+    }
+  }
+
+  for (auto& p : serializer_list_) {
+    const BaseType* t = p.second;
+    GenListSerializer(stream, *t, proto);
+  }
+}
+
 }  // namespace tidl
index 1b77539..958d9ff 100644 (file)
@@ -33,24 +33,36 @@ class CppGeneratorBase : public Generator {
   virtual ~CppGeneratorBase() = default;
 
   void GenStructuresForHeader(std::ofstream& stream);
-  void GenStructureForHeader(std::ofstream& stream, const Structure& st);
   void GenStructuresForBody(std::ofstream& stream);
-  void GenStructureForBody(std::ofstream& stream, const Structure& st);
   void GenSerializer(std::ofstream& stream);
-  void GenSerializer(std::ofstream& stream, const Structure& st);
   void GenDeSerializer(std::ofstream& stream);
-  void GenDeSerializer(std::ofstream& stream, const Structure& st);
+  void GenListSerializer(std::ofstream& stream, bool proto = false);
+  void GenPrototype(std::ofstream& stream);
 
   std::string ConvertTypeToString(const BaseType& type);
   std::string Tab(int cnt);
   std::string NLine(int cnt);
 
+ private:
+  void GenSetter(std::ofstream& stream, const Element& ele);
+  void GenGetter(std::ofstream& stream, const Element& ele);
+  void AddSerializerList(const BaseType& type);
+  void GenListSerializer(std::ofstream& stream, const BaseType& type,
+                         bool proto = false);
+  void GenDeSerializer(std::ofstream& stream, const Structure& st,
+                       bool proto = false);
+  void GenSerializer(std::ofstream& stream, const Structure& st,
+                     bool proto = false);
+  void GenStructureForHeader(std::ofstream& stream, const Structure& st);
+  void GenStructureForBody(std::ofstream& stream, const Structure& st);
+
  protected:
   const int TAB_SIZE = 2;
 
  private:
   std::map<std::string, std::string> type_map_;
   std::map<std::string, std::string> parcel_type_map_;
+  std::map<std::string, const BaseType*> serializer_list_;
 };
 
 }  // namespace tidl
index 84dd56d..823035a 100644 (file)
@@ -22,14 +22,20 @@ CppProxyBodyGen::CppProxyBodyGen(std::shared_ptr<Document> doc)
     : CppGeneratorBase(doc) {}
 
 void CppProxyBodyGen::OnInitGen(std::ofstream& stream) {
-  stream << "#include <string>" << NLine(1)
-         << "#include <memory>" << NLine(1)
-         << "#include <stdlib>" << NLine(1)
-         << "#include <assert>" << NLine(1)
+  std::string key(".cc");
+  std::string header_file = FileName;
+
+  std::size_t found = header_file.rfind(key);
+  if (found != std::string::npos)
+    header_file.replace(found, key.length(), ".h");
+
+  stream << "#include <stdlib.h>" << NLine(1)
+         << "#include <assert.h>" << NLine(1)
          <<  NLine(1)
          << "#include <rpc-port-parcel.h>" << NLine(1)
-         << "#include <rpc-port.h>" << NLine(1);
-  stream <<  NLine(1);
+         << "#include <rpc-port.h>" << NLine(1)
+         <<  NLine(1)
+         << "#include \"" << header_file << "\"" << NLine(2);
   GenNamespace(stream);
 }
 
@@ -39,11 +45,14 @@ void CppProxyBodyGen::OnFiniGen(std::ofstream& stream) {
 void CppProxyBodyGen::GenNamespace(std::ofstream& stream) {
   stream << "namespace rpc_port ";
   GenBrace(stream, 0, [&]() {
+    stream <<  NLine(1);
+    GenPrototype(stream);
     GenStructuresForBody(stream);
     GenSerializer(stream);
     GenDeSerializer(stream);
-    stream << Tab(1) << "namespace proxy ";
-    GenBrace(stream, TAB_SIZE, [&]() {
+    GenListSerializer(stream);
+    stream << "namespace proxy ";
+    GenBrace(stream, 0, [&]() {
 //      GenInterfaces(stream);
     }, false);
   }, false);
index a82be74..761342d 100644 (file)
@@ -24,13 +24,11 @@ CppProxyHeaderGen::CppProxyHeaderGen(std::shared_ptr<Document> doc)
 void CppProxyHeaderGen::OnInitGen(std::ofstream& stream) {
   stream << "#pragma once" << NLine(1)
          <<  NLine(1)
-         << "#include <string>" << NLine(1)
-         << "#include <memory>" << NLine(1)
-         << "#include <list>" << NLine(1)
+         << "#include <bundle.h>" << NLine(1)
          <<  NLine(1)
-         << "#include <rpc-port-parcel.h>" << NLine(1)
-         << "#include <rpc-port.h>" << NLine(1);
-  stream <<  NLine(1);
+         << "#include <string>" << NLine(1)
+         << "#include <vector>" << NLine(1)
+         << "#include <list>" << NLine(2);
   GenNamespace(stream);
 }
 
@@ -40,9 +38,10 @@ void CppProxyHeaderGen::OnFiniGen(std::ofstream& stream) {
 void CppProxyHeaderGen::GenNamespace(std::ofstream& stream) {
   stream << "namespace rpc_port ";
   GenBrace(stream, 0, [&]() {
+    stream <<  NLine(1);
     GenStructuresForHeader(stream);
-    stream << Tab(1) << "namespace proxy ";
-    GenBrace(stream, TAB_SIZE, [&]() {
+    stream << "namespace proxy ";
+    GenBrace(stream, 0, [&]() {
 //      GenInterfaces(stream);
     }, false);
   }, false);
index 8fd1a82..c5cc0ca 100644 (file)
@@ -22,14 +22,20 @@ CppStubBodyGen::CppStubBodyGen(std::shared_ptr<Document> doc)
     : CppGeneratorBase(doc) {}
 
 void CppStubBodyGen::OnInitGen(std::ofstream& stream) {
-  stream << "#include <string>" << NLine(1)
-         << "#include <memory>" << NLine(1)
-         << "#include <stdlib>" << NLine(1)
-         << "#include <assert>" << NLine(1)
+  std::string key(".cc");
+  std::string header_file = FileName;
+
+  std::size_t found = header_file.rfind(key);
+  if (found != std::string::npos)
+    header_file.replace(found, key.length(), ".h");
+
+  stream << "#include <stdlib.h>" << NLine(1)
+         << "#include <assert.h>" << NLine(1)
          <<  NLine(1)
          << "#include <rpc-port-parcel.h>" << NLine(1)
-         << "#include <rpc-port.h>" << NLine(1);
-  stream <<  NLine(1);
+         << "#include <rpc-port.h>" << NLine(1)
+         <<  NLine(1)
+         << "#include \"" << header_file << "\"" << NLine(2);
   GenNamespace(stream);
 }
 
@@ -38,11 +44,14 @@ void CppStubBodyGen::OnFiniGen(std::ofstream& stream) {}
 void CppStubBodyGen::GenNamespace(std::ofstream& stream) {
   stream << "namespace rpc_port ";
   GenBrace(stream, 0, [&]() {
+    stream <<  NLine(1);
+    GenPrototype(stream);
     GenStructuresForBody(stream);
     GenSerializer(stream);
     GenDeSerializer(stream);
-    stream << Tab(1) << "namespace stub ";
-    GenBrace(stream, TAB_SIZE, [&]() {
+    GenListSerializer(stream);
+    stream << "namespace stub ";
+    GenBrace(stream, 0, [&]() {
 //      GenInterfaces(stream);
     }, false);
   }, false);
index db1a4bf..70996c7 100644 (file)
@@ -24,13 +24,11 @@ CppStubHeaderGen::CppStubHeaderGen(std::shared_ptr<Document> doc)
 void CppStubHeaderGen::OnInitGen(std::ofstream& stream) {
   stream << "#pragma once" << NLine(1)
          <<  NLine(1)
-         << "#include <string>" << NLine(1)
-         << "#include <memory>" << NLine(1)
-         << "#include <list>" << NLine(1)
+         << "#include <bundle.h>" << NLine(1)
          <<  NLine(1)
-         << "#include <rpc-port-parcel.h>" << NLine(1)
-         << "#include <rpc-port.h>" << NLine(1);
-  stream <<  NLine(1);
+         << "#include <string>" << NLine(1)
+         << "#include <vector>" << NLine(1)
+         << "#include <list>" << NLine(2);
   GenNamespace(stream);
 }
 
@@ -40,9 +38,10 @@ void CppStubHeaderGen::OnFiniGen(std::ofstream& stream) {
 void CppStubHeaderGen::GenNamespace(std::ofstream& stream) {
   stream << "namespace rpc_port ";
   GenBrace(stream, 0, [&]() {
+    stream <<  NLine(1);
     GenStructuresForHeader(stream);
-    stream << Tab(1) << "namespace stub ";
-    GenBrace(stream, TAB_SIZE, [&]() {
+    stream << "namespace stub ";
+    GenBrace(stream, 0, [&]() {
 //      GenInterfaces(stream);
     }, false);
   }, false);
index 60d0fdf..abcb8d7 100644 (file)
@@ -52,7 +52,7 @@ class Generator {
 
   template<typename T>
   void GenBrace(std::ofstream& stream, int indent, T cb,
-                bool start_indent = true) {
+                bool start_indent = true, bool ended_new_line = true) {
     if (start_indent) {
       for(int i = 0; i < indent; i++)
         stream << " ";
@@ -61,7 +61,9 @@ class Generator {
     cb();
     for(int i = 0; i < indent; i++)
         stream << " ";
-    stream << "}" << std::endl;
+    stream << "}";
+    if (ended_new_line)
+      stream << std::endl;
   }
 
   virtual void OnInitGen(std::ofstream& stream) = 0;