From 4e0681e2461099e1364b1d49e9ba2b6022f1ab92 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Sun, 23 Jan 2022 23:44:55 -0500 Subject: [PATCH] Refactor c++ generator - Replace 'GenTemplate' to 'ReplaceAll' Change-Id: I81e1b0e6925377032849b04529e46e55ec4f1e19 Signed-off-by: jh9216.park --- idlc/gen/cpp_gen_base.cc | 497 +++++++++++++++++---------------------- idlc/gen/cpp_gen_base.h | 4 +- idlc/gen/cpp_gen_base_cb.h | 120 +++++++++- idlc/gen/cpp_proxy_body_gen.cc | 20 +- idlc/gen/cpp_proxy_body_gen_cb.h | 4 +- idlc/gen/cpp_proxy_header_gen.cc | 4 +- idlc/gen/cpp_stub_header_gen.cc | 2 +- idlc/gen/replace_all.cc | 24 ++ idlc/gen/replace_all.h | 7 +- 9 files changed, 373 insertions(+), 309 deletions(-) diff --git a/idlc/gen/cpp_gen_base.cc b/idlc/gen/cpp_gen_base.cc index c0c5eaf..0329841 100644 --- a/idlc/gen/cpp_gen_base.cc +++ b/idlc/gen/cpp_gen_base.cc @@ -68,28 +68,14 @@ void CppGeneratorBase::GenStructuresForHeader(std::ofstream& stream) { continue; Structure& st = static_cast(*i); GenStructureForHeader(stream, st); - stream << std::endl; } } void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, const Structure& st) { - const char ctor[] = " $$();\n" \ - " $$($$);\n"; - const char variable[] = "$$\n"; - - stream << "class " << st.GetID() << " final "; - - GenBrace(stream, 0, [&]() { - stream << " public:" << NLine(1); - GenTemplate(ctor, stream, - [&]()->std::string { - return st.GetID(); - }, - [&]()->std::string { - return st.GetID(); - }, - [&]()->std::string { + ReplaceAll(CB_STRUCTURE_FOR_HEADER) + .Change("", st.GetID()) + .Change("", [&]() { std::string str; int n = 1; for (const auto& i : st.GetElements()) { @@ -99,18 +85,18 @@ void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, n++; } return str; - }); - - stream << NLine(1); - for (const auto& i : st.GetElements()) { - GenSetter(stream, *i); - GenGetter(stream, *i); - stream << NLine(1); - } - - stream << " private:"; - GenTemplate(variable, stream, - [&]()->std::string { + }) + .Change("", [&]() { + std::string str = NLine(1); + for (const auto& i : st.GetElements()) { + str += GetSetter(*i); + str += NLine(1); + str += GetGetter(*i); + str += NLine(1); + } + return str; + }) + .Change("", [&]() { std::string str; for (const auto& i : st.GetElements()) { str += NLine(1) + Tab(1) @@ -125,68 +111,51 @@ void CppGeneratorBase::GenStructureForHeader(std::ofstream& stream, } str += NLine(1); return str; - }); - }, false, false); - stream << ";" << NLine(1); + }) + .Out(stream); } -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" || - ele.GetType().ToString() == "file") { - return "std::move(" + ele.GetID() + ")"; - } +std::string CppGeneratorBase::GetSetter(const Element& ele) { + constexpr const char setter[] = + " void Set( ) {\n" \ + " _ = ;\n" \ + " }\n"; + + return std::string(ReplaceAll(setter) + .Change("", ele.GetID()) + .Change("", ConvertTypeToString(ele.GetType())) + .Change("", [&]() { + if (ele.GetType().IsUserDefinedType() || + ele.GetType().GetMetaType() != nullptr || + ele.GetType().ToString() == "string" || + ele.GetType().ToString() == "bundle" || + ele.GetType().ToString() == "file") { + return "std::move(" + ele.GetID() + ")"; + } - return ele.GetID(); - }); - stream << NLine(1); + return ele.GetID(); + })); } -void CppGeneratorBase::GenGetter(std::ofstream& stream, const Element& ele) { - const char getter[] = - "$$ Get$$() const {\n" \ - " return $$_;\n" \ - "}\n"; +std::string CppGeneratorBase::GetGetter(const Element& ele) { + constexpr 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" || - ele.GetType().ToString() == "file") { - return "const " + ConvertTypeToString(ele.GetType()) + "&"; - } + return std::string(ReplaceAll(getter) + .Change("", [&]() { + if (ele.GetType().IsUserDefinedType() || + ele.GetType().GetMetaType() != nullptr || + ele.GetType().ToString() == "string" || + ele.GetType().ToString() == "bundle" || + ele.GetType().ToString() == "file") { + return "const " + ConvertTypeToString(ele.GetType()) + "&"; + } - return ConvertTypeToString(ele.GetType()); - }, - [&]()->std::string { - return ele.GetID(); - }, - [&]()->std::string { - return ele.GetID(); - }); + return ConvertTypeToString(ele.GetType()); + }) + .Change("", ele.GetID())); } void CppGeneratorBase::GenStructuresForBody(std::ofstream& stream) { @@ -214,29 +183,27 @@ void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, v.push_back(p); } - stream << ReplaceAll(ctor, { - { "", st.GetID() }, - { "", Inject([&]() { - std::string str; - for (auto& i : v) { - str += i.first + " " + i.second; - if (i != v.back()) - str += ", "; - } - return str; - }) - }, - { "", Inject([&]() { - std::string str; - for (auto& i : v) { - str += i.second + "_(std::move(" + i.second + "))"; - if (i != v.back()) - str += ", "; - } - return str; - }) - } - }); + ReplaceAll(ctor) + .Change("", st.GetID()) + .Change("", [&]() { + std::string str; + for (auto& i : v) { + str += i.first + " " + i.second; + if (i != v.back()) + str += ", "; + } + return str; + }) + .Change("", [&]() { + std::string str; + for (auto& i : v) { + str += i.second + "_(std::move(" + i.second + "))"; + if (i != v.back()) + str += ", "; + } + return str; + }) + .Out(stream); stream << NLine(2); } @@ -264,24 +231,26 @@ void CppGeneratorBase::GenPrototype(std::ofstream& stream) { void CppGeneratorBase::GenSerializer(std::ofstream& stream, const Structure& st, bool proto) { - const char parcel_str[] = "rpc_port_parcel_h"; - - stream << parcel_str << " operator << (" - << parcel_str << " h, const " << st.GetID() << "& param)"; if (proto) { - stream << ";" << NLine(1); + ReplaceAll(CB_SERIALIZER_HEADER) + .Change("", st.GetID()) + .Out(stream); return; } - stream << " "; - GenBrace(stream, 0, [&]() { - for (const auto& i : st.GetElements()) { - stream << AddIndent(TAB_SIZE, - ConvertTypeToSerializer(i->GetType(), + ReplaceAll(CB_SERIALIZER_BODY) + .Change("", st.GetID()) + .Change("", [&]() { + std::string str; + for (const auto& i : st.GetElements()) { + str += AddIndent(TAB_SIZE, + ConvertTypeToSerializer(i->GetType(), "param.Get" + i->GetID() + "()", "h")); - } - stream << Tab(1) << "return h;" << NLine(1); - }, false); + } + str += Tab(1) + "return h;"; + return str; + }) + .Out(stream); } void CppGeneratorBase::GenDeSerializer(std::ofstream& stream) { @@ -296,25 +265,27 @@ void CppGeneratorBase::GenDeSerializer(std::ofstream& stream) { void CppGeneratorBase::GenDeSerializer(std::ofstream& stream, const Structure& st, bool proto) { - const char parcel_str[] = "rpc_port_parcel_h"; - - stream << parcel_str << " operator >> (" - << parcel_str << " h, " << st.GetID() << "& param)"; if (proto) { - stream << ";" << NLine(1); + ReplaceAll(CB_DESERIALIZER_HEADER) + .Change("", st.GetID()) + .Out(stream); return; } - stream << " "; - GenBrace(stream, 0, [&]() { - for (const auto& i : st.GetElements()) { - stream << AddIndent(TAB_SIZE, - ConvertTypeToDeserializer(i->GetType(), i->GetID(), "h")); - stream << Tab(1) << "param.Set" << i->GetID() << "(std::move(" - << i->GetID() << "));" << NLine(2); - } - stream << Tab(1) << "return h;" << NLine(1); - }, false); + ReplaceAll(CB_DESERIALIZER_BODY) + .Change("", st.GetID()) + .Change("", [&]() { + std::string str; + for (const auto& i : st.GetElements()) { + str += AddIndent(TAB_SIZE, + ConvertTypeToDeserializer(i->GetType(), i->GetID(), "h")); + str += Tab(1) + "param.Set" + i->GetID() + "(std::move(" + + i->GetID() + "));" + NLine(2); + } + str += Tab(1) + "return h;"; + return str; + }) + .Out(stream); } std::string CppGeneratorBase::ConvertTypeToString(const BaseType& type) { @@ -353,53 +324,39 @@ void CppGeneratorBase::AddSerializerList(const BaseType& type) { 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); + ReplaceAll(CB_SERIALIZER_HEADER) + .Change("", ConvertTypeToString(type)) + .Out(stream); + ReplaceAll(CB_DESERIALIZER_HEADER) + .Change("", ConvertTypeToString(type)) + .Out(stream); return; } - stream << " "; - GenBrace(stream, 0, [&]() { - stream << Tab(1) - << "rpc_port_parcel_write_array_count(h, c.size());" - << NLine(1); - stream << Tab(1) << "for (const auto& i : c) "; - GenBrace(stream, TAB_SIZE, [&]() { - auto* ptr = type.GetMetaType(); - if (ptr == nullptr) - return; - - auto& mt = *ptr; - stream << AddIndent(TAB_SIZE * 2, ConvertTypeToSerializer(mt, "i", "h")); - }, false); - stream << Tab(1) << "return h;" << NLine(1); - }, false); - stream << NLine(1); + ReplaceAll(CB_SERIALIZER_LIST_BODY) + .Change("", ConvertTypeToString(type)) + .Change("", [&]() { + auto* ptr = type.GetMetaType(); + if (ptr == nullptr) + return std::string(""); + auto& mt = *ptr; + return std::string(AddIndent(TAB_SIZE * 2, + ConvertTypeToSerializer(mt, "i", "h"))); + }) + .Out(stream); - 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* ptr = type.GetMetaType(); - if (ptr == nullptr) - return; - - auto& mt = *ptr; - stream << AddIndent(TAB_SIZE * 2, - ConvertTypeToDeserializer(mt, "v", "h", true)); - stream << Tab(2) << "c.push_back(std::move(v));" << NLine(1); - }, false); - stream << Tab(1) << "return h;" << NLine(1); - }, false); + ReplaceAll(CB_DESERIALIZER_LIST_BODY) + .Change("", ConvertTypeToString(type)) + .Change("", [&]() { + auto* ptr = type.GetMetaType(); + if (ptr == nullptr) + return std::string(""); + auto& mt = *ptr; + return std::string(AddIndent(TAB_SIZE * 2, + ConvertTypeToDeserializer(mt, "v", "h", true))); + }) + .Out(stream); } void CppGeneratorBase::GenListSerializer(std::ofstream& stream, bool proto) { @@ -432,34 +389,40 @@ void CppGeneratorBase::GenListSerializer(std::ofstream& stream, bool proto) { void CppGeneratorBase::GenMethodId(std::ofstream& stream, const Interface& iface) { - stream << Tab(1) << "enum class MethodId : int "; - GenBrace(stream, TAB_SIZE, [&]() { - int cnt = 2; - stream << Tab(2) << "__Result = 0," << NLine(1); - stream << Tab(2) << "__Callback = 1," << NLine(1); - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) - continue; - stream << Tab(2) - << i->GetID() << " = " << cnt++ << "," << NLine(1); - } - }, false, false); - stream << ";" << NLine(2); + ReplaceAll(CB_METHOD_IDS) + .Change("", [&]() { + std::string str; + int cnt = 2; + for (const auto& i : iface.GetDeclarations()) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) + continue; + str += Tab(1) + i->GetID() + " = " + std::to_string(cnt++) + "," + + NLine(1); + } + + return str; + }) + .AddIndent(TAB_SIZE) + .Out(stream); } void CppGeneratorBase::GenDelegateId(std::ofstream& stream, const Interface& iface) { - stream << Tab(1) << "enum class DelegateId : int "; - GenBrace(stream, TAB_SIZE, [&]() { - int cnt = 1; - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() != Declaration::MethodType::DELEGATE) - continue; - stream << Tab(2) - << i->GetID() << " = " << cnt++ << "," << NLine(1); - } - }, false, false); - stream << ";" << NLine(2); + ReplaceAll(CB_DELEGATE_IDS) + .Change("", [&]() { + int cnt = 1; + std::string str; + for (const auto& i : iface.GetDeclarations()) { + if (i->GetMethodType() != Declaration::MethodType::DELEGATE) + continue; + str += Tab(1) + i->GetID() + " = " + std::to_string(cnt++) + "," + + NLine(1); + } + + return str; + }) + .AddIndent(TAB_SIZE) + .Out(stream); } void CppGeneratorBase::GenParameters(std::ofstream& stream, @@ -503,25 +466,11 @@ std::string CppGeneratorBase::GenPrivateSharingRequest(const BaseType& type, std::string id) { std::string ret; if (type.GetMetaType() != nullptr && (type.GetMetaType()->GetFullName() == "file")) { - ret += std::string("for (const auto& i : " + id + ") {\n") - + std::string(" std::vector v;\n") - + std::string(" std::string name = i.GetFileName();\n") - + std::string(" v.push_back(name.c_str());\n") - + std::string(" int r = rpc_port_set_private_sharing_array(port_, v.data(), v.size());\n") - + std::string(" if (r != RPC_PORT_ERROR_NONE) {\n") - + std::string(" _E(\"Failed to set private sharing\");\n") - + std::string(" throw InvalidIOException();\n") - + std::string(" }\n") - + std::string("}\n"); + ret = ReplaceAll(CB_PRIVATE_SHARING_REQUEST_META) + .Change("", id); } else if (type.ToString() == "file") { - ret += "std::vector v;\n" - + std::string("std::string name = " + id + ".GetFileName();\n") - + std::string("v.push_back(name.c_str());\n") - + std::string("int r = rpc_port_set_private_sharing_array(port_, v.data(), v.size());\n") - + std::string("if (r != RPC_PORT_ERROR_NONE) {\n") - + std::string(" _E(\"Failed to set private sharing\");\n") - + std::string(" throw InvalidIOException();\n") - + std::string("}\n"); + ret = ReplaceAll(CB_PRIVATE_SHARING_REQUEST) + .Change("", id); } return ret; } @@ -634,73 +583,69 @@ void CppGeneratorBase::GenBodyCallbacks(std::ofstream& stream, void CppGeneratorBase::GenBodyCallback(std::ofstream& stream, const Interface& iface, const Declaration& decl, bool is_proxy) { if (!is_proxy) { - GenTemplate(CB_CALLBACK_INVOKE_METHOD, stream, - [&]()->std::string { - return iface.GetID(); - }, - [&]()->std::string { - return decl.GetID(); - }, - [&]()->std::string { - return GetParameters(decl.GetParameters()); - }, - [&]()->std::string { - std::string m; - for (const auto& i : decl.GetParameters()) { - auto& pt = i->GetParameterType(); - m += AddIndent(TAB_SIZE, - GenPrivateSharingRequest(pt.GetBaseType(), i->GetID())); - m += AddIndent(TAB_SIZE, - ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p")); - } - return m; - }); + ReplaceAll(CB_CALLBACK_INVOKE_METHOD) + .Change("", iface.GetID()) + .Change("", decl.GetID()) + .Change("", GetParameters(decl.GetParameters())) + .Change("", [&]() { + std::string m; + for (const auto& i : decl.GetParameters()) { + auto& pt = i->GetParameterType(); + m += AddIndent(TAB_SIZE, + GenPrivateSharingRequest(pt.GetBaseType(), i->GetID())); + m += AddIndent(TAB_SIZE, + ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p")); + } + return m; + }) + .Out(stream); } else { - GenTemplate(CB_CALLBACK_ON_RECEIVED_EVENT_METHOD, stream, - [&]()->std::string { - return iface.GetID(); - }, - [&]()->std::string { - return decl.GetID(); - }, - [&]()->std::string { - int cnt = 1; - std::string ret; - for (const auto& i : decl.GetParameters()) { - std::string v = "param" + std::to_string(cnt); - std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), v, "parcel"); - ret += AddIndent(TAB_SIZE, c) + NLine(1); - cnt++; - } + ReplaceAll(CB_CALLBACK_ON_RECEIVED_EVENT_METHOD) + .Change("", iface.GetID()) + .Change("", decl.GetID()) + .Change("", [&]() { + int cnt = 1; + std::string ret; + for (const auto& i : decl.GetParameters()) { + std::string v = "param" + std::to_string(cnt); + std::string c = ConvertTypeToDeserializer( + i->GetParameterType().GetBaseType(), v, "parcel"); + ret += AddIndent(TAB_SIZE, c) + NLine(1); + cnt++; + } - cnt = 1; - ret += Tab(1) + "OnReceived("; - for (auto i = decl.GetParameters().begin(); - i != decl.GetParameters().end(); ++i) { - if (cnt != 1) { - ret += ", "; + cnt = 1; + ret += Tab(1) + "OnReceived("; + for (auto i = decl.GetParameters().begin(); + i != decl.GetParameters().end(); ++i) { + if (cnt != 1) { + ret += ", "; + } + ret += "std::move(param" + std::to_string(cnt) + ")"; + cnt++; } - ret += "std::move(param" + std::to_string(cnt) + ")"; - cnt++; - } - ret += ");"; + ret += ");"; - return ret; - }); + return ret; + }) + .Out(stream); } } void CppGeneratorBase::GenHeaderCallbacks(std::ofstream& stream, const Interface& iface, bool is_proxy) { - stream << CB_CALLBACK_BASE_HEADER_FRONT; - if (is_proxy) { - stream << Tab(1) - << " virtual void OnReceivedEvent(rpc_port_parcel_h port) = 0;" - << NLine(1); - } - stream << CB_CALLBACK_BASE_HEADER_BACK; + ReplaceAll(CB_CALLBACK_BASE_HEADER) + .Change("", [&]() { + std::string str; + if (is_proxy) { + str = Tab(2) + + "virtual void OnReceivedEvent(rpc_port_parcel_h port) = 0;"; + } + + return str; + }) + .Out(stream); for (const auto& i : iface.GetDeclarations()) { if (i->GetMethodType() != Declaration::MethodType::DELEGATE) diff --git a/idlc/gen/cpp_gen_base.h b/idlc/gen/cpp_gen_base.h index 4b575fc..25400a8 100644 --- a/idlc/gen/cpp_gen_base.h +++ b/idlc/gen/cpp_gen_base.h @@ -63,8 +63,8 @@ class CppGeneratorBase : public Generator { void GenVersionDefinition(std::ofstream& stream); private: - void GenSetter(std::ofstream& stream, const Element& ele); - void GenGetter(std::ofstream& stream, const Element& ele); + std::string GetSetter(const Element& ele); + std::string GetGetter(const Element& ele); void AddSerializerList(const BaseType& type); void GenListSerializer(std::ofstream& stream, const BaseType& type, bool proto = false); diff --git a/idlc/gen/cpp_gen_base_cb.h b/idlc/gen/cpp_gen_base_cb.h index 25b0994..66b1e85 100644 --- a/idlc/gen/cpp_gen_base_cb.h +++ b/idlc/gen/cpp_gen_base_cb.h @@ -136,16 +136,13 @@ R"__cpp_cb(/* */ )__cpp_cb"; -const char CB_CALLBACK_BASE_HEADER_FRONT[] = +const char CB_CALLBACK_BASE_HEADER[] = R"__cpp_cb( class CallbackBase { public: CallbackBase(int delegate_id, bool once); virtual ~CallbackBase() = default; -)__cpp_cb"; - -const char CB_CALLBACK_BASE_HEADER_BACK[] = -R"__cpp_cb( + int GetId() const; int GetSeqId() const; bool IsOnce() const; @@ -200,7 +197,7 @@ R"__cpp_cb( const char CB_CALLBACK_INVOKE_METHOD[] = R"__cpp_cb( -void $$::$$::Invoke($$) { +void ::::Invoke() { if (port_ == nullptr) throw NotConnectedSocketException(); if (service_.lock().get() == nullptr) @@ -213,7 +210,7 @@ void $$::$$::Invoke($$) { rpc_port_parcel_create(&p); rpc_port_parcel_write_int32(p, static_cast(MethodId::__Callback)); p << *this; -$$ + // Send set_last_result(rpc_port_parcel_send(p, port_)); rpc_port_parcel_destroy(p); @@ -223,8 +220,8 @@ $$ const char CB_CALLBACK_ON_RECEIVED_EVENT_METHOD[] = R"__cpp_cb( -void $$::$$::OnReceivedEvent(rpc_port_parcel_h parcel) { -$$ +void ::::OnReceivedEvent(rpc_port_parcel_h parcel) { + } )__cpp_cb"; @@ -271,4 +268,109 @@ R"__cpp_cb( #endif )__cpp_cb"; +constexpr const char CB_STRUCTURE_FOR_HEADER[] = +R"__cpp_cb( +class final { + public: + (); + (); + + private: +}; +)__cpp_cb"; + +constexpr const char CB_SERIALIZER_HEADER[] = +R"__cpp_cb( +rpc_port_parcel_h operator << (rpc_port_parcel_h h, const & param); +)__cpp_cb"; + +constexpr const char CB_SERIALIZER_BODY[] = +R"__cpp_cb( +rpc_port_parcel_h operator << (rpc_port_parcel_h h, const & param) { + +} +)__cpp_cb"; + +constexpr const char CB_SERIALIZER_LIST_BODY[] = +R"__cpp_cb( +rpc_port_parcel_h operator << (rpc_port_parcel_h h, const & param) { + rpc_port_parcel_write_array_count(h, param.size()); + for (const auto& i : param) { + + } + + return h; +} +)__cpp_cb"; + +constexpr const char CB_DESERIALIZER_HEADER[] = +R"__cpp_cb( +rpc_port_parcel_h operator >> (rpc_port_parcel_h h, & param); +)__cpp_cb"; + +constexpr const char CB_DESERIALIZER_BODY[] = +R"__cpp_cb( +rpc_port_parcel_h operator >> (rpc_port_parcel_h h, & param) { + +} +)__cpp_cb"; + +constexpr const char CB_DESERIALIZER_LIST_BODY[] = +R"__cpp_cb( +rpc_port_parcel_h operator >> (rpc_port_parcel_h h, & param) { + int l = 0; + rpc_port_parcel_read_array_count(h, &l); + + for (int i = 0; i < l; i++) { + + param.push_back(std::move(v)); + } + + return h; +} +)__cpp_cb"; + +constexpr const char CB_METHOD_IDS[] = +R"__cpp_cb( +enum class MethodId : int { + __Result = 0, + __Callback = 1, + +}; +)__cpp_cb"; + +constexpr const char CB_DELEGATE_IDS[] = +R"__cpp_cb( +enum class DelegateId : int { + +}; +)__cpp_cb"; + +constexpr const char CB_PRIVATE_SHARING_REQUEST_META[] = +R"__cpp_cb( +for (const auto& i : ) { + std::vector v; + std::string name = i.GetFileName(); + v.push_back(name.c_str()); + int r = rpc_port_set_private_sharing_array(port_, v.data(), v.size()); + if (r != RPC_PORT_ERROR_NONE) { + _E("Failed to set private sharing"); + throw InvalidIOException(); + } +} +)__cpp_cb"; + +constexpr const char CB_PRIVATE_SHARING_REQUEST[] = +R"__cpp_cb( +std::vector v; +std::string name = .GetFileName(); +v.push_back(name.c_str()); +rpc_port_set_private_sharing_array(port_, v.data(), v.size()); +int r = rpc_port_set_private_sharing_array(port_, v.data(), v.size()); +if (r != RPC_PORT_ERROR_NONE) { + _E("Failed to set private sharing"); + throw InvalidIOException(); +} +)__cpp_cb"; + #endif // IDLC_CPP_GEN_CPP_GEN_BASE_CB_H_ diff --git a/idlc/gen/cpp_proxy_body_gen.cc b/idlc/gen/cpp_proxy_body_gen.cc index ac8db10..b82b2d9 100644 --- a/idlc/gen/cpp_proxy_body_gen.cc +++ b/idlc/gen/cpp_proxy_body_gen.cc @@ -92,24 +92,16 @@ void CppProxyBodyGen::GenInterface(std::ofstream& stream, void CppProxyBodyGen::GenConstructor(std::ofstream& stream, const Interface& iface) { - GenTemplate(CB_PROXY_INTERFACE_CTOR, stream, - [&]()->std::string { - return iface.GetID(); - }, - [&]()->std::string { - return iface.GetID(); - }); + ReplaceAll(CB_PROXY_INTERFACE_CTOR) + .Change("", iface.GetID()) + .Out(stream); } void CppProxyBodyGen::GenDestructor(std::ofstream& stream, const Interface& iface) { - GenTemplate(CB_DTOR, stream, - [&]()->std::string { - return iface.GetID(); - }, - [&]()->std::string { - return iface.GetID(); - }); + ReplaceAll(CB_DTOR) + .Change("", iface.GetID()) + .Out(stream); } void CppProxyBodyGen::GenHelperMethods(std::ofstream& stream, diff --git a/idlc/gen/cpp_proxy_body_gen_cb.h b/idlc/gen/cpp_proxy_body_gen_cb.h index c9708bd..8ef6c15 100644 --- a/idlc/gen/cpp_proxy_body_gen_cb.h +++ b/idlc/gen/cpp_proxy_body_gen_cb.h @@ -19,7 +19,7 @@ const char CB_DTOR[] = R"__cpp_cb( -$$::~$$() { +::~() { if (proxy_) rpc_port_proxy_destroy(proxy_); } @@ -219,7 +219,7 @@ void ##::OnReceivedCB(const char *ep, const char *port_name, void *data) { const char CB_PROXY_INTERFACE_CTOR[] = R"__cpp_cb( -$$::$$(IEventListener* listener, const std::string& target_appid) +::(IEventListener* listener, const std::string& target_appid) : port_(nullptr), callback_port_(nullptr), proxy_(nullptr), listener_(listener), target_appid_(target_appid) { int r = rpc_port_proxy_create(&proxy_); diff --git a/idlc/gen/cpp_proxy_header_gen.cc b/idlc/gen/cpp_proxy_header_gen.cc index 05924df..9bd1b3f 100644 --- a/idlc/gen/cpp_proxy_header_gen.cc +++ b/idlc/gen/cpp_proxy_header_gen.cc @@ -39,8 +39,8 @@ void CppProxyHeaderGen::GenNamespace(std::ofstream& stream) { GenBrace(stream, 0, [&]() { stream << "namespace " << GetFileNamespace() << " "; GenBrace(stream, 0, [&]() { - stream << NLine(1); GenStructuresForHeader(stream); + stream << NLine(1); stream << "namespace proxy "; GenBrace(stream, 0, [&]() { GenExceptions(stream); @@ -80,7 +80,7 @@ void CppProxyHeaderGen::GenInterface(std::ofstream& stream, return iface.GetID(); }); GenMethods(stream, iface); - stream << NLine(1) << " private:" << NLine(1); + stream << NLine(1) << " private:"; GenMethodId(stream, iface); GenDelegateId(stream, iface); stream << CB_PRIVATE_MEMBERS; diff --git a/idlc/gen/cpp_stub_header_gen.cc b/idlc/gen/cpp_stub_header_gen.cc index 386b269..a50af98 100644 --- a/idlc/gen/cpp_stub_header_gen.cc +++ b/idlc/gen/cpp_stub_header_gen.cc @@ -93,7 +93,7 @@ void CppStubHeaderGen::GenPublic(std::ofstream& stream, void CppStubHeaderGen::GenPrivate(std::ofstream& stream, const Interface& iface) { - stream << " private:" << NLine(1); + stream << " private:"; GenMethodId(stream, iface); GenDelegateId(stream, iface); stream << CB_PRIVATE_MEMBERS; diff --git a/idlc/gen/replace_all.cc b/idlc/gen/replace_all.cc index a119b36..f7f7fca 100644 --- a/idlc/gen/replace_all.cc +++ b/idlc/gen/replace_all.cc @@ -16,6 +16,7 @@ #include +#include #include "idlc/gen/replace_all.h" @@ -73,4 +74,27 @@ void ReplaceAll::Out(std::ofstream& stream) { stream << str_; } +ReplaceAll& ReplaceAll::AddIndent(int indent, bool space) { + std::stringstream ss(str_); + std::string result; + std::string to; + + while (std::getline(ss, to, '\n')) { + if (to.length() > 0) { + for (int i = 0; i < indent; i++) { + if (space) + result += " "; + else + result += "\t"; + } + } + + result += to; + result += "\n"; + } + + str_ = result; + return *this; +} + } // namespace tidl diff --git a/idlc/gen/replace_all.h b/idlc/gen/replace_all.h index 553760d..57ec0a2 100755 --- a/idlc/gen/replace_all.h +++ b/idlc/gen/replace_all.h @@ -37,7 +37,7 @@ class ReplaceAll { template ReplaceAll& Change(const std::string& from, T t) { - std::string str = t(); + std::string str(t()); return Change(from, str); } @@ -46,7 +46,7 @@ class ReplaceAll { template ReplaceAll& Once(const std::string& from, T t) { - std::string str = t(); + std::string str(t()); return Once(from, str); } @@ -58,11 +58,12 @@ class ReplaceAll { template ReplaceAll& Transform(T t) { - std::string str = t(str_); + std::string str(t(str_)); str_ = str; return *this; } + ReplaceAll& AddIndent(int indent, bool space = true); void Out(std::ofstream& stream); private: -- 2.7.4