From: jh9216.park Date: Wed, 26 Jan 2022 01:13:01 +0000 (-0500) Subject: Refactor GenInvocation() for c++ generator X-Git-Tag: accepted/tizen/unified/20220216.010312~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F04%2F270104%2F3;p=platform%2Fcore%2Fappfw%2Ftidl.git Refactor GenInvocation() for c++ generator - Make it as one block for readability and maintenance Change-Id: Iaccfd46bd287436200bb1e88fb448c88cc9c504c Signed-off-by: jh9216.park --- diff --git a/idlc/gen/cpp_gen_base.cc b/idlc/gen/cpp_gen_base.cc index 0329841..5b606bd 100644 --- a/idlc/gen/cpp_gen_base.cc +++ b/idlc/gen/cpp_gen_base.cc @@ -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; diff --git a/idlc/gen/cpp_gen_base.h b/idlc/gen/cpp_gen_base.h index 25400a8..de96d9f 100644 --- a/idlc/gen/cpp_gen_base.h +++ b/idlc/gen/cpp_gen_base.h @@ -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, diff --git a/idlc/gen/cpp_proxy_body_gen.cc b/idlc/gen/cpp_proxy_body_gen.cc index b82b2d9..b9984f7 100644 --- a/idlc/gen/cpp_proxy_body_gen.cc +++ b/idlc/gen/cpp_proxy_body_gen.cc @@ -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(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 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("", + { CB_INVOCATION_ASYNC, CB_INVOCATION_SYNC }, [&]() { + if (decl.GetMethodType() == Declaration::MethodType::ASYNC) + return 0; + return 1; + }) + .Change("", ConvertTypeToString(decl.GetType())) + .Change("", iface.GetID()) + .Change("", decl.GetID()) + .Change("", GetParameters(decl.GetParameters())) + .Change("", [&]() { + 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 lock(mutex_);" + + NLine(1); - auto parcel_read = [&]() -> std::string { + if (!l.empty()) + ret += AddIndent(TAB_SIZE, l); + return ret; + }) + .Change("", [&]() { 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 diff --git a/idlc/gen/cpp_proxy_body_gen.h b/idlc/gen/cpp_proxy_body_gen.h index b6e7c1d..a064d0f 100644 --- a/idlc/gen/cpp_proxy_body_gen.h +++ b/idlc/gen/cpp_proxy_body_gen.h @@ -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 diff --git a/idlc/gen/cpp_proxy_body_gen_cb.h b/idlc/gen/cpp_proxy_body_gen_cb.h index 8ef6c15..d81ebb1 100644 --- a/idlc/gen/cpp_proxy_body_gen_cb.h +++ b/idlc/gen/cpp_proxy_body_gen_cb.h @@ -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( + ::() { + 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(MethodId::)); + // 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(); } + +} )__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; bool done_ = false; do { rpc_port_parcel_h parcel_received = nullptr; @@ -84,13 +89,10 @@ R"__cpp_cb( } done_ = true; -[PARCEL_READ] + 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;