From 1226f1e8f5f5269b54f92da55cfad2b967852796 Mon Sep 17 00:00:00 2001 From: "jh9216.park" Date: Thu, 13 Jan 2022 21:59:50 -0500 Subject: [PATCH] Introduce class 'Inject' - It changes lamda function to string - example 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; }) } }); Change-Id: I97f065d14a77255ad233b4d7dc7c30298e85e20e Signed-off-by: jh9216.park --- idlc/gen/cpp_gen_base.cc | 48 +++++++++++++++++++++++++----------------------- idlc/gen/generator.h | 15 +++++++++++++++ 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/idlc/gen/cpp_gen_base.cc b/idlc/gen/cpp_gen_base.cc index 7f83b86..ff15437 100644 --- a/idlc/gen/cpp_gen_base.cc +++ b/idlc/gen/cpp_gen_base.cc @@ -202,9 +202,9 @@ 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 (const auto& i : st.GetElements()) { std::pair p; @@ -214,27 +214,29 @@ void CppGeneratorBase::GenStructureForBody(std::ofstream& stream, v.push_back(p); } - GenTemplate(ReplaceAll(ctor, "##", st.GetID()), stream, - [&]()->std::string { - std::string str; - for (auto& i : v) { - str += i.first + " " + i.second; - - if (i != v.back()) - str += ", "; - } - return str; - }, - [&]()->std::string { - std::string str; - for (auto& i : v) { - str += i.second + "_(std::move(" + i.second + "))"; - - if (i != v.back()) - str += ", "; + 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; + }) } - return str; - }); + }); stream << NLine(2); } diff --git a/idlc/gen/generator.h b/idlc/gen/generator.h index a7beed2..cfdab3d 100755 --- a/idlc/gen/generator.h +++ b/idlc/gen/generator.h @@ -135,6 +135,21 @@ class Generator { int type_ = 0; }; +class Inject { + public: + template + explicit Inject(T t) { + str_ = t(); + } + + operator std::string() const { + return str_; + } + + private: + std::string str_; +}; + } // namespace tidl #endif // IDLC_GENERATOR_H_ -- 2.7.4