void CsGeneratorBase::GenStructure(std::ofstream& stream, const Structure& st) {
std::vector<std::string> 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) {
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) {
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
void GenCodeBlock(std::string filename,
void (*cb)(Generator& gen, int pos) = nullptr);
+ template<typename T, typename ...ARGS>
+ 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<typename T>
+ 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<typename T>
+ 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;