Refactor GenInvocation() for c++ generator 04/270104/3
authorjh9216.park <jh9216.park@samsung.com>
Wed, 26 Jan 2022 01:13:01 +0000 (20:13 -0500)
committerjh9216.park <jh9216.park@samsung.com>
Wed, 26 Jan 2022 01:23:40 +0000 (20:23 -0500)
- Make it as one block for readability and maintenance

Change-Id: Iaccfd46bd287436200bb1e88fb448c88cc9c504c
Signed-off-by: jh9216.park <jh9216.park@samsung.com>
idlc/gen/cpp_gen_base.cc
idlc/gen/cpp_gen_base.h
idlc/gen/cpp_proxy_body_gen.cc
idlc/gen/cpp_proxy_body_gen.h
idlc/gen/cpp_proxy_body_gen_cb.h

index 0329841..5b606bd 100644 (file)
@@ -453,15 +453,6 @@ std::string CppGeneratorBase::GetParameters(const Parameters& ps) {
   return ret;
 }
 
-void CppGeneratorBase::GenDeclaration(std::ofstream& stream,
-                                     const Interface& iface,
-                                     const Declaration& decl) {
-  stream << ConvertTypeToString(decl.GetType()) << " " << iface.GetID() << "::"
-         << decl.GetID() << "(";
-  GenParameters(stream, decl.GetParameters());
-  stream << ") ";
-}
-
 std::string CppGeneratorBase::GenPrivateSharingRequest(const BaseType& type,
     std::string id) {
   std::string ret;
index 25400a8..de96d9f 100644 (file)
@@ -42,8 +42,6 @@ class CppGeneratorBase : public Generator {
   void GenMethodId(std::ofstream& stream, const Interface& iface);
   void GenDelegateId(std::ofstream& stream, const Interface& iface);
   void GenParameters(std::ofstream& stream, const Parameters& ps);
-  void GenDeclaration(std::ofstream& stream, const Interface& iface,
-                      const Declaration& decl);
   void GenBodyCallbacks(std::ofstream& stream, const Interface& iface,
                         bool is_proxy);
   void GenHeaderCallbacks(std::ofstream& stream, const Interface& iface,
index b82b2d9..b9984f7 100644 (file)
@@ -117,56 +117,47 @@ void CppProxyBodyGen::GenMethods(std::ofstream& stream,
   for (const auto& i : decls) {
     if (i->GetMethodType() == Declaration::MethodType::DELEGATE)
       continue;
-
-    GenDeclaration(stream, iface, *i);
-    GenBrace(stream, 0, [&]() {
-      GenInvocation(stream, *i);
-    }, false);
-    stream << NLine(1);
+    GenInvocation(stream, iface, *i);
   }
 }
 
 void CppProxyBodyGen::GenInvocation(std::ofstream& stream,
-    const Declaration& decl) {
-  stream << CB_INVOCATION_PRE;
-
-  // Serialize
-  stream << Tab(1)
-         << "rpc_port_parcel_write_int32(p, static_cast<int>(MethodId::"
-         << decl.GetID() << "));" << NLine(1);
-  std::string m;
-  std::string l;
-  for (const auto& i : decl.GetParameters()) {
-    auto& pt = i->GetParameterType();
-    if (pt.GetDirection() == ParameterType::Direction::OUT)
-      continue;
-    m += GenPrivateSharingRequest(pt.GetBaseType(), i->GetID());
-    m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p");
-    if (IsDelegateType(pt.GetBaseType())) {
-      l += "delegate_list_.emplace_back(" + i->GetID() + ".release());\n";
-    }
-  }
-  stream << AddIndent(TAB_SIZE, m) << NLine(1);
-
-  stream << Tab(1) << "std::lock_guard<std::recursive_mutex> lock(mutex_);"
-         << NLine(1);
-
-  if (!l.empty())
-    stream << AddIndent(TAB_SIZE, l);
-
-  stream << CB_INVOCATION_MID;
-
-  // Deserialize
-  if (decl.GetMethodType() == Declaration::MethodType::ASYNC) {
-    stream << Tab(1) << "rpc_port_parcel_destroy(p);"
-         << NLine(1);
-    return;
-  }
+    const Interface& iface, const Declaration& decl) {
+  ReplaceAll(CB_INVOCATION)
+      .Select("<ASYNC_OR_SYNC>",
+          { CB_INVOCATION_ASYNC, CB_INVOCATION_SYNC }, [&]() {
+        if (decl.GetMethodType() == Declaration::MethodType::ASYNC)
+          return 0;
+        return 1;
+      })
+      .Change("<RET_TYPE>", ConvertTypeToString(decl.GetType()))
+      .Change("<IFACE>", iface.GetID())
+      .Change("<METHOD>", decl.GetID())
+      .Change("<PARAMS>", GetParameters(decl.GetParameters()))
+      .Change("<SERIALIZE>", [&]() {
+        std::string m;
+        std::string l;
+        std::string ret;
+        for (const auto& i : decl.GetParameters()) {
+          auto& pt = i->GetParameterType();
+          if (pt.GetDirection() == ParameterType::Direction::OUT)
+            continue;
+          m += GenPrivateSharingRequest(pt.GetBaseType(), i->GetID());
+          m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p");
+          if (IsDelegateType(pt.GetBaseType())) {
+            l += "delegate_list_.emplace_back(" + i->GetID() + ".release());\n";
+          }
+        }
 
-  stream << NLine(1);
-  stream << Tab(1) << ConvertTypeToString(decl.GetType()) << " ret;";
+        ret = AddIndent(TAB_SIZE, m) + NLine(1);
+        ret += Tab(1) + "std::lock_guard<std::recursive_mutex> lock(mutex_);"
+            + NLine(1);
 
-  auto parcel_read = [&]() -> std::string {
+        if (!l.empty())
+          ret += AddIndent(TAB_SIZE, l);
+        return ret;
+      })
+      .Change("<DESERIALIZE>", [&]() {
         std::string code;
         for (const auto& i : decl.GetParameters()) {
           if (i->GetParameterType().GetDirection() ==
@@ -187,10 +178,8 @@ void CppProxyBodyGen::GenInvocation(std::ofstream& stream,
         }
 
         return code;
-      };
-  stream << ReplaceAll(CB_INVOCATION_RECEIVE, "[PARCEL_READ]", parcel_read());
-
-  stream << CB_INVOCATION_END;
+      })
+      .Out(stream);
 }
 
 }  // namespace tidl
index b6e7c1d..a064d0f 100644 (file)
@@ -41,7 +41,8 @@ class CppProxyBodyGen : public CppGeneratorBase {
   void GenDestructor(std::ofstream& stream, const Interface& iface);
   void GenHelperMethods(std::ofstream& stream, const Interface& iface);
   void GenMethods(std::ofstream& stream, const Interface& iface);
-  void GenInvocation(std::ofstream& stream, const Declaration& decl);
+  void GenInvocation(std::ofstream& stream, const Interface& iface,
+      const Declaration& decl);
 };
 
 }  // namespace tidl
index 8ef6c15..d81ebb1 100644 (file)
@@ -25,8 +25,10 @@ R"__cpp_cb(
 }
 )__cpp_cb";
 
-const char CB_INVOCATION_PRE[] =
-R"__cpp_cb(  if (port_ == nullptr) {
+const char CB_INVOCATION[] =
+R"__cpp_cb(
+<RET_TYPE> <IFACE>::<METHOD>(<PARAMS>) {
+  if (port_ == nullptr) {
     _E("Not connected");
     throw NotConnectedSocketException();
   }
@@ -40,10 +42,8 @@ R"__cpp_cb(  if (port_ == nullptr) {
   int seq_num_ = -1;
   rpc_port_parcel_header_get_seq_num(header_, &seq_num_);
   _W("[Version] \"%s\", [Sequence] %d", TIDL_VERSION, seq_num_);
-)__cpp_cb";
-
-const char CB_INVOCATION_MID[] =
-R"__cpp_cb(
+  rpc_port_parcel_write_int32(p, static_cast<int>(MethodId::<METHOD>));
+<SERIALIZE>
   // Send
   int r = rpc_port_parcel_send(p, port_);
   if (r != RPC_PORT_ERROR_NONE) {
@@ -51,13 +51,18 @@ R"__cpp_cb(
     rpc_port_parcel_destroy(p);
     throw InvalidIOException();
   }
+<ASYNC_OR_SYNC>
+}
 )__cpp_cb";
 
-/**
- * [PARCEL_READ] The implementation to read the data from the parcel.
- */
-const char CB_INVOCATION_RECEIVE[] =
+const char CB_INVOCATION_ASYNC[] =
 R"__cpp_cb(
+  rpc_port_parcel_destroy(p);
+)__cpp_cb";
+
+const char CB_INVOCATION_SYNC[] =
+R"__cpp_cb(
+  <RET_TYPE> ret;
   bool done_ = false;
   do {
     rpc_port_parcel_h parcel_received = nullptr;
@@ -84,13 +89,10 @@ R"__cpp_cb(
     }
     done_ = true;
 
-[PARCEL_READ]
+<DESERIALIZE>
     rpc_port_parcel_destroy(parcel_received);
   } while (!done_);
-)__cpp_cb";
 
-const char CB_INVOCATION_END[] =
-R"__cpp_cb(
   rpc_port_parcel_destroy(p);
 
   return ret;