From 282ec9b2ebba1827c3318ecbdc4b522e5f0086f6 Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 27 Aug 2018 20:26:42 +0900 Subject: [PATCH] Modified C Generator List setter/getter is modified. Adds new API for handling list: - rpc_port__remove_() - rpc_port__get__length() Change-Id: I9f826b12593cabe5dae4a4da75727ce2f8c88e05 Signed-off-by: Hwankyu Jhun --- idlc/c_gen/c_body_gen_base.cc | 206 ++++++++++++++++++-------------------- idlc/c_gen/c_body_gen_base.h | 2 + idlc/c_gen/c_body_gen_base_cb.h | 61 ++++++----- idlc/c_gen/c_header_gen_base.cc | 52 ++++++++++ idlc/c_gen/c_header_gen_base.h | 2 + idlc/c_gen/c_header_gen_base_cb.h | 10 ++ 6 files changed, 201 insertions(+), 132 deletions(-) diff --git a/idlc/c_gen/c_body_gen_base.cc b/idlc/c_gen/c_body_gen_base.cc index bc8eaea..018e7d5 100644 --- a/idlc/c_gen/c_body_gen_base.cc +++ b/idlc/c_gen/c_body_gen_base.cc @@ -77,6 +77,8 @@ void CBodyGeneratorBase::GenStructure(std::ofstream& stream, GenStructureSetter(stream, st); GenStructureGetter(stream, st); GenStructureIterator(stream, st); + GenStructureRemover(stream, st); + GenStructureLengthGetter(stream, st); } void CBodyGeneratorBase::GenStructureDeclaration(std::ofstream& stream, @@ -329,6 +331,75 @@ void CBodyGeneratorBase::GenStructureCloner(std::ofstream& stream, })); } +void CBodyGeneratorBase::GenStructureRemover(std::ofstream& stream, + const Structure& st) { + for (auto& i : st.GetElements().GetElms()) { + if (i->GetType().ToString() != "list") + continue; + + stream << SmartIndent(GenTemplateString(CB_STRUCT_REMOVER, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + if (i->GetType().GetMetaType()->IsUserDefinedType() || + i->GetType().GetMetaType()->ToString() == "list" || + i->GetType().GetMetaType()->ToString() == "array") + return GetParcelParamTypeString(*i->GetType().GetMetaType()); + + return ConvertTypeToString(ParameterType::Direction::IN, + *i->GetType().GetMetaType()); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + std::string str; + str += NLine(1); + str += "h->" + i->GetID() + " = g_list_remove(h-> " + + i->GetID() + ", "; + if (i->GetType().GetMetaType()->IsUserDefinedType() || + i->GetType().GetMetaType()->ToString() == "list" || + i->GetType().GetMetaType()->ToString() == "array" || + i->GetType().GetMetaType()->ToString() == "string" || + i->GetType().GetMetaType()->ToString() == "bundle") + str += i->GetID() + ");"; + else + str += "GUINT_TO_POINTER(" + i->GetID() + "));"; + str += NLine(1); + return str; + })); + } +} + +void CBodyGeneratorBase::GenStructureLengthGetter(std::ofstream& stream, + const Structure& st) { + for (auto& i : st.GetElements().GetElms()) { + if (i->GetType().ToString() != "list") + continue; + + stream << SmartIndent(GenTemplateString(CB_STRUCT_LENGTH_GETTER, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + return i->GetID(); + })); + } +} + std::string CBodyGeneratorBase::GetParcelTypeString(const BaseType& type, bool meta_type) { if (type.IsUserDefinedType()) @@ -382,7 +453,7 @@ std::string CBodyGeneratorBase::GetParcelWriteString(const std::string& id, return "h->" + id; }, [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); + return GetParcelParamTypeString(*type.GetMetaType(), true); }, [&]()->std::string { return GenTemplateString(parcel, @@ -398,7 +469,7 @@ std::string CBodyGeneratorBase::GetParcelWriteString(const std::string& id, return "value"; if (type.GetMetaType()->ToString() == "string") return ReplaceAll(ternary_operation, "##", "value"); - return "*value"; + return "GPOINTER_TO_UINT(value)"; }); }); } else if (type.ToString() == "array") { @@ -454,7 +525,16 @@ std::string CBodyGeneratorBase::GetParcelReadString(const std::string& id, }); }, [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); + return GetParcelParamTypeString(*type.GetMetaType(), false); + }, + [&]()->std::string { + if (type.GetMetaType()->IsUserDefinedType() || + type.GetMetaType()->ToString() == "list" || + type.GetMetaType()->ToString() == "array" || + type.GetMetaType()->ToString() == "string" || + type.GetMetaType()->ToString() == "bundle") + return "NULL;"; + return "0;"; }, [&]()->std::string { std::string s; @@ -491,24 +571,12 @@ std::string CBodyGeneratorBase::GetParcelReadString(const std::string& id, return "&value"; }); } else { - s += "value = calloc(1, sizeof(*value));" + NLine(1); - s += GenTemplateString(CB_IF_STATEMENT_WITH_BRACES, - [&]()->std::string { - return "!value"; - }, - [&]()->std::string { - std::string ss; - ss += "_E(\"Out of memory\");" + NLine(1); - ss += "return;"; - return ss; - }); - s += NLine(1); s += GenTemplateString(parcel, [&]()->std::string { return GetParcelReadFunctionString(*type.GetMetaType()); }, [&]()->std::string { - return "value"; + return "&value"; }); } @@ -519,6 +587,15 @@ std::string CBodyGeneratorBase::GetParcelReadString(const std::string& id, }, [&]()->std::string { return "h->" + id; + }, + [&]()->std::string { + if (type.GetMetaType()->IsUserDefinedType() || + type.GetMetaType()->ToString() == "list" || + type.GetMetaType()->ToString() == "array" || + type.GetMetaType()->ToString() == "string" || + type.GetMetaType()->ToString() == "bundle") + return "value"; + return "GUINT_TO_POINTER(value)"; }); } else if (type.ToString() == "array") { str += GenTemplateString(ReplaceAll(CB_READ_ARRAY_BLOCK, "##", id), @@ -601,26 +678,8 @@ std::string CBodyGeneratorBase::GetFinalizeString(const std::string& id, return str; if (type.ToString() == "list") { - str += GenTemplateString(CB_FINALIZE_LIST_BLOCK, - [&]()->std::string { - return "h->" + id; - }, - [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); - }, - [&]()->std::string { - return GenTemplateString(CB_IF_STATEMENT, - [&]()->std::string { - return "value"; - }, - [&]()->std::string { - return GetDestructorString(*type.GetMetaType(), - "value", true) + NLine(1); - }); - }, - [&]()->std::string { - return "h->" + id; - }); + str += "g_list_free(h->" + id + ");"; + str += NLine(1); } else if (type.ToString() == "array") { if (!type.GetMetaType()->IsUserDefinedType() && type.GetMetaType()->ToString() != "list" && @@ -709,36 +768,18 @@ std::string CBodyGeneratorBase::GetSetterString(const std::string& id, return "h->" + id; }, [&]()->std::string { - if (type.GetMetaType()->ToString() == "string") - return "g_list_append(h->" + id + ", strdup(" + id + "))"; return "g_list_append(h->" + id + ", " + id + ")"; }); } else { str += GenTemplateString(CB_SETTER_LIST_BLOCK, [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); - }, - [&]()->std::string { - std::string s; - s += GenTemplateString(CB_IF_STATEMENT_WITH_BRACES, - [&]()->std::string { - return "!value"; - }, - [&]()->std::string { - std::string s; - s += "_E(\"Out of memory\");" + NLine(1); - s += "return -1;"; - return s; - }); - s += NLine(1); - s += GetSetterString("*value", id); - return s; + return id; }, [&]()->std::string { return "h->" + id; }, [&]()->std::string { - return "h->" + id; + return "h->" + id; }); } } else if (type.ToString() == "array") { @@ -829,7 +870,7 @@ std::string CBodyGeneratorBase::GetIteratorString(const std::string& id, type.GetMetaType()->ToString() == "bundle") return "value"; - return "*value"; + return "GPOINTER_TO_UINT(value)"; }); return str; @@ -871,57 +912,6 @@ std::string CBodyGeneratorBase::GetClonerString(const std::string& id, return "h->" + id; }, [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); - }, - [&]()->std::string { - return GetParcelParamTypeString(*type.GetMetaType()); - }, - [&]()->std::string { - return GetStructIdWithNamespace(st); - }, - [&]()->std::string { - std::string s; - if (type.GetMetaType()->IsUserDefinedType() || - type.GetMetaType()->ToString() == "list" || - type.GetMetaType()->ToString() == "array" || - type.GetMetaType()->ToString() == "string" || - type.GetMetaType()->ToString() == "bundle") { - s += GetSetterString(*type.GetMetaType(), - "new_value", "value"); - s += GenTemplateString(CB_IF_STATEMENT_WITH_BRACES, - [&]()->std::string { - return "!new_value"; - }, - [&]()->std::string { - std::string ss; - ss += "_E(\"Failed to duplicate value\");" + NLine(1); - ss += "rpc_port_" + GetStructIdWithNamespace(st) - + "_destroy(handle);" + NLine(1); - ss += "return -1;"; - return ss; - }); - } else { - s += "new_value = calloc(1, sizeof(*new_value));" + NLine(1); - s += GenTemplateString(CB_IF_STATEMENT_WITH_BRACES, - [&]()->std::string { - return "!new_value"; - }, - [&]()->std::string { - std::string tmp; - tmp += "_E(\"Out of memory\");" + NLine(1); - tmp += "rpc_port_" + GetStructIdWithNamespace(st) - + "_destroy(handle);" + NLine(1); - tmp += "return -1;"; - return tmp; - }); - s += NLine(1); - s += GetSetterString(*type.GetMetaType(), - "*new_value", "*value"); - } - s += NLine(1); - return s; - }, - [&]()->std::string { return "handle->" + id; }, [&]()->std::string { diff --git a/idlc/c_gen/c_body_gen_base.h b/idlc/c_gen/c_body_gen_base.h index 1126d5d..35ff0fa 100644 --- a/idlc/c_gen/c_body_gen_base.h +++ b/idlc/c_gen/c_body_gen_base.h @@ -64,6 +64,8 @@ class CBodyGeneratorBase : public CGeneratorBase { void GenStructureGetter(std::ofstream& stream, const Structure& st); void GenStructureIterator(std::ofstream& stream, const Structure& st); void GenStructureCloner(std::ofstream& stream, const Structure& st); + void GenStructureRemover(std::ofstream& stream, const Structure& st); + void GenStructureLengthGetter(std::ofstream& stream, const Structure& st); private: std::string GetParcelTypeString(const BaseType& type, bool meta_type); diff --git a/idlc/c_gen/c_body_gen_base_cb.h b/idlc/c_gen/c_body_gen_base_cb.h index 647e090..1790b18 100644 --- a/idlc/c_gen/c_body_gen_base_cb.h +++ b/idlc/c_gen/c_body_gen_base_cb.h @@ -167,15 +167,42 @@ do { $$value = iter->data; iter = g_list_next(iter); - if (!value) { - _W("Warning: value is NULL"); - continue; - } + if (!value) + _W("Warning: value is nullptr"); + $$ } } while (0); )__c_cb"; +const char CB_STRUCT_REMOVER[] = +R"__c_cb( +int rpc_port_$$_remove_$$(rpc_port_$$_h h, $$$$) +{ + if (!h) { + _E("Invalid parameter"); + return -1; + } +$$ + return 0; +} +)__c_cb"; + +const char CB_STRUCT_LENGTH_GETTER[] = +R"__c_cb( +int rpc_port_$$_get_$$_length(rpc_port_$$_h h, unsigned int *length) +{ + if (!h || !length) { + _E("Invalid parameter"); + return -1; + } + + *length = g_list_length(h->$$); + + return 0; +} +)__c_cb"; + const char CB_WRITE_ARRAY_BLOCK[] = R"__c_cb( do { @@ -191,10 +218,10 @@ R"__c_cb(do { $$ for (int i = 0; i < len; i++) { - $$value = NULL; + $$value = $$; $$ - $$ = g_list_append($$, value); + $$ = g_list_append($$, $$); } } while (0); )__c_cb"; @@ -246,10 +273,8 @@ do { const char CB_SETTER_LIST_BLOCK[] = R"__c_cb( do { - $$value; + gpointer value = GUINT_TO_POINTER($$); - value = calloc(1, sizeof(*value)); - $$ $$ = g_list_append($$, value); } while (0); )__c_cb"; @@ -301,10 +326,8 @@ do { $$value = iter->data; iter = g_list_next(iter); - if (!value) { - _W("Warning: value is NULL"); - continue; - } + if (!value) + _W("Warning: value is nullptr"); bool ret = callback($$, user_data); if (!ret) @@ -320,17 +343,7 @@ do { iter = $$; while (iter) { - $$new_value; - $$value = iter->data; - - if (!value) { - _E("Error: value is NULL"); - rpc_port_$$_destroy(handle); - return -1; - } - - $$ - $$ = g_list_append($$, new_value); + $$ = g_list_append($$, iter->data); iter = g_list_next(iter); } } while (0); diff --git a/idlc/c_gen/c_header_gen_base.cc b/idlc/c_gen/c_header_gen_base.cc index 8848fe4..347c8e4 100644 --- a/idlc/c_gen/c_header_gen_base.cc +++ b/idlc/c_gen/c_header_gen_base.cc @@ -78,6 +78,8 @@ void CHeaderGeneratorBase::GenStructure(std::ofstream& stream, GenStructureSetter(stream, st); GenStructureGetter(stream, st); GenStructureIterator(stream, st); + GenStructureRemover(stream, st); + GenStructureLengthGetter(stream, st); } void CHeaderGeneratorBase::GenStructureDeclaration(std::ofstream& stream, @@ -250,4 +252,54 @@ void CHeaderGeneratorBase::GenStructureCloner(std::ofstream& stream, }); } +void CHeaderGeneratorBase::GenStructureRemover(std::ofstream& stream, + const Structure& st) { + for (auto& i : st.GetElements().GetElms()) { + if (i->GetType().ToString() != "list") + continue; + + GenTemplate(CB_STRUCT_REMOVER, stream, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + if (i->GetType().GetMetaType()->IsUserDefinedType() || + i->GetType().GetMetaType()->ToString() == "list" || + i->GetType().GetMetaType()->ToString() == "array") + return GetParcelParamTypeString(*i->GetType().GetMetaType()); + + return ConvertTypeToString(ParameterType::Direction::IN, + *i->GetType().GetMetaType()); + }, + [&]()->std::string { + return i->GetID(); + }); + } +} + +void CHeaderGeneratorBase::GenStructureLengthGetter(std::ofstream& stream, + const Structure& st) { + for (auto& i : st.GetElements().GetElms()) { + if (i->GetType().ToString() != "list") + continue; + + GenTemplate(CB_STRUCT_LENGTH_GETTER, stream, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + return GetStructIdWithNamespace(st); + }); + } +} + } // namespace tidl diff --git a/idlc/c_gen/c_header_gen_base.h b/idlc/c_gen/c_header_gen_base.h index f9d0505..f506c58 100644 --- a/idlc/c_gen/c_header_gen_base.h +++ b/idlc/c_gen/c_header_gen_base.h @@ -46,6 +46,8 @@ class CHeaderGeneratorBase : public CGeneratorBase { void GenStructureGetter(std::ofstream& stream, const Structure& st); void GenStructureIterator(std::ofstream& stream, const Structure& st); void GenStructureCloner(std::ofstream& stream, const Structure& st); + void GenStructureRemover(std::ofstream& stream, const Structure& st); + void GenStructureLengthGetter(std::ofstream& stream, const Structure& st); }; } // namespace tidl diff --git a/idlc/c_gen/c_header_gen_base_cb.h b/idlc/c_gen/c_header_gen_base_cb.h index d8e8e37..c05aafb 100644 --- a/idlc/c_gen/c_header_gen_base_cb.h +++ b/idlc/c_gen/c_header_gen_base_cb.h @@ -67,4 +67,14 @@ R"__c_cb( int rpc_port_$$_clone(rpc_port_$$_h h, rpc_port_$$_h *clone); )__c_cb"; +const char CB_STRUCT_REMOVER[] = +R"__c_cb( +int rpc_port_$$_remove_$$(rpc_port_$$_h h, $$$$); +)__c_cb"; + +const char CB_STRUCT_LENGTH_GETTER[] = +R"__c_cb( +int rpc_port_$$_get_$$_length(rpc_port_$$_h h, unsigned int *length); +)__c_cb"; + #endif // IDLC_C_GEN_C_HEADER_GEN_BASE_CB_H_ -- 2.7.4