diff options
Diffstat (limited to 'idlc/cpp_gen/cpp_gen_base.cc')
-rw-r--r-- | idlc/cpp_gen/cpp_gen_base.cc | 122 |
1 files changed, 34 insertions, 88 deletions
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<std::pair<std::string, std::string>> 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<ServiceBase>(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<Interface&>(*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) { |