From 849a6aaa4a2821deff20f3c82822aeed619f2ad6 Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Wed, 20 Dec 2017 10:36:02 +0900 Subject: [PATCH] Provide utility API to generate code easily Change-Id: I830acd782dce83eb1375cdb78c8f73b60b570de9 Signed-off-by: Junghoon Park --- idlc/cs_gen/cs_gen_base.cc | 40 ++++++++++++++++++++++++++++------------ idlc/cs_gen/cs_proxy_gen.cc | 6 +++--- idlc/cs_gen/cs_stub_gen.cc | 6 +++--- idlc/generator.h | 28 ++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 18 deletions(-) diff --git a/idlc/cs_gen/cs_gen_base.cc b/idlc/cs_gen/cs_gen_base.cc index 724f1e4..ca703f2 100644 --- a/idlc/cs_gen/cs_gen_base.cc +++ b/idlc/cs_gen/cs_gen_base.cc @@ -42,25 +42,41 @@ void CsGeneratorBase::GenStructures(std::ofstream& stream) { void CsGeneratorBase::GenStructure(std::ofstream& stream, const Structure& st) { std::vector v; + const char property[] = " public $$ $$ { get; set; }\n"; + const char ctor[] = " public $$()\n" \ + " {\n" \ + "$$" \ + " }\n"; + stream << TAB <<"sealed class " << st.GetID() << std::endl; - stream << TAB <<"{" << std::endl; + GenBrace(stream, 4, [&]() { for (auto& i : st.GetElements().GetElms()) { - stream << TAB << TAB <<"public " - << ConvertTypeToString(i->GetType()) << " " - << i->GetID() << " { get; set; }" << std::endl; + GenTemplate(property, stream, + [&]()->std::string { + return ConvertTypeToString(i->GetType()); + }, + [&]()->std::string { + return i->GetID(); + } + ); if (i->GetType().ToString() == "bundle") { v.push_back(i->GetID() + " = " + "new Bundle()"); } } - stream << TAB << TAB << "public " + st.GetID() << "()" << std::endl; - stream << TAB << TAB << "{" << std::endl; - for (auto& i : v) { - stream << TAB << TAB << TAB << i << std::endl; - } - stream << TAB << TAB << "}" << std::endl; - - stream << TAB << "}" << std::endl; + GenTemplate(ctor, stream, + [&]()->std::string { + return st.GetID(); + }, + [&]()->std::string { + std::string str; + for (auto& i : v) { + str += " " + i + ";\n"; + } + return str; + } + ); + }); } std::string CsGeneratorBase::ConvertTypeToString(const BaseType& type) { diff --git a/idlc/cs_gen/cs_proxy_gen.cc b/idlc/cs_gen/cs_proxy_gen.cc index 449528b..f2d47a0 100644 --- a/idlc/cs_gen/cs_proxy_gen.cc +++ b/idlc/cs_gen/cs_proxy_gen.cc @@ -37,9 +37,9 @@ void CsProxyGen::OnFiniGen(std::ofstream& stream) { void CsProxyGen::GenNamespace(std::ofstream& stream) { stream << "namespace RPCPort" << std::endl; - stream << "{" << std::endl; - GenStructures(stream); - stream << "} // RPCPort" << std::endl; + GenBrace(stream, 0, [&]() { + GenStructures(stream); + }); } void CsProxyGen::GenInterop(std::ofstream& stream) { diff --git a/idlc/cs_gen/cs_stub_gen.cc b/idlc/cs_gen/cs_stub_gen.cc index afe70ab..016983e 100644 --- a/idlc/cs_gen/cs_stub_gen.cc +++ b/idlc/cs_gen/cs_stub_gen.cc @@ -42,9 +42,9 @@ void CsStubGen::GenInterop(std::ofstream& stream) { void CsStubGen::GenNamespace(std::ofstream& stream) { stream << "namespace RPCPort" << std::endl; - stream << "{" << std::endl; - GenStructures(stream); - stream << "} // RPCPort" << std::endl; + GenBrace(stream, 0, [&]() { + GenStructures(stream); + }); } } // namespace tidl diff --git a/idlc/generator.h b/idlc/generator.h index 2564106..54b3167 100644 --- a/idlc/generator.h +++ b/idlc/generator.h @@ -37,6 +37,34 @@ class Generator { void GenCodeBlock(std::string filename, void (*cb)(Generator& gen, int pos) = nullptr); + template + void GenTemplate(std::string templ, std::ofstream& stream, T cb, ARGS... args) { + size_t f = templ.find("$$"); + templ.replace(f, std::string("$$").length(), cb()); + GenTemplate(std::move(templ), stream, args...); + } + + template + void GenTemplate(std::string templ, std::ofstream& stream, T cb) { + size_t f = templ.find("$$"); + templ.replace(f, std::string("$$").length(), cb()); + stream << templ; + } + + template + void GenBrace(std::ofstream& stream, int indent, T cb, + bool start_indent = true) { + if (start_indent) { + for(int i = 0; i < indent; i++) + stream << " "; + } + stream << "{" << std::endl; + cb(); + for(int i = 0; i < indent; i++) + stream << " "; + stream << "}" << std::endl; + } + virtual void OnInitGen(std::ofstream& stream) = 0; virtual void OnFiniGen(std::ofstream& stream) = 0; -- 2.7.4