From 57a54008462a3522b183868663dcf2f5449fedc6 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Tue, 3 Apr 2018 10:05:14 +0900 Subject: [PATCH] Refactor tidl generator - Move some methods to base class - Use ReplaceAll method to reduce redundant code Change-Id: I530ee4e9331dc92006c3164d84b9ec3f099a04b0 Signed-off-by: Junghoon Park --- idlc/c_gen/c_body_gen_base.cc | 2 +- idlc/c_gen/c_gen_base.cc | 22 ------ idlc/c_gen/c_gen_base.h | 17 ----- idlc/c_gen/c_header_gen_base.cc | 2 +- idlc/c_gen/c_proxy_body_gen.cc | 4 +- idlc/c_gen/c_proxy_header_gen.cc | 2 +- idlc/c_gen/c_stub_body_gen.cc | 6 +- idlc/c_gen/c_stub_header_gen.cc | 2 +- idlc/cpp_gen/cpp_gen_base.cc | 122 +++++++++------------------------ idlc/cpp_gen/cpp_gen_base.h | 1 - idlc/cpp_gen/cpp_gen_base_cb.h | 33 +++++---- idlc/cpp_gen/cpp_proxy_body_gen.cc | 24 +------ idlc/cpp_gen/cpp_proxy_body_gen_cb.h | 26 +++---- idlc/cpp_gen/cpp_proxy_header_gen.cc | 13 +--- idlc/cpp_gen/cpp_proxy_header_gen_cb.h | 19 ++++- idlc/cpp_gen/cpp_stub_body_gen.cc | 21 +----- idlc/cpp_gen/cpp_stub_body_gen_cb.h | 17 +++-- idlc/cpp_gen/cpp_stub_header_gen.cc | 21 +----- idlc/cpp_gen/cpp_stub_header_gen_cb.h | 23 ++++++- idlc/cs_gen/cs_gen_base.cc | 22 +----- idlc/cs_gen/cs_gen_base.h | 1 - idlc/cs_gen/cs_proxy_gen.cc | 2 +- idlc/generator.cc | 35 ++++++++++ idlc/generator.h | 18 +++++ 24 files changed, 188 insertions(+), 267 deletions(-) diff --git a/idlc/c_gen/c_body_gen_base.cc b/idlc/c_gen/c_body_gen_base.cc index efdf2d5..6b23ffb 100644 --- a/idlc/c_gen/c_body_gen_base.cc +++ b/idlc/c_gen/c_body_gen_base.cc @@ -52,7 +52,7 @@ void CBodyGeneratorBase::GenStructures(std::ofstream& stream) { const Interface &inf = static_cast(*i); for (auto& d : inf.GetDeclarations().GetDecls()) { for (auto& p : d->GetParameters().GetParams()) { - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) continue; AddStructureFromType(p->GetParameterType().GetBaseType()); } diff --git a/idlc/c_gen/c_gen_base.cc b/idlc/c_gen/c_gen_base.cc index bcaed46..05fe810 100644 --- a/idlc/c_gen/c_gen_base.cc +++ b/idlc/c_gen/c_gen_base.cc @@ -239,28 +239,6 @@ void CGeneratorBase::GenGNUSourceDefinition(std::ofstream& stream) { stream << std::string(format); } -std::string CGeneratorBase::ReplaceAll(std::string str, - const std::string& from, - const std::string& to) { - std::size_t pos = 0; - while ((pos = str.find(from, pos)) != std::string::npos) { - str.replace(pos, from.length(), to); - pos += to.length(); - } - return str; -} - -bool CGeneratorBase::TypeIsDelegator(const Interface& inf, - const BaseType& type) { - for (auto& i : inf.GetDeclarations().GetDecls()) { - if (i->GetMethodType() != Declaration::MethodType::DELEGATE) - continue; - if (i->GetID() == type.GetFullName()) - return true; - } - return false; -} - bool CGeneratorBase::StructureExist(const Structure& st) { for (auto& i : GetDocument().GetBlocks()) { if (i->GetType() != Block::TYPE_STRUCTURE) diff --git a/idlc/c_gen/c_gen_base.h b/idlc/c_gen/c_gen_base.h index 9d1c16e..2db9a6a 100644 --- a/idlc/c_gen/c_gen_base.h +++ b/idlc/c_gen/c_gen_base.h @@ -32,22 +32,6 @@ class CGeneratorBase : public Generator { explicit CGeneratorBase(std::shared_ptr doc); virtual ~CGeneratorBase() = default; - template - std::string GenTemplateString(std::string templ, T cb, ARGS... args) { - size_t f = templ.find("$$"); - templ.replace(f, std::string("$$").length(), cb()); - return GenTemplateString(std::move(templ), args...); - } - - template - std::string GenTemplateString(std::string templ, T cb) { - size_t f = templ.find("$$"); - templ.replace(f, std::string("$$").length(), cb()); - return templ; - } - - std::string ReplaceAll(std::string str, - const std::string& from, const std::string& to); std::string Trim(const std::string& str); std::string SmartIndent(std::string lines); std::string Tab(int cnt); @@ -62,7 +46,6 @@ class CGeneratorBase : public Generator { const BaseType& type); std::string GetStringFromElementType(const BaseType& type); std::string GetErrorValue(const BaseType& type); - bool TypeIsDelegator(const Interface& inf, const BaseType& type); void AddStructureFromType(const BaseType& type); const std::map>& GetStructures(void) { return structures_; diff --git a/idlc/c_gen/c_header_gen_base.cc b/idlc/c_gen/c_header_gen_base.cc index 0c0960e..8770870 100644 --- a/idlc/c_gen/c_header_gen_base.cc +++ b/idlc/c_gen/c_header_gen_base.cc @@ -55,7 +55,7 @@ void CHeaderGeneratorBase::GenStructures(std::ofstream& stream) { const Interface &inf = static_cast(*i); for (auto& d : inf.GetDeclarations().GetDecls()) { for (auto& p : d->GetParameters().GetParams()) { - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) continue; AddStructureFromType(p->GetParameterType().GetBaseType()); } diff --git a/idlc/c_gen/c_proxy_body_gen.cc b/idlc/c_gen/c_proxy_body_gen.cc index 36ef713..c316ad8 100644 --- a/idlc/c_gen/c_proxy_body_gen.cc +++ b/idlc/c_gen/c_proxy_body_gen.cc @@ -309,7 +309,7 @@ void CProxyBodyGen::GenInterfaceMethods(std::ofstream& stream, std::string str; for (auto& p : i->GetParameters().GetParams()) { str += ", "; - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) { str += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + p->GetParameterType().GetBaseType().ToString() + "_h " + p->GetID(); @@ -390,7 +390,7 @@ std::string CProxyBodyGen::GetMethodWriteString(const Interface& inf, for (auto& p : decl.GetParameters().GetParams()) { if (p->GetParameterType().GetDirection() == ParameterType::Direction::OUT) continue; - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) { str += GenTemplateString(ReplaceAll(CB_DELEGATE_BLOCK, "##", GetInterfaceIdWithNamespace(inf) + "_" + p->GetParameterType().GetBaseType().ToString()), [&]()->std::string { diff --git a/idlc/c_gen/c_proxy_header_gen.cc b/idlc/c_gen/c_proxy_header_gen.cc index 13de012..59e21de 100644 --- a/idlc/c_gen/c_proxy_header_gen.cc +++ b/idlc/c_gen/c_proxy_header_gen.cc @@ -128,7 +128,7 @@ void CProxyHeaderGen::GenInterfaceMethods(std::ofstream& stream, std::string str; for (auto& p : i->GetParameters().GetParams()) { str += ", "; - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) { str += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + p->GetParameterType().GetBaseType().ToString() + "_h " + p->GetID(); diff --git a/idlc/c_gen/c_stub_body_gen.cc b/idlc/c_gen/c_stub_body_gen.cc index 7e6d3fa..c46f1f0 100644 --- a/idlc/c_gen/c_stub_body_gen.cc +++ b/idlc/c_gen/c_stub_body_gen.cc @@ -171,7 +171,7 @@ std::string CStubBodyGen::GetMethodString(const Interface& inf, "} while (0);\n"; int cnt = 0; for (auto& i : decl.GetParameters().GetParams()) { - if (TypeIsDelegator(inf, i->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, i->GetParameterType().GetBaseType())) { str += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + i->GetParameterType().GetBaseType().ToString() + "_h " + i->GetID() + ";" + NLine(1); @@ -191,7 +191,7 @@ std::string CStubBodyGen::GetMethodString(const Interface& inf, if (i->GetParameterType().GetBaseType().IsUserDefinedType() || i->GetParameterType().GetBaseType().ToString() == "list" || i->GetParameterType().GetBaseType().ToString() == "array") { - if (TypeIsDelegator(inf, i->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, i->GetParameterType().GetBaseType())) { str += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + i->GetParameterType().GetBaseType().ToString() + "_create(&" + i->GetID() + ");" + NLine(1); @@ -295,7 +295,7 @@ std::string CStubBodyGen::GetMethodString(const Interface& inf, } for (auto& i: decl.GetParameters().GetParams()) { - if (TypeIsDelegator(inf, i->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, i->GetParameterType().GetBaseType())) { str += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + i->GetParameterType().GetBaseType().ToString() + "_destroy(" + i->GetID() + ");" + NLine(1); diff --git a/idlc/c_gen/c_stub_header_gen.cc b/idlc/c_gen/c_stub_header_gen.cc index 7c74b5b..999c193 100644 --- a/idlc/c_gen/c_stub_header_gen.cc +++ b/idlc/c_gen/c_stub_header_gen.cc @@ -79,7 +79,7 @@ void CStubHeaderGen::GenInterfaceDeclaration(std::ofstream& stream, [&]()->std::string { std::string s; for (auto& p : i->GetParameters().GetParams()) { - if (TypeIsDelegator(inf, p->GetParameterType().GetBaseType())) { + if (IsDelegateType(inf, p->GetParameterType().GetBaseType())) { s += "rpc_port_" + GetInterfaceIdWithNamespace(inf) + "_" + p->GetParameterType().GetBaseType().ToString() + "_h " + p->GetID(); diff --git a/idlc/cpp_gen/cpp_gen_base.cc b/idlc/cpp_gen/cpp_gen_base.cc index 698189e..c198d87 100644 --- a/idlc/cpp_gen/cpp_gen_base.cc +++ b/idlc/cpp_gen/cpp_gen_base.cc @@ -184,8 +184,8 @@ void CppGeneratorBase::GenStructuresForBody(std::ofstream& stream) { void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, const Structure& st) { std::vector> v; - const char ctor[] = "$$::$$() {}\n\n" \ - "$$::$$($$)\n" \ + const char ctor[] = "##::##() {}\n\n" \ + "##::##($$)\n" \ " : $$ {}"; for (auto& i : st.GetElements().GetElms()) { @@ -196,19 +196,7 @@ void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, const Structur v.push_back(p); } - GenTemplate(ctor, stream, - [&]()->std::string { - return st.GetID(); - }, - [&]()->std::string { - return st.GetID(); - }, - [&]()->std::string { - return st.GetID(); - }, - [&]()->std::string { - return st.GetID(); - }, + GenTemplate(ReplaceAll(ctor, "##", st.GetID()), stream, [&]()->std::string { std::string str; for (auto& i : v) { @@ -312,7 +300,7 @@ void CppGeneratorBase::GenDeSerializer(std::ofstream& stream, std::string CppGeneratorBase::ConvertTypeToString(const BaseType& type) { if (type.IsUserDefinedType()) { - if (IsDelegateType(type.ToString())) { + if (IsDelegateType(type)) { return "std::unique_ptr<" + type.ToString() + ">"; } return type.ToString(); @@ -496,7 +484,7 @@ std::string CppGeneratorBase::ConvertTypeToSerializer( ret += "rpc_port_parcel_write_bundle(" + parcel + ", " + id + ".GetHandle());\n"; } else if (type.GetMetaType() || type.IsUserDefinedType()) { - if (type.IsUserDefinedType() && IsDelegateType(type.ToString())) + if (type.IsUserDefinedType() && IsDelegateType(type)) ret += parcel + " << *" + id + ";\n"; else ret += parcel + " << " + id + ";\n"; @@ -537,20 +525,20 @@ std::string CppGeneratorBase::ConvertTypeToDeserializer( } else if (type.GetMetaType() != nullptr || type.IsUserDefinedType()) { std::string n; - if (type.GetMetaType() != nullptr || IsDelegateType(type.ToString())) + if (type.GetMetaType() != nullptr || IsDelegateType(type)) n = ConvertTypeToString(type); else n = type.ToString(); if (make_new_type) { ret += n + " "; - if (IsDelegateType(type.ToString())) { + if (IsDelegateType(type)) { ret += id + "(new " + type.ToString() + "(port, std::weak_ptr(b)));\n"; } else { ret += id + ";\n"; } } - if (IsDelegateType(type.ToString())) + if (IsDelegateType(type)) ret += parcel + " >> *" + id + ";\n"; else ret += parcel + " >> " + id + ";\n"; @@ -566,16 +554,7 @@ std::string CppGeneratorBase::ConvertTypeToDeserializer( void CppGeneratorBase::GenBodyCallbacks(std::ofstream& stream, const Interface& iface, bool is_proxy) { - GenTemplate(CB_CALLBACK_BASE, stream, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); } - ); + stream << ReplaceAll(CB_CALLBACK_BASE, "##", iface.GetID()); for (auto& i : iface.GetDeclarations().GetDecls()) { if (i->GetMethodType() != Declaration::MethodType::DELEGATE) @@ -651,7 +630,7 @@ void CppGeneratorBase::GenHeaderCallbacks(std::ofstream& stream, stream << Tab(1) << " virtual void OnReceivedEvent(rpc_port_parcel_h port) = 0;" << NLine(1); } - stream << CB_CALLBACK_BASE_HEADER_BACK << NLine(1); + stream << CB_CALLBACK_BASE_HEADER_BACK; for (auto& i : iface.GetDeclarations().GetDecls()) { if (i->GetMethodType() != Declaration::MethodType::DELEGATE) @@ -663,65 +642,32 @@ void CppGeneratorBase::GenHeaderCallbacks(std::ofstream& stream, void CppGeneratorBase::GenHeaderCallback(std::ofstream& stream, const Declaration& decl, bool is_proxy) { - stream << Tab(1) << "class " << decl.GetID() - << " : public CallbackBase "; - GenBrace(stream, TAB_SIZE, [&]() { - stream << Tab(1) << " public:"; - if (!is_proxy) { - GenTemplate(CB_CALLBACK_CTOR_STUB, stream, - [&]()->std::string { - return decl.GetID(); - }, - [&]()->std::string { - return decl.GetID(); - } - ); - } else { - GenTemplate(CB_CALLBACK_CTOR_PROXY, stream, - [&]()->std::string { - return decl.GetID(); - }, - [&]()->std::string { - return decl.GetID(); + stream << GenTemplateString(CB_CALLBACK_CLASS, + [&]()->std::string { + return decl.GetID(); + }, + [&]()->std::string { + return ReplaceAll( + is_proxy ? CB_CALLBACK_CTOR_PROXY : CB_CALLBACK_CTOR_STUB, + "##", decl.GetID()); + }, + [&]()->std::string { + std::string ret; + if (is_proxy) { + ret = Tab(2) + "virtual void OnReceived(" + + GetParameters(decl.GetParameters()) + + ") {}" + NLine(1); + } else { + ret = Tab(2) + "void Invoke(" + + GetParameters(decl.GetParameters()) + + ");" + NLine(1); } - ); - } - stream << NLine(1); - if (is_proxy) { - stream << Tab(2) << "virtual void OnReceived("; - GenParameters(stream, decl.GetParameters()); - stream << ") {}" << NLine(1); - } else { - stream << Tab(2) << "void Invoke("; - GenParameters(stream, decl.GetParameters()); - stream << ");" << NLine(2); - } - - stream << Tab(1) << " private:"; - if (!is_proxy) { - stream << CB_CALLBACK_PRIVATE_STUB; - } else { - stream << CB_CALLBACK_PRIVATE_PROXY; - } - }, false, false); - stream << ";" << NLine(2); -} -bool CppGeneratorBase::IsDelegateType(const std::string type_name) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_INTERFACE) - continue; - Interface& iface = static_cast(*i); - - for (auto& j : iface.GetDeclarations().GetDecls()) { - if (j->GetMethodType() == Declaration::MethodType::DELEGATE) { - if (j->GetID() == type_name) - return true; - } - } - } - - return false; + return ret; + }, + [&]()->std::string { + return is_proxy ? CB_CALLBACK_PRIVATE_PROXY : CB_CALLBACK_PRIVATE_STUB; + }); } void CppGeneratorBase::GenCopyright(std::ofstream& stream) { diff --git a/idlc/cpp_gen/cpp_gen_base.h b/idlc/cpp_gen/cpp_gen_base.h index 6b93ec4..3a379b1 100644 --- a/idlc/cpp_gen/cpp_gen_base.h +++ b/idlc/cpp_gen/cpp_gen_base.h @@ -48,7 +48,6 @@ class CppGeneratorBase : public Generator { bool is_proxy); void GenHeaderCallbacks(std::ofstream& stream, const Interface& iface, bool is_proxy); - bool IsDelegateType(const std::string type_name); std::string ConvertTypeToString(const BaseType& type); std::string Tab(int cnt); std::string NLine(int cnt); diff --git a/idlc/cpp_gen/cpp_gen_base_cb.h b/idlc/cpp_gen/cpp_gen_base_cb.h index 3b0e28a..11653ae 100644 --- a/idlc/cpp_gen/cpp_gen_base_cb.h +++ b/idlc/cpp_gen/cpp_gen_base_cb.h @@ -58,30 +58,30 @@ const char CB_BUNDLE[] = R"__cls_bundle(class Bundle final { const char CB_CALLBACK_BASE[] = R"__cpp_cb( -int $$::CallbackBase::seq_num_ = 0; +int ##::CallbackBase::seq_num_ = 0; -$$::CallbackBase::CallbackBase(int delegate_id, bool once) +##::CallbackBase::CallbackBase(int delegate_id, bool once) : id_(delegate_id), once_(once) { seq_id_ = seq_num_++; } -int $$::CallbackBase::GetId() const { +int ##::CallbackBase::GetId() const { return id_; } -int $$::CallbackBase::GetSeqId() const { +int ##::CallbackBase::GetSeqId() const { return seq_id_; } -bool $$::CallbackBase::IsOnce() const { +bool ##::CallbackBase::IsOnce() const { return once_; } -std::string $$::CallbackBase::GetTag() const { +std::string ##::CallbackBase::GetTag() const { return std::to_string(id_) + "::" + std::to_string(seq_id_); } -rpc_port_parcel_h operator << (rpc_port_parcel_h h, const $$::CallbackBase& cb) { +rpc_port_parcel_h operator << (rpc_port_parcel_h h, const ##::CallbackBase& cb) { rpc_port_parcel_write_int32(h, cb.id_); rpc_port_parcel_write_int32(h, cb.seq_id_); rpc_port_parcel_write_bool(h, cb.once_); @@ -89,7 +89,7 @@ rpc_port_parcel_h operator << (rpc_port_parcel_h h, const $$::CallbackBase& cb) return h; } -rpc_port_parcel_h operator >> (rpc_port_parcel_h h, $$::CallbackBase& cb) { +rpc_port_parcel_h operator >> (rpc_port_parcel_h h, ##::CallbackBase& cb) { rpc_port_parcel_read_int32(h, &cb.id_); rpc_port_parcel_read_int32(h, &cb.seq_id_); rpc_port_parcel_read_bool(h, &cb.once_); @@ -144,10 +144,19 @@ R"__cpp_cb( }; )__cpp_cb"; +const char CB_CALLBACK_CLASS[] = +R"__cpp_cb( + class $$ : public CallbackBase { + public:$$ +$$ + private:$$ + }; +)__cpp_cb"; + const char CB_CALLBACK_CTOR_STUB[] = R"__cpp_cb( - $$(rpc_port_parcel_h port, std::weak_ptr service) - : CallbackBase(static_cast(DelegateId::$$), false) { + ##(rpc_port_parcel_h port, std::weak_ptr service) + : CallbackBase(static_cast(DelegateId::##), false) { port_ = port; service_ = std::move(service); } @@ -155,8 +164,8 @@ R"__cpp_cb( const char CB_CALLBACK_CTOR_PROXY[] = R"__cpp_cb( - $$(bool once = false) - : CallbackBase(static_cast(DelegateId::$$), once) {} + ##(bool once = false) + : CallbackBase(static_cast(DelegateId::##), once) {} )__cpp_cb"; const char CB_CALLBACK_PRIVATE_PROXY[] = diff --git a/idlc/cpp_gen/cpp_proxy_body_gen.cc b/idlc/cpp_gen/cpp_proxy_body_gen.cc index fa6b827..7a0c720 100644 --- a/idlc/cpp_gen/cpp_proxy_body_gen.cc +++ b/idlc/cpp_gen/cpp_proxy_body_gen.cc @@ -108,26 +108,8 @@ void CppProxyBodyGen::GenDestructor(std::ofstream& stream, void CppProxyBodyGen::GenHelperMethods(std::ofstream& stream, const Interface& iface) { - GenTemplate(CB_PROXY_HELPER_METHODS, stream, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); } - ); - stream << NLine(1); + stream << ReplaceAll(CB_PROXY_HELPER_METHODS, "##", iface.GetID()) + << NLine(1); } void CppProxyBodyGen::GenMethods(std::ofstream& stream, @@ -160,7 +142,7 @@ void CppProxyBodyGen::GenInvocation(std::ofstream& stream, const Declaration& de if (pt.GetDirection() == ParameterType::Direction::OUT) continue; m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p"); - if (IsDelegateType(pt.GetBaseType().ToString())) { + if (IsDelegateType(pt.GetBaseType())) { l += "delegate_list_.emplace_back(" + i->GetID() + ".release());\n"; } } diff --git a/idlc/cpp_gen/cpp_proxy_body_gen_cb.h b/idlc/cpp_gen/cpp_proxy_body_gen_cb.h index 0a68ea0..78336e9 100644 --- a/idlc/cpp_gen/cpp_proxy_body_gen_cb.h +++ b/idlc/cpp_gen/cpp_proxy_body_gen_cb.h @@ -53,8 +53,8 @@ R"__cpp_cb( const char CB_PROXY_HELPER_METHODS[] = R"__cpp_cb( -void $$::Connect() { - int ret = rpc_port_proxy_connect(proxy_, target_appid_.c_str(), "$$"); +void ##::Connect() { + int ret = rpc_port_proxy_connect(proxy_, target_appid_.c_str(), "##"); switch (ret) { case RPC_PORT_ERROR_INVALID_PARAMETER: @@ -68,7 +68,7 @@ void $$::Connect() { } } -void $$::DisposeCallback(const std::string& tag) { +void ##::DisposeCallback(const std::string& tag) { for (auto& i : delegate_list_) { if (i->GetTag() == tag) { delegate_list_.remove(i); @@ -77,7 +77,7 @@ void $$::DisposeCallback(const std::string& tag) { } } -void $$::ProcessReceivedEvent(rpc_port_parcel_h parcel) { +void ##::ProcessReceivedEvent(rpc_port_parcel_h parcel) { int id = 0; int seq_id = 0; bool once = false; @@ -96,7 +96,7 @@ void $$::ProcessReceivedEvent(rpc_port_parcel_h parcel) { } } -void $$::ConsumeCommand(rpc_port_parcel_h* parcel, rpc_port_h port) { +void ##::ConsumeCommand(rpc_port_parcel_h* parcel, rpc_port_h port) { do { rpc_port_parcel_h p; int ret = rpc_port_parcel_create_from_port(&p, port); @@ -117,24 +117,24 @@ void $$::ConsumeCommand(rpc_port_parcel_h* parcel, rpc_port_h port) { *parcel = nullptr; } -void $$::OnConnectedCB(const char *ep, const char *port_name, rpc_port_h port, void *data) { - $$* l = static_cast<$$*>(data); +void ##::OnConnectedCB(const char *ep, const char *port_name, rpc_port_h port, void *data) { + ##* l = static_cast<##*>(data); l->port_ = port; l->listener_->OnConnected(); } -void $$::OnDisconnectedCB(const char *ep, const char *port_name, void *data) { - $$* l = static_cast<$$*>(data); +void ##::OnDisconnectedCB(const char *ep, const char *port_name, void *data) { + ##* l = static_cast<##*>(data); l->listener_->OnDisconnected(); } -void $$::OnRejectedCB(const char *ep, const char *port_name, void *data) { - $$* l = static_cast<$$*>(data); +void ##::OnRejectedCB(const char *ep, const char *port_name, void *data) { + ##* l = static_cast<##*>(data); l->listener_->OnRejected(); } -void $$::OnReceivedCB(const char *ep, const char *port_name, void *data) { - $$* l = static_cast<$$*>(data); +void ##::OnReceivedCB(const char *ep, const char *port_name, void *data) { + ##* l = static_cast<##*>(data); int cmd; rpc_port_parcel_h parcel_received; diff --git a/idlc/cpp_gen/cpp_proxy_header_gen.cc b/idlc/cpp_gen/cpp_proxy_header_gen.cc index 72941a6..ea3b50d 100644 --- a/idlc/cpp_gen/cpp_proxy_header_gen.cc +++ b/idlc/cpp_gen/cpp_proxy_header_gen.cc @@ -27,18 +27,7 @@ CppProxyHeaderGen::CppProxyHeaderGen(std::shared_ptr doc) void CppProxyHeaderGen::OnInitGen(std::ofstream& stream) { GenCopyright(stream); - stream << NLine(1); - stream << "#pragma once" << NLine(1) - << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(2) - << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(2); + stream << CB_HEADER; GenNamespace(stream); } diff --git a/idlc/cpp_gen/cpp_proxy_header_gen_cb.h b/idlc/cpp_gen/cpp_proxy_header_gen_cb.h index 0f88387..c4e270e 100644 --- a/idlc/cpp_gen/cpp_proxy_header_gen_cb.h +++ b/idlc/cpp_gen/cpp_proxy_header_gen_cb.h @@ -25,7 +25,8 @@ class InvalidIDException : public Exception {}; )__cpp_cb"; const char CB_PUBLIC_MEMBERS[] = -R"__cpp_cb( class IEventListener { +R"__cpp_cb( + class IEventListener { public: /// /// This method will be invoked when the client app is connected to the servicece app. @@ -98,3 +99,19 @@ R"__cpp_cb( void ProcessReceivedEvent(rpc_port_parcel_h parcel); std::list> delegate_list_; std::string target_appid_; )__cpp_cb"; + +const char CB_HEADER[] = +R"__cpp_cb( +#pragma once + +#include +#include +#include + +#include +#include +#include +#include +#include + +)__cpp_cb"; diff --git a/idlc/cpp_gen/cpp_stub_body_gen.cc b/idlc/cpp_gen/cpp_stub_body_gen.cc index 76c4303..0f27964 100644 --- a/idlc/cpp_gen/cpp_stub_body_gen.cc +++ b/idlc/cpp_gen/cpp_stub_body_gen.cc @@ -92,11 +92,7 @@ void CppStubBodyGen::GenServiceBase(std::ofstream& stream, void CppStubBodyGen::GenConstructor(std::ofstream& stream, const Interface& iface) { - GenTemplate(CB_CTOR_FRONT, stream, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); } - ); + stream << ReplaceAll(CB_CTOR_FRONT, "##", iface.GetID()); for (auto& i : iface.GetAttributes().GetAttrs()) { if (i->GetKey() == "privilege") { @@ -114,18 +110,7 @@ void CppStubBodyGen::GenConstructor(std::ofstream& stream, void CppStubBodyGen::GenDefaultMethods(std::ofstream& stream, const Interface& iface) { GenConstructor(stream, iface); - GenTemplate(CB_DEFAULT_METHODS, stream, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); }, - [&]()->std::string { return iface.GetID(); } - ); + stream << ReplaceAll(CB_DEFAULT_METHODS, "##", iface.GetID()); } void CppStubBodyGen::GenReceivedEvent(std::ofstream& stream, @@ -190,7 +175,7 @@ void CppStubBodyGen::GenInvocation(std::ofstream& stream, const Declaration& dec if (pt.GetDirection() == ParameterType::Direction::OUT) { d += ConvertTypeToString(pt.GetBaseType()) + " " + v + ";\n"; } - if (IsDelegateType(pt.GetBaseType().ToString())) { + if (IsDelegateType(pt.GetBaseType())) { m += "std::move("; m += v; m += ")"; diff --git a/idlc/cpp_gen/cpp_stub_body_gen_cb.h b/idlc/cpp_gen/cpp_stub_body_gen_cb.h index f5bcb47..41f20ac 100644 --- a/idlc/cpp_gen/cpp_stub_body_gen_cb.h +++ b/idlc/cpp_gen/cpp_stub_body_gen_cb.h @@ -16,8 +16,8 @@ const char CB_CTOR_FRONT[] = R"__cpp_cb( -$$::$$() { - int r = rpc_port_stub_create(&stub_, "$$"); +##::##() { + int r = rpc_port_stub_create(&stub_, "##"); if (r != RPC_PORT_ERROR_NONE) throw InvalidIOException(); rpc_port_stub_add_connected_event_cb(stub_, OnConnectedCB, this); @@ -27,7 +27,7 @@ $$::$$() { const char CB_DEFAULT_METHODS[] = R"__cpp_cb( -$$::~$$() { +##::~##() { for (auto& i : services_) { i->OnTerminate(); } @@ -37,8 +37,7 @@ $$::~$$() { } } -void $$::Listen( - std::shared_ptr<$$::ServiceBase::Factory> service_factory) { +void ##::Listen(std::shared_ptr<##::ServiceBase::Factory> service_factory) { service_factory_ = std::move(service_factory); int r = rpc_port_stub_listen(stub_); @@ -49,15 +48,15 @@ void $$::Listen( } } -void $$::OnConnectedCB(const char* sender, const char* instance, void *data) { - $$* stub = static_cast<$$*>(data); +void ##::OnConnectedCB(const char* sender, const char* instance, void *data) { + ##* stub = static_cast<##*>(data); auto s = stub->service_factory_->CreateService(sender, instance); s->OnCreate(); stub->services_.emplace_back(std::move(s)); } -void $$::OnDisconnectedCB(const char* sender, const char* instance, void *data) { - $$* stub = static_cast<$$*>(data); +void ##::OnDisconnectedCB(const char* sender, const char* instance, void *data) { + ##* stub = static_cast<##*>(data); for (auto& i : stub->services_) { if (i->GetInstance() == instance) { diff --git a/idlc/cpp_gen/cpp_stub_header_gen.cc b/idlc/cpp_gen/cpp_stub_header_gen.cc index bfe7f64..503d566 100644 --- a/idlc/cpp_gen/cpp_stub_header_gen.cc +++ b/idlc/cpp_gen/cpp_stub_header_gen.cc @@ -27,17 +27,7 @@ CppStubHeaderGen::CppStubHeaderGen(std::shared_ptr doc) void CppStubHeaderGen::OnInitGen(std::ofstream& stream) { GenCopyright(stream); - stream << NLine(1); - stream << "#pragma once" << NLine(1) - << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(1) - << "#include " << NLine(2); + stream << CB_HEADER; GenNamespace(stream); } @@ -121,14 +111,7 @@ void CppStubHeaderGen::GenServiceBase(std::ofstream& stream, void CppStubHeaderGen::GenPublicMethods(std::ofstream& stream, const Interface& iface) { - GenTemplate(CB_PUBLIC_METHODS, stream, - [&]()->std::string { - return iface.GetID(); - }, - [&]()->std::string { - return iface.GetID(); - } - ); + stream << ReplaceAll(CB_PUBLIC_METHODS, "##", iface.GetID()); } } // namespace tidl diff --git a/idlc/cpp_gen/cpp_stub_header_gen_cb.h b/idlc/cpp_gen/cpp_stub_header_gen_cb.h index 2ba8a8c..21abaf3 100644 --- a/idlc/cpp_gen/cpp_stub_header_gen_cb.h +++ b/idlc/cpp_gen/cpp_stub_header_gen_cb.h @@ -33,7 +33,9 @@ R"__cpp_cb( static void OnConnectedCB(const char* sender, const char* instance, std::list> services_; )__cpp_cb"; -const char CB_SERVICE_BASE_FRONT[] = R"__cpp_cb( class ServiceBase { +const char CB_SERVICE_BASE_FRONT[] = +R"__cpp_cb( + class ServiceBase { public: class Factory { public: @@ -84,8 +86,8 @@ R"__cpp_cb( };)__cpp_cb"; const char CB_PUBLIC_METHODS[] = -R"__cpp_cb( $$(); - ~$$(); +R"__cpp_cb( ##(); + ~##(); /// /// Listens to client apps @@ -105,3 +107,18 @@ R"__cpp_cb( $$(); } )__cpp_cb"; + +const char CB_HEADER[] = +R"__cpp_cb( +#pragma once + +#include +#include +#include + +#include +#include +#include +#include + +)__cpp_cb"; diff --git a/idlc/cs_gen/cs_gen_base.cc b/idlc/cs_gen/cs_gen_base.cc index 0f75a46..982a252 100644 --- a/idlc/cs_gen/cs_gen_base.cc +++ b/idlc/cs_gen/cs_gen_base.cc @@ -274,7 +274,7 @@ std::string CsGeneratorBase::ConvertTypeToDeserializer( if (make_new_type) ret = n + " "; - if (IsDelegateType(type.ToString())) { + if (IsDelegateType(type)) { ret += id + " = new " + n +"(port, new WeakReference(b));\n"; ret += "CallbackBase."; } else { @@ -310,7 +310,7 @@ std::string CsGeneratorBase::ConvertTypeToSerializer( if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { - if (IsDelegateType(type.ToString())) + if (IsDelegateType(type)) return "CallbackBase.Serialize(" + parcel + ", " + id + ");\n"; if (iface_id != "") ret += iface_id + "."; @@ -512,23 +512,6 @@ void CsGeneratorBase::GenInvokeMethod(std::ofstream& stream, ); } -bool CsGeneratorBase::IsDelegateType(const std::string type_name) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_INTERFACE) - continue; - Interface& iface = static_cast(*i); - - for (auto& j : iface.GetDeclarations().GetDecls()) { - if (j->GetMethodType() == Declaration::MethodType::DELEGATE) { - if (j->GetID() == type_name) - return true; - } - } - } - - return false; -} - void CsGeneratorBase::GenCopyright(std::ofstream& stream) { GenTemplate(::cs_cb_copyright, stream, [&]()->std::string { @@ -542,5 +525,4 @@ void CsGeneratorBase::GenCopyright(std::ofstream& stream) { ); stream << NLine(1); } - } // namespace tidl diff --git a/idlc/cs_gen/cs_gen_base.h b/idlc/cs_gen/cs_gen_base.h index bb8642b..d9fa18c 100644 --- a/idlc/cs_gen/cs_gen_base.h +++ b/idlc/cs_gen/cs_gen_base.h @@ -58,7 +58,6 @@ class CsGeneratorBase : public Generator { const std::string iface_id = ""); std::string ConvertTypeToParcelType(const std::string& key); std::string GetParameters(const Parameters& ps); - bool IsDelegateType(const std::string type_name); std::string Tab(int cnt); std::string NLine(int cnt); diff --git a/idlc/cs_gen/cs_proxy_gen.cc b/idlc/cs_gen/cs_proxy_gen.cc index 51f283f..2e98940 100644 --- a/idlc/cs_gen/cs_proxy_gen.cc +++ b/idlc/cs_gen/cs_proxy_gen.cc @@ -153,7 +153,7 @@ void CsProxyGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { if (pt.GetDirection() == ParameterType::Direction::OUT) continue; m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p"); - if (IsDelegateType(pt.GetBaseType().ToString())) { + if (IsDelegateType(pt.GetBaseType())) { l += "_delegateList.Add(" + i->GetID() + ");\n"; } } diff --git a/idlc/generator.cc b/idlc/generator.cc index c2cb336..41f41be 100644 --- a/idlc/generator.cc +++ b/idlc/generator.cc @@ -72,4 +72,39 @@ std::string Generator::GetFileNamespace() const { return FileName.substr(p1, p2 - p1); } +std::string Generator::ReplaceAll(std::string str, + const std::string& from, + const std::string& to) { + std::size_t pos = 0; + while ((pos = str.find(from, pos)) != std::string::npos) { + str.replace(pos, from.length(), to); + pos += to.length(); + } + return str; +} + +bool Generator::IsDelegateType(const BaseType& type) { + for (auto& i : GetDocument().GetBlocks()) { + if (i->GetType() != Block::TYPE_INTERFACE) + continue; + Interface& iface = static_cast(*i); + + if (IsDelegateType(iface, type)) + return true; + } + + return false; +} + +bool Generator::IsDelegateType(const Interface& inf, + const BaseType& type) { + for (auto& i : inf.GetDeclarations().GetDecls()) { + if (i->GetMethodType() != Declaration::MethodType::DELEGATE) + continue; + if (i->GetID() == type.ToString()) + return true; + } + return false; +} + } // namespace tidl diff --git a/idlc/generator.h b/idlc/generator.h index 9257cac..6062c44 100644 --- a/idlc/generator.h +++ b/idlc/generator.h @@ -36,6 +36,10 @@ class Generator { void Run(const std::string& file_name); std::string AddIndent(int indent, std::string lines, bool space = true); std::string GetFileNamespace() const; + std::string ReplaceAll(std::string str, + const std::string& from, const std::string& to); + bool IsDelegateType(const Interface& inf, const BaseType& type); + bool IsDelegateType(const BaseType& type); void EnableNamespace(bool enable) { hasNamespace_ = enable; @@ -59,6 +63,20 @@ class Generator { stream << templ; } + template + std::string GenTemplateString(std::string templ, T cb, ARGS... args) { + size_t f = templ.find("$$"); + templ.replace(f, std::string("$$").length(), cb()); + return GenTemplateString(std::move(templ), args...); + } + + template + std::string GenTemplateString(std::string templ, T cb) { + size_t f = templ.find("$$"); + templ.replace(f, std::string("$$").length(), cb()); + return templ; + } + template void GenBrace(std::ofstream& stream, int indent, T cb, bool start_indent = true, bool ended_new_line = true) { -- 2.7.4