From bd68ccd9002f033d8c5b32585112b952710a53b4 Mon Sep 17 00:00:00 2001 From: jusung Date: Wed, 4 Jan 2023 10:47:58 +0900 Subject: [PATCH] Implement C group generator Change-Id: I53ce8134cb61a54ca714e6cbc3ecc53fb50a05cf Signed-off-by: jusung --- idlc/gen/generator.h | 12 +- idlc/gen/version2/c_body_generator_base.cc | 119 ++-- idlc/gen/version2/c_body_generator_base.hh | 1 + idlc/gen/version2/c_group_body_generator.cc | 506 ++++++++++++++++ idlc/gen/version2/c_group_body_generator.hh | 82 +++ idlc/gen/version2/c_group_body_generator_cb.hh | 670 +++++++++++++++++++++ idlc/gen/version2/c_group_header_generator.cc | 188 ++++++ idlc/gen/version2/c_group_header_generator.hh | 57 ++ idlc/gen/version2/c_group_header_generator_cb.hh | 158 +++++ idlc/gen/version2/c_proxy_body_generator_cb.hh | 2 +- idlc/gen_cion/c_cion_body_gen_base.cc | 2 +- idlc/gen_cion/c_cion_gen_base.cc | 12 +- idlc/gen_cion/cpp_cion_gen_base.cc | 10 +- idlc/gen_cion/cpp_cion_group_body_gen.cc | 3 +- idlc/gen_cion/cpp_cion_proxy_body_gen.cc | 3 +- idlc/gen_cion/cpp_cion_stub_body_gen.cc | 3 +- idlc/gen_mqtt_plugin/mqtt_plugin_cs_base_gen.cc | 11 +- idlc/gen_mqtt_plugin/mqtt_plugin_cs_interop_gen.cc | 11 +- .../mqtt_plugin_internal_body_gen.cc | 16 +- .../mqtt_plugin_internal_header_gen.cc | 8 +- idlc/main.cc | 162 +++-- 21 files changed, 1881 insertions(+), 155 deletions(-) mode change 100755 => 100644 idlc/gen/generator.h create mode 100644 idlc/gen/version2/c_group_body_generator.cc create mode 100644 idlc/gen/version2/c_group_body_generator.hh create mode 100644 idlc/gen/version2/c_group_body_generator_cb.hh create mode 100644 idlc/gen/version2/c_group_header_generator.cc create mode 100644 idlc/gen/version2/c_group_header_generator.hh create mode 100644 idlc/gen/version2/c_group_header_generator_cb.hh diff --git a/idlc/gen/generator.h b/idlc/gen/generator.h old mode 100755 new mode 100644 index 1a771fb..e6ab5ec --- a/idlc/gen/generator.h +++ b/idlc/gen/generator.h @@ -32,6 +32,12 @@ namespace tidl { class Generator { public: + enum ChannelType { + TYPE_UNKNOWN = 0, + TYPE_PROXY, + TYPE_STUB, + TYPE_GROUP + }; explicit Generator(std::shared_ptr doc); virtual ~Generator() = default; @@ -65,11 +71,11 @@ class Generator { return isGenAPI_; } - void SetType(int type) { + void SetChannelType(ChannelType type) { type_ = type; } - int GetType() const { + int GetChannelType() const { return type_; } @@ -138,7 +144,7 @@ class Generator { bool hasNamespace_ = true; bool isProxy_ = true; bool isGenAPI_ = false; - int type_ = 0; + ChannelType type_ = TYPE_UNKNOWN; }; } // namespace tidl diff --git a/idlc/gen/version2/c_body_generator_base.cc b/idlc/gen/version2/c_body_generator_base.cc index 2577c02..62404ff 100644 --- a/idlc/gen/version2/c_body_generator_base.cc +++ b/idlc/gen/version2/c_body_generator_base.cc @@ -26,56 +26,7 @@ namespace tidl { namespace version2 { CBodyGeneratorBase::CBodyGeneratorBase(std::shared_ptr doc) - : tidl::CBodyGeneratorBase(std::move(doc)) { - if (IsProxy()) { - AddParameterType( - std::make_shared( - new BaseType("delegate", "delegate", true), - ParameterType::Direction::OUT)); - } - - AddParameterType( - std::make_shared( - new BaseType("int", "int"), ParameterType::Direction::IN)); - AddParameterType( - std::make_shared( - new BaseType("int", "int"), ParameterType::Direction::OUT)); - AddParameterType( - std::make_shared( - new BaseType("bool", "bool"), ParameterType::Direction::IN)); - AddParameterType( - std::make_shared( - new BaseType("bool", "bool"), ParameterType::Direction::OUT)); - - for (auto& block : GetDocument().GetBlocks()) { - if (block->GetType() == Block::TYPE_INTERFACE) { - auto& iface = static_cast(*block); - for (const auto& decl : iface.GetDeclarations()) { - for (const auto& param : decl->GetParameters()) { - auto& type = param->GetParameterType().GetBaseType(); - AddParameterType(iface, type, - param->GetParameterType().GetDirection()); - } - - if (decl->GetMethodType() == Declaration::MethodType::SYNC) { - auto& type = decl->GetType(); - AddParameterType(iface, type, ParameterType::Direction::OUT); - } - } - } else { - auto& st = static_cast(*block); - for (auto& elm : st.GetElements()) { - auto& type = elm->GetType(); - AddParameterType( - std::make_shared( - new BaseType(type), ParameterType::Direction::IN)); - AddParameterType( - std::make_shared( - new BaseType(type), ParameterType::Direction::OUT)); - } - } - } -} + : tidl::CBodyGeneratorBase(std::move(doc)) { } void CBodyGeneratorBase::GenStructureDefinition(std::ofstream& stream) { for (auto& b : GetDocument().GetBlocks()) { @@ -573,9 +524,65 @@ void CBodyGeneratorBase::GenUnitMapDefinition(std::ofstream& stream) { stream << SmartIndent(CB_UNIT_MAP_DEFS); } +void CBodyGeneratorBase::GenParameterMap() { + if (GetChannelType() == ChannelType::TYPE_PROXY) { + AddParameterType( + std::make_shared( + new BaseType("delegate", "delegate", true), + ParameterType::Direction::OUT)); + } + + AddParameterType( + std::make_shared( + new BaseType("int", "int"), ParameterType::Direction::IN)); + AddParameterType( + std::make_shared( + new BaseType("int", "int"), ParameterType::Direction::OUT)); + + if (GetChannelType() != ChannelType::TYPE_GROUP) { + AddParameterType( + std::make_shared( + new BaseType("bool", "bool"), ParameterType::Direction::IN)); + AddParameterType( + std::make_shared( + new BaseType("bool", "bool"), ParameterType::Direction::OUT)); + } + + for (auto& block : GetDocument().GetBlocks()) { + if (block->GetType() == Block::TYPE_INTERFACE) { + auto& iface = static_cast(*block); + for (const auto& decl : iface.GetDeclarations()) { + for (const auto& param : decl->GetParameters()) { + auto& type = param->GetParameterType().GetBaseType(); + AddParameterType(iface, type, + param->GetParameterType().GetDirection()); + } + + if (decl->GetMethodType() == Declaration::MethodType::SYNC) { + auto& type = decl->GetType(); + AddParameterType(iface, type, ParameterType::Direction::OUT); + } + } + } else { + auto& st = static_cast(*block); + for (auto& elm : st.GetElements()) { + auto& type = elm->GetType(); + AddParameterType( + std::make_shared( + new BaseType(type), ParameterType::Direction::IN)); + AddParameterType( + std::make_shared( + new BaseType(type), ParameterType::Direction::OUT)); + } + } + } +} + void CBodyGeneratorBase::GenUnitMapBase(std::ofstream& stream) { stream << SmartIndent(CB_UNIT_MAP_BASE); + GenParameterMap(); + std::string code; for (const auto& iter : param_types_) { if (!code.empty()) @@ -583,14 +590,18 @@ void CBodyGeneratorBase::GenUnitMapBase(std::ofstream& stream) { auto& param_type = *iter.second; if (param_type.GetDirection() == ParameterType::Direction::IN) { - if (IsProxy()) + if (GetChannelType() == ChannelType::TYPE_GROUP) { code += GenUnitMapWrite(param_type); - else code += GenUnitMapRead(param_type); + } else if (GetChannelType() == ChannelType::TYPE_PROXY) { + code += GenUnitMapWrite(param_type); + } else { + code += GenUnitMapRead(param_type); + } } else { - if (IsProxy()) + if (GetChannelType() == ChannelType::TYPE_PROXY) code += GenUnitMapRead(param_type); - else + else if (GetChannelType() == ChannelType::TYPE_STUB) code += GenUnitMapWrite(param_type); } } diff --git a/idlc/gen/version2/c_body_generator_base.hh b/idlc/gen/version2/c_body_generator_base.hh index 506a2e6..aaf4a71 100644 --- a/idlc/gen/version2/c_body_generator_base.hh +++ b/idlc/gen/version2/c_body_generator_base.hh @@ -77,6 +77,7 @@ class CBodyGeneratorBase : public tidl::CBodyGeneratorBase { std::string GenUnitMapWrite(const ParameterType& param_type); std::string GenUnitMapRead(const ParameterType& param_type); + void GenParameterMap(); private: std::unordered_map> param_types_; diff --git a/idlc/gen/version2/c_group_body_generator.cc b/idlc/gen/version2/c_group_body_generator.cc new file mode 100644 index 0000000..312973c --- /dev/null +++ b/idlc/gen/version2/c_group_body_generator.cc @@ -0,0 +1,506 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include + +#include "idlc/gen/version2/c_group_body_generator.hh" + +namespace { +#include "idlc/gen/version2/c_group_body_generator_cb.hh" + +constexpr const char PREFIX_RPC_PORT_GROUP[] = "rpc_port_group"; +} + +namespace tidl { +namespace version2 { + +CGroupBodyGenerator::CGroupBodyGenerator(std::shared_ptr doc) + : CBodyGeneratorBase(doc) { +} + +void CGroupBodyGenerator::OnInitGen(std::ofstream& stream) { + GenVersion(stream); + GenGNUSourceDefinition(stream); + GenIncludeDefaultHeaders(stream, true); + GenIncludeHeader(stream); + GenLogTag(stream, std::string("RPC_PORT_GROUP")); + GenLogDefinition(stream); + GenVersionDefinition(stream); + GenBaseDefinition(stream); + GenUnitMapDefinition(stream); + GenInterfaceMethodHandlerType(stream); + GenInterfaceEnums(stream); + GenStructureDefinition(stream); + GenInterfaceDefinition(stream); + GenUnitMapBase(stream); + GenHelper(stream); + GenStructure(stream); + GenInterfaces(stream); +} + +void CGroupBodyGenerator::OnFiniGen(std::ofstream& stream) { +} + +std::string CGroupBodyGenerator::GetHandlePrefix() { + std::string prefix = PREFIX_RPC_PORT_GROUP; + if (!HasNamespace()) + return prefix; + + return prefix + "_" + GetFileNamespace(); +} + +void CGroupBodyGenerator::GenIncludeDefaultHeaders(std::ofstream& stream, bool body) { + CGeneratorBase::GenIncludeDefaultHeaders(stream, body); + stream << CB_INTERFACE_EXTRA_HEADER; +} + +void CGroupBodyGenerator::GenHelper(std::ofstream& stream) { + stream << SmartIndent(CB_HELPER); +} + +void CGroupBodyGenerator::GenInterfaceDefinition(std::ofstream& stream) { + for (auto& iter : GetDocument().GetBlocks()) { + if (iter->GetType() != Block::TYPE_INTERFACE) + continue; + + const auto& iface = static_cast(*iter); + GenInterfaceBaseDefinition(stream, iface); + } +} + +void CGroupBodyGenerator::GenInterfaceBaseDefinition(std::ofstream& stream, + const Interface& iface) { + ReplaceAll(CB_INTERFACE_BASE_DEF) + .Change("", GetHandlePrefix()) + .Change("", iface.GetID()) + .Transform([&](std::string str) { return SmartIndent(str); }) + .Out(stream); +} + +void CGroupBodyGenerator::GenInterfaceEnums(std::ofstream& stream) { + for (auto& b : GetDocument().GetBlocks()) { + if (b->GetType() != Block::TYPE_INTERFACE) + continue; + + auto& iface = static_cast(*b); + GenInterfaceEnum(stream, iface); + } +} + +// @see #CB_INTERFACE_METHOD_ENUM +std::string CGroupBodyGenerator::GenMethodEnums(const Interface& iface) { + std::string method_enums; + + for (const auto& d : iface.GetDeclarations()) { + std::string method_enum(ReplaceAll(CB_INTERFACE_METHOD_ENUM, { + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", d->GetID() } + })); + + method_enums += RemoveLine(method_enum); + } + std::transform(method_enums.begin(), method_enums.end(), method_enums.begin(), + ::toupper); + + return method_enums; +} + +void CGroupBodyGenerator::GenInterfaceEnum(std::ofstream& stream, + const Interface& iface) { + ReplaceAll(CB_INTERFACE_METHOD_ENUM_BASE, { + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", GenMethodEnums(iface) } + }) + .Transform([&](std::string str){ + return SmartIndent(str); + }) + .Out(stream); +} + +void CGroupBodyGenerator::GenInterfaces(std::ofstream& stream) { + for (auto& i : GetDocument().GetBlocks()) { + if (i->GetType() != Block::TYPE_INTERFACE) + continue; + + const Interface &iface = static_cast(*i); + GenInterface(stream, iface); + } +} + +void CGroupBodyGenerator::GenInterface(std::ofstream& stream, const Interface& iface) { + for (const auto& d : iface.GetDeclarations()) { + GenInterfaceMethodHandlerBase(stream, iface, *d); + } + + GenInterfaceMethodTable(stream, iface); + GenInterfaceBase(stream, iface); + + for (const auto& d : iface.GetDeclarations()) { + GenInterfaceMethodBase(stream, iface, *d); + } +} + +// @see #CB_INTERFACE_METHOD_TABLE +void CGroupBodyGenerator::GenInterfaceMethodTable(std::ofstream& stream, + const Interface& iface) { + ReplaceAll(CB_INTERFACE_METHOD_TABLE, { + { "", iface.GetID() }, + { "", GenMethodHandlers(iface) } + }) + .Transform([&](std::string str){ + return SmartIndent(str); + }) + .Out(stream); +} + +// @see #CB_INTERFACE_METHOD_HANDLER +std::string CGroupBodyGenerator::GenMethodHandlers(const Interface& iface) { + std::string code; + for (const auto& d : iface.GetDeclarations()) { + if (d->GetMethodType() == Declaration::MethodType::DELEGATE) + continue; + + std::string enum_value = GetHandlePrefix() + "_" + iface.GetID() + + "_METHOD_" + d->GetID(); + std::transform(enum_value.begin(), enum_value.end(), enum_value.begin(), + ::toupper); + + std::string method_handler(ReplaceAll(CB_INTERFACE_METHOD_HANDLER, { + { "", enum_value }, + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", d->GetID() } + })); + + code += RemoveLine(method_handler); + } + + return code; +} + +// @see #CB_INTERFACE_BASE +void CGroupBodyGenerator::GenInterfaceBase(std::ofstream& stream, + const Interface& iface) { + std::string prefix = GetHandlePrefix(); + std::string name = iface.GetID(); + ReplaceAll(CB_INTERFACE_BASE) + .Change("", prefix) + .Change("", name) + .Transform([&](std::string str) { + return SmartIndent(str); + }) + .Out(stream); +} + +// @see #CB_INTERFACE_METHOD_HANDLER_TYPE +void CGroupBodyGenerator::GenInterfaceMethodHandlerType(std::ofstream& stream) { + stream << SmartIndent(CB_INTERFACE_METHOD_HANDLER_TYPE); +} + +// @see #CB_INTERFACE_METHOD_HANDLER_BASE +void CGroupBodyGenerator::GenInterfaceMethodHandlerBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl) { + ReplaceAll(CB_INTERFACE_METHOD_HANDLER_BASE, { + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", decl.GetID() }, + { "", GenMethodHandlerArgsDecl(iface, decl) }, + { "", + GenMethodUnitMapRead(iface, decl) }, + { "", + GenMethodHandlerCallbackInvoke(decl) }, + { "", GenMethodHandlerArgsFree(iface, decl) } + }) + .Transform([&](std::string str){ + return SmartIndent(str); + }) + .Out(stream); +} + +// @see #CB_INTERFACE_METHOD_CALLBACK_INVOKE +std::string CGroupBodyGenerator::GenMethodHandlerCallbackInvoke( + const Declaration& decl) { + std::string code(ReplaceAll(CB_INTERFACE_METHOD_CALLBACK_INVOKE, + "", decl.GetID())); + + if (decl.GetMethodType() == Declaration::MethodType::SYNC) + code = ReplaceAll(code, "", "res_ = "); + else + code = ReplaceAll(code, "", ""); + + std::string args; + bool first = true; + for (const auto& p : decl.GetParameters()) { + if (!first) + args += ", "; + auto& param_type = p->GetParameterType(); + if (param_type.GetDirection() != ParameterType::Direction::IN) + args += "&"; + + args += p->GetID(); + first = false; + } + code = ReplaceAll(code, "", args); + + return RemoveLine(code); +} + +// @see #CB_INTERFACE_METHOD_DELEGATE_PARCEL_READ +// @see #CB_INTERFACE_METHOD_USER_DEFINED_PARCEL_READ +// @see #CB_INTERFACE_METHOD_BUNDLE_PARCEL_READ +// @see #CB_INTERFACE_METHOD_STRING_PARCEL_READ +// @see #CB_INTERFACE_METHOD_BASE_PARCEL_READ +std::string CGroupBodyGenerator::GenMethodHandlerParcelRead(const Interface& iface, + const Declaration& decl) { + std::string code; + for (const auto& p : decl.GetParameters()) { + std::string parcel_read_code; + auto& param_type = p->GetParameterType(); + if (param_type.GetDirection() != ParameterType::Direction::IN) + continue; + + auto& type = param_type.GetBaseType(); + if (type.IsUserDefinedType() || + type.ToString() == "list" || + type.ToString() == "array") { + parcel_read_code = ReplaceAll( + CB_INTERFACE_METHOD_USER_DEFINED_PARCEL_READ, "", + GetHandlePrefix()); + parcel_read_code = ReplaceAll(parcel_read_code, "", + GetFullNameFromType(type, iface)); + parcel_read_code = ReplaceAll(parcel_read_code, "", p->GetID()); + } else if (type.ToString() == "bundle") { + parcel_read_code = ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_PARCEL_READ, + "", p->GetID()); + } else if (type.ToString() == "string") { + parcel_read_code = ReplaceAll(CB_INTERFACE_METHOD_STRING_PARCEL_READ, + "", p->GetID()); + } else { + parcel_read_code = ReplaceAll(CB_INTERFACE_METHOD_BASE_PARCEL_READ, + "", GetParcelType(type)); + parcel_read_code = ReplaceAll(parcel_read_code, "", p->GetID()); + } + + code += parcel_read_code; + } + + return RemoveLine(code); +} + +std::string CGroupBodyGenerator::GenMethodHandlerArgsDecl(const Interface& iface, + const Declaration& decl) { + std::string args_decl; + for (const auto& p : decl.GetParameters()) { + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + args_decl += GetArgTypeString(type, iface) + p->GetID() + " = " + + GetErrorValue(type) + ";" + NLine(1); + } + + if (decl.GetMethodType() == Declaration::MethodType::SYNC) { + args_decl += "rpc_port_parcel_h parcel_;" + NLine(1); + args_decl += GetArgTypeString(decl.GetType(), iface) + "res_ = " + + GetErrorValue(decl.GetType()) + ";" + NLine(1); + } + + return args_decl; +} + +// @see #CB_INTERFACE_METHOD_USER_DEFINED_FREE +// @see #CB_INTERFACE_METHOD_BUNDLE_FREE +// @see #CB_INTERFACE_METHOD_STRING_FREE +std::string CGroupBodyGenerator::GenMethodHandlerArgsFree(const Interface& iface, + const Declaration& decl) { + std::string free_code; + std::string code; + for (const auto& p : decl.GetParameters()) { + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + if (type.IsUserDefinedType() || + type.ToString() == "list" || + type.ToString() == "array") { + free_code = ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_FREE, + "", GetHandlePrefix()); + free_code = ReplaceAll(free_code, "", + GetFullNameFromType(type, iface)); + free_code = ReplaceAll(free_code, "", p->GetID()); + } else if (type.ToString() == "bundle") { + free_code = ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_FREE, + "", p->GetID()); + } else if (type.ToString() == "string") { + free_code = ReplaceAll(CB_INTERFACE_METHOD_STRING_FREE, + "", p->GetID()); + } else { + free_code.clear(); + } + + code += free_code; + } + + return RemoveLine(code); +} + +std::string CGroupBodyGenerator::GenMethodParams(const Interface& iface, + const Declaration& decl) { + std::string params; + for (const auto& p : decl.GetParameters()) { + params += ", "; + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + params += GetParamTypeString(param_type.GetDirection(), type, iface) + + p->GetID(); + } + + return params; +} + +std::string CGroupBodyGenerator::GenMethodUnitMapWrite(const Interface& iface, + const Declaration& decl) { + std::string code; + for (const auto& param : decl.GetParameters()) { + auto& param_type = param->GetParameterType(); + + auto& type = param_type.GetBaseType(); + if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { + code += ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_WRITE) + .Change("", GetFullNameFromType(type, iface)) + .Change("", param->GetID()); + code += GetPrivateSharingString(type, iface, "h->port", param->GetID()); + } else if (type.ToString() == "bundle") { + code += ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_WRITE) + .Change("", param->GetID()); + } else if (type.ToString() == "string") { + code += ReplaceAll(CB_INTERFACE_METHOD_STRING_UNIT_MAP_WRITE) + .Change("", param->GetID()); + } else if (type.ToString() == "file") { + code += ReplaceAll(CB_INTERFACE_METHOD_STRING_UNIT_MAP_WRITE) + .Change("", param->GetID()); + code += GetPrivateSharingString(type, iface, "h->port", param->GetID()); + } else { + code += ReplaceAll(CB_INTERFACE_METHOD_BASE_UNIT_MAP_WRITE) + .Change("", GetFullNameFromType(type, iface)) + .Change("", param->GetID()); + } + } + + return RemoveLine(code); +} + +std::string CGroupBodyGenerator::GenMethodUnitMapReadBase( + const Interface& iface, const BaseType& type, + const std::string& arg_name, const std::string& arg) { + std::string code; + if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { + code = ReplaceAll(CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_READ) + .Change("", GetFullNameFromType(type, iface)) + .Change("", arg_name) + .Change("", arg); + } else if (type.ToString() == "bundle") { + code = ReplaceAll(CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_READ) + .Change("", arg_name) + .Change("", arg); + } else if (type.ToString() == "string" || type.ToString() == "file") { + code = ReplaceAll(CB_INTERFACE_METHOD_STRING_UNIT_MAP_READ) + .Change("", arg_name) + .Change("", arg); + } else { + code = ReplaceAll(CB_INTERFACE_METHOD_BASE_UNIT_MAP_READ) + .Change("", GetFullNameFromType(type, iface)) + .Change("", arg_name) + .Change("", arg); + } + + return code; +} + +std::string CGroupBodyGenerator::GenMethodUnitMapRead(const Interface& iface, + const Declaration& decl) { + std::string code; + std::string parcel_read_code; + for (const auto& p : decl.GetParameters()) { + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + code += GenMethodUnitMapReadBase(iface, type, p->GetID(), p->GetID()); + } + + return RemoveLine(code); +} + + +std::string CGroupBodyGenerator::GenMethodParamsCheck(const Interface& iface, + const Declaration& decl) { + std::string params_check; + for (const auto& p : decl.GetParameters()) { + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + if (type.IsUserDefinedType() || + type.ToString() == "list" || + type.ToString() == "array" || + type.ToString() == "bundle" || + type.ToString() == "string" || + type.ToString() == "file") + params_check += " || " + p->GetID() + " == nullptr"; + } + + return params_check; +} + +// @see #CB_INTERFACE_METHOD_ASYNC_BASE +std::string CGroupBodyGenerator::GenMethodAsyncBase(const Interface& iface, + const Declaration& decl) { + std::string prefix = GetHandlePrefix(); + std::string name = iface.GetID(); + std::string method_name = decl.GetID(); + std::string code(ReplaceAll(CB_INTERFACE_METHOD_ASYNC_BASE) + .Change("", prefix) + .Change("", name) + .Change("", method_name) + .Change("", GenMethodParams(iface, decl)) + .Change("", GenMethodParamsCheck(iface, decl)) + .ChangeToUpper("", prefix) + .ChangeToUpper("", name) + .ChangeToUpper("", method_name) + .Change("", GenMethodUnitMapWrite(iface, decl))); + + return code; +} + +std::string CGroupBodyGenerator::GenMethodArgs(const Interface& iface, + const Declaration& decl) { + std::string args_code; + for (const auto& p : decl.GetParameters()) { + auto& param_type = p->GetParameterType(); + if (param_type.GetDirection() != ParameterType::Direction::OUT) + continue; + + auto& type = param_type.GetBaseType(); + args_code += GetReturnTypeString(type) + "new_" + p->GetID() + ";"; + args_code += NLine(1); + } + + return args_code; +} + +void CGroupBodyGenerator::GenInterfaceMethodBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl) { + std::string code = GenMethodAsyncBase(iface, decl); + stream << SmartIndent(code); +} + +} // namespace version2 +} // namespace tidl diff --git a/idlc/gen/version2/c_group_body_generator.hh b/idlc/gen/version2/c_group_body_generator.hh new file mode 100644 index 0000000..113330f --- /dev/null +++ b/idlc/gen/version2/c_group_body_generator.hh @@ -0,0 +1,82 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_GEN_C_GROUP_BODY_GENERATOR_HH_ +#define IDLC_GEN_C_GROUP_BODY_GENERATOR_HH_ + +#include +#include + +#include "idlc/gen/version2/c_body_generator_base.hh" +#include "idlc/options.h" + +namespace tidl { +namespace version2 { + +class CGroupBodyGenerator : public CBodyGeneratorBase { + public: + explicit CGroupBodyGenerator(std::shared_ptr doc); + virtual ~CGroupBodyGenerator() = default; + + void OnInitGen(std::ofstream& stream) override; + void OnFiniGen(std::ofstream& stream) override; + std::string GetHandlePrefix() override; + void GenIncludeDefaultHeaders(std::ofstream& stream, bool body) override; + + private: + void GenInterfaceDefinition(std::ofstream& stream); + void GenInterfaceBaseDefinition(std::ofstream& stream, const Interface& iface); + void GenInterfaceMethodHandlerType(std::ofstream& stream); + void GenInterfaceMethodHandlerBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl); + std::string GenMethodHandlerArgsDecl(const Interface& iface, + const Declaration& decl); + std::string GenMethodHandlerArgsFree(const Interface& iface, + const Declaration& decl); + void GenInterfaceMethodTable(std::ofstream& stream, + const Interface& iface); + std::string GenMethodHandlerCallbackInvoke(const Declaration& decl); + std::string GenMethodHandlerParcelRead(const Interface& iface, + const Declaration& decl); + std::string GenMethodHandlers(const Interface& iface); + std::string GenMethodEnums(const Interface& iface); + void GenInterfaceEnums(std::ofstream& stream); + void GenInterfaceEnum(std::ofstream& stream, const Interface& iface); + void GenInterfaces(std::ofstream& stream); + void GenInterface(std::ofstream& stream, const Interface& iface); + void GenInterfaceBase(std::ofstream& stream, const Interface& iface); + void GenInterfaceMethodEnumBase(std::ofstream& stream, const Interface& iface); + void GenInterfaceMethodBase(std::ofstream& stream, const Interface& iface, + const Declaration& decl); + + std::string GenMethodAsyncBase(const Interface& iface, const Declaration& decl); + std::string GenMethodParams(const Interface& iface, const Declaration& decl); + std::string GenMethodUnitMapWrite(const Interface& iface, + const Declaration& decl); + std::string GenMethodArgs(const Interface& iface, const Declaration& decl); + std::string GenMethodUnitMapRead(const Interface& iface, + const Declaration& decl); + std::string GenMethodUnitMapReadBase( + const Interface& iface, const BaseType& type, + const std::string& arg_name, const std::string& arg); + std::string GenMethodParamsCheck(const Interface& iface, const Declaration& decl); + void GenHelper(std::ofstream& stream); +}; + +} // namespace version2 +} // namespace tidl + +#endif // IDLC_GEN_C_GROUP_BODY_GENERATOR_HH_ \ No newline at end of file diff --git a/idlc/gen/version2/c_group_body_generator_cb.hh b/idlc/gen/version2/c_group_body_generator_cb.hh new file mode 100644 index 0000000..c9a837b --- /dev/null +++ b/idlc/gen/version2/c_group_body_generator_cb.hh @@ -0,0 +1,670 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_GEN_C_GROUP_BODY_GENERATOR_CB_HH_ +#define IDLC_GEN_C_GROUP_BODY_GENERATOR_CB_HH_ + + +constexpr const char CB_INTERFACE_METHOD_HANDLER_TYPE[] = +R"__c_cb( +typedef int (*rpc_port_group_method_handler)(rpc_port_unit_map_h map, void *data); +)__c_cb"; + +/** + * The enumeration declarations of methods. + * The prefix of the interface. + * The name of the interface. + */ +constexpr const char CB_INTERFACE_METHOD_ENUM_BASE[] = +R"__c_cb( +typedef enum { + +} __method_e; +)__c_cb"; + + +/** + * The uppercase prefix of the interface. + * The uppercase name of the interface. + * The uppercase method name of the interface. + */ +constexpr const char CB_INTERFACE_METHOD_ENUM[] = +R"__c_cb( +__METHOD_, +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + */ +constexpr const char CB_INTERFACE_BASE_DEF[] = +R"__c_cb( +typedef struct __s { + __callback_s callback; + event_handler_h event_handler; + char *sender_appid; + bool is_system; + GRecMutex mutex; + void *user_data; +} __t; +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + * The method name of the interface. + * The declarations for arguments of the method. + * The implementation to read the parameter from the parcel. + * The implementation to invoke the callback function of the method. + * The implementation to write the result to the parcel. + * The implementation to release arguments. + */ +constexpr const char CB_INTERFACE_METHOD_HANDLER_BASE[] = +R"__c_cb( +static int ____method__handler(rpc_port_unit_map_h map_, void *user_data) +{ + __t *h = user_data; + int ret_ = RPC_PORT_ERROR_NONE; + + + if (h == nullptr) { + _E("Invalid parameter"); + ret_ = RPC_PORT_ERROR_INVALID_PARAMETER; + goto out; + } + + + + out: + + return ret_; +} +)__c_cb"; + +constexpr const char CB_HELPER[] = +R"__c_cb( +static rpc_port_parcel_h __get_parcel_from_bundle(bundle *b) +{ + void *raw = nullptr; + size_t size = 0; + rpc_port_parcel_h p; + + int ret = bundle_get_byte(b, "TIDL_RAW", &raw, &size); + if (ret != BUNDLE_ERROR_NONE) + return nullptr; + + ret = rpc_port_parcel_create_from_raw(&p, raw, (unsigned int)size); + if (ret != RPC_PORT_ERROR_NONE) + return nullptr; + + return p; +} + +static bundle *__get_bundle_from_parcel(rpc_port_parcel_h p, bundle* b) +{ + void *raw = nullptr; + unsigned int size = 0; + + int ret = rpc_port_parcel_get_raw(p, &raw, &size); + if (ret != RPC_PORT_ERROR_NONE) + return nullptr; + + ret = bundle_add_byte(b, "TIDL_RAW", raw, size); + if (ret != BUNDLE_ERROR_NONE) + return nullptr; + + return b; +} + +static char *__get_event_name(const char *appid, bool is_system, const char *iface_name) +{ + size_t size; + char *buf; + + if (is_system) { + size = strlen("tizen.system.event.tidl_iface_") + strlen(iface_name) + 1; + buf = calloc(1, size); + if (!buf) + return nullptr; + snprintf(buf, size, "tizen.system.event.tidl_iface_%s", iface_name); + return buf; + } + + size = strlen(appid) + strlen(iface_name) + 19; + buf = calloc(1, size); + if (!buf) + return nullptr; + snprintf(buf, size, "event.%s.tidl_iface_%s", appid, iface_name); + return buf; +} +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + */ +constexpr const char CB_INTERFACE_BASE[] = +R"__c_cb( + +static void ___event_system_cb(const char *event_name, bundle *event_data, void *user_data) +{ + __t *handle = (__t *)(user_data); + rpc_port_parcel_h p = __get_parcel_from_bundle(event_data); + if (p == nullptr) { + _E("Invalid bundle"); + return; + } + + rpc_port_parcel_h header; + int seq_num = -1; + int cmd; + rpc_port_unit_map_h map; + + rpc_port_parcel_get_header(p, &header); + rpc_port_parcel_header_get_seq_num(header, &seq_num); + + _W("[Sequence] %d", seq_num); + + map = rpc_port_unit_map_create(); + if (map == nullptr) { + _E("Failed to create unit map"); + rpc_port_parcel_destroy(p); + return; + } + + rpc_port_parcel_read(p, &map->parcelable, map); + rpc_port_parcel_destroy(p); + + rpc_port_unit_map_read_int(map, "[METHOD]", &cmd); + + if (cmd >= 0 && cmd < ARRAY_SIZE(___method_table)) { + if (___method_table[cmd]) + ___method_table[cmd](map, handle); + } else { + _W("Invalid protocol. cmd(%d)", cmd); + } + rpc_port_unit_map_clear(map); + rpc_port_unit_map_destroy(map); +} + +int __create(const char *sender_appid, __callback_s *callback, void *user_data, bool is_system, __h *h) +{ + __t *handle; + int ret; + char *ev_name; + + if (sender_appid == nullptr || callback == nullptr || h == nullptr) { + _E("Invalid parameter"); + return RPC_PORT_ERROR_INVALID_PARAMETER; + } + + handle = calloc(1, sizeof(__t)); + if (handle == nullptr) { + _E("Out of memory"); + return RPC_PORT_ERROR_OUT_OF_MEMORY; + } + + ev_name = __get_event_name(sender_appid, is_system, ""); + if (ev_name == nullptr) { + free(handle); + return RPC_PORT_ERROR_OUT_OF_MEMORY; + } + + ret = event_add_event_handler(ev_name, ___event_system_cb, handle, &handle->event_handler); + free(ev_name); + + if (ret != EVENT_ERROR_NONE) { + _E("Failed to register events. result(%d)", ret); + free(handle); + return RPC_PORT_ERROR_INVALID_PARAMETER; + } + + handle->sender_appid = strdup(sender_appid); + if (handle->sender_appid == nullptr) { + _E("Out of memory"); + event_remove_event_handler(handle->event_handler); + free(handle); + return RPC_PORT_ERROR_OUT_OF_MEMORY; + } + + g_rec_mutex_init(&handle->mutex); + handle->callback = *callback; + handle->user_data = user_data; + handle->is_system = is_system; + + *h = handle; + + return RPC_PORT_ERROR_NONE; +} + +int __create_for_sender(const char *sender_appid, bool is_system, __h *h) +{ + __t *handle; + + if (h == nullptr || sender_appid == nullptr) { + _E("Invalid parameter"); + return RPC_PORT_ERROR_INVALID_PARAMETER; + } + + handle = calloc(1, sizeof(__t)); + if (handle == nullptr) { + _E("Out of memory"); + return RPC_PORT_ERROR_OUT_OF_MEMORY; + } + + handle->sender_appid = strdup(sender_appid); + if (handle->sender_appid == nullptr) { + _E("Out of memory"); + free(handle); + return RPC_PORT_ERROR_OUT_OF_MEMORY; + } + + g_rec_mutex_init(&handle->mutex); + handle->is_system = is_system; + *h = handle; + + return RPC_PORT_ERROR_NONE; +} + +int __destroy(__h h) +{ + if (h == nullptr) { + _E("Invalid parameter"); + return RPC_PORT_ERROR_INVALID_PARAMETER; + } + + g_rec_mutex_lock(&h->mutex); + g_rec_mutex_unlock(&h->mutex); + g_rec_mutex_clear(&h->mutex); + + if (h->event_handler) + event_remove_event_handler(h->event_handler); + if (h->sender_appid) + free(h->sender_appid); + + free(h); + + return RPC_PORT_ERROR_NONE; +} +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + * The method name of the interface. + * The parameters of the method of the interface + * The parameters check of the method. + * The uppercase prefix of the interface. + * The uppercase name of the interface. + * The uppercase method name of the interface. + * The implementation to write arguments to the parcel. + */ +constexpr const char CB_INTERFACE_METHOD_ASYNC_BASE[] = +R"__c_cb( +void __invoke_(__h h) +{ + rpc_port_parcel_h parcel_; + rpc_port_parcel_header_h header_; + int seq_num_ = -1; + int res_; + bundle* b; + rpc_port_unit_map_h map_; + + if (h == nullptr) { + _E("Invalid parameter"); + set_last_result(RPC_PORT_ERROR_INVALID_PARAMETER); + return; + } + + g_rec_mutex_lock(&h->mutex); + res_ = rpc_port_parcel_create(&parcel_); + if (res_ != RPC_PORT_ERROR_NONE) { + _E("Failed to create parcel handle. error(%d)", res_); + g_rec_mutex_unlock(&h->mutex); + set_last_result(res_); + return; + } + + rpc_port_parcel_get_header(parcel_, &header_); + rpc_port_parcel_header_set_tag(header_, TIDL_VERSION); + rpc_port_parcel_header_get_seq_num(header_, &seq_num_); + _W("[Version] \"%s\", [Sequence] %d", TIDL_VERSION, seq_num_); + + map_ = rpc_port_unit_map_create(); + if (map_ == nullptr) { + _E("Failed to create unit map"); + rpc_port_parcel_destroy(parcel_); + set_last_result(RPC_PORT_ERROR_OUT_OF_MEMORY); + return; + } + + rpc_port_unit_map_write_int(map_, "[METHOD]", __METHOD_); + + + + rpc_port_parcel_write(parcel_, &map_->parcelable, map_); + rpc_port_unit_map_destroy(map_); + + b = bundle_create(); + if (__get_bundle_from_parcel(parcel_, b) == nullptr) { + _E("Failed to make bundle from parcel"); + bundle_free(b); + g_rec_mutex_unlock(&h->mutex); + set_last_result(RPC_PORT_ERROR_OUT_OF_MEMORY); + return; + } + + int ret = event_publish_app_event(__get_event_name(h->sender_appid, h->is_system, ""), b); + if (ret != EVENT_ERROR_NONE) { + _E("Failed to publish event. result(%d)", ret); + bundle_free(b); + g_rec_mutex_unlock(&h->mutex); + set_last_result(RPC_PORT_ERROR_IO_ERROR); + return; + } + + bundle_free(b); + g_rec_mutex_unlock(&h->mutex); + set_last_result(RPC_PORT_ERROR_NONE); +} +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_USER_DEFINED_PARCEL_WRITE[] = +R"__c_cb( +rpc_port_parcel_write(parcel_, &->parcelable, ); +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BUNDLE_PARCEL_WRITE[] = +R"__c_cb( +rpc_port_parcel_write_bundle(parcel_, ); +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_STRING_PARCEL_WRITE[] = +R"__c_cb( +rpc_port_parcel_write_string(parcel_, ); +)__c_cb"; + +/** + * The type of the parcel. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BASE_PARCEL_WRITE[] = +R"__c_cb( +rpc_port_parcel_write_(parcel_, ); +)__c_cb"; + +/** + * The prefix of the structure. + * The name of the structure. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_USER_DEFINED_PARCEL_READ[] = +R"__c_cb( + = nullptr; +__create(&); +if ( == nullptr) { + _E("Failed to create handle"); + ret_ = RPC_PORT_ERROR_OUT_OF_MEMORY; + goto out; +} + +rpc_port_parcel_read(parcel, &->parcelable, ); +if (get_last_result() != RPC_PORT_ERROR_NONE) { + _E("Failed to read data"); + __destroy(); + = nullptr; + ret_ = get_last_result(); + goto out; +} +)__c_cb"; + + +/** + * The enumeration value of the method. + * The prefix of the interface. + * The name of the interface. + * The method name of the interface. + */ +constexpr const char CB_INTERFACE_METHOD_HANDLER[] = +R"__c_cb( +[] = ____method__handler, +)__c_cb"; + +/** + * The argument. + * The prefix of the structure. + * The name of the structure. + */ +constexpr const char CB_INTERFACE_METHOD_USER_DEFINED_FREE[] = +R"__c_cb( +if () + __destroy(); +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BUNDLE_FREE[] = +R"__c_cb( +if () + bundle_free(); +)__c_cb"; + +/** + * The implemention to set the result of the callback function. + * The name of the method of the interface. + * The arguments of the method. + */ +constexpr const char CB_INTERFACE_METHOD_CALLBACK_INVOKE[] = +R"__c_cb( +if (h->callback.) + h->callback.(, h->user_data); +)__c_cb"; + + +/** + * The argument. + * The prefix of the interface. + * The name of the interface + */ +constexpr const char CB_INTERFACE_METHOD_DELEGATE_PARCEL_READ[] = +R"__c_cb( +ret_ = __create(&); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to create handle. error(%d)", ret_); + goto out; +} + +rpc_port_parcel_read(parcel, &->parcelable, ); +ret_ = get_last_result(); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to read data. error(%d)", ret_); + goto out; +} +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_STRING_FREE[] = +R"__c_cb( +if () + free(); +)__c_cb"; + +/** + * The name of the interface. + * The declarations of method handlers. + */ +constexpr const char CB_INTERFACE_METHOD_TABLE[] = +R"__c_cb( +static rpc_port_group_method_handler ___method_table[] = { + +}; +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BUNDLE_PARCEL_READ[] = +R"__c_cb( + = nullptr; +rpc_port_parcel_read_bundle(parcel, &); +if ( == nullptr) { + _E("Failed to read data"); + ret_ = RPC_PORT_ERROR_OUT_OF_MEMORY; + goto out; + +} +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_STRING_PARCEL_READ[] = +R"__c_cb( + = nullptr; +rpc_port_parcel_read_string(parcel, &); +if ( == nullptr) { + _E("Failed to read data"); + ret_ = RPC_PORT_ERROR_OUT_OF_MEMORY; + goto out; +} +)__c_cb"; + +/** + * The type name of the argument. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_WRITE[] = +R"__c_cb( +rpc_port_unit_map_write_(map_, "", ); +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_WRITE[] = +R"__c_cb( +rpc_port_unit_map_write_bundle(map_, "", ); +)__c_cb"; + +/** + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_STRING_UNIT_MAP_WRITE[] = +R"__c_cb( +rpc_port_unit_map_write_string(map_, "", ); +)__c_cb"; + +/** + * The type name of the argument. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BASE_UNIT_MAP_WRITE[] = +R"__c_cb( +rpc_port_unit_map_write_(map_, "", ); +)__c_cb"; + +/** + * The type name of the argument. + * The argument name. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_USER_DEFINED_UNIT_MAP_READ[] = +R"__c_cb( + = nullptr; +ret_ = rpc_port_unit_map_read_(map_, "", &); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to read . error(%d)", ret_); + goto out; +} +)__c_cb"; + +/** + * The argument name. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BUNDLE_UNIT_MAP_READ[] = +R"__c_cb( + = nullptr; +ret_ = rpc_port_unit_map_read_bundle(map_, "", &); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to read bundle. error(%d)", ret_); + goto out; +} +)__c_cb"; + +/** + * The argument name. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_STRING_UNIT_MAP_READ[] = +R"__c_cb( + = nullptr; +ret_ = rpc_port_unit_map_read_string(map_, "", &); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to read string. error(%d)", ret_); + goto out; +} +)__c_cb"; + +/** + * The type name of the argument. + * The argument name. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BASE_UNIT_MAP_READ[] = +R"__c_cb( +ret_ = rpc_port_unit_map_read_(map_, "", &); +if (ret_ != RPC_PORT_ERROR_NONE) { + _E("Failed to read . error(%d)", ret_); + goto out; +} +)__c_cb"; + +/** + * The type of the parcel. + * The argument. + */ +constexpr const char CB_INTERFACE_METHOD_BASE_PARCEL_READ[] = +R"__c_cb( +rpc_port_parcel_read_(parcel, &); +)__c_cb"; + +/** + * The name of the parameter. + */ +constexpr const char CB_INTERFACE_METHOD_PARAM_SET[] = +R"__c_cb( +* = new_; +)__c_cb"; + +constexpr const char CB_INTERFACE_EXTRA_HEADER[] = +R"__c_cb( +#include +)__c_cb"; + +#endif // IDLC_GEN_C_GROUP_BODY_GENERATOR_CB_HH_ diff --git a/idlc/gen/version2/c_group_header_generator.cc b/idlc/gen/version2/c_group_header_generator.cc new file mode 100644 index 0000000..b6d1a28 --- /dev/null +++ b/idlc/gen/version2/c_group_header_generator.cc @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "idlc/gen/version2/c_group_header_generator.hh" + +namespace { +#include "idlc/gen/version2/c_group_header_generator_cb.hh" + +constexpr const char PREFIX_RPC_PORT_GROUP[] = "rpc_port_group"; +} + +namespace tidl { +namespace version2 { + +CGroupHeaderGenerator::CGroupHeaderGenerator(std::shared_ptr doc) + : CHeaderGeneratorBase(doc) {} + +void CGroupHeaderGenerator::OnInitGen(std::ofstream& stream) { + GenVersion(stream); + GenPragmaOnce(stream); + GenIncludeDefaultHeaders(stream, false); + GenExplicitLinkageOpen(stream); + GenStructureHandles(stream); + GenInterfaceHandles(stream); + GenStructures(stream); + GenInterfaceCallbacks(stream); + GenInterfaces(stream); +} + +void CGroupHeaderGenerator::OnFiniGen(std::ofstream& stream) { + GenExplicitLinkageClose(stream); +} + +std::string CGroupHeaderGenerator::GetHandlePrefix() { + std::string prefix = PREFIX_RPC_PORT_GROUP; + if (!HasNamespace()) + return prefix; + + return prefix + "_" + GetFileNamespace(); +} + +void CGroupHeaderGenerator::GenInterfaceHandles(std::ofstream& stream) { + for (auto& b : GetDocument().GetBlocks()) { + if (b->GetType() != Block::TYPE_INTERFACE) + continue; + + auto& iface = static_cast(*b); + GenInterfaceHandle(stream, iface); + } +} + +void CGroupHeaderGenerator::GenInterfaceCallbacks(std::ofstream& stream) { + for (auto& b : GetDocument().GetBlocks()) { + if (b->GetType() != Block::TYPE_INTERFACE) + continue; + + auto& iface = static_cast(*b); + for (const auto& d : iface.GetDeclarations()) { + GenInterfaceMethodCallbackBase(stream, iface, *d); + } + } +} + +// @see #CB_INTERFACE_METHOD_CALLBACK_BASE +void CGroupHeaderGenerator::GenInterfaceMethodCallbackBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl) { + ReplaceAll(CB_INTERFACE_METHOD_CALLBACK_BASE, { + { "", GetReturnTypeString(decl.GetType()) }, + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", decl.GetID() }, + { "", GenMethodParams(iface, decl) } + }) + .Transform([&](std::string code) { + return SmartIndent(code); + }) + .Out(stream); +} + +// @see #CB_INTERFACE_HANDLE +void CGroupHeaderGenerator::GenInterfaceHandle(std::ofstream& stream, + const Interface& iface) { + ReplaceAll(CB_INTERFACE_HANDLE) + .Change("", GetHandlePrefix()) + .Change("", iface.GetID()) + .Transform([&](std::string code) { + return SmartIndent(code); + }) + .Out(stream); +} + +void CGroupHeaderGenerator::GenInterfaces(std::ofstream& stream) { + for (auto& i : GetDocument().GetBlocks()) { + if (i->GetType() != Block::TYPE_INTERFACE) + continue; + + const Interface &iface = static_cast(*i); + GenInterface(stream, iface); + } +} + +void CGroupHeaderGenerator::GenInterface(std::ofstream& stream, + const Interface& iface) { + GenInterfaceBase(stream, iface); + + for (const auto& d : iface.GetDeclarations()) + GenInterfaceMethodBase(stream, iface, *d); +} + +// @see #CB_INTERFACE_BASE +void CGroupHeaderGenerator::GenInterfaceBase(std::ofstream& stream, + const Interface& iface) { + ReplaceAll(CB_INTERFACE_BASE, { + { "", GetHandlePrefix() }, + { "", iface.GetID()}, + { "", GenMethodCallbackDecls(iface) } + }) + .Transform([&](std::string code) { + return SmartIndent(code); + }) + .Out(stream); +} + +// @see #CB_INTERFACE_METHOD_CALLBACK_DECL +std::string CGroupHeaderGenerator::GenMethodCallbackDecls(const Interface& iface) { + std::string method_callback_decls; + for (const auto& d : iface.GetDeclarations()) { + std::string method_callback_decl(ReplaceAll(CB_INTERFACE_METHOD_CALLBACK_DECL, { + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", d->GetID() } + })); + + method_callback_decl = RemoveLine(method_callback_decl); + method_callback_decls += RemoveLine(method_callback_decl, 2); + } + + return method_callback_decls; +} + +std::string CGroupHeaderGenerator::GenMethodParams(const Interface& iface, + const Declaration& decl) { + std::string params; + bool first = true; + for (const auto& p : decl.GetParameters()) { + if (!first) + params += ", "; + auto& param_type = p->GetParameterType(); + auto& type = param_type.GetBaseType(); + params += GetParamTypeString(param_type.GetDirection(), type, iface) + + p->GetID(); + first = false; + } + + return params; +} + +// @see #CB_INTERFACE_METHOD_BASE +void CGroupHeaderGenerator::GenInterfaceMethodBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl) { + ReplaceAll(CB_INTERFACE_METHOD_BASE, { + { "", GetReturnTypeString(decl.GetType()) }, + { "", GetHandlePrefix() }, + { "", iface.GetID() }, + { "", decl.GetID() }, + { "", GenMethodParams(iface, decl) } + }) + .Transform([&](std::string code) { + return SmartIndent(code); + }) + .Out(stream); +} + +} // namespace version2 +} // namespace tidl diff --git a/idlc/gen/version2/c_group_header_generator.hh b/idlc/gen/version2/c_group_header_generator.hh new file mode 100644 index 0000000..b067cee --- /dev/null +++ b/idlc/gen/version2/c_group_header_generator.hh @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_GEN_C_GROUP_HEADER_GENERATOR_HH_ +#define IDLC_GEN_C_GROUP_HEADER_GENERATOR_HH_ + +#include +#include + +#include "idlc/gen/version2/c_header_generator_base.hh" + +namespace tidl { +namespace version2 { + +class CGroupHeaderGenerator : public CHeaderGeneratorBase { + public: + explicit CGroupHeaderGenerator(std::shared_ptr doc); + virtual ~CGroupHeaderGenerator() = default; + + void OnInitGen(std::ofstream& stream) override; + void OnFiniGen(std::ofstream& stream) override; + std::string GetHandlePrefix() override; + + private: + void GenInterfaceHandles(std::ofstream& stream); + void GenInterfaceHandle(std::ofstream& stream, const Interface& iface); + void GenInterfaces(std::ofstream& stream); + void GenInterface(std::ofstream& stream, const Interface& iface); + + void GenInterfaceCallbacks(std::ofstream& stream); + void GenInterfaceMethodCallbackBase(std::ofstream& stream, + const Interface& iface, const Declaration& decl); + std::string GenMethodCallbackDecls(const Interface& iface); + void GenInterfaceBase(std::ofstream& stream, const Interface& iface); + void GenInterfaceMethodBase(std::ofstream& stream, const Interface& iface, + const Declaration& decl); + + std::string GenMethodParams(const Interface& iface, const Declaration& decl); +}; + +} // namespace version2 +} // namespace tidl + +#endif // IDLC_GEN_C_GROUP_HEADER_GENERATOR_H_ \ No newline at end of file diff --git a/idlc/gen/version2/c_group_header_generator_cb.hh b/idlc/gen/version2/c_group_header_generator_cb.hh new file mode 100644 index 0000000..ecbe717 --- /dev/null +++ b/idlc/gen/version2/c_group_header_generator_cb.hh @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef IDLC_C_GROUP_HEADER_GENERATOR_CB_HH_ +#define IDLC_C_GROUP_HEADER_GENERATOR_CB_HH_ + +/** + * The prefix of the interface. + * The name of the interface. + */ +constexpr const char CB_INTERFACE_HANDLE[] = +R"__c_cb( +/** + * @brief The _ handle. + */ +typedef struct __s *__h; +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + */ +constexpr const char CB_INTERFACE_BASE[] = +R"__c_cb( +/** + * @brief The structure type containing the set of callback functions for handling events. + * @details It is one of the input parameters of the __register() function. + * + * @see __create_cb + * @see __terminate_cb + */ +typedef struct { + +} __callback_s; + +/** + * @brief Creates a _ handle. + * @remarks The @a h handle should be released using + * the __destroy() if it's no longer needed. + * + * @param[in] sender_appid ID of app sending events + * @param[in] callback The set of callback functions to handle events + * @param[in] user_data The user data to be passed to the callback function + * @param[in] is_system True on system events + * @param[out] h The _ handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #RPC_PORT_ERROR_OUT_OF_MEMORY Out of memory + * @retval #RPC_PORT_ERROR_IO_ERROR IO error + + * @see __destroy() + * @see #__callback_s + */ +int __create(const char *sender_appid, __callback_s *callback, void *user_data, bool is_system, __h *h); + +/** + * @brief Creates a _ handle for sending events. + * @remarks The @a h handle should be released using + * the __destroy() if it's no longer needed. + * + * @param[in] sender_appid ID of app sending events + * @param[in] is_system True on system events + * @param[out] h The _ handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + * @retval #RPC_PORT_ERROR_OUT_OF_MEMORY Out of memory + * @retval #RPC_PORT_ERROR_IO_ERROR IO error + + * @see __destroy() + */ +int __create_for_sender(const char *sender_appid, bool is_system, __h *h); + +/** + * @brief Destroys the _ handle. + * + * @param[in] h The _ handle + * @return @c 0 on success, + * otherwise a negative error value + * @retval #RPC_PORT_ERROR_NONE Successful + * @retval #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + * @see __create() + */ +int __destroy(__h h); + +)__c_cb"; + +/** + * The return type of the method. + * The prefix of the interface. + * The name of the interface. + * The name of the method of the interface. + * The parameters of the method. + */ +constexpr const char CB_INTERFACE_METHOD_CALLBACK_BASE[] = +R"__c_cb( +/** + * @brief Called when the payload is received. + * + * @param[in] ... + * @param[in] user_data The user data passed from the registration function + * @see #__callback_s; + */ +typedef (*___cb)(, void *user_data); +)__c_cb"; + +/** + * The prefix of the interface. + * The name of the interface. + * The name of the method of the interface. + */ +constexpr const char CB_INTERFACE_METHOD_CALLBACK_DECL[] = +R"__c_cb( +___cb ; /**< This callback function is invoked when the request is delivered. */ +)__c_cb"; + +/** + * The type of the return value. + * The prefix of the interface. + * The of the interface. + * The name of the method. + * The parameters of the method. + */ +constexpr const char CB_INTERFACE_METHOD_BASE[] = +R"__c_cb( +/** + * @brief Calls the () method. + * @details The return value and args are decided by the interface declaration. + * You can get the result using get_last_result(). + * Before returning the function, the function sets the result using set_last_result(). + * @remarks The specific error code can be obtained using the get_last_result() function. Error codes are described in Exception section. + * @param[in] h The _ handle + * @param[in] ... + * @exception #RPC_PORT_ERROR_NONE Successful + * @exception #RPC_PORT_ERROR_INVALID_PARAMETER Invalid parameter + * @exception #RPC_PORT_ERROR_OUT_OF_MEMORY Out of memory + * @exception #RPC_PORT_ERROR_IO_ERROR I/O error + */ +__invoke_(__h h, ); +)__c_cb"; + +#endif // IDLC_C_GROUP_HEADER_GENERATOR_CB_HH_ \ No newline at end of file diff --git a/idlc/gen/version2/c_proxy_body_generator_cb.hh b/idlc/gen/version2/c_proxy_body_generator_cb.hh index 0129f76..31359f7 100644 --- a/idlc/gen/version2/c_proxy_body_generator_cb.hh +++ b/idlc/gen/version2/c_proxy_body_generator_cb.hh @@ -825,7 +825,7 @@ void __invoke_(__h h) if (map_ == nullptr) { _E("Failed to create unit map"); rpc_port_parcel_destroy(parcel_); - set_lat_result(RPC_PORT_ERROR_OUT_OF_MEMORY); + set_last_result(RPC_PORT_ERROR_OUT_OF_MEMORY); return; } diff --git a/idlc/gen_cion/c_cion_body_gen_base.cc b/idlc/gen_cion/c_cion_body_gen_base.cc index 954d567..cbd25bd 100644 --- a/idlc/gen_cion/c_cion_body_gen_base.cc +++ b/idlc/gen_cion/c_cion_body_gen_base.cc @@ -444,7 +444,7 @@ std::string CCionBodyGeneratorBase::GenBaseElements(const Elements& elms) { void CCionBodyGeneratorBase::GenCommonFuntions(std::ofstream& stream) { std::string code = GetTransportable().C().GenBodyPeerInfoBase(); - if(GetType() != 3) { + if(GetChannelType() != ChannelType::TYPE_GROUP) { code += GetTransportable().C().GenBodyConnectionBase(); code += GetTransportable().C().GenBodyPayloadBase(); } diff --git a/idlc/gen_cion/c_cion_gen_base.cc b/idlc/gen_cion/c_cion_gen_base.cc index dc72940..85d9590 100644 --- a/idlc/gen_cion/c_cion_gen_base.cc +++ b/idlc/gen_cion/c_cion_gen_base.cc @@ -362,7 +362,9 @@ void CCionGeneratorBase::GenIncludeDefaultHeaders(std::ofstream& stream, str = CB_HEADER; } - str = ReplaceAll(str, "", GetTransportable().C().GenInclude(body, GetType())); + str = ReplaceAll(str, "", + GetTransportable().C().GenInclude( + body, static_cast(GetChannelType()))); str = ReplaceAll(str, "", GetHandlePrefix()); str = ReplaceAll(str, "", GetFileNamespace()); stream << str; @@ -479,14 +481,14 @@ std::string CCionGeneratorBase::GetInternalHandlePrefix() { std::string CCionGeneratorBase::GetHandlePrefix() { std::string prefix; - switch(static_cast(GetType())) { - case tidl::Options::Type::TYPE_STUB: + switch(GetChannelType()) { + case ChannelType::TYPE_STUB: prefix = GetTransportable().C().GenStubPrefix(); break; - case tidl::Options::Type::TYPE_PROXY: + case ChannelType::TYPE_PROXY: prefix = GetTransportable().C().GenProxyPrefix(); break; - case tidl::Options::Type::TYPE_GROUP: + case ChannelType::TYPE_GROUP: prefix = GetTransportable().C().GenGroupPrefix(); break; default: diff --git a/idlc/gen_cion/cpp_cion_gen_base.cc b/idlc/gen_cion/cpp_cion_gen_base.cc index 90d08dd..85a7a23 100644 --- a/idlc/gen_cion/cpp_cion_gen_base.cc +++ b/idlc/gen_cion/cpp_cion_gen_base.cc @@ -810,14 +810,14 @@ std::string CppCionGeneratorBase::GetInternalHandlePrefix() { std::string CppCionGeneratorBase::GetHandlePrefix() { std::string prefix; - switch(static_cast(GetType())) { - case tidl::Options::Type::TYPE_STUB: + switch(GetChannelType()) { + case ChannelType::TYPE_STUB: prefix = GetTransportable().Cpp().GenStubPrefix(); break; - case tidl::Options::Type::TYPE_PROXY: + case ChannelType::TYPE_PROXY: prefix = GetTransportable().Cpp().GenProxyPrefix(); break; - case tidl::Options::Type::TYPE_GROUP: + case ChannelType::TYPE_GROUP: prefix = GetTransportable().Cpp().GenGroupPrefix(); break; default: @@ -911,7 +911,7 @@ void CppCionGeneratorBase::GenConnectionBase(std::ofstream& stream) { void CppCionGeneratorBase::GenCommonFunctions(std::ofstream& stream) { std::string code = GetTransportable().Cpp().GenBodyPeerInfoBase(); - if(GetType() != 3) { + if(GetChannelType() != ChannelType::TYPE_GROUP) { code += GetTransportable().Cpp().GenBodyConnectionBase(); code += GetTransportable().Cpp().GenBodyPayloadBase(); } diff --git a/idlc/gen_cion/cpp_cion_group_body_gen.cc b/idlc/gen_cion/cpp_cion_group_body_gen.cc index d0c57e6..08bb172 100644 --- a/idlc/gen_cion/cpp_cion_group_body_gen.cc +++ b/idlc/gen_cion/cpp_cion_group_body_gen.cc @@ -40,7 +40,8 @@ void CppCionGroupBodyGen::OnInitGen(std::ofstream& stream) { << "#include " << NLine(1) << "#include " << NLine(1) << NLine(1) - << ReplaceAll(GetTransportable().Cpp().GenInclude(GetType()), + << ReplaceAll(GetTransportable().Cpp().GenInclude( + static_cast(GetChannelType())), "", GetFileNamespace()) << NLine(2) << "#include \"" << header_file << "\"" << NLine(2); diff --git a/idlc/gen_cion/cpp_cion_proxy_body_gen.cc b/idlc/gen_cion/cpp_cion_proxy_body_gen.cc index 6c5846b..e61e3dd 100644 --- a/idlc/gen_cion/cpp_cion_proxy_body_gen.cc +++ b/idlc/gen_cion/cpp_cion_proxy_body_gen.cc @@ -39,7 +39,8 @@ void CppCionProxyBodyGen::OnInitGen(std::ofstream& stream) { << "#include " << NLine(1) << "#include " << NLine(1) << NLine(1) - << ReplaceAll(GetTransportable().Cpp().GenInclude(GetType()), + << ReplaceAll(GetTransportable().Cpp().GenInclude( + static_cast(GetChannelType())), "", GetFileNamespace()) << NLine(2) << "#include \"" << header_file << "\"" << NLine(2); diff --git a/idlc/gen_cion/cpp_cion_stub_body_gen.cc b/idlc/gen_cion/cpp_cion_stub_body_gen.cc index 474d551..761a218 100644 --- a/idlc/gen_cion/cpp_cion_stub_body_gen.cc +++ b/idlc/gen_cion/cpp_cion_stub_body_gen.cc @@ -41,7 +41,8 @@ void CppCionStubBodyGen::OnInitGen(std::ofstream& stream) { << "#include " << NLine(1) << "#include " << NLine(1) << NLine(1) - << ReplaceAll(GetTransportable().Cpp().GenInclude(GetType()), + << ReplaceAll(GetTransportable().Cpp().GenInclude( + static_cast(GetChannelType())), "", GetFileNamespace()) << NLine(2) << "#include \"" << header_file << "\"" << NLine(2); diff --git a/idlc/gen_mqtt_plugin/mqtt_plugin_cs_base_gen.cc b/idlc/gen_mqtt_plugin/mqtt_plugin_cs_base_gen.cc index 24f9a34..1ec6b6f 100644 --- a/idlc/gen_mqtt_plugin/mqtt_plugin_cs_base_gen.cc +++ b/idlc/gen_mqtt_plugin/mqtt_plugin_cs_base_gen.cc @@ -54,19 +54,16 @@ void MqttPluginCsBaseGen::GenBase(std::ofstream& stream) { stream << CB_CONNECTION_RESULT; stream << CB_CONNECTION_STATUS; - switch (GetType()) { - // client - case 1: + switch (GetChannelType()) { + case ChannelType::TYPE_PROXY: stream << CB_CLIENT_SAFE_HANDLE; stream << CB_CLIENT_BASE; break; - // server - case 2: + case ChannelType::TYPE_STUB: stream << CB_SERVER_SAFE_HANDLE; stream << CB_SERVER_BASE; break; - // group - case 3: + case ChannelType::TYPE_GROUP: stream << CB_GROUP_SAFE_HANDLE; stream << CB_GROUP_BASE; break; diff --git a/idlc/gen_mqtt_plugin/mqtt_plugin_cs_interop_gen.cc b/idlc/gen_mqtt_plugin/mqtt_plugin_cs_interop_gen.cc index d4dba40..3c484d7 100644 --- a/idlc/gen_mqtt_plugin/mqtt_plugin_cs_interop_gen.cc +++ b/idlc/gen_mqtt_plugin/mqtt_plugin_cs_interop_gen.cc @@ -59,17 +59,14 @@ void MqttPluginCsInteropGen::GenInterop(std::ofstream& stream) { stream << CB_INTEROP_PAYLOAD_ASYNC_RESULT; stream << CB_INTEROP_PEER_INFO; stream << CB_INTEROP_CONNECTION_RESULT; - switch (GetType()) { - // client - case 1: + switch (GetChannelType()) { + case ChannelType::TYPE_PROXY: stream << CB_INTEROP_CLIENT; break; - // server - case 2: + case ChannelType::TYPE_STUB: stream << CB_INTEROP_SERVER; break; - // group - case 3: + case ChannelType::TYPE_GROUP: stream << CB_INTEROP_GROUP; break; default: diff --git a/idlc/gen_mqtt_plugin/mqtt_plugin_internal_body_gen.cc b/idlc/gen_mqtt_plugin/mqtt_plugin_internal_body_gen.cc index 9bce062..fb20a73 100644 --- a/idlc/gen_mqtt_plugin/mqtt_plugin_internal_body_gen.cc +++ b/idlc/gen_mqtt_plugin/mqtt_plugin_internal_body_gen.cc @@ -51,18 +51,18 @@ void MqttPluginInternalBodyGen::GenLoadModule(std::ofstream& stream) { code += CB_LOAD_MOUDLE_PEERINFO; code += CB_LOAD_MOUDLE_CONNECTION; - switch (GetType()) { - case 1: + switch (GetChannelType()) { + case ChannelType::TYPE_PROXY: { code += CB_LOAD_MOUDLE_CLIENT; break; } - case 2: + case ChannelType::TYPE_STUB: { code += CB_LOAD_MOUDLE_SERVER; break; } - case 3: + case ChannelType::TYPE_GROUP: { code += CB_LOAD_MOUDLE_GROUP; break; @@ -77,18 +77,18 @@ void MqttPluginInternalBodyGen::GenLoadModule(std::ofstream& stream) { void MqttPluginInternalBodyGen::GenBody(std::ofstream& stream) { stream << tidl::ReplaceAll(CB_BODY_BASE, "", GetHandlePrefix()); - switch (GetType()) { - case 1: + switch (GetChannelType()) { + case ChannelType::TYPE_PROXY: { stream << tidl::ReplaceAll(CB_BODY_CLIENT, "", GetHandlePrefix()); break; } - case 2: + case ChannelType::TYPE_STUB: { stream << tidl::ReplaceAll(CB_BODY_SERVER, "", GetHandlePrefix()); break; } - case 3: + case ChannelType::TYPE_GROUP: { stream << tidl::ReplaceAll(CB_BODY_GROUP, "", GetHandlePrefix()); break; diff --git a/idlc/gen_mqtt_plugin/mqtt_plugin_internal_header_gen.cc b/idlc/gen_mqtt_plugin/mqtt_plugin_internal_header_gen.cc index d9b3ada..00b890e 100644 --- a/idlc/gen_mqtt_plugin/mqtt_plugin_internal_header_gen.cc +++ b/idlc/gen_mqtt_plugin/mqtt_plugin_internal_header_gen.cc @@ -31,20 +31,20 @@ void MqttPluginInternalHeaderGen::OnInitGen(std::ofstream& stream) { stream << tidl::ReplaceAll(CB_HEADER_START, "", GetHandlePrefix()); stream << tidl::ReplaceAll(CB_HEADER_DEF_COMMON, "", GetHandlePrefix()); - switch (GetType()) { - case 1: + switch (GetChannelType()) { + case ChannelType::TYPE_PROXY: { stream << tidl::ReplaceAll(CB_HEADER_DEF_CLIENT, "", GetHandlePrefix()); stream << tidl::ReplaceAll(CB_HEADER_FUN_CLIENT, "", GetHandlePrefix()); break; } - case 2: + case ChannelType::TYPE_STUB: { stream << tidl::ReplaceAll(CB_HEADER_DEF_SERVER, "", GetHandlePrefix()); stream << tidl::ReplaceAll(CB_HEADER_FUN_SERVER, "", GetHandlePrefix()); break; } - case 3: + case ChannelType::TYPE_GROUP: { stream << tidl::ReplaceAll(CB_HEADER_DEF_GROUP, "", GetHandlePrefix()); stream << tidl::ReplaceAll(CB_HEADER_FUN_GROUP, "", GetHandlePrefix()); diff --git a/idlc/main.cc b/idlc/main.cc index a2489af..889eca5 100644 --- a/idlc/main.cc +++ b/idlc/main.cc @@ -40,6 +40,8 @@ #include "idlc/gen/dart_stub_gen.h" #include "idlc/gen/version2/c_proxy_header_generator.hh" #include "idlc/gen/version2/c_proxy_body_generator.hh" +#include "idlc/gen/version2/c_group_header_generator.hh" +#include "idlc/gen/version2/c_group_body_generator.hh" #include "idlc/gen_cion/c_cion_proxy_header_gen.h" #include "idlc/gen_cion/c_cion_proxy_body_gen.h" #include "idlc/gen_cion/c_cion_stub_header_gen.h" @@ -87,11 +89,13 @@ void GenerateStubCodes(std::shared_ptr options, { tidl::CCionStubHeaderGen stub_header(ps.GetDoc(), trans); stub_header.EnableNamespace(options->HasNamespace()); - stub_header.SetType(static_cast(options->GetType())); + stub_header.SetChannelType( + static_cast(options->GetType())); stub_header.Run(options->GetOutput() + ".h"); tidl::CCionStubBodyGen stub_body(ps.GetDoc(), options, trans); stub_body.EnableNamespace(options->HasNamespace()); - stub_body.SetType(static_cast(options->GetType())); + stub_body.SetChannelType( + static_cast(options->GetType())); stub_body.Run(options->GetOutput() + ".c"); break; } @@ -140,25 +144,29 @@ void GenerateStubCodes(std::shared_ptr options, tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.Run(options->GetOutput() + "_internal.c"); tidl::CCionStubHeaderGen stub_header(ps.GetDoc(), trans); stub_header.EnableNamespace(options->HasNamespace()); stub_header.EnableGeneratedAPI(true); - stub_header.SetType(static_cast(options->GetType())); + stub_header.SetChannelType( + static_cast(options->GetType())); stub_header.Run(options->GetOutput() + ".h"); tidl::CCionStubBodyGen stub_body(ps.GetDoc(), options, trans); stub_body.EnableNamespace(options->HasNamespace()); - stub_body.EnableGeneratedAPI(true); - stub_body.SetType(static_cast(options->GetType())); + stub_body.EnableGeneratedAPI(true); + stub_body.SetChannelType( + static_cast(options->GetType())); stub_body.Run(options->GetOutput() + ".c"); break; @@ -168,23 +176,27 @@ void GenerateStubCodes(std::shared_ptr options, tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.Run(options->GetOutput() + "_internal.c"); tidl::CppCionStubHeaderGen stub_header(ps.GetDoc(), options, trans); - stub_header.SetType(static_cast(options->GetType())); + stub_header.SetChannelType( + static_cast(options->GetType())); stub_header.EnableGeneratedAPI(true); stub_header.EnableNamespace(options->HasNamespace()); stub_header.Run(options->GetOutput() + ".h"); tidl::CppCionStubBodyGen stub_body(ps.GetDoc(), options, trans); - stub_body.SetType(static_cast(options->GetType())); + stub_body.SetChannelType( + static_cast(options->GetType())); stub_body.EnableGeneratedAPI(true); stub_body.EnableNamespace(options->HasNamespace()); stub_body.Run(options->GetOutput() + ".cc"); @@ -193,10 +205,12 @@ void GenerateStubCodes(std::shared_ptr options, case tidl::Options::LANGUAGE_TYPE_CSHARP: { tidl::MqttPluginCsInteropGen interop(ps.GetDoc(), trans); - interop.SetType(static_cast(options->GetType())); + interop.SetChannelType( + static_cast(options->GetType())); interop.Run("Interop.MqttPlugin.cs"); tidl::MqttPluginCsBaseGen base(ps.GetDoc(), trans); - base.SetType(static_cast(options->GetType())); + base.SetChannelType( + static_cast(options->GetType())); base.Run("MqttPluginBase.cs"); tidl::CsCionStubGen stub(ps.GetDoc(), trans); @@ -281,11 +295,13 @@ void GenerateProxyCodes(std::shared_ptr options, { tidl::CCionProxyHeaderGen proxy_header(ps.GetDoc(), trans); proxy_header.EnableNamespace(options->HasNamespace()); - proxy_header.SetType(static_cast(options->GetType())); + proxy_header.SetChannelType( + static_cast(options->GetType())); proxy_header.Run(options->GetOutput() + ".h"); tidl::CCionProxyBodyGen proxy_body(ps.GetDoc(), options, trans); proxy_body.EnableNamespace(options->HasNamespace()); - proxy_body.SetType(static_cast(options->GetType())); + proxy_body.SetChannelType( + static_cast(options->GetType())); proxy_body.Run(options->GetOutput() + ".c"); break; } @@ -297,7 +313,6 @@ void GenerateProxyCodes(std::shared_ptr options, proxy_body.Run(options->GetOutput() + ".cc"); break; } - break; case tidl::Options::LANGUAGE_TYPE_CSHARP: { tidl::CsCionProxyGen proxy(ps.GetDoc(), trans); @@ -335,38 +350,44 @@ void GenerateProxyCodes(std::shared_ptr options, tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.Run(options->GetOutput() + "_internal.c"); tidl::CCionProxyHeaderGen proxy_header(ps.GetDoc(), trans); proxy_header.EnableNamespace(options->HasNamespace()); proxy_header.EnableGeneratedAPI(true); - proxy_header.SetType(static_cast(options->GetType())); + proxy_header.SetChannelType( + static_cast(options->GetType())); proxy_header.Run(options->GetOutput() + ".h"); tidl::CCionProxyBodyGen proxy_body(ps.GetDoc(), options, trans); proxy_body.EnableNamespace(options->HasNamespace()); proxy_body.EnableGeneratedAPI(true); - proxy_body.SetType(static_cast(options->GetType())); + proxy_body.SetChannelType( + static_cast(options->GetType())); proxy_body.Run(options->GetOutput() + ".c"); break; } case tidl::Options::LANGUAGE_TYPE_CPP: { tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); internal_body.Run(options->GetOutput() + "_internal.c"); @@ -374,23 +395,27 @@ void GenerateProxyCodes(std::shared_ptr options, tidl::CppCionProxyHeaderGen proxy_header(ps.GetDoc(), trans); proxy_header.EnableNamespace(options->HasNamespace()); proxy_header.EnableGeneratedAPI(true); - proxy_header.SetType(static_cast(options->GetType())); + proxy_header.SetChannelType( + static_cast(options->GetType())); proxy_header.Run(options->GetOutput() + ".h"); tidl::CppCionProxyBodyGen proxy_body(ps.GetDoc(), trans); proxy_body.EnableNamespace(options->HasNamespace()); proxy_body.EnableGeneratedAPI(true); - proxy_body.SetType(static_cast(options->GetType())); + proxy_body.SetChannelType( + static_cast(options->GetType())); proxy_body.Run(options->GetOutput() + ".cc"); break; } case tidl::Options::LANGUAGE_TYPE_CSHARP: { tidl::MqttPluginCsInteropGen interop(ps.GetDoc(), trans); - interop.SetType(static_cast(options->GetType())); + interop.SetChannelType( + static_cast(options->GetType())); interop.Run("Interop.MqttPlugin.cs"); tidl::MqttPluginCsBaseGen base(ps.GetDoc(), trans); - base.SetType(static_cast(options->GetType())); + base.SetChannelType( + static_cast(options->GetType())); base.Run("MqttPluginBase.cs"); tidl::CsCionProxyGen proxy(ps.GetDoc(), trans); @@ -487,12 +512,14 @@ void GenerateGroupCodes(std::shared_ptr options, { tidl::CCionGroupHeaderGen group_header(ps.GetDoc(), trans); group_header.EnableNamespace(options->HasNamespace()); - group_header.SetType(static_cast(options->GetType())); + group_header.SetChannelType( + static_cast(options->GetType())); group_header.Run(options->GetOutput() + ".h"); tidl::CCionGroupBodyGen group_body(ps.GetDoc(), options, trans); group_body.EnableNamespace(options->HasNamespace()); - group_body.SetType(static_cast(options->GetType())); + group_body.SetChannelType( + static_cast(options->GetType())); group_body.Run(options->GetOutput() + ".c"); break; } @@ -540,26 +567,30 @@ void GenerateGroupCodes(std::shared_ptr options, tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.Run(options->GetOutput() + "_internal.c"); tidl::CCionGroupHeaderGen group_header(ps.GetDoc(), trans); group_header.EnableNamespace(options->HasNamespace()); group_header.EnableGeneratedAPI(true); - group_header.SetType(static_cast(options->GetType())); + group_header.SetChannelType( + static_cast(options->GetType())); group_header.EnableGeneratedAPI(true); group_header.Run(options->GetOutput() + ".h"); tidl::CCionGroupBodyGen group_body(ps.GetDoc(), options, trans); group_body.EnableNamespace(options->HasNamespace()); group_body.EnableGeneratedAPI(true); - group_body.SetType(static_cast(options->GetType())); + group_body.SetChannelType( + static_cast(options->GetType())); group_body.EnableGeneratedAPI(true); group_body.Run(options->GetOutput() + ".c"); break; @@ -567,25 +598,29 @@ void GenerateGroupCodes(std::shared_ptr options, case tidl::Options::LANGUAGE_TYPE_CPP: { tidl::MqttPluginInternalHeaderGen internal_header(ps.GetDoc(), trans); - internal_header.SetType(static_cast(options->GetType())); + internal_header.SetChannelType( + static_cast(options->GetType())); internal_header.EnableNamespace(options->HasNamespace()); internal_header.EnableGeneratedAPI(true); internal_header.Run(options->GetOutput() + "_internal.h"); tidl::MqttPluginInternalBodyGen internal_body(ps.GetDoc(), trans); - internal_body.SetType(static_cast(options->GetType())); + internal_body.SetChannelType( + static_cast(options->GetType())); internal_body.EnableNamespace(options->HasNamespace()); internal_body.EnableGeneratedAPI(true); internal_body.Run(options->GetOutput() + "_internal.c"); tidl::CppCionGroupHeaderGen group_header(ps.GetDoc(), trans); - group_header.SetType(static_cast(options->GetType())); + group_header.SetChannelType( + static_cast(options->GetType())); group_header.EnableNamespace(options->HasNamespace()); group_header.EnableGeneratedAPI(true); group_header.Run(options->GetOutput() + ".h"); tidl::CppCionGroupBodyGen group_body(ps.GetDoc(), trans); - group_body.SetType(static_cast(options->GetType())); + group_body.SetChannelType( + static_cast(options->GetType())); group_body.EnableNamespace(options->HasNamespace()); group_body.EnableGeneratedAPI(true); group_body.Run(options->GetOutput() + ".cc"); @@ -594,10 +629,12 @@ void GenerateGroupCodes(std::shared_ptr options, case tidl::Options::LANGUAGE_TYPE_CSHARP: { tidl::MqttPluginCsInteropGen interop(ps.GetDoc(), trans); - interop.SetType(static_cast(options->GetType())); + interop.SetChannelType( + static_cast(options->GetType())); interop.Run("Interop.MqttPlugin.cs"); tidl::MqttPluginCsBaseGen base(ps.GetDoc(), trans); - base.SetType(static_cast(options->GetType())); + base.SetChannelType( + static_cast(options->GetType())); base.Run("MqttPluginBase.cs"); tidl::CsCionGroupGen group(ps.GetDoc(), trans); @@ -628,33 +665,44 @@ void GenerateGroupCodes(std::shared_ptr options, } } else { switch (options->GetLanguage()) { - case tidl::Options::LANGUAGE_TYPE_CPP: - { - tidl::CppGroupHeaderGen group_header(ps.GetDoc()); - group_header.Run(options->GetOutput() + ".h"); - tidl::CppGroupBodyGen group_body(ps.GetDoc()); - group_body.Run(options->GetOutput() + ".cc"); - break; - } + case tidl::Options::LANGUAGE_TYPE_CPP: + { + tidl::CppGroupHeaderGen group_header(ps.GetDoc()); + group_header.Run(options->GetOutput() + ".h"); + tidl::CppGroupBodyGen group_body(ps.GetDoc()); + group_body.Run(options->GetOutput() + ".cc"); + break; + } - case tidl::Options::LANGUAGE_TYPE_C: - { + case tidl::Options::LANGUAGE_TYPE_C: + { + if (ps.GetVersion() == 2) { + tidl::version2::CGroupHeaderGenerator group_header(ps.GetDoc()); + group_header.SetChannelType( + static_cast(options->GetType())); + group_header.Run(options->GetOutput() + ".h"); + tidl::version2::CGroupBodyGenerator group_body(ps.GetDoc()); + group_body.SetChannelType( + static_cast(options->GetType())); + group_body.Run(options->GetOutput() + ".c"); + } else { tidl::CGroupHeaderGen group_header(ps.GetDoc()); group_header.Run(options->GetOutput() + ".h"); tidl::CGroupBodyGen group_body(ps.GetDoc()); group_body.Run(options->GetOutput() + ".c"); - break; } + break; + } - case tidl::Options::LANGUAGE_TYPE_CSHARP: - { - tidl::CsGroupGen group(ps.GetDoc()); - group.Run(options->GetOutput() + ".cs"); - break; - } + case tidl::Options::LANGUAGE_TYPE_CSHARP: + { + tidl::CsGroupGen group(ps.GetDoc()); + group.Run(options->GetOutput() + ".cs"); + break; + } - default: - break; + default: + break; } } } -- 2.7.4