From 3c60f40e4de2da9a64c2c178a69b48800ef1862a Mon Sep 17 00:00:00 2001 From: Junghoon Park Date: Wed, 29 Aug 2018 14:36:43 +0900 Subject: [PATCH] Adds new APIs for handling list - rpc_port__remove_() - rpc_port__get__length() Change-Id: I2dbc8ce2942109d5f879fade9d534d83f14d3662 Signed-off-by: Junghoon Park --- idlc/c_gen/c_body_gen_base.cc | 104 +++++++++++++++++++++++++++++++++----- idlc/c_gen/c_body_gen_base.h | 5 +- idlc/c_gen/c_body_gen_base_cb.h | 39 ++++++++++++++ idlc/c_gen/c_gen_base.cc | 10 ++-- idlc/c_gen/c_gen_base.h | 2 +- idlc/c_gen/c_header_gen_base.cc | 40 +++++++++++++++ idlc/c_gen/c_header_gen_base.h | 2 + idlc/c_gen/c_header_gen_base_cb.h | 10 ++++ 8 files changed, 193 insertions(+), 19 deletions(-) diff --git a/idlc/c_gen/c_body_gen_base.cc b/idlc/c_gen/c_body_gen_base.cc index bc8eaea..0eb9956 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, @@ -142,7 +144,7 @@ void CBodyGeneratorBase::GenStructureDestructor(std::ofstream& stream, [&]()->std::string { std::string str; for (auto& i : st.GetElements().GetElms()) { - str += GetFinalizeString(i->GetID(), i->GetType()); + str += GetFinalizeString(i->GetID(), i->GetType(), "h->"); } return str; })); @@ -315,6 +317,82 @@ void CBodyGeneratorBase::GenStructureIterator(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 { + return i->GetID(); + }, + [&]()->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(), false); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + return i->GetID(); + }, + [&]()->std::string { + std::string str; + + if (i->GetType().GetMetaType()->IsUserDefinedType() || + i->GetType().GetMetaType()->ToString() == "list" || + i->GetType().GetMetaType()->ToString() == "array") { + str = GetHandlePrefix() + + GetFullNameFromType(*i->GetType().GetMetaType()) + + "_destroy(value);"; + } else { + str = GetFinalizeString("value", *i->GetType().GetMetaType(), ""); + } + + if (str == "") + return "free(value);\n"; + 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(); + })); + } +} + void CBodyGeneratorBase::GenStructureCloner(std::ofstream& stream, const Structure& st) { stream << SmartIndent(GenTemplateString( @@ -590,7 +668,7 @@ std::string CBodyGeneratorBase::GetParcelReadString(const std::string& id, } std::string CBodyGeneratorBase::GetFinalizeString(const std::string& id, - const BaseType& type) { + const BaseType& type, const std::string& handle) { std::string str; if (!type.IsUserDefinedType() && @@ -603,7 +681,7 @@ std::string CBodyGeneratorBase::GetFinalizeString(const std::string& id, if (type.ToString() == "list") { str += GenTemplateString(CB_FINALIZE_LIST_BLOCK, [&]()->std::string { - return "h->" + id; + return handle + id; }, [&]()->std::string { return GetParcelParamTypeString(*type.GetMetaType()); @@ -619,7 +697,7 @@ std::string CBodyGeneratorBase::GetFinalizeString(const std::string& id, }); }, [&]()->std::string { - return "h->" + id; + return handle + id; }); } else if (type.ToString() == "array") { if (!type.GetMetaType()->IsUserDefinedType() && @@ -629,36 +707,36 @@ std::string CBodyGeneratorBase::GetFinalizeString(const std::string& id, type.GetMetaType()->ToString() != "bundle") { return GenTemplateString(CB_IF_STATEMENT, [&]()->std::string { - return "h->" + id; + return handle + id; }, [&]()->std::string { - return "free(h->" + id + ");"; + return "free(" + handle + id + ");"; }); } str += GenTemplateString(CB_FINALIZE_ARRAY_BLOCK, [&]()->std::string { - return "h->" + id + "_size"; + return handle + id + "_size"; }, [&]()->std::string { return GenTemplateString(CB_IF_STATEMENT, [&]()->std::string { - return "h->" + id + "[i]"; + return handle + id + "[i]"; }, [&]()->std::string { return GetDestructorString(*type.GetMetaType(), - "h->" + id + "[i]", true) + NLine(1); + handle + id + "[i]", true) + NLine(1); }); }, [&]()->std::string { - return "h->" + id; + return handle + id; }); } else { str += GenTemplateString(CB_IF_STATEMENT, [&]()->std::string { - return "h->" + id; + return handle + id; }, [&]()->std::string { - return GetDestructorString(type, "h->" + id) + NLine(1); + return GetDestructorString(type, handle + id) + NLine(1); }); } @@ -742,7 +820,7 @@ std::string CBodyGeneratorBase::GetSetterString(const std::string& id, }); } } else if (type.ToString() == "array") { - str += GetFinalizeString(id, type) + NLine(1); + str += GetFinalizeString(id, type, "h->") + NLine(1); str += GetSetterString("h->" + id, "NULL"); str += NLine(1); str += GenTemplateString(ReplaceAll(CB_SETTER_ARRAY_BLOCK, "##", id), diff --git a/idlc/c_gen/c_body_gen_base.h b/idlc/c_gen/c_body_gen_base.h index 1126d5d..32af40a 100644 --- a/idlc/c_gen/c_body_gen_base.h +++ b/idlc/c_gen/c_body_gen_base.h @@ -64,12 +64,15 @@ 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); std::string GetParcelWriteString(const std::string& id, const BaseType& type); std::string GetParcelReadString(const std::string& id, const BaseType& type); - std::string GetFinalizeString(const std::string& id, const BaseType& type); + std::string GetFinalizeString(const std::string& id, const BaseType& type, + const std::string& handle); std::string GetSetterString(const std::string& id, const BaseType& type); std::string GetGetterString(const std::string& id, const BaseType& type); std::string GetIteratorString(const std::string& id, const BaseType& 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..db0aa52 100644 --- a/idlc/c_gen/c_body_gen_base_cb.h +++ b/idlc/c_gen/c_body_gen_base_cb.h @@ -134,6 +134,45 @@ $$ } )__c_cb"; +const char CB_STRUCT_REMOVER[] = +R"__c_cb( +int rpc_port_$$_remove_$$(rpc_port_$$_h h, unsigned int nth) +{ + GList *iter; + + if (!h) { + _E("Invalid parameter"); + return -1; + } + + iter = g_list_nth(h->$$, nth); + if (iter == NULL) + return -1; + + $$value = iter->data; + h->$$ = g_list_remove_link(h->$$, iter); +$$ + g_list_free(iter); + + 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_STRUCT_CLONER[] = R"__c_cb( int rpc_port_##_clone(rpc_port_##_h h, rpc_port_##_h *clone) diff --git a/idlc/c_gen/c_gen_base.cc b/idlc/c_gen/c_gen_base.cc index 69b6229..aff005a 100644 --- a/idlc/c_gen/c_gen_base.cc +++ b/idlc/c_gen/c_gen_base.cc @@ -60,7 +60,7 @@ std::string CGeneratorBase::NLine(int cnt) { } std::string CGeneratorBase::ConvertTypeToString( - ParameterType::Direction direction, const BaseType& type) { + ParameterType::Direction direction, const BaseType& type, bool bconst) { if (type.IsUserDefinedType()) { if (direction == ParameterType::Direction::IN) return GetHandlePrefix() + type.ToString() + "_h "; @@ -77,13 +77,15 @@ std::string CGeneratorBase::ConvertTypeToString( } if (type.ToString() == "string") { - if (direction == ParameterType::Direction::IN) + if (direction == ParameterType::Direction::IN) { + if (!bconst) + return "char *"; return "const char *"; - else + } else return "char **"; } - if (direction == ParameterType::Direction::IN) + if (direction == ParameterType::Direction::IN && bconst) return type_map_[type.ToString()]; return type_map_[type.ToString()] + "*"; diff --git a/idlc/c_gen/c_gen_base.h b/idlc/c_gen/c_gen_base.h index 2db9a6a..33d6c07 100644 --- a/idlc/c_gen/c_gen_base.h +++ b/idlc/c_gen/c_gen_base.h @@ -37,7 +37,7 @@ class CGeneratorBase : public Generator { std::string Tab(int cnt); std::string NLine(int cnt); std::string ConvertTypeToString(ParameterType::Direction direction, - const BaseType& type); + const BaseType& type, bool bconst = true); std::string GetFullNameFromType(const BaseType& type); std::string GetParcelParamTypeString(const BaseType& type, bool is_pointer = true); diff --git a/idlc/c_gen/c_header_gen_base.cc b/idlc/c_gen/c_header_gen_base.cc index 8848fe4..c726d2f 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, @@ -236,6 +238,44 @@ void CHeaderGeneratorBase::GenStructureIterator(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); + }); + } +} + +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); + }); + } +} + void CHeaderGeneratorBase::GenStructureCloner(std::ofstream& stream, const Structure& st) { GenTemplate(CB_STRUCT_CLONER, stream, diff --git a/idlc/c_gen/c_header_gen_base.h b/idlc/c_gen/c_header_gen_base.h index f9d0505..494fac4 100644 --- a/idlc/c_gen/c_header_gen_base.h +++ b/idlc/c_gen/c_header_gen_base.h @@ -45,6 +45,8 @@ class CHeaderGeneratorBase : public CGeneratorBase { void GenStructureSetter(std::ofstream& stream, const Structure& st); void GenStructureGetter(std::ofstream& stream, const Structure& st); void GenStructureIterator(std::ofstream& stream, const Structure& st); + void GenStructureRemover(std::ofstream& stream, const Structure& st); + void GenStructureLengthGetter(std::ofstream& stream, const Structure& st); void GenStructureCloner(std::ofstream& stream, const Structure& st); }; diff --git a/idlc/c_gen/c_header_gen_base_cb.h b/idlc/c_gen/c_header_gen_base_cb.h index d8e8e37..e0229f5 100644 --- a/idlc/c_gen/c_header_gen_base_cb.h +++ b/idlc/c_gen/c_header_gen_base_cb.h @@ -62,6 +62,16 @@ int rpc_port_$$_foreach_$$(rpc_port_$$_h h, bool (*callback)($$$$, void *user_data), void *user_data); )__c_cb"; +const char CB_STRUCT_REMOVER[] = +R"__c_cb( +int rpc_port_$$_remove_$$(rpc_port_$$_h h, unsigned int nth); +)__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"; + const char CB_STRUCT_CLONER[] = R"__c_cb( int rpc_port_$$_clone(rpc_port_$$_h h, rpc_port_$$_h *clone); -- 2.7.4