From: pjh9216 Date: Mon, 25 Nov 2024 04:31:30 +0000 (+0900) Subject: Refactor Unit in C# generator for improving performance X-Git-Tag: accepted/tizen/unified/x/20241223.070831~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=refs%2Fchanges%2F74%2F315374%2F10;p=platform%2Fcore%2Fappfw%2Ftidl.git Refactor Unit in C# generator for improving performance Change-Id: Ib0ed3e560cace08572e99032b182c7cd2df4d484 Signed-off-by: pjh9216 --- diff --git a/idlc/gen/generator.cc b/idlc/gen/generator.cc index ee01beac..0f5fbc66 100644 --- a/idlc/gen/generator.cc +++ b/idlc/gen/generator.cc @@ -154,4 +154,8 @@ std::string Generator::SnakeToPascal(const std::string& snake_case) { return pascal_case; } +std::string operator ""_hash(const char* str, std::size_t) { + return std::to_string(Generator::GetHashCode(str)); +} + } // namespace tidl diff --git a/idlc/gen/generator.h b/idlc/gen/generator.h index d3abae20..9874baf5 100644 --- a/idlc/gen/generator.h +++ b/idlc/gen/generator.h @@ -48,7 +48,7 @@ class Generator { bool IsDelegateType(const BaseType& type); std::string GetInterfaceNameFromDelegate(const BaseType& type); - int32_t GetHashCode(std::string_view str) const { + static int32_t GetHashCode(std::string_view str) { int32_t hash = 0; for (auto& a : str) { @@ -58,6 +58,10 @@ class Generator { return hash; } + static std::string GetHashCodeStr(std::string_view str) { + return std::to_string(GetHashCode(str)); + } + void EnableNamespace(bool enable) { hasNamespace_ = enable; } @@ -160,6 +164,8 @@ class Generator { ChannelType type_ = TYPE_UNKNOWN; }; +std::string operator ""_hash(const char* str, std::size_t); + } // namespace tidl #endif // IDLC_GENERATOR_H_ diff --git a/idlc/gen/replace_all.h b/idlc/gen/replace_all.h index 02df11ad..e523731b 100755 --- a/idlc/gen/replace_all.h +++ b/idlc/gen/replace_all.h @@ -134,6 +134,9 @@ class ReplaceAll { ReplaceAll& AddIndent(int indent, bool space = true); void Out(std::ofstream& stream); + std::string ToString() { + return str_; + } private: int GetTagStartPos(int pos); diff --git a/idlc/gen/version2/cpp_group_header_generator_cb.hh b/idlc/gen/version2/cpp_group_header_generator_cb.hh index 229c57ee..707a3c1f 100644 --- a/idlc/gen/version2/cpp_group_header_generator_cb.hh +++ b/idlc/gen/version2/cpp_group_header_generator_cb.hh @@ -38,6 +38,7 @@ R"__cpp_cb( #include #include #include +#include #include #include #include diff --git a/idlc/gen/version2/cs_generator_base.cc b/idlc/gen/version2/cs_generator_base.cc index 8c7aa5d4..bfe77dce 100644 --- a/idlc/gen/version2/cs_generator_base.cc +++ b/idlc/gen/version2/cs_generator_base.cc @@ -44,140 +44,93 @@ CsGeneratorBase::CsGeneratorBase(std::shared_ptr doc) {"bundle", "Bundle"}, {"file", "String"}}; } -void CsGeneratorBase::GenStructures(std::ofstream& stream) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_STRUCTURE) continue; - Structure& st = static_cast(*i); - GenStructure(stream, st); - stream << std::endl; - } -} - -void CsGeneratorBase::GenRemoteException(std::ofstream& stream) { - stream << CB_REMOTE_EXCEPTION_BASE; -} - -void CsGeneratorBase::GenUsingNamespace(std::ofstream& stream) { - stream << CB_USING_NAMESPACE; -} - -std::string CsGeneratorBase::GenEnum(const Block& block, int tab_size) { - std::string str; - for (const auto& e : block.GetEnums()) { - str += Tab(tab_size) + "public enum " + e->GetID() + " {" + NLine(1); - for (auto& f : e->GetFields()) { - if (f->GetValue().empty()) { - str += Tab(tab_size + 1) + f->GetID() + "," + NLine(1); - } else { - str += Tab(tab_size + 1) + f->GetID() + " = " + f->GetValue() + "," + - NLine(1); - } - } - str += Tab(tab_size) + "}" + NLine(1); - } - return str; -} - -void CsGeneratorBase::GenEnum(std::ofstream& stream, const Block& block, - int tab_size) { - stream << GenEnum(block, tab_size); -} - -void CsGeneratorBase::GenStructure(std::ofstream& stream, const Structure& st) { - std::vector v; - - stream << Tab(1) << "public class " << st.GetID(); - - if (st.GetBase() == nullptr) { - stream << NLine(1); - } else { - stream << " : " << st.GetBase()->GetID() << NLine(1); - } - - GenBrace(stream, TAB_SIZE, [&]() { - GenEnum(stream, st, 2); - for (const auto& i : st.GetElements()) { - GenTemplate( - CB_PROPERTY, stream, - [&]() -> std::string { return ConvertTypeToString(i->GetType()); }, - [&]() -> std::string { return i->GetID(); }); - if (i->GetType().ToString() == "bundle" || - i->GetType().ToString() == "map" || - i->GetType().ToString() == "set") { - v.push_back(i->GetID() + " = " + "new " + - ConvertTypeToString(i->GetType()) + "()"); - } - } - - GenTemplate( - CB_CTOR, stream, [&]() -> std::string { return st.GetID(); }, - [&]() -> std::string { - std::string str; - for (auto& i : v) { - str += " " + i + ";\n"; - } - return str; - }); - }); -} - -void CsGeneratorBase::GenSerializer(std::ofstream& stream, - const Structure& st) { - stream << GetSerializer(st); -} - -std::string CsGeneratorBase::GetStructureSerialize(const Structure& st) { - std::string code; - std::string type_str; - - for (const auto& i : st.GetElements()) { - code += Tab(4) + "map.Write(\"" + i->GetID() + "\", param." + - i->GetID() + ");" + NLine(1); - } - - if (st.GetBase() != nullptr) - code += GetStructureSerialize(*st.GetBase()); - - return code; -} - -std::string CsGeneratorBase::GetStructureDeserialize(const Structure& st) { - std::string code; - std::string type_str; - - for (const auto& i : st.GetElements()) { - auto& t = i->GetType(); - if (t.IsEnumType()) - type_str = "int"; - else - type_str = GetTypeString(t); - - code += Tab(4) + "map.Read(\"" + i->GetID() + "\", out param." + - i->GetID() + ");" + NLine(1); - } - - if (st.GetBase() != nullptr) - code += GetStructureDeserialize(*st.GetBase()); - - return code; +std::string CsGeneratorBase::GetStructures() { + auto structures = [&](auto& r, const auto& i) { + if (i->GetType() != Block::Type::TYPE_STRUCTURE) return false; + + auto& st = static_cast(*i); + + r.Remove("CLASS_BASE", st.GetBase() == nullptr) + .Replace("CLASS_ID", st.GetID()) + .Replace("BASE_CLASS_ID", st.GetBase()->GetID()) + .Repeat("PROPERTIES", st.GetElements(), + [&](auto& r, const auto& i) { + r.Replace("S_TYPE", ConvertTypeToString(i->GetType())) + .Replace("S_VAR", i->GetID()); + return true; + }) + .Repeat("PROPERTY_INITS", st.GetElements(), + [&](auto& r, const auto& i) { + if (i->GetType().ToString() != "bundle" && + i->GetType().ToString() != "map" && + i->GetType().ToString() != "set") + return false; + + r.Replace("S_TYPE", ConvertTypeToString(i->GetType())) + .Replace("S_VAR", i->GetID()); + return true; + }) + .Replace("ENUMS", GetEnum(st, 1)); + + return true; + }; + + return ReplaceAll(CB_STRUCTES) + .Repeat("STRUCTES", GetDocument().GetBlocks(), structures) + .ToString(); +} + +std::string CsGeneratorBase::GetRemoteException() { + return CB_REMOTE_EXCEPTION_BASE; +} + +std::string CsGeneratorBase::GetEnum(const Block& block, int tab_size) { + auto enums = [&](auto& r, const auto& i) { + r.Repeat("FIELDS", i->GetFields(), [&](auto& r, const auto& i) { + r.Remove("VALUE", i->GetValue().empty()) + .Remove("NO_VALUE", !i->GetValue().empty()) + .Replace("FIELD_ID", i->GetID()) + .Replace("VAL", i->GetValue()); + return true; + }).Replace("ID", i->GetID()); + + return true; + }; + + return AddIndent( + TAB_SIZE * tab_size, + ReplaceAll(CB_ENUMS).Repeat("ENUMS", block.GetEnums(), enums).ToString()); } std::string CsGeneratorBase::GetSerializer(const Structure& st) { - std::string str(ReplaceAll(CB_SERIALIZER) - .Change("", st.GetID()) - .Change("", GetStructureSerialize(st))); - - str += ReplaceAll(CB_DESERIALIZER) - .Change("", st.GetID()) - .Change("", GetStructureDeserialize(st)); - return str; -} - -void CsGeneratorBase::GenSerializer(std::ofstream& stream) { - stream << GetSerializer(); - - if (GetChannelType() != ChannelType::TYPE_GROUP) - stream << CB_REMOTE_EXCEPTION_SERIALIZE; + auto elements = [&](auto& r, const auto& i) { + auto& t = i->GetType(); + bool default_type = IsDefaultType(&t); + bool is_enum = t.IsEnumType(); + + r.Remove("USER_TYPE", default_type) + .Remove("DEFAULT_TYPE", !default_type) + .Replace("FROM_CAST", + is_enum ? std::string("()") : std::string()) + .Replace("ID", i->GetID()) + .Replace("PARAM_TYPE", ConvertTypeToString(t)) + .Replace("CAST", is_enum ? std::string("(int)") : std::string()) + .Replace("PARCEL_TYPE", ConvertTypeToParcelType(&t)); + return true; + }; + + return ReplaceAll(CB_STRUCT_SERIALIZER) + .RemoveAll("HAS_BASE", !st.GetBase()) + .Replace("BASE_TYPE", + [&]() { + if (!st.GetBase()) return std::string(); + + return st.GetBase()->GetID(); + }) + .Repeat("ELEMENTS_SER", st.GetElements(), elements) + .Repeat("ELEMENTS_DER", st.GetElements(), elements) + .Replace("TYPE", st.GetID()) + .ToString(); } std::string CsGeneratorBase::GetSerializer() { @@ -225,86 +178,50 @@ void CsGeneratorBase::AddSerializerList(const BaseType& type, } } -std::string CsGeneratorBase::GenListWrite(const BaseType& type, - const std::string& type_str) { - std::string ret; - - if (type.ToString() == "map") { - ret = CB_LIST_MAP_WRITE; - } else if (type.ToString() == "set") { - ret = CB_LIST_SET_WRITE; - } else { - ret = CB_LIST_WRITE; - } - - return ret; -} - -std::string CsGeneratorBase::GenListRead(const BaseType& type, - const std::string& type_str) { - std::string ret; - - if (type.ToString() == "map") { - if (type.GetValueType()->IsEnumType()) { - ret = ReplaceAll(CB_LIST_MAP_READ) - .Change("", ConvertTypeToString(*type.GetKeyType())) - .Change("", type.GetValueType()->ToString()); - } else { - ret = - ReplaceAll(CB_LIST_MAP_READ) - .Change("", ConvertTypeToString(*type.GetKeyType())) - .Change("", - ConvertTypeToString(*type.GetValueType())); - } - } else if (type.ToString() == "set") { - ret = ReplaceAll(CB_LIST_SET_READ) - .Change("", ConvertTypeToString(*type.GetMetaType())); - } else if (type.GetMetaType()->IsEnumType()) { - std::string list_method; - if (type.ToString() == "list") - list_method = "Last"; - - ret = ReplaceAll(CB_LIST_READ) - .Change("", list_method) - .Change("", type.GetMetaType()->ToString()); - } else { - std::string list_method; - if (type.ToString() == "list") - list_method = "Last"; +bool CsGeneratorBase::IsDefaultType(const BaseType* type) const { + if (!type) return true; - ret = ReplaceAll(CB_LIST_READ) - .Change("", list_method) - .Change("", ConvertTypeToString(*type.GetMetaType())); - } + if (type->GetUserDefinedType() == BaseType::UserType::ENUM) return true; - return ret; + return !type->IsUserDefinedType() && type->GetMetaType() == nullptr && + type->GetKeyType() == nullptr; } -std::string CsGeneratorBase::GetLengthName(const BaseType& type) { - if (type.ToString() == "list") { - return "length"; - } else { - return "size"; - } -} std::string CsGeneratorBase::GetListSerializer(const BaseType& type, const std::string& type_str) { - std::string code; - code = ReplaceAll(CB_LIST_SERIALIZER) - .Change("", type_str) - .Change("", GetLengthName(type)) - .Change("", GenListWrite(type, type_str)); - - code += ReplaceAll(CB_LIST_DESERIALIZER) - .Change("", type_str) - .Change("", GetLengthName(type)) - .Change("", GenListRead(type, type_str)); - - return code; -} - -void CsGeneratorBase::GenListSerializer(std::ofstream& stream) { - stream << GetListSerializer(); + auto* key_type = type.GetKeyType(); + auto* value_type = type.GetValueType(); + auto* meta_type = type.GetMetaType(); + + return ReplaceAll(CB_LIST_SERIALIZER) + .RemoveAll("MAP", type.ToString() != "map") + .RemoveAll("SET", type.ToString() != "set") + .RemoveAll("LIST", type.ToString() != "list") + .RemoveAll("ARRAY", type.ToString() != "array") + .RemoveAll("DEFAULT_KEY_TYPE", !IsDefaultType(key_type)) + .RemoveAll("USER_KEY_TYPE", IsDefaultType(key_type)) + .RemoveAll("DEFAULT_VALUE_TYPE", !IsDefaultType(value_type)) + .RemoveAll("USER_VALUE_TYPE", IsDefaultType(value_type)) + .RemoveAll("DEFAULT_META_TYPE", !IsDefaultType(meta_type)) + .RemoveAll("USER_META_TYPE", IsDefaultType(meta_type)) + .Replace("TYPE", type_str) + .Replace("CAST", ((value_type && value_type->IsEnumType()) || + (meta_type && meta_type->IsEnumType())) + ? std::string("(int)") + : std::string()) + .Replace("MAP_FROM_CAST", (value_type && value_type->IsEnumType()) + ? std::string("()") + : std::string()) + .Replace("LIST_FROM_CAST", (meta_type && meta_type->IsEnumType()) + ? std::string("()") + : std::string()) + .Replace("KEY_TYPE", key_type ? ConvertTypeToString(*key_type) : "") + .Replace("VALUE_TYPE", value_type ? ConvertTypeToString(*value_type) : "") + .Replace("META_TYPE", meta_type ? ConvertTypeToString(*meta_type) : "") + .Replace("PARCEL_TYPE_KEY", ConvertTypeToParcelType(key_type)) + .Replace("PARCEL_TYPE_VALUE", ConvertTypeToParcelType(value_type)) + .Replace("PARCEL_TYPE_META", ConvertTypeToParcelType(meta_type)) + .ToString(); } std::string CsGeneratorBase::GetListSerializer() { @@ -345,8 +262,7 @@ std::string CsGeneratorBase::ConvertTypeToString(const BaseType& type, return st_id + "." + type.ToString(); } - if (type.IsUserDefinedType()) - return type.ToString(); + if (type.IsUserDefinedType()) return type.ToString(); if (type.GetMetaType() != nullptr) return type_map_[type.ToString()] + "<" + @@ -365,45 +281,12 @@ std::string CsGeneratorBase::ConvertTypeToParcelType(const std::string& key) { return parcel_type_map_[key]; } -std::string CsGeneratorBase::ConvertTypeToDeserializer( - const BaseType& type, std::string gen_id, std::string param_id, - std::string map, bool make_new_type, std::string iface_id) { - std::string ret; - std::string new_type = ""; - - if (make_new_type) { - if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { - if (type.GetMetaType() != nullptr) - new_type = ConvertTypeToString(type); - else - new_type = type.ToString(); - } else { - new_type = ConvertTypeToString(type); - } - } - - ret += map + ".Read(" + "\"" + param_id + "\", out "; - if (IsDelegateType(type)) { - ret += "CallbackBase del_" + gen_id + ");\n"; - ret += ReplaceAll(CB_DLELGATGE_CREATE) - .Change("", new_type) - .Change("", gen_id); - } else { - if (make_new_type) { - ret += new_type + " " + gen_id + ");\n"; - } else { - ret += gen_id + ");\n"; - } - } - - return ret; -} +std::string CsGeneratorBase::ConvertTypeToParcelType(const BaseType* type) { + if (!type) return ""; -std::string CsGeneratorBase::ConvertTypeToSerializer(std::string gen_id, - std::string param_id, - std::string map) { + if (type->GetUserDefinedType() == BaseType::UserType::ENUM) return "Int"; - return map + ".Write(\"" + param_id + "\", " + gen_id + ");\n"; + return ConvertTypeToParcelType(type->ToString()); } std::string CsGeneratorBase::Tab(int cnt) { @@ -426,50 +309,36 @@ std::string CsGeneratorBase::NLine(int cnt) { return t; } -void CsGeneratorBase::GenWriteBundle(std::ofstream& stream, - const std::string& id) { - GenTemplate( - CB_WRITE_BUNDLE, stream, [&]() -> std::string { return id; }, - [&]() -> std::string { return id; }); -} - std::string CsGeneratorBase::GetMethodId(const Interface& iface) { - return std::string(ReplaceAll(CB_METHOD_IDS).Change("", [&]() { - int cnt = 2; - std::string str; - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) continue; - str += - Tab(4) + i->GetID() + " = " + std::to_string(cnt++) + "," + NLine(1); - } - return str; - })); -} + int cnt = 2; + return ReplaceAll(CB_METHOD_IDS) + .Repeat( + "METHOD_IDS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) + return false; -void CsGeneratorBase::GenMethodId(std::ofstream& stream, - const Interface& iface) { - stream << GetMethodId(iface); + r.Replace("ID", i->GetID()).Replace("VAL", std::to_string(cnt++)); + return true; + }) + .ToString(); } -void CsGeneratorBase::GenDelegateId(std::ofstream& stream, - const Interface& iface) { - if (HasDelegate(iface) == false) - return; +std::string CsGeneratorBase::GetDelegateId(const Interface& iface) { + if (HasDelegate(iface) == false) return ""; - stream << Tab(3) << "private enum DelegateId : int" << NLine(1); - GenBrace(stream, TAB_SIZE * 3, [&]() { - int cnt = 1; - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() != Declaration::MethodType::DELEGATE) continue; - stream << Tab(4) << i->GetID() << " = " << cnt++ << "," << NLine(1); - } - }); - stream << NLine(1); -} + int cnt = 1; + return ReplaceAll(CB_DELEGATE_IDS) + .Repeat( + "DELEGATE_IDS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { + if (i->GetMethodType() != Declaration::MethodType::DELEGATE) + return false; -void CsGeneratorBase::GenDeclaration(std::ofstream& stream, - const Declaration& decl, bool semicol) { - stream << GetDeclaration(decl, semicol); + r.Replace("ID", i->GetID()).Replace("VAL", std::to_string(cnt++)); + return true; + }) + .ToString(); } std::string CsGeneratorBase::GetDeclaration(const Declaration& decl, @@ -484,11 +353,6 @@ std::string CsGeneratorBase::GetDeclaration(const Declaration& decl, return ret; } -void CsGeneratorBase::GenParameters(std::ofstream& stream, - const Parameters& ps) { - stream << GetParameters(ps); -} - bool CsGeneratorBase::HasDelegate(const Interface& iface) { bool found = false; for (const auto& i : iface.GetDeclarations()) { @@ -523,29 +387,16 @@ std::string CsGeneratorBase::GetParameters(const Parameters& ps) { return ret; } -std::string CsGeneratorBase::GenUnitMapParamType(const BaseType& base_type, - const std::string& type_str) { - std::string ret; - if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) - ret = "CallbackBase"; - else if (base_type.GetFullName() == "[REMOTE_EXCEPTION]") - ret = "RemoteException"; - else - ret = type_str; - - return ret; -} - std::string CsGeneratorBase::GenUnitMapParamInit(const Interface& iface, const BaseType& base_type, const std::string& type_str) { std::string ret; - if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) { + if (type_str == "CallbackBase") { ret = "value = new CallbackBase(1, false);"; } else if (type_str == "string") { ret = "value = new string(\"\");"; - } else if (type_str == "[REMOTE_EXCEPTION]") { + } else if (type_str == "RemoteException") { ret = "value = new RemoteException();"; } else { ret = "value = new " + type_str + "();"; @@ -557,8 +408,7 @@ std::string CsGeneratorBase::GenUnitMapParamInit(const Interface& iface, std::string CsGeneratorBase::GetEnumTypeString(const BaseType& base_type) { std::string ret = base_type.GetFullName(true); auto pos = ret.find('.'); - if (pos == std::string::npos) - return ret; + if (pos == std::string::npos) return ret; std::string block_id = ret.substr(0, pos); std::string type_id = ret.substr(pos + 1, ret.size() - (pos + 1)); @@ -595,9 +445,8 @@ std::string CsGeneratorBase::GetStructTypeString(const BaseType& base_type) { std::string CsGeneratorBase::GetTypeString(const BaseType& base_type) { std::string ret; - if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr && - base_type.GetKeyType() == nullptr) { - if (base_type.ToString() == "file") + if (IsDefaultType(&base_type)) { + if (base_type.ToString() == "file" || base_type.ToString() == "bundle") ret = base_type.ToString(); else ret = ConvertTypeToString(base_type); @@ -618,16 +467,14 @@ std::string CsGeneratorBase::GenUnitMaUintWrite(const Interface& iface, const BaseType& base_type) { std::string ret; - if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr && - base_type.GetKeyType() == nullptr) { - ret = "unit.parcel.Write" + ConvertTypeToParcelType(base_type.ToString()) + - "(value);"; - } else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { - ret = "unit.parcel.WriteInt((int)value);"; + if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { + ret = "parcel.WriteInt((int)value);"; } else if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) { - ret = "CallbackBase.Serialize(unit.parcel, value);"; + ret = "CallbackBase.Serialize(parcel, value);"; + } else if (IsDefaultType(&base_type)) { + ret = "parcel.Write" + ConvertTypeToParcelType(&base_type) + "(value);"; } else { - ret = iface.GetID() + ".Serialize(unit.parcel, value);"; + ret = iface.GetID() + ".Serialize(parcel, value);"; } return ret; @@ -638,15 +485,14 @@ std::string CsGeneratorBase::GenUnitMaUintRead(const Interface& iface, const std::string& type_str) { std::string ret; - if (!base_type.IsUserDefinedType() && base_type.GetMetaType() == nullptr && - base_type.GetKeyType() == nullptr) { - ret = "value = unit.parcel.Read" + - ConvertTypeToParcelType(base_type.ToString()) + "();"; - } else if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { + if (base_type.GetUserDefinedType() == BaseType::UserType::ENUM) { ret = "int enum_int = unit.parcel.ReadInt();" + NLine(1) + Tab(1) + "value = (" + type_str + ")enum_int;"; } else if (base_type.GetUserDefinedType() == BaseType::UserType::DELEGATE) { ret = "CallbackBase.Deserialize(unit.parcel, value);"; + } else if (IsDefaultType(&base_type)) { + ret = "value = unit.parcel.Read" + ConvertTypeToParcelType(&base_type) + + "();"; } else { ret = iface.GetID() + ".Deserialize(unit.parcel, value);"; } @@ -658,18 +504,6 @@ void CsGeneratorBase::AddTypeMap( std::unordered_map& types, const BaseType& base_type, const std::string& st_id) { types[ConvertTypeToString(base_type, st_id)] = base_type; - - if (base_type.GetMetaType() != nullptr) { - AddTypeMap(types, *base_type.GetMetaType(), st_id); - } - - if (base_type.GetKeyType() != nullptr) { - AddTypeMap(types, *base_type.GetKeyType(), st_id); - - if (base_type.GetValueType() != nullptr) { - AddTypeMap(types, *base_type.GetValueType(), st_id); - } - } } std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { @@ -679,8 +513,10 @@ std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { BaseType bB("bool", "bool"); if (GetChannelType() != ChannelType::TYPE_GROUP) { - BaseType bE("[REMOTE_EXCEPTION]", "", BaseType::UserType::STRUCTURE); + BaseType bCB("CallbackBase", "", BaseType::UserType::DELEGATE); + BaseType bE("RemoteException", "", BaseType::UserType::STRUCTURE); types[bE.GetFullName()] = bE; + types[bCB.GetFullName()] = bCB; } types[bI.GetFullName()] = bI; @@ -689,38 +525,14 @@ std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { for (const auto& i : iface.GetDeclarations()) { types[ConvertTypeToString(i->GetType())] = i->GetType(); - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) { - BaseType del(i->GetID(), "", BaseType::UserType::DELEGATE); - AddTypeMap(types, del); - } for (const auto& j : i->GetParameters()) { auto& type = j->GetParameterType().GetBaseType(); + if (type.GetUserDefinedType() == BaseType::UserType::DELEGATE) continue; AddTypeMap(types, type); } } - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() == Block::TYPE_STRUCTURE) { - const Structure& st = static_cast(*i); - for (const auto& i : st.GetElements()) { - auto& type = i->GetType(); - - std::string name = type.GetFullName(); - std::size_t pos = name.find('.'); - - if (pos == std::string::npos) { - AddTypeMap(types, type, st.GetID()); - } else { - std::string st_id = name.substr(0, pos); - std::string enum_id = name.substr(pos + 1, name.size() - (pos + 1)); - BaseType enum_type(enum_id, "", BaseType::UserType::ENUM); - AddTypeMap(types, enum_type, st_id); - } - } - } - } - types.erase("void"); std::string code = ""; @@ -734,133 +546,88 @@ std::string CsGeneratorBase::GenUnitMapReadWrite(const Interface& iface) { else unit_type = GetTypeString(base_type); - code += - ReplaceAll(CB_UNITMAP_READ_READ_BASE) - .Change("", GenUnitMapParamType(base_type, type_str)) - .Change("", - GenUnitMapParamInit(iface, base_type, type_str)) - .Change("", unit_type) - .Change("", - GenUnitMaUintRead(iface, base_type, type_str)); + if (base_type.ToString() == "RemoteException") + unit_type = "remote_exception"; + code += ReplaceAll(CB_UNITMAP_READ_WRITE_BASE) - .Change("", GenUnitMapParamType(base_type, type_str)) - .Change("", unit_type) - .Change("", GenUnitMaUintWrite(iface, base_type)); + .Replace("PARAM_TYPE", type_str) + .Replace("PARAM_INIT", + GenUnitMapParamInit(iface, base_type, type_str)) + .Replace("UNIT_TYPE", unit_type) + .Replace("HASH_UNIT_TYPE", GetHashCodeStr(unit_type)) + .Replace("UINT_READ", GenUnitMaUintRead(iface, base_type, type_str)) + .Replace("UINT_WRITE", GenUnitMaUintWrite(iface, base_type)); } return AddIndent(TAB_SIZE, code); } -void CsGeneratorBase::GenUnitMap(std::ofstream& stream, const Interface& iface, - int tabSize) { - stream << GetUnitMap(iface, tabSize); -} - std::string CsGeneratorBase::GetUnitMap(const Interface& iface, int tabSize) { - std::string ret_string(CB_UNIT_BASE); - ret_string += ReplaceAll(CB_UNITMAP_BASE) - .Change("", GenUnitMapReadWrite(iface)); - return AddIndent(TAB_SIZE * tabSize, ret_string); -} - -void CsGeneratorBase::GenCallbacks(std::ofstream& stream, - const Interface& iface, bool is_proxy) { - if (HasDelegate(iface) == false) - return; - stream << CB_CALLBACK_BASE; - - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() != Declaration::MethodType::DELEGATE) - continue; - GenCallback(stream, *i, iface.GetID(), is_proxy); - } + std::string ret = ReplaceAll(CB_UNITMAP_BASE) + .Replace("PARAM_READ_WRITE", GenUnitMapReadWrite(iface)) + .ToString(); + return AddIndent(TAB_SIZE * tabSize, ret); } -void CsGeneratorBase::GenCallback(std::ofstream& stream, - const Declaration& decl, - const std::string& id, bool is_proxy) { - stream << Tab(3) << "public sealed class " << decl.GetID() - << " : CallbackBase" << NLine(1); - GenBrace(stream, TAB_SIZE * 3, [&]() { - if (is_proxy) { - GenTemplate( - CB_CALLBACK_CTOR_PROXY, stream, - [&]() -> std::string { return decl.GetID(); }, - [&]() -> std::string { return decl.GetID(); }); - } else { - GenTemplate( - CB_CALLBACK_CTOR_STUB, stream, - [&]() -> std::string { return decl.GetID(); }, - [&]() -> std::string { return decl.GetID(); }, - [&]() -> std::string { return decl.GetID(); }); - } - stream << NLine(1); - - if (is_proxy) { - stream << Tab(4) << "public delegate void Callback("; - GenParameters(stream, decl.GetParameters()); - stream << ");" << NLine(1); - stream << Tab(4) << "public event Callback Received;" << NLine(2); - GenReceivedEvent(stream, decl, id); - } else { - stream << CB_CALLBACK_STUB_MEMBERS; - GenInvokeMethod(stream, decl, id); - } - }); - stream << NLine(1); -} +std::string CsGeneratorBase::GetCallbacks(const Interface& iface, + bool is_proxy) { + auto callbacks = [&](auto& r, const auto& i) { + if (i->GetMethodType() != Declaration::MethodType::DELEGATE) return false; -void CsGeneratorBase::GenReceivedEvent(std::ofstream& stream, - const Declaration& decl, - const std::string& id) { - stream << Tab(4) << "internal override void OnReceivedEvent(UnitMap map)" - << NLine(1); - GenBrace(stream, TAB_SIZE * 4, [&]() { int cnt = 1; - for (const auto& i : decl.GetParameters()) { - std::string v = "param" + std::to_string(cnt); - std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), v, i->GetID(), "map", true, id); - stream << AddIndent(TAB_SIZE * 5, c); - cnt++; - } + auto serializer = [&](auto& r, const auto& i) { + r.Replace("HASH_ID", GetHashCodeStr(i->GetID())) + .Replace("ID", i->GetID()); + return true; + }; + + auto params = [&]() { + std::string m; + int cnt = 1; + for (const auto& j : i->GetParameters()) { + (void)j; + if (cnt != 1) { + m += ", "; + } - cnt = 1; - stream << NLine(1) << Tab(5) << "Received?.Invoke("; - for (auto i = decl.GetParameters().begin(); i != decl.GetParameters().end(); - ++i) { - if (cnt != 1) { - stream << ", "; + m += "param" + std::to_string(cnt); + cnt++; } - std::string v = "param" + std::to_string(cnt); - stream << v; - cnt++; - } - stream << ");" << NLine(1); - }); - stream << NLine(1); -} - -void CsGeneratorBase::GenInvokeMethod(std::ofstream& stream, - const Declaration& decl, - const std::string& id) { - GenTemplate( - CB_CALLBACK_INVOKE_METHOD, stream, - [&]() -> std::string { return GetParameters(decl.GetParameters()); }, - [&]() -> std::string { - std::string m; - for (const auto& i : decl.GetParameters()) { - m += ConvertTypeToSerializer(i->GetID(), i->GetID(), "map"); - } - return AddIndent(TAB_SIZE * 5, m); - }); -} -void CsGeneratorBase::GenVersion(std::ofstream& stream) { - GenTemplate(::cs_cb_version, stream, - [&]() -> std::string { return FULLVER; }); - stream << NLine(1); + return m; + }; + + auto param_decl = [&](auto& r, const auto& i) { + std::string v = "param" + std::to_string(cnt++); + r.Replace("HASH_PARAM", GetHashCodeStr(i->GetID())) + .Replace("PARAM", i->GetID()) + .Replace("PARAM_TYPE", + ConvertTypeToString(i->GetParameterType().GetBaseType())) + .Replace("VAL", v); + + return true; + }; + + r.Remove("PROXY", !is_proxy) + .Remove("STUB", is_proxy) + .Repeat("PARAMS_WRITE", i->GetParameters(), serializer) + .Repeat("PARAMS_DECL", i->GetParameters(), param_decl) + .Replace("CLASS_ID", i->GetID()) + .Replace("CALLBACK_PARMS", GetParameters(i->GetParameters())) + .Replace("PARAMS", params) + .Replace("HASH_METHOD", "[METHOD]"_hash) + .Replace("HASH_DELEGATE", "delegate"_hash); + return true; + }; + + return ReplaceAll(CB_CALLBACK_BASE) + .Repeat("CALLBACKS", iface.GetDeclarations(), callbacks) + .ToString(); +} + +std::string CsGeneratorBase::GetRemoteExceptionSerializer() { + return CB_REMOTE_EXCEPTION_SERIALIZE; } } // namespace version2 diff --git a/idlc/gen/version2/cs_generator_base.h b/idlc/gen/version2/cs_generator_base.h index 39ffa75c..bcfbc18c 100644 --- a/idlc/gen/version2/cs_generator_base.h +++ b/idlc/gen/version2/cs_generator_base.h @@ -34,82 +34,54 @@ class CsGeneratorBase : public Generator { explicit CsGeneratorBase(std::shared_ptr doc); virtual ~CsGeneratorBase() = default; - void GenVersion(std::ofstream& stream); - void GenStructures(std::ofstream& stream); - void GenStructure(std::ofstream& stream, const Structure& st); - void GenSerializer(std::ofstream& stream); + std::string GetStructures(); std::string GetSerializer(); - void GenSerializer(std::ofstream& stream, const Structure& st); - std::string GetSerializer(const Structure& st); - void GenListSerializer(std::ofstream& stream); std::string GetListSerializer(); - std::string GetListSerializer(const BaseType& type, - const std::string& st_id = ""); - void GenMethodId(std::ofstream& stream, const Interface& iface); + std::string GetRemoteExceptionSerializer(); std::string GetMethodId(const Interface& iface); - void GenDelegateId(std::ofstream& stream, const Interface& iface); - void GenDeclaration(std::ofstream& stream, const Declaration& decl, - bool semicol = true); + std::string GetDelegateId(const Interface& iface); std::string GetDeclaration(const Declaration& decl, bool semicol = true); - void GenParameters(std::ofstream& stream, const Parameters& ps); - void GenCallbacks(std::ofstream& stream, const Interface& iface, - bool is_proxy); + std::string GetCallbacks(const Interface& iface, bool is_proxy); std::string ConvertTypeToString(const BaseType& type, const std::string& st_id = ""); - std::string ConvertTypeToDeserializer(const BaseType& type, - std::string gen_id, - std::string parma_id, std::string map, - bool make_new_type = true, - const std::string iface_id = ""); - std::string ConvertTypeToSerializer(std::string gen_id, std::string param_id, - std::string map); std::string ConvertTypeToParcelType(const std::string& key); + std::string ConvertTypeToParcelType(const BaseType* type); std::string GetParameters(const Parameters& ps); std::string Tab(int cnt); std::string NLine(int cnt); - void GenUnitMap(std::ofstream& stream, const Interface& iface, int tabSize); std::string GetUnitMap(const Interface& iface, int tabSize); std::string GetTypeString(const BaseType& base_type); - void GenUsingNamespace(std::ofstream& stream); - void GenEnum(std::ofstream& stream, const Block& block, int tab); - std::string GenEnum(const Block& block, int tab_size); + std::string GetEnum(const Block& block, int tab_size); bool HasDelegate(const Interface& iface); - void GenRemoteException(std::ofstream& stream); + std::string GetRemoteException(); protected: const int TAB_SIZE = 4; private: + std::string GetSerializer(const Structure& st); + std::string GetListSerializer(const BaseType& type, + const std::string& st_id = ""); std::string GetEnumTypeString(const BaseType& base_type); void AddTypeMap(std::unordered_map& types, const BaseType& base_type, const std::string& st_id = ""); void GenWriteBundle(std::ofstream& stream, const std::string& id); void AddSerializerList(const BaseType& type, const std::string& st_id = ""); - void GenCallback(std::ofstream& stream, const Declaration& decl, - const std::string& id, bool is_proxy); void GenReceivedEvent(std::ofstream& stream, const Declaration& decl, const std::string& id); - void GenInvokeMethod(std::ofstream& stream, const Declaration& decl, - const std::string& id); std::string GenUnitMapReadWrite(const Interface& iface); - std::string GenUnitMapParamType(const BaseType& base_type, - const std::string& type_str); std::string GenUnitMaUintRead(const Interface& iface, const BaseType& base_type, const std::string& type_str); std::string GenUnitMaUintWrite(const Interface& iface, const BaseType& base_type); - std::string GenListWrite(const BaseType& type, const std::string& type_str); - std::string GenListRead(const BaseType& type, const std::string& type_str); - std::string GetLengthName(const BaseType& type); std::string GenUnitMapParamInit(const Interface& iface, const BaseType& base_type, const std::string& type_str); - std::string GetStructureSerialize(const Structure& st); - std::string GetStructureDeserialize(const Structure& st); std::string GetStructTypeString(const BaseType& base_type); std::string GetStructTypeString(const Structure& st); + bool IsDefaultType(const BaseType* type) const; private: std::map type_map_; diff --git a/idlc/gen/version2/cs_generator_base_cb.h b/idlc/gen/version2/cs_generator_base_cb.h index 60aae641..dba833eb 100644 --- a/idlc/gen/version2/cs_generator_base_cb.h +++ b/idlc/gen/version2/cs_generator_base_cb.h @@ -17,104 +17,74 @@ #ifndef IDLC_CS_GEN_VERSION2_CS_GENRATOR_BASE_CB_H_ #define IDLC_CS_GEN_VERSION2_CS_GENRATOR_BASE_CB_H_ -const char CB_USING_NAMESPACE[] = +const char CB_STRUCTES[] = R"__cs_cb( -using System; -using System.Collections.Generic; -using System.Collections.Concurrent; -using System.Threading; -using System.Reflection; -using Tizen.Applications; -using Tizen.Applications.RPCPort; + +public class : -)__cs_cb"; - -const char CB_REMOTE_EXCEPTION_BASE[] = -R"__cs_cb( - public class RemoteException : Exception - { - public RemoteException() - { - } - - public RemoteException(string message) - { - this.Message = message; - } +{ + - public RemoteException(string message, int cause) - { - this.Message = message; - this.Cause = cause; - } + + public ; + - public new string Message { get; set; } - public int Cause { get; set; } + public () + { + + = new (); + } +} -)__cs_cb"; + -const char CB_REMOTE_EXCEPTION_SERIALIZE[] = -R"__cs_cb( - private static void Serialize(Parcel h, RemoteException param) - { - UnitMap map = new UnitMap(); - map.Write("cause", param.Cause); - map.Write("message", param.Message); - - map.Serialize(h); - } - - private static void Deserialize(Parcel h, RemoteException param) - { - UnitMap map = new UnitMap(); - map.Deserialize(h); - map.Read("cause", out int cause); - param.Cause = cause; - map.Read("message", out string message); - param.Message = message; - } )__cs_cb"; -const char CB_UNIT_BASE[] = +const char CB_REMOTE_EXCEPTION_BASE[] = R"__cs_cb( -internal class Unit +public class RemoteException : Exception { - internal string name; - internal string type; - internal Parcel parcel; - internal Unit() + public RemoteException() { - parcel = new Parcel(false); } - internal void Serialize(Parcel h) + public RemoteException(string message) { - h.WriteString(name); - h.WriteString(type); - - byte[] raw = parcel.ToBytes(); - h.WriteArrayCount(raw.Length & int.MaxValue); - h.Write(raw); + this.Message = message; } - internal void Deserialize(Parcel h) + public RemoteException(string message, int cause) { - name = h.ReadString(); - type = h.ReadString(); - - int size = h.ReadArrayCount(); - byte[] raw = h.Read(size); - parcel.Write(raw); + this.Message = message; + this.Cause = cause; } + + public new string Message { get; set; } + public int Cause { get; set; } } + )__cs_cb"; +const char CB_REMOTE_EXCEPTION_SERIALIZE[] = +R"__cs_cb( +private static void Serialize(Parcel h, RemoteException param) +{ + h.WriteInt(param.Cause); + h.WriteString(param.Message); +} + +private static void Deserialize(Parcel h, RemoteException param) +{ + param.Cause = h.ReadInt(); + param.Message = h.ReadString(); +} +)__cs_cb"; -const char CB_UNITMAP_READ_READ_BASE[] = +const char CB_UNITMAP_READ_WRITE_BASE[] = R"__cs_cb( -internal void Read(string name, out value) +internal void Read(int name, out value) { if (map.ContainsKey(name) == false) @@ -124,41 +94,88 @@ internal void Read(string name, out value) } Unit unit = map[name]; - if (unit.type != "") + if (unit.type != /**/) { Tizen.Log.Error("RPC_PORT", "type is not : " + unit.type); return; } } -)__cs_cb"; -const char CB_UNITMAP_READ_WRITE_BASE[] = -R"__cs_cb( -internal void Write(string name, value) +internal void Write(int name, value) { Unit unit = new Unit(); unit.name = name; - unit.type = ""; - + unit.type = ; /**/ + unit.cmds.Add((parcel) => + { + + }); + map[name] = unit; } )__cs_cb"; const char CB_UNITMAP_BASE[] = R"__cs_cb( +internal class Unit +{ + internal int name; + internal int type; + internal Parcel parcel; + internal List> cmds = new List>(); + internal Unit() + { + } + + internal void Serialize(Parcel h) + { + h.WriteInt(name); + h.WriteInt(type); + + var sizePos = h.GetDataSize(); + h.Reserve(4); + + foreach(var cb in cmds) + { + cb(h); + } + + var curPos = h.GetDataSize(); + var size = curPos - sizePos - 4; + h.SetDataSize(sizePos); + h.WriteInt((int)size); + h.SetDataSize(curPos); + } + + internal void Deserialize(Parcel h) + { + name = h.ReadInt(); + type = h.ReadInt(); + var size = h.ReadArrayCount(); + if (size == 0) + return; + + var startPos = h.GetReader(); + parcel = new Parcel(h, startPos, (uint)size); + parcel.Reserve((uint)size); + h.SetReader(startPos + (uint)size); + } +} + internal class UnitMap { - internal Dictionary map; + internal Dictionary map; internal UnitMap() { - map = new Dictionary(); + map = new Dictionary(); } internal void Serialize(Parcel parcel) { parcel.WriteArrayCount(map.Count & int.MaxValue); - foreach (Unit unit in map.Values) { + foreach (Unit unit in map.Values) + { unit.Serialize(parcel); } } @@ -166,8 +183,10 @@ internal class UnitMap internal void Deserialize(Parcel parcel) { int size = parcel.ReadArrayCount(); + parcel.Pin(); - for (int i = 0; i < size; i++) { + for (int i = 0; i < size; i++) + { Unit unit = new Unit(); unit.Deserialize(parcel); @@ -175,241 +194,313 @@ internal class UnitMap } } - internal bool ContainsName(string name) + internal bool ContainsName(int name) { return map.ContainsKey(name); } + } )__cs_cb"; const char CB_CALLBACK_BASE[] = R"__cs_cb( - public class CallbackBase - { - internal int Id; - internal int SeqId; - internal bool Once; - private static volatile int _seqNum = 0; - - public string Tag - { - get - { - return Id.ToString() + "::" + SeqId.ToString(); - } - } - - public CallbackBase(int delegateId, bool once) - { - Id = delegateId; - SeqId = _seqNum++; - Once = once; - } - - internal virtual void OnReceivedEvent(UnitMap map) {} - - internal static void Serialize(Parcel h, CallbackBase param) - { - UnitMap map = new UnitMap(); - map.Write("id", param.Id); - map.Write("seq_id", param.SeqId); - map.Write("once", param.Once); - map.Serialize(h); - } - - internal static void Deserialize(Parcel h, CallbackBase param) - { - UnitMap map = new UnitMap(); - map.Deserialize(h); - map.Read("id", out param.Id); - map.Read("seq_id", out param.SeqId); - map.Read("once", out param.Once); - } - } +public class CallbackBase +{ + internal int Id; + internal int SeqId; + internal bool Once; + private static volatile int _seqNum = 0; -)__cs_cb"; + public string Tag + { + get + { + return Id.ToString() + "::" + SeqId.ToString(); + } + } -const char CB_CALLBACK_CTOR_PROXY[] = -R"__cs_cb( public $$(bool once = false) : base((int)DelegateId.$$, once) - { - } -)__cs_cb"; + public CallbackBase(int delegateId, bool once) + { + Id = delegateId; + SeqId = _seqNum++; + Once = once; + } -const char CB_CALLBACK_CTOR_STUB[] = -R"__cs_cb( internal $$(Port port, WeakReference service) : base((int)DelegateId.$$, false) - { - _port = port; - _service = service; - } - - internal $$(Port port, WeakReference service, CallbackBase cb) : base(cb.Id, cb.Once) - { - _port = port; - _service = service; - SeqId = cb.SeqId; - } -)__cs_cb"; + internal virtual void OnReceivedEvent(UnitMap map) {} -const char CB_CALLBACK_STUB_MEMBERS[] = -R"__cs_cb( private Port _port; - private WeakReference _service; - private bool _valid = true; - internal LocalExecution _lem; -)__cs_cb"; + internal static void Serialize(Parcel h, CallbackBase param) + { + h.WriteInt(param.Id); + h.WriteInt(param.SeqId); + h.WriteBool(param.Once); + } -const char CB_CALLBACK_INVOKE_METHOD[] = -R"__cs_cb( - public void Invoke($$) - { - if (_port == null && _lem == null) - throw new NotConnectedSocketException(); - if (!_service.IsAlive) - throw new InvalidProtocolException(); - if (Once && !_valid) - throw new InvalidCallbackException(); - if (_lem != null && _lem._online == false) - throw new NotConnectedSocketException(); - - var p = new Parcel(); - UnitMap map = new UnitMap(); - - map.Write("[METHOD]", (int)MethodId.__Callback); - map.Write("delegate", this); - -$$ - map.Serialize(p); - - if (_lem != null) - { - _lem.SendRequest(p); - } else - { - // Send - p.Send(_port); - } - _valid = false; - } -)__cs_cb"; + internal static void Deserialize(Parcel h, CallbackBase param) + { + param.Id = h.ReadInt(); + param.SeqId = h.ReadInt(); + param.Once = h.ReadBool(); + } +} -const char CB_PROPERTY[] = R"__cs_cb( public $$ $$; -)__cs_cb"; + +public sealed class : CallbackBase +{ + + public (bool once = false) : base((int)DelegateId., once) + { + } -const char CB_CTOR[] = R"__cs_cb( - public $$() + public delegate void Callback(); + public event Callback Received; + + internal override void OnReceivedEvent(UnitMap map) + { + + map.Read( /**/, out ); + + + Received?.Invoke(); + } + + + + internal (Port port, WeakReference service) : base((int)DelegateId., false) + { + _port = port; + _service = service; + } + + internal (Port port, WeakReference service, CallbackBase cb) : base(cb.Id, cb.Once) + { + _port = port; + _service = service; + SeqId = cb.SeqId; + } + + private Port _port; + private WeakReference _service; + private bool _valid = true; + internal LocalExecution _lem; + + public void Invoke() + { + if (_port == null && _lem == null) + throw new NotConnectedSocketException(); + if (!_service.IsAlive) + throw new InvalidProtocolException(); + if (Once && !_valid) + throw new InvalidCallbackException(); + if (_lem != null && _lem._online == false) + throw new NotConnectedSocketException(); + + var p = new Parcel(); + UnitMap map = new UnitMap(); + int methodId = (int)MethodId.__Callback; + + map.Write( /*[METHOD]*/, methodId); + map.Write( /*delegate*/, this); + + map.Write( /**/, ); + + map.Serialize(p); + + if (_lem != null) + { + _lem.SendRequest(p); + } else { -$$ + // Send + p.Send(_port); } -)__cs_cb"; - -const char CB_DLELGATGE_CREATE[] = -R"__cs_cb( -Port cb_port; -if (b._lem != null) - cb_port = null; -else - cb_port = GetPort(Port.Type.Callback, instance); + _valid = false; + } + +} - = new (cb_port, new WeakReference(b), del_); + -if (b._lem != null) - param3._lem = b._lem; )__cs_cb"; - -const char CB_WRITE_BUNDLE[] = +const char CB_ENUMS[] = R"__cs_cb( - if (param.$$ != null) - { - h.WriteBundle(param.$$); - } - else - { - h.WriteBundle(new Bundle()); - } + +public enum +{ + + + = , + + + , + + + +} + + )__cs_cb"; const char CB_METHOD_IDS[] = R"__cs_cb( - private enum MethodId : int - { - __Result = 0, - __Callback = 1, - - } +private enum MethodId : int +{ + __Result = 0, + __Callback = 1, + + = , + +} )__cs_cb"; -const char CB_SERIALIZER[] = +const char CB_DELEGATE_IDS[] = R"__cs_cb( - private static void Serialize(Parcel h, param) - { - UnitMap map = new UnitMap(); - - map.Serialize(h); - } +private enum DelegateId : int +{ + + = , + +} )__cs_cb"; -const char CB_DESERIALIZER[] = +const char CB_STRUCT_SERIALIZER[] = R"__cs_cb( - private static void Deserialize(Parcel h, param) - { - UnitMap map = new UnitMap(); - map.Deserialize(h); - - } -)__cs_cb"; - -constexpr const char CB_LIST_MAP_WRITE[] = -R"__cs_cb( map.Write("key-" + index.ToString(), i.Key); - map.Write("value-" + index.ToString(), i.Value);)__cs_cb"; - -constexpr const char CB_LIST_SET_WRITE[] = -R"__cs_cb( map.Write("key-" + index.ToString(), i);)__cs_cb"; - -constexpr const char CB_LIST_WRITE[] = -R"__cs_cb( map.Write(index.ToString(), i);)__cs_cb"; - -constexpr const char CB_LIST_MAP_READ[] = -R"__cs_cb( map.Read("key-" + index.ToString(), out key); - map.Read("value-" + index.ToString(), out value); - param[key] = value;)__cs_cb"; - -constexpr const char CB_LIST_SET_READ[] = -R"__cs_cb( map.Read("key-" + index.ToString(), out value); - param.Add(value);)__cs_cb"; +private static void Serialize(Parcel h, param) +{ + + Serialize(h, ()param); + + + + Serialize(h, param.); + + + h.Write(param.); + + +} -constexpr const char CB_LIST_READ[] = -R"__cs_cb( map.Read("key-" + index.ToString(), out value); - param.Add(value);)__cs_cb"; +private static void Deserialize(Parcel h, param) +{ + + Deserialize(h, ()param); + + + + param. = new (); + Deserialize(h, param.); + + + var = h.Read(); + param. = ; + + +} +)__cs_cb"; const char CB_LIST_SERIALIZER[] = R"__cs_cb( - private static void Serialize(Parcel h, param) - { - UnitMap map = new UnitMap(); - map.Write("", param.Count & int.MaxValue); - int index = 0; - foreach (var i in param) - { - - index++; - } - map.Serialize(h); - } -)__cs_cb"; +private static void Serialize(Parcel h, param) +{ + h.WriteArrayCount(param.Count); + foreach (var i in param) + { + + + h.Write(i.Key); + + + Serialize(h, i.Key); + + + h.Write(i.Value); + + + Serialize(h, i.Value); + + + + + h.Write(i); + + + Serialize(h, i); + + + + + h.Write(i); + + + Serialize(h, i); + + + + + h.Write(i); + + + Serialize(h, i); + + + } +} -const char CB_LIST_DESERIALIZER[] = -R"__cs_cb( - private static void Deserialize(Parcel h, param) - { - UnitMap map = new UnitMap(); - map.Deserialize(h); - map.Read("", out int length); - for (int index = 0; index < length; index++) - { - - } - } +private static void Deserialize(Parcel h, param) +{ + int l = h.ReadArrayCount(); + for (int i = 0; i < l; i++) + { + + + var k = h.Read(); + + + var k = new (); + Deserialize(h, k); + + + var v = h.Read(); + + + var v = new (); + Deserialize(h, v); + + param.Add(k, v); + + + + var k = h.Read(); + + + var k = new (); + Deserialize(h, k); + + param.Add(k); + + + + var k = h.Read(); + + + var k = new (); + Deserialize(h, k); + + param.AddLast(k); + + + + var k = h.Read(); + + + var k = new (); + Deserialize(h, k); + + param.Add(k); + + } +} )__cs_cb"; diff --git a/idlc/gen/version2/cs_group_generator.cc b/idlc/gen/version2/cs_group_generator.cc index 747062c4..933c3e94 100644 --- a/idlc/gen/version2/cs_group_generator.cc +++ b/idlc/gen/version2/cs_group_generator.cc @@ -27,114 +27,80 @@ CsGroupGen::CsGroupGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsGroupGen::OnInitGen(std::ofstream& stream) { SetChannelType(Generator::ChannelType::TYPE_GROUP); - GenVersion(stream); - GenUsingNamespace(stream); - stream << CB_USING_NAMESPACE_GROUP; - - GenNamespace(stream); + auto interfaces = [&](auto& r, const auto& i) { + if (i->GetType() != Block::TYPE_INTERFACE) return false; + auto& iface = static_cast(*i); + + r.Replace("ENUMS", GetEnum(iface, 4)) + .Replace("UNITMAP", GetUnitMap(iface, 4)) + .Replace("METHOD_IDS", AddIndent(TAB_SIZE * 4, GetMethodId(iface))) + .Replace("SERIALIZER", AddIndent(TAB_SIZE * 4, GetSerializer())) + .Replace("LIST_SERIALIZER", + AddIndent(TAB_SIZE * 4, GetListSerializer())) + .Repeat("METHODS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { return RepeatMethods(r, i); }) + .Repeat("DISPATCHERS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { return RepeatDispatchers(r, i); }) + .Replace("CLS_NAME", iface.GetID()); + + return true; + }; + + ReplaceAll(CB_MAIN_GROUP) + .Replace("FULLVER", std::string(FULLVER)) + .Replace("FILE_NAMESPACE", GetFileNamespace()) + .Replace("STRUCTRES", AddIndent(TAB_SIZE * 2, GetStructures())) + .Repeat("INTERFACES", GetDocument().GetBlocks(), interfaces) + .Replace("HASH_METHOD", "[METHOD]"_hash) + .Replace("HASH_INT", "int"_hash) + .Out(stream); } void CsGroupGen::OnFiniGen(std::ofstream& stream) {} -void CsGroupGen::GenNamespace(std::ofstream& stream) { - stream << NLine(1); - stream << "namespace RPCPort" << NLine(1); - GenBrace(stream, 0, [&]() { - stream << "namespace " << GetFileNamespace() << NLine(1); - GenBrace(stream, 0, [&]() { - GenStructures(stream); - stream << Tab(1) << "namespace Group" << NLine(1); - GenBrace(stream, TAB_SIZE, [&]() { - GenInterfaces(stream); +bool CsGroupGen::RepeatMethods(ReplaceAll& r, + const std::unique_ptr& i) { + r.Replace("PARAMS", GetParameters(i->GetParameters())) + .Replace("COMMENTS", i->GetComments().empty() + ? "" + : AddIndent(TAB_SIZE * 3, i->GetComments())) + .Replace("DECL", GetDeclaration(*i, false)) + .Replace("METHOD_ID", i->GetID()) + .Repeat("PARAMS", i->GetParameters(), [&](auto& r, const auto& i) { + auto& pt = i->GetParameterType(); + if (pt.GetDirection() == ParameterType::Direction::OUT) return false; + r.Replace("ID", i->GetID()) + .Replace("HASH_ID", GetHashCodeStr(i->GetID())); + + return true; }); - }); - }); -} - -void CsGroupGen::GenInterfaces(std::ofstream& stream) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_INTERFACE) continue; - Interface& iface = static_cast(*i); - GenInterface(stream, iface); - stream << NLine(1); - } -} - -void CsGroupGen::GenInterface(std::ofstream& stream, const Interface& iface) { - ReplaceAll(CB_INTERFACE_FULL) - .Change("", iface.GetID()) - .Change("", GenEnum(iface, 3)) - .Change("", GetUnitMap(iface, 3)) - .Change("", GetMethodId(iface)) - .Change("", GetSerializer()) - .Change("", GetListSerializer()) - .Change("", GetMethods(iface)) - .Change("", GetDispatchers(iface)) - .Out(stream); -} - -std::string CsGroupGen::GetDispatchers(const Interface& iface) { - std::string ret; - for (const auto& i : iface.GetDeclarations()) { - ret += ReplaceAll(CB_DISPATCHER) - .Change("", i->GetID()) - .Change("", GetParamDeserializer(*i)) - .Change("", GetCallParams(*i)); - } - - return ret; -} - -std::string CsGroupGen::GetMethods(const Interface& iface) { - std::string ret; - auto& decls = iface.GetDeclarations(); - for (const auto& i : decls) { - ret += ReplaceAll(CB_METHOD) - .Change("", GetParameters(i->GetParameters())) - .Change("", - i->GetComments().empty() - ? "" - : AddIndent(TAB_SIZE * 3, i->GetComments())) - .Change("", GetDeclaration(*i, false)) - .Change("", i->GetID()) - .Change("", GetParamSerializer(*i)) - .Change("", iface.GetID()); - } - - return ret; -} - -std::string CsGroupGen::GetParamSerializer(const Declaration& decl) { - std::string m; - for (const auto& i : decl.GetParameters()) { - auto& pt = i->GetParameterType(); - if (pt.GetDirection() == ParameterType::Direction::OUT) - continue; - m += Tab(5) + ConvertTypeToSerializer(i->GetID(), i->GetID(), "map"); - } - return m; + return true; } -std::string CsGroupGen::GetParamDeserializer(const Declaration& decl) { +bool CsGroupGen::RepeatDispatchers(ReplaceAll& r, + const std::unique_ptr& i) { int cnt = 1; - std::string m; - for (const auto& i : decl.GetParameters()) { + auto params = [&](auto& r, const auto& i) { if (i->GetParameterType().GetDirection() == ParameterType::Direction::OUT) { cnt++; - continue; + return false; } - std::string v = "param" + std::to_string(cnt); - std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), v, i->GetID(), "map"); - m += AddIndent(TAB_SIZE * 6, c); - - cnt++; - } - - return m; + auto& pt = i->GetParameterType(); + r.Replace("ID", i->GetID()) + .Replace("HASH_ID", GetHashCodeStr(i->GetID())) + .Replace("ID_TMP", "param" + std::to_string(cnt++)) + .Replace("TYPE", ConvertTypeToString(pt.GetBaseType())); + + return true; + }; + + r.Replace("METHOD_ID", i->GetID()) + .Repeat("PARAMS", i->GetParameters(), params) + .Replace("CALL_PARAMS", GetCallParams(*i)); + return true; } std::string CsGroupGen::GetCallParams(const Declaration& decl) { diff --git a/idlc/gen/version2/cs_group_generator.h b/idlc/gen/version2/cs_group_generator.h index 19d2c1ee..a0673ae7 100644 --- a/idlc/gen/version2/cs_group_generator.h +++ b/idlc/gen/version2/cs_group_generator.h @@ -34,13 +34,8 @@ class CsGroupGen : public CsGeneratorBase { void OnFiniGen(std::ofstream& stream) override; private: - void GenNamespace(std::ofstream& stream); - void GenInterfaces(std::ofstream& stream); - void GenInterface(std::ofstream& stream, const Interface& iface); - std::string GetMethods(const Interface& iface); - std::string GetParamSerializer(const Declaration& decl); - std::string GetParamDeserializer(const Declaration& decl); - std::string GetDispatchers(const Interface& iface); + bool RepeatMethods(ReplaceAll& r, const std::unique_ptr& i); + bool RepeatDispatchers(ReplaceAll& r, const std::unique_ptr& i); std::string GetCallParams(const Declaration& decl); }; diff --git a/idlc/gen/version2/cs_group_generator_cb.h b/idlc/gen/version2/cs_group_generator_cb.h index 074ebe60..045b0560 100644 --- a/idlc/gen/version2/cs_group_generator_cb.h +++ b/idlc/gen/version2/cs_group_generator_cb.h @@ -17,137 +17,155 @@ #ifndef IDLC_GEN_VERSION2_CS_GROUP_GENRATOR_CB_H_ #define IDLC_GEN_VERSION2_CS_GROUP_GENRATOR_CB_H_ -const char CB_USING_NAMESPACE_GROUP[] = -R"__cs_cb(using Tizen.Applications.EventManager; - -)__cs_cb"; - -const char CB_INTERFACE_FULL[] = +const char CB_MAIN_GROUP[] = R"__cs_cb( - public class : IDisposable - { - - private EventReceiver _eventReceiver; - private Object _lock = new Object(); - private bool _disposedValue = false; - private string _senderAppid; - - private Bundle GetBundleFromParcel(Parcel p) - { - byte[] bytes = p.ToBytes(); - Bundle b = new Bundle(); - b.AddItem("TIDL_RAW", bytes); - return b; - } +/* + * Generated by tidlc . + */ - private Parcel GetParcelFromBundle(Bundle b) +using System; +using System.Collections.Generic; +using Tizen.Applications; +using Tizen.Applications.EventManager; +using Tizen.Applications.RPCPort; + +namespace RPCPort +{ + namespace + { + + namespace Group + { + + public class : IDisposable { - byte[] bytes = (byte[])b.GetItem("TIDL_RAW"); + + private EventReceiver _eventReceiver; + private Object _lock = new Object(); + private bool _disposedValue = false; + private string _senderAppid; - return new Parcel(bytes); - } + - private string GetEventName(string appid, string iface_name) - { - return "event." + appid + ".tidl_iface_" + iface_name; - } + private Bundle GetBundleFromParcel(Parcel p) + { + byte[] bytes = p.ToBytes(); + Bundle b = new Bundle(); + b.AddItem("TIDL_RAW", bytes); + return b; + } - private void OnReceive(Parcel p) - { - UnitMap map = new UnitMap(); - map.Deserialize(p); + private Parcel GetParcelFromBundle(Bundle b) + { + byte[] bytes = (byte[])b.GetItem("TIDL_RAW"); - map.Read("[METHOD]", "int", out int cmd); + return new Parcel(bytes); + } - switch ((MethodId)cmd) + private string GetEventName(string appid, string iface_name) { - + return "event." + appid + ".tidl_iface_" + iface_name; } - } - - - - - public (string senderAppid) - { - string eventName = GetEventName(senderAppid, ""); - _senderAppid = senderAppid; - _eventReceiver = new EventReceiver(eventName); - _eventReceiver.Received += (obj, args) => + private void OnReceive(Parcel p) { - Bundle b = args.Data; - using (var parcel = GetParcelFromBundle(b)) + UnitMap map = new UnitMap(); + map.Deserialize(p); + + map.Read( /*[METHOD]*/, out int cmd); + + switch ((MethodId)cmd) { - OnReceive(parcel); + + case MethodId.: + { + + map.Read( /**/, out ); + + Event?.Invoke(); + break; + } + } - }; - } + } - ~() - { - Dispose(false); - } + + + - protected void Dispose(bool disposing) - { - if (!_disposedValue) + public (string senderAppid) { - if (disposing) + string eventName = GetEventName(senderAppid, ""); + _senderAppid = senderAppid; + _eventReceiver = new EventReceiver(eventName); + _eventReceiver.Received += (obj, args) => { - if (_eventReceiver != null) + Bundle b = args.Data; + using (var parcel = GetParcelFromBundle(b)) { - _eventReceiver.Dispose(); - _eventReceiver = null; + OnReceive(parcel); } - } - - _disposedValue = true; + }; } - } - public void Dispose() - { - Dispose(true); - GC.SuppressFinalize(this); - } + ~() + { + Dispose(false); + } - + protected void Dispose(bool disposing) + { + if (!_disposedValue) + { + if (disposing) + { + if (_eventReceiver != null) + { + _eventReceiver.Dispose(); + _eventReceiver = null; + } + } - } -)__cs_cb"; + _disposedValue = true; + } + } -const char CB_METHOD[] = -R"__cs_cb( - public delegate void EventHandler(); - public event EventHandler Event; - - public - { - using (Parcel p = new Parcel()) + public void Dispose() { - UnitMap map = new UnitMap(); - map.Write("[METHOD]", "int", (int)MethodId.); - - map.Serialize(p); - - var bundle = GetBundleFromParcel(p); - string eventName = GetEventName(_senderAppid, ""); - ApplicationEventManager.Publish(eventName, bundle); + Dispose(true); + GC.SuppressFinalize(this); } - } -)__cs_cb"; -const char CB_DISPATCHER[] = -R"__cs_cb( - case MethodId.: + + public delegate void EventHandler(); + public event EventHandler Event; + + + public + { + using (Parcel p = new Parcel()) { - - Event?.Invoke(); - break; + UnitMap map = new UnitMap(); + var __methodId = (int)MethodId.; + map.Write( /*[METHOD]*/, __methodId); + + + map.Write( /**/, ); + + map.Serialize(p); + + var bundle = GetBundleFromParcel(p); + string eventName = GetEventName(_senderAppid, ""); + ApplicationEventManager.Publish(eventName, bundle); } -)__cs_cb"; - + } + + } + + } + } +} +)__cs_cb"; #endif // IDLC_GEN_VERSION2_CS_GROUP_GENRATOR_CB_H_ \ No newline at end of file diff --git a/idlc/gen/version2/cs_proxy_generator.cc b/idlc/gen/version2/cs_proxy_generator.cc index 8218a32d..d0978bd7 100644 --- a/idlc/gen/version2/cs_proxy_generator.cc +++ b/idlc/gen/version2/cs_proxy_generator.cc @@ -27,198 +27,89 @@ CsProxyGen::CsProxyGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsProxyGen::OnInitGen(std::ofstream& stream) { SetChannelType(Generator::ChannelType::TYPE_PROXY); - GenVersion(stream); - GenUsingNamespace(stream); - GenNamespace(stream); -} - -void CsProxyGen::OnFiniGen(std::ofstream& stream) {} - -void CsProxyGen::GenNamespace(std::ofstream& stream) { - stream << "namespace RPCPort" << NLine(1); - GenBrace(stream, 0, [&]() { - stream << "namespace " << GetFileNamespace() << NLine(1); - GenBrace(stream, 0, [&]() { - GenLemBase(stream); - GenRemoteException(stream); - GenStructures(stream); - stream << Tab(1) << "namespace Proxy" << NLine(1); - GenBrace(stream, TAB_SIZE, [&]() { - GenInterfaces(stream); - }); - }); - }); -} - -void CsProxyGen::GenLemBase(std::ofstream& stream) { - stream << CB_LEM_BASE; -} - -void CsProxyGen::GenInterfaces(std::ofstream& stream) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_INTERFACE) continue; - Interface& iface = static_cast(*i); - GenInterface(stream, iface); - stream << NLine(1); - } -} - -void CsProxyGen::GenInterface(std::ofstream& stream, const Interface& iface) { - stream << Tab(2) << "public class " << iface.GetID() << " : ProxyBase" - << NLine(1); - GenBrace(stream, TAB_SIZE * 2, [&]() { - GenEnum(stream, iface, 3); - GenMemberData(stream, iface); - GenUnitMap(stream, iface, 3); - GenCallbacks(stream, iface, true); - GenDelegateId(stream, iface); - GenMethodId(stream, iface); - GenEventMethod(stream, iface); - GenSerializer(stream); - GenListSerializer(stream); - GenCtor(stream, iface); - GenConnectMethod(stream, iface); - GenMethods(stream, iface); - }); -} - -void CsProxyGen::GenMemberData(std::ofstream& stream, const Interface& iface) { - ReplaceAll(CB_DATA_MEMBERS) - .Change("", std::string(FULLVER)) - .Change("", - HasDelegate(iface) ? CB_DATA_CALLBACK_LIST : std::string("")) - .Out(stream); -} - -void CsProxyGen::GenEventMethod(std::ofstream& stream, const Interface& iface) { - bool found = HasDelegate(iface); - - ReplaceAll(CB_EVENT_METHODS) - .Change("", - found ? CB_EVENT_PROCESS_EVENT_BASE : std::string("")) - .Change("", - found ? CB_EVENT_RECEIVE_EVENT_BASE : std::string("")) + auto interfaces = [&](auto& r, const auto& i) { + if (i->GetType() != Block::TYPE_INTERFACE) return false; + auto& iface = static_cast(*i); + + r.Replace("CLASS_ID", iface.GetID()) + .Replace("ENUMS", GetEnum(iface, 4)) + .Replace("UNITMAP", GetUnitMap(iface, 4)) + .Replace("CALLBACKS", + AddIndent(TAB_SIZE * 4, GetCallbacks(iface, true))) + .Replace("METHOD_IDS", AddIndent(TAB_SIZE * 4, GetMethodId(iface))) + .Replace("DELEGATE_IDS", AddIndent(TAB_SIZE * 4, GetDelegateId(iface))) + .Replace("SERIALIZER", AddIndent(TAB_SIZE * 4, GetSerializer())) + .Replace("LIST_SERIALIZER", + AddIndent(TAB_SIZE * 4, GetListSerializer())) + .Replace("REMOTE_EXCEPTION_SERIALIZER", + AddIndent(TAB_SIZE * 4, GetRemoteExceptionSerializer())) + .Repeat("METHODS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { return RepeatMethods(r, i); }); + return true; + }; + + ReplaceAll(CB_MAIN_PROXY) + .Replace("FILE_NAMESPACE", GetFileNamespace()) + .Replace("REMOTE_EXCEPTION", + AddIndent(TAB_SIZE * 2, GetRemoteException())) + .Replace("STRUCTURES", AddIndent(TAB_SIZE * 2, GetStructures())) + .Repeat("INTERFACES", GetDocument().GetBlocks(), interfaces) + .Replace("FULLVER", std::string(FULLVER)) + .Replace("HASH_METHOD", "[METHOD]"_hash) + .Replace("HASH_INT", "int"_hash) + .Replace("HASH_REMOTE_EXCEPTION", "[REMOTE_EXCEPTION]"_hash) + .Replace("HASH_DELEGATE", "delegate"_hash) + .Replace("HASH_RESULT", "[RESULT]"_hash) .Out(stream); } -void CsProxyGen::GenCtor(std::ofstream& stream, const Interface& iface) { - ReplaceAll(CB_CONSTRUCTOR_METHOD) - .Change("", iface.GetID()) - .Out(stream); -} - -void CsProxyGen::GenConnectMethod(std::ofstream& stream, - const Interface& iface) { - GenTemplate( - CB_CONNECT_METHOD, stream, - [&]() -> std::string { return iface.GetID(); }, - [&]() -> std::string { return iface.GetID(); }); - - if (HasDelegate(iface)) { - stream << CB_DISPOSE_CALLBACK_METHOD; - } - - stream << NLine(1); -} - -void CsProxyGen::GenMethods(std::ofstream& stream, const Interface& iface) { - auto& decls = iface.GetDeclarations(); - - for (const auto& i : decls) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) continue; - - if (!i->GetComments().empty()) - stream << AddIndent(TAB_SIZE * 3, i->GetComments()); - - stream << Tab(3) << "public "; - GenDeclaration(stream, *i, false); - stream << NLine(1); - GenBrace(stream, TAB_SIZE * 3, [&]() { GenInvocation(stream, *i); }); - stream << NLine(1); - } -} - -void CsProxyGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { - GenTemplate(CB_INVOCATION_PRE, stream, [&]() -> std::string { - std::string st; - for (const auto& i : decl.GetParameters()) { - auto& pt = i->GetParameterType(); - if (pt.GetDirection() == ParameterType::Direction::OUT) continue; - - if (pt.GetBaseType().ToString() == "file" || - (pt.GetBaseType().GetMetaType() != nullptr && - pt.GetBaseType().GetMetaType()->ToString() == "file")) { - st += GenTemplateString(CB_SHARE_FILE, [&]() -> std::string { - std::string str = ""; - str += Tab(6) + "Port.ShareFile(" + i->GetID() + ");"; - return str; - }); - } - } - - std::string m; - std::string l; - - m += Tab(5) + NLine(1) + "UnitMap map = new UnitMap();" + NLine(1); - m += ConvertTypeToSerializer("(int)MethodId." + decl.GetID(), "[METHOD]", - "map"); - - for (const auto& i : decl.GetParameters()) { - auto& pt = i->GetParameterType(); - if (pt.GetDirection() == ParameterType::Direction::OUT) - continue; - - m += ConvertTypeToSerializer(i->GetID(), i->GetID(), "map"); - - if (IsDelegateType(pt.GetBaseType())) { - l += "_delegateList.Add(" + i->GetID() + ");\n"; - } - } - m += NLine(1) + "map.Serialize(p);" + NLine(1); - - st += AddIndent(TAB_SIZE * 5, m) + NLine(1); - - st += Tab(5) + "lock (_lock)" + NLine(1); - st += Tab(5) + "{" + NLine(1); - if (!l.empty()) st += AddIndent(TAB_SIZE * 6, l) + NLine(1); - - - - // Deserialize - if (decl.GetMethodType() == Declaration::MethodType::ASYNC) { - st += CB_INVOCATION_ASYNC_MID + NLine(1); - st += Tab(5) + "}"; - return st; - } else { - st += CB_INVOCATION_SYNC_MID + NLine(1); - } - - st += NLine(1) + AddIndent(TAB_SIZE * 5, CB_INVOCATION_RECEIVE); - st += NLine(1); - - for (const auto& i : decl.GetParameters()) { - if (i->GetParameterType().GetDirection() == - ParameterType::Direction::IN) { - continue; - } - - std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), i->GetID(), i->GetID(), - "mapReceived", false); - if (c != "") st += AddIndent(TAB_SIZE * 5, c); - } - - if (decl.GetType().ToString() != "void") { - st += AddIndent(TAB_SIZE * 5, CB_INVOCATION_REMOTE_EXCEPTION); - st += AddIndent(TAB_SIZE * 6, NLine(1) + ConvertTypeToDeserializer( - decl.GetType(), "ret", - "[RESULT]", "mapReceived")); - st += AddIndent(TAB_SIZE * 6, NLine(1) + "return ret;"); - } - st += AddIndent(TAB_SIZE * 5, NLine(1) + "}"); +void CsProxyGen::OnFiniGen(std::ofstream& stream) {} - return st; - }); +bool CsProxyGen::RepeatMethods(ReplaceAll& r, + const std::unique_ptr& i) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) return false; + + r.Replace("DECLARATION", GetDeclaration(*i, false)) + .Repeat("FILES", i->GetParameters(), + [&](auto& r, const auto& i) { + auto& pt = i->GetParameterType(); + if (pt.GetDirection() == ParameterType::Direction::OUT) + return false; + if (pt.GetBaseType().ToString() == "file" || + (pt.GetBaseType().GetMetaType() != nullptr && + pt.GetBaseType().GetMetaType()->ToString() == "file")) { + r.Replace("PARAM_ID", i->GetID()); + return true; + } + return false; + }) + .Remove("ASYNC", i->GetMethodType() != Declaration::MethodType::ASYNC) + .Remove("SYNC", i->GetMethodType() == Declaration::MethodType::ASYNC) + .Remove("RETURN", i->GetType().ToString() == "void") + .Replace("ID", i->GetID()) + .Repeat("PARAMS", i->GetParameters(), + [&](auto& r, const auto& i) { + if (i->GetParameterType().GetDirection() == + ParameterType::Direction::OUT) + return false; + r.Remove("DELEGATE", + !i->GetParameterType().GetBaseType().IsDelegateType()) + .Replace("PARAM", i->GetID()) + .Replace("HASH_PARAM", GetHashCodeStr(i->GetID())); + return true; + }) + .Repeat("OUTS", i->GetParameters(), + [&](auto& r, const auto& i) { + auto& pt = i->GetParameterType(); + if (pt.GetDirection() != ParameterType::Direction::OUT && + pt.GetDirection() != ParameterType::Direction::REF) + return false; + r.Replace("HASH_OUT", GetHashCodeStr(i->GetID())) + .Replace("OUT", i->GetID()); + return true; + }) + .Replace("RET_TYPE", ConvertTypeToString(i->GetType())); + return true; } } // namespace version2 diff --git a/idlc/gen/version2/cs_proxy_generator.h b/idlc/gen/version2/cs_proxy_generator.h index a9290417..e6cfdc96 100644 --- a/idlc/gen/version2/cs_proxy_generator.h +++ b/idlc/gen/version2/cs_proxy_generator.h @@ -34,16 +34,7 @@ class CsProxyGen : public CsGeneratorBase { void OnFiniGen(std::ofstream& stream) override; private: - void GenNamespace(std::ofstream& stream); - void GenInterfaces(std::ofstream& stream); - void GenInterface(std::ofstream& stream, const Interface& iface); - void GenCtor(std::ofstream& stream, const Interface& iface); - void GenConnectMethod(std::ofstream& stream, const Interface& iface); - void GenMethods(std::ofstream& stream, const Interface& iface); - void GenInvocation(std::ofstream& stream, const Declaration& decl); - void GenEventMethod(std::ofstream& stream, const Interface& iface); - void GenMemberData(std::ofstream& stream, const Interface& iface); - void GenLemBase(std::ofstream& stream); + bool RepeatMethods(ReplaceAll& r, const std::unique_ptr& i); }; } // namespace version2 diff --git a/idlc/gen/version2/cs_proxy_generator_cb.h b/idlc/gen/version2/cs_proxy_generator_cb.h index 49013921..e29d1208 100644 --- a/idlc/gen/version2/cs_proxy_generator_cb.h +++ b/idlc/gen/version2/cs_proxy_generator_cb.h @@ -17,481 +17,511 @@ #ifndef IDLC_CS_GEN_VERSION2_CS_PROXY_GENRATOR_CB_H_ #define IDLC_CS_GEN_VERSION2_CS_PROXY_GENRATOR_CB_H_ -/** - * Version of TIDL Compiler. +const char CB_MAIN_PROXY[] = +R"__cs_cb( +/* + * Generated by tidlc . */ -const char CB_DATA_MEMBERS[] = -R"__cs_cb( public event EventHandler Connected; - public event EventHandler Disconnected; - public event EventHandler Rejected; - - private static readonly string _tidlVersion = ""; - private bool _online = false; - private string _appId; - private Object _lock = new Object(); - private bool _connecting = false; - private LocalExecution _lem; - private SynchronizationContext _async_context = TizenSynchronizationContext.Current; - -)__cs_cb"; - -const char CB_LEM_BASE[] = -R"__cs_cb( internal class LocalExecution +using System; +using System.Collections.Concurrent; +using System.Collections.Generic; +using System.Reflection; +using System.Threading; +using Tizen.Applications; +using Tizen.Applications.RPCPort; + +namespace RPCPort +{ + namespace { - internal bool IsListen = false; - object _instance = null; - string _instance_id = null; - string _appId = null; - static int _seq = 0; - MethodInfo _send = null; - MethodInfo _connect = null; - MethodInfo _disconnect = null; - ConcurrentQueue _result = new ConcurrentQueue(); - - internal LocalExecution(string appId, string interfaceName) + internal class LocalExecution { - string current_appid = Application.Current.ApplicationInfo.ApplicationId; + internal bool IsListen = false; + object _instance = null; + string _instance_id = null; + string _appId = null; + static int _seq = 0; + MethodInfo _send = null; + MethodInfo _connect = null; + MethodInfo _disconnect = null; + ConcurrentQueue _result = new ConcurrentQueue(); + + internal LocalExecution(string appId, string interfaceName) + { + string current_appid = Application.Current.ApplicationInfo.ApplicationId; - if (current_appid != appId) - return; + if (current_appid != appId) + return; - _seq++; - _instance_id = appId + "::" + _seq.ToString(); - _appId = appId; + _seq++; + _instance_id = appId + "::" + _seq.ToString(); + _appId = appId; - Assembly assembly = Assembly.GetExecutingAssembly(); - Type type = null; + Assembly assembly = Assembly.GetExecutingAssembly(); + Type type = null; - foreach (Type t in assembly.GetTypes()) - { - if (t.Name != interfaceName) - continue; + foreach (Type t in assembly.GetTypes()) + { + if (t.Name != interfaceName) + continue; - string[] name = t.FullName?.Split('.'); + string[] name = t.FullName?.Split('.'); - if (name == null || name.Length != 4) - continue; + if (name == null || name.Length != 4) + continue; - if (name[0] == "RPCPort" && name[2] == "Stub") - { - if (type == null) - { - type = t; - } - else + if (name[0] == "RPCPort" && name[2] == "Stub") { - type = null; - break; + if (type == null) + { + type = t; + } + else + { + type = null; + break; + } } } - } - if (type == null) - return; + if (type == null) + return; - MethodInfo getInstance = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.NonPublic); - if (getInstance == null) { return; } + MethodInfo getInstance = type.GetMethod("GetInstance", BindingFlags.Static | BindingFlags.NonPublic); + if (getInstance == null) { return; } - _instance = getInstance.Invoke(null, null); - if (_instance == null) { return; } + _instance = getInstance.Invoke(null, null); + if (_instance == null) { return; } - _send = type.GetMethod("OnLemReceivedEvent", BindingFlags.NonPublic | BindingFlags.Instance); - if (_send == null) { return; } + _send = type.GetMethod("OnLemReceivedEvent", BindingFlags.NonPublic | BindingFlags.Instance); + if (_send == null) { return; } - _connect = type.GetMethod("OnLemConnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); - if (_connect == null) { return; } + _connect = type.GetMethod("OnLemConnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); + if (_connect == null) { return; } - _disconnect = type.GetMethod("OnLemDisconnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); - if (_disconnect == null) { return; } + _disconnect = type.GetMethod("OnLemDisconnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); + if (_disconnect == null) { return; } - MethodInfo getListen = type.GetMethod("GetListenStatus", BindingFlags.NonPublic | BindingFlags.Instance); - if (getListen == null) { return; } + MethodInfo getListen = type.GetMethod("GetListenStatus", BindingFlags.NonPublic | BindingFlags.Instance); + if (getListen == null) { return; } - object ret = getListen.Invoke(_instance, null); - if (ret == null) { return; } - IsListen = (bool)ret; - } + object ret = getListen.Invoke(_instance, null); + if (ret == null) { return; } + IsListen = (bool)ret; + } - internal void Connect(object proxy_instance) - { - _connect.Invoke(_instance, new object[] { proxy_instance, _appId, _instance_id }); - } + internal void Connect(object proxy_instance) + { + _connect.Invoke(_instance, new object[] { proxy_instance, _appId, _instance_id }); + } - internal void Disconnect() - { - _disconnect.Invoke(_instance, new object[] { _appId, _instance_id }); - } + internal void Disconnect() + { + _disconnect.Invoke(_instance, new object[] { _appId, _instance_id }); + } - internal void Send(Parcel p) - { - _send.Invoke(_instance, new object[] { _instance_id, p}); - } + internal void Send(Parcel p) + { + _send.Invoke(_instance, new object[] { _instance_id, p}); + } - internal void Send(Parcel p, ref Parcel resutl) - { - Send(p); + internal void Send(Parcel p, ref Parcel resutl) + { + Send(p); - int count = 0; - while (_result.IsEmpty && count++ < 100) - Thread.Sleep(100); + int count = 0; + while (_result.IsEmpty && count++ < 100) + Thread.Sleep(100); - if (ResultQueuePop(out resutl)) + if (ResultQueuePop(out resutl)) + { + Tizen.Log.Error("RPC_PORT", "ResultQueuePop"); + return; + } + + Tizen.Log.Error("RPC_PORT_PROXY", "Failed to get result from server"); + throw new InvalidIOException(); + } + + internal void ResultQueuePush(Parcel p) { - Tizen.Log.Error("RPC_PORT", "ResultQueuePop"); - return; + _result.Enqueue(p); } - Tizen.Log.Error("RPC_PORT_PROXY", "Failed to get result from server"); - throw new InvalidIOException(); + internal bool ResultQueuePop(out Parcel p) + { + return _result.TryDequeue(out p); + } } - internal void ResultQueuePush(Parcel p) - { - _result.Enqueue(p); - } + + - internal bool ResultQueuePop(out Parcel p) + namespace Proxy { - return _result.TryDequeue(out p); - } - } -)__cs_cb"; - -const char CB_DATA_CALLBACK_LIST[] = -R"__cs_cb( - private List _delegateList = new List(); -)__cs_cb"; -const char CB_EVENT_PROCESS_EVENT_BASE[] = -R"__cs_cb( - private void ProcessReceivedEvent(Parcel parcelReceived) + + public class : ProxyBase { - UnitMap map = new UnitMap(); - map.Deserialize(parcelReceived); - - int cmd; - map.Read("[METHOD]", out cmd); - - if (cmd != (int)MethodId.__Callback) + + + public event EventHandler Connected; + public event EventHandler Disconnected; + public event EventHandler Rejected; + + private static readonly string _tidlVersion = ""; + private bool _online = false; + private string _appId; + private Object _lock = new Object(); + private bool _connecting = false; + private LocalExecution _lem; + private SynchronizationContext _async_context = TizenSynchronizationContext.Current; + private List _delegateList = new List(); + + + + + + + protected override void OnConnectedEvent(string endPoint, string portName, Port port) { - return; + _online = true; + _connecting = false; + Connected?.Invoke(this, null); } - CallbackBase cb; - map.Read("delegate", out cb); - - foreach (var i in _delegateList) + protected override void OnDisconnectedEvent(string endPoint, string portName) { - if ((int)i.Id == cb.Id && i.SeqId == cb.SeqId) - { - i.OnReceivedEvent(map); - if (i.Once) - _delegateList.Remove(i); - break; - } + _online = false; + Disconnected?.Invoke(this, null); } - } - internal void OnLemReceivedEvent(Parcel parcelReceived) - { - _async_context.Post((data) => + internal void OnLemDisconnectedEvent() { - ProcessReceivedEvent(parcelReceived); - - }, null); - } - -)__cs_cb"; + _async_context.Post((data) => + { + OnDisconnectedEvent(null, null); -const char CB_EVENT_RECEIVE_EVENT_BASE[] = -R"__cs_cb( Parcel parcelReceived; + }, null); - try - { - parcelReceived = new Parcel(CallbackPort); } - catch (InvalidIOException) + + internal void OnLemResultEvent(Parcel result) { - return; + _lem.ResultQueuePush(result); } - using (parcelReceived) + protected override void OnRejectedEvent(string endPoint, string portName) { - ProcessReceivedEvent(parcelReceived); + _connecting = false; + Rejected?.Invoke(this, null); } -)__cs_cb"; -const char CB_EVENT_METHODS[] = -R"__cs_cb( - protected override void OnConnectedEvent(string endPoint, string portName, Port port) - { - _online = true; - _connecting = false; - Connected?.Invoke(this, null); - } + private void ProcessReceivedEvent(Parcel parcelReceived) + { + UnitMap map = new UnitMap(); + map.Deserialize(parcelReceived); - protected override void OnDisconnectedEvent(string endPoint, string portName) - { - _online = false; - Disconnected?.Invoke(this, null); - } + int cmd; + map.Read( /*[METHOD]*/, out cmd); - internal void OnLemDisconnectedEvent() - { - _async_context.Post((data) => - { - OnDisconnectedEvent(null, null); + if (cmd != (int)MethodId.__Callback) + { + return; + } - }, null); + CallbackBase cb; + map.Read( /*delegate*/, out cb); - } + foreach (var i in _delegateList) + { + if ((int)i.Id == cb.Id && i.SeqId == cb.SeqId) + { + i.OnReceivedEvent(map); + if (i.Once) + _delegateList.Remove(i); + break; + } + } + } - internal void OnLemResultEvent(Parcel result) - { - _lem.ResultQueuePush(result); - } + internal void OnLemReceivedEvent(Parcel parcelReceived) + { + _async_context.Post((data) => + { + ProcessReceivedEvent(parcelReceived); - protected override void OnRejectedEvent(string endPoint, string portName) - { - _connecting = false; - Rejected?.Invoke(this, null); - } - - protected override void OnReceivedEvent(string endPoint, string portName) - { - - } + }, null); + } - private void ConsumeCommand(out UnitMap unitMap, Port port, ParcelHeader header) - { - do + protected override void OnReceivedEvent(string endPoint, string portName) { - Parcel p; + Parcel parcelReceived; try { - p = new Parcel(port); + parcelReceived = new Parcel(CallbackPort); } catch (InvalidIOException) { - unitMap = null; return; } - ParcelHeader headerReceived = p.GetHeader(); - if (!string.IsNullOrEmpty(headerReceived.GetTag())) + using (parcelReceived) { - if (headerReceived.GetSequenceNumber() != header.GetSequenceNumber()) + ProcessReceivedEvent(parcelReceived); + } + } + + private void ConsumeCommand(out UnitMap unitMap, Port port, ParcelHeader header) + { + do + { + Parcel p; + + try { - Tizen.Log.Error("RPC_PORT_PROXY", headerReceived.GetSequenceNumber().ToString() + " - " + headerReceived.GetTag() + " : " + header.GetSequenceNumber().ToString() + " - " + header.GetTag()); - continue; + p = new Parcel(port); + } + catch (InvalidIOException) + { + unitMap = null; + return; } - } - UnitMap map = new UnitMap(); - map.Deserialize(p); + ParcelHeader headerReceived = p.GetHeader(); + if (!string.IsNullOrEmpty(headerReceived.GetTag())) + { + if (headerReceived.GetSequenceNumber() != header.GetSequenceNumber()) + { + Tizen.Log.Error("RPC_PORT_PROXY", headerReceived.GetSequenceNumber().ToString() + " - " + headerReceived.GetTag() + " : " + header.GetSequenceNumber().ToString() + " - " + header.GetTag()); + continue; + } + } - unitMap = map; - return; - } while (true); - } -)__cs_cb"; + UnitMap map = new UnitMap(); + map.Deserialize(p); -const char CB_DISPOSE_CALLBACK_METHOD[] = -R"__cs_cb( - /// - /// Disposes delegate objects in this interface - /// - /// The tag string from delegate object - void DisposeCallback(string tag) - { - foreach (var i in _delegateList) - { - if (i.Tag.Equals(tag)) - { - _delegateList.Remove(i); + unitMap = map; return; - } + } while (true); } - } -)__cs_cb"; -const char CB_CONSTRUCTOR_METHOD[] = -R"__cs_cb( - public (string appId) - { - _appId = appId; - _lem = new LocalExecution(appId, ""); - } -)__cs_cb"; + + + + public (string appId) + { + _appId = appId; + _lem = new LocalExecution(appId, ""); + } -const char CB_CONNECT_METHOD[] = -R"__cs_cb( - /// - /// Connects to the stub app. - /// - /// http://tizen.org/privilege/appmanager.launch - /// http://tizen.org/privilege/datasharing - /// - /// Thrown when the appid to connect is invalid. - /// - /// - /// Thrown when internal I/O error happen. - /// - /// - /// Thrown when the permission is denied. - /// - /// If you want to use this method, you must add privileges. - public void Connect() - { - if (_lem.IsListen) + /// + /// Connects to the stub app. + /// + /// http://tizen.org/privilege/appmanager.launch + /// http://tizen.org/privilege/datasharing + /// + /// Thrown when the appid to connect is invalid. + /// + /// + /// Thrown when internal I/O error happen. + /// + /// + /// Thrown when the permission is denied. + /// + /// If you want to use this method, you must add privileges. + public void Connect() { - if (_online || _connecting) - throw new InvalidIDException(); + if (_lem.IsListen) + { + if (_online || _connecting) + throw new InvalidIDException(); - _connecting = true; - _lem.Connect(this); + _connecting = true; + _lem.Connect(this); - _async_context.Post((data) => + _async_context.Post((data) => + { + OnConnectedEvent(null, null, null); + }, null); + } + else { - OnConnectedEvent(null, null, null); - }, null); - } - else - { - Connect(_appId, "$$"); + Connect(_appId, ""); + } } - } - /// - /// Disconnects from the stub app. - /// - /// - /// Thrown when an internal IO error occurrs. - /// - public void Disconnect() - { - if (_lem.IsListen) - { - if (_online) - _lem.Disconnect(); - } - else + /// + /// Disconnects from the stub app. + /// + /// + /// Thrown when an internal IO error occurrs. + /// + public void Disconnect() { - Port?.Disconnect(); + if (_lem.IsListen) + { + if (_online) + _lem.Disconnect(); + } + else + { + Port?.Disconnect(); + } + _online = false; } - _online = false; - } - /// - /// Connects to the stub app synchronously. - /// - /// http://tizen.org/privilege/appmanager.launch - /// http://tizen.org/privilege/datasharing - /// - /// Thrown when the appid to connect is invalid. - /// - /// - /// Thrown when internal I/O error happen. - /// - /// - /// Thrown when the permission is denied. - /// - /// If you want to use this method, you must add privileges. - public void ConnectSync() - { - if (_lem.IsListen) + /// + /// Connects to the stub app synchronously. + /// + /// http://tizen.org/privilege/appmanager.launch + /// http://tizen.org/privilege/datasharing + /// + /// Thrown when the appid to connect is invalid. + /// + /// + /// Thrown when internal I/O error happen. + /// + /// + /// Thrown when the permission is denied. + /// + /// If you want to use this method, you must add privileges. + public void ConnectSync() { - if (_online || _connecting) - throw new InvalidIDException(); + if (_lem.IsListen) + { + if (_online || _connecting) + throw new InvalidIDException(); - _connecting = true; - _lem.Connect(this); - OnConnectedEvent(null, null, null); - } - else - { - ConnectSync(_appId, "$$"); + _connecting = true; + _lem.Connect(this); + OnConnectedEvent(null, null, null); + } + else + { + ConnectSync(_appId, ""); + } } - } -)__cs_cb"; - -const char CB_INVOCATION_PRE[] = -R"__cs_cb( if (!_online) - throw new NotConnectedSocketException(); - using (Parcel p = new Parcel()) + /// + /// Disposes delegate objects in this interface + /// + /// The tag string from delegate object + void DisposeCallback(string tag) { - ParcelHeader header = p.GetHeader(); - header.SetTag(_tidlVersion); -$$ + foreach (var i in _delegateList) + { + if (i.Tag.Equals(tag)) + { + _delegateList.Remove(i); + return; + } + } } -)__cs_cb"; + + public + { + if (!_online) + throw new NotConnectedSocketException(); -const char CB_SHARE_FILE[] = -R"__cs_cb( - try + using (Parcel p = new Parcel()) { -$$ - } - catch (InvalidIOException) - { - throw new InvalidIOException(); - } -)__cs_cb"; - -const char CB_INVOCATION_ASYNC_MID[] = -R"__cs_cb( // Send - if (_lem.IsListen) + ParcelHeader header = p.GetHeader(); + header.SetTag(_tidlVersion); + try { - _lem.Send(p); + + Port.ShareFile(); + } - else + catch (InvalidIOException) { - p.Send(Port); + throw new InvalidIOException(); } -)__cs_cb"; -const char CB_INVOCATION_SYNC_MID[] = -R"__cs_cb( // Send - Parcel ret_parcel = new Parcel(); - // Send - if (_lem.IsListen) - { - _lem.Send(p, ref ret_parcel); - } - else + var methodId = (int)MethodId.; + UnitMap map = new UnitMap(); + map.Write( /*[METHOD]*/, methodId); + + map.Write( /**/, ); + + _delegateList.Add(); + + + map.Serialize(p); + + lock (_lock) { - p.Send(Port); + + // Send + if (_lem.IsListen) + { + _lem.Send(p); + } + else + { + p.Send(Port); + } + + + // Send + Parcel ret_parcel = new Parcel(); + // Send + if (_lem.IsListen) + { + _lem.Send(p, ref ret_parcel); + } + else + { + p.Send(Port); + } + + UnitMap mapReceived; + if (_lem.IsListen) + { + mapReceived = new UnitMap(); + mapReceived.Deserialize(ret_parcel); + } + else + { + // Receive + ConsumeCommand(out mapReceived, Port, header); + } + + if (mapReceived == null) + { + throw new InvalidProtocolException(); + } + + + if (mapReceived.ContainsName( /*[REMOTE_EXCEPTION]*/)) + { + mapReceived.Read( /*[REMOTE_EXCEPTION]*/, out RemoteException exception); + throw exception; + } + + + mapReceived.Read( /**/, out ); + + mapReceived.Read( /*[RESULT]*/, out ret); + + return ret; + + } -)__cs_cb"; + } + } -const char CB_INVOCATION_RECEIVE[] = -R"__cs_cb( UnitMap mapReceived; - if (_lem.IsListen) - { - mapReceived = new UnitMap(); - mapReceived.Deserialize(ret_parcel); - } - else - { - // Receive - ConsumeCommand(out mapReceived, Port, header); - } + + } - if (mapReceived == null) - { - throw new InvalidProtocolException(); + + } } -)__cs_cb"; +} -const char CB_INVOCATION_REMOTE_EXCEPTION[] = -R"__cs_cb( if (mapReceived.ContainsName("[REMOTE_EXCEPTION]")) - { - mapReceived.Read("[REMOTE_EXCEPTION]", out RemoteException exception); - throw exception; - } )__cs_cb"; #endif // IDLC_CS_GEN_VERSION2_CS_PROXY_GENRATOR_CB_H_ \ No newline at end of file diff --git a/idlc/gen/version2/cs_stub_generator.cc b/idlc/gen/version2/cs_stub_generator.cc index a74d6517..5c7cd5e1 100644 --- a/idlc/gen/version2/cs_stub_generator.cc +++ b/idlc/gen/version2/cs_stub_generator.cc @@ -27,143 +27,125 @@ CsStubGen::CsStubGen(std::shared_ptr doc) : CsGeneratorBase(doc) {} void CsStubGen::OnInitGen(std::ofstream& stream) { SetChannelType(Generator::ChannelType::TYPE_STUB); - GenVersion(stream); - GenUsingNamespace(stream); - GenNamespace(stream); + auto interfaces = [&](auto& r, const auto& i) { + if (i->GetType() != Block::TYPE_INTERFACE) return false; + auto& iface = static_cast(*i); + + r.Replace("CLASS_ID", iface.GetID()) + .Replace("ENUMS", GetEnum(iface, 4)) + .Replace("UNITMAP", GetUnitMap(iface, 4)) + .Replace("CALLBACKS", + AddIndent(TAB_SIZE * 4, GetCallbacks(iface, false))) + .Replace("METHOD_IDS", AddIndent(TAB_SIZE * 4, GetMethodId(iface))) + .Replace("SERIALIZER", AddIndent(TAB_SIZE * 4, GetSerializer())) + .Replace("LIST_SERIALIZER", + AddIndent(TAB_SIZE * 4, GetListSerializer())) + .Replace("REMOTE_EXCEPTION_SERIALIZER", + AddIndent(TAB_SIZE * 4, GetRemoteExceptionSerializer())) + .Replace("DELEGATE_IDS", AddIndent(TAB_SIZE * 4, GetDelegateId(iface))) + .Repeat("ATTRIBUTES", iface.GetAttributes(), + [&](auto& r, const auto& i) { + r.Remove("PRIVILEGE", i->GetKey() != "privilege") + .Remove("TRUSTED", !(i->GetKey() == "trusted" && + i->GetValue() == "true")) + .Replace("VAL", i->GetValue()); + return true; + }) + .Repeat("PRIVILEGE_MAP", iface.GetDeclarations(), + [&](auto& r, const auto& i) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) + return false; + + std::string code = GetPrivileges(*i, i->GetID()); + if (code.empty()) return false; + + r.Replace("METHOD_ID", i->GetID()) + .Replace("PRIVILEGE_LIST", code); + return true; + }) + .Repeat("METHODS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { return RepeatMethods(r, i); }) + .Repeat( + "INVOCATIONS", iface.GetDeclarations(), + [&](auto& r, const auto& i) { return RepeatInvocations(r, i); }); + return true; + }; + + ReplaceAll(CB_MAIN_STUB) + .Replace("FILE_NAMESPACE", GetFileNamespace()) + .Replace("REMOTE_EXCEPTION", + AddIndent(TAB_SIZE * 2, GetRemoteException())) + .Replace("STRUCTURES", AddIndent(TAB_SIZE * 2, GetStructures())) + .Repeat("INTERFACES", GetDocument().GetBlocks(), interfaces) + .Replace("FULLVER", std::string(FULLVER)) + .Replace("HASH_METHOD", "[METHOD]"_hash) + .Replace("HASH_INT", "int"_hash) + .Replace("HASH_REMOTE_EXCEPTION", "[REMOTE_EXCEPTION]"_hash) + .Replace("HASH_RESULT", "[RESULT]"_hash) + .Out(stream); } void CsStubGen::OnFiniGen(std::ofstream& stream) {} -void CsStubGen::GenNamespace(std::ofstream& stream) { - stream << "namespace RPCPort" << NLine(1); - GenBrace(stream, 0, [&]() { - stream << "namespace " << GetFileNamespace() << NLine(1); - GenBrace(stream, 0, [&]() { - GenLemBase(stream); - GenRemoteException(stream); - GenStructures(stream); - stream << Tab(1) << "namespace Stub" << NLine(1); - GenBrace(stream, TAB_SIZE, [&]() { GenInterfaces(stream); }); - }); - }); -} - -void CsStubGen::GenLemBase(std::ofstream& stream) { - stream << CB_LEM_BASE; -} - -void CsStubGen::GenInterfaces(std::ofstream& stream) { - for (auto& i : GetDocument().GetBlocks()) { - if (i->GetType() != Block::TYPE_INTERFACE) continue; - Interface& iface = static_cast(*i); - GenInterface(stream, iface); - stream << std::endl; - } -} - -void CsStubGen::GenInterface(std::ofstream& stream, const Interface& iface) { - stream << Tab(2) << "public sealed class " << iface.GetID() << " : StubBase" - << NLine(1); - GenBrace(stream, TAB_SIZE * 2, [&]() { - GenEnum(stream, iface, 3); - stream << ReplaceAll(CB_DATA_MEMBERS, "", FULLVER); - GenServiceBase(stream, iface); - GenUnitMap(stream, iface, 3); - GenCallbacks(stream, iface, false); - GenDelegateId(stream, iface); - GenMethodId(stream, iface); - GenSerializer(stream); - GenListSerializer(stream); - GenCheckPrivilege(stream); - GenReceivedEvent(stream, iface); - GenConnectedEvent(stream); - GenDisconnectedEvent(stream); - GenCtor(stream, iface); - GenCommonMethods(stream); - GenDisposable(stream, iface); - }); -} - -void CsStubGen::GenServiceBase(std::ofstream& stream, const Interface& iface) { - stream << CB_SERVICE_BASE_FRONT; - GenDeclarations(stream, iface.GetDeclarations()); - stream << NLine(1); - stream << AddIndent(TAB_SIZE * 3, "}\n"); -} +bool CsStubGen::RepeatMethods(ReplaceAll& r, + const std::unique_ptr& i) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) return false; + r.Replace("DECLARATION", GetDeclaration(*i)); -void CsStubGen::GenDeclarations(std::ofstream& stream, - const Declarations& decls) { - for (const auto& i : decls) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) continue; - if (!i->GetComments().empty()) - stream << NLine(1) << AddIndent(TAB_SIZE * 4, i->GetComments()); - stream << Tab(4) << "public abstract "; - GenDeclaration(stream, *i); - stream << NLine(1); - } + return true; } -void CsStubGen::GenCheckPrivilege(std::ofstream& stream) { - stream << CB_CHECK_PRIVILEGE_METHOD; -} - -void CsStubGen::GenReceivedEvent(std::ofstream& stream, - const Interface& iface) { - stream << CB_ON_RECEIVED_EVENT_FRONT; - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) continue; - stream << Tab(6) << "case MethodId." << i->GetID() << ":" << NLine(1); - GenBrace(stream, TAB_SIZE * 7, [&]() { - GenInvocation(stream, *i); - stream << Tab(8) << "break;" << NLine(1); - }); - } - stream << CB_ON_RECEIVED_EVENT_BACK; -} - - -std::string CsStubGen::GetWriteOutParam(const Declaration& decl) { - int cnt = 0; - std::string code; +bool CsStubGen::RepeatInvocations(ReplaceAll& r, + const std::unique_ptr& i) { + if (i->GetMethodType() == Declaration::MethodType::DELEGATE) return false; - for (const auto& i : decl.GetParameters()) { - auto& pt = i->GetParameterType(); - cnt++; - if (pt.GetDirection() == ParameterType::Direction::IN) - continue; + int cnt = 1; + int param_cnt = 1; + auto params = [&](auto& r, const auto& i) { + if (i->GetParameterType().GetDirection() == ParameterType::Direction::OUT) { + cnt++; + return false; + } - code += ConvertTypeToSerializer("param" + std::to_string(cnt), - i->GetID(), "result_map"); - } + bool is_del = i->GetParameterType().GetBaseType().GetUserDefinedType() == + BaseType::UserType::DELEGATE; + + r.Remove("DELEGATE", !is_del) + .Remove("NO_DELEGATE", is_del) + .Replace("HASH_ID", GetHashCodeStr(i->GetID())) + .Replace("PARAM_ID", i->GetID()) + .Replace("TYPE", + ConvertTypeToString(i->GetParameterType().GetBaseType())) + .Replace("CNT", std::to_string(cnt++)); + return true; + }; + + auto out_params = [&](auto& r, const auto& i) { + if (i->GetParameterType().GetDirection() == ParameterType::Direction::IN) { + param_cnt++; + return false; + } + r.Replace("HASH_ID", GetHashCodeStr(i->GetID())) + .Replace("PARAM_ID", i->GetID()) + .Replace("CNT", std::to_string(param_cnt++)); - return code; -} + return true; + }; -std::string CsStubGen::GetInvocationMethod(const Declaration& decl) { - std::string code; - if (decl.GetType().ToString() != "void") { - code = - ReplaceAll(CB_INVOCATION_REMOTE_EXCEPTION) - .Change("", GetInvocationMethodParam(decl, true)) - .Change("", ConvertTypeToSerializer( - "retVal", "[RESULT]", "result_map")) - .Change("", GetWriteOutParam(decl)); - } else { - code = NLine(1) + GetInvocationMethodParam(decl, false); - } + r.RemoveAll("SYNC", i->GetMethodType() == Declaration::MethodType::ASYNC) + .RemoveAll("ASYNC", i->GetMethodType() != Declaration::MethodType::ASYNC) + .Repeat("PARAMS", i->GetParameters(), params) + .Repeat("OUT_PARAMS", i->GetParameters(), out_params) + .Replace("ID", i->GetID()) + .Replace("INVOKE_PARAMS", GetInvocationMethodParam(*i)); - return code; + return true; } -std::string CsStubGen::GetInvocationMethodParam(const Declaration& decl, - bool hasRet) { +std::string CsStubGen::GetInvocationMethodParam(const Declaration& decl) { int cnt = 1; std::string code; - if (hasRet) - code = "var retVal = "; - - code += "b." + decl.GetID() + "("; for (const auto& i : decl.GetParameters()) { if (cnt != 1) { code += ", "; @@ -180,61 +162,14 @@ std::string CsStubGen::GetInvocationMethodParam(const Declaration& decl, cnt++; } - code += ");\n"; - return code; } -void CsStubGen::GenInvocation(std::ofstream& stream, const Declaration& decl) { - int cnt = 1; - // Deserialize - for (const auto& i : decl.GetParameters()) { - if (i->GetParameterType().GetDirection() == ParameterType::Direction::OUT) { - cnt++; - continue; - } - - std::string v = "param" + std::to_string(cnt); - std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), v, i->GetID(), "map"); - stream << AddIndent(TAB_SIZE * 8, c); - cnt++; - } - - // Invoke - std::string m; - - m += NLine(1) + "UnitMap result_map = new UnitMap();"; - - m += ReplaceAll(CB_INVOCATION_CHECK_PRIVILEGE) - .Change("", - AddIndent(TAB_SIZE, GetInvocationMethod(decl))); - - stream << AddIndent(TAB_SIZE * 8, m); - - // Serialize - if (decl.GetMethodType() == Declaration::MethodType::ASYNC) - return; - - cnt = 0; - m = CB_INVOCATION_RESULT; - stream << AddIndent(TAB_SIZE * 8, m); -} - -void CsStubGen::GenConnectedEvent(std::ofstream& stream) { - stream << CB_ON_CONNECTED_EVENT; -} - -void CsStubGen::GenDisconnectedEvent(std::ofstream& stream) { - stream << CB_ON_DISCONNECTED_EVENT; -} - -std::string CsStubGen::GenPrivileges(const Declaration& decl, +std::string CsStubGen::GetPrivileges(const Declaration& decl, const std::string& id) { std::string code; for (const auto& attr : decl.GetAttributes()) { - if (attr->GetKey() != "privilege") - continue; + if (attr->GetKey() != "privilege") continue; code += Tab(4) + "list" + id + ".Add(\"" + attr->GetValue() + "\");" + NLine(1); @@ -243,45 +178,5 @@ std::string CsStubGen::GenPrivileges(const Declaration& decl, return code; } -void CsStubGen::GenCtor(std::ofstream& stream, const Interface& iface) { - GenTemplate( - CB_CTOR_FRONT, stream, - [&]() -> std::string { return iface.GetID(); }, - [&]() -> std::string { return iface.GetID(); }); - - for (const auto& i : iface.GetAttributes()) { - if (i->GetKey() == "privilege") { - stream << Tab(4) << "AddPrivilege(\"" << i->GetValue() << "\");" - << NLine(1); - } else if (i->GetKey() == "trusted" && i->GetValue() == "true") { - stream << Tab(4) << "SetTrusted(" << i->GetValue() << ");" << NLine(1); - } - } - - for (const auto& i : iface.GetDeclarations()) { - if (i->GetMethodType() == Declaration::MethodType::DELEGATE) - continue; - - std::string code = GenPrivileges(*i, i->GetID()); - if (code.empty()) - continue; - - ReplaceAll(CB_PRIVILEGE_MAP_BASE) - .Change("", i->GetID()) - .Change("", code) - .Out(stream); - } - - stream << Tab(3) << "}" << NLine(1); -} - -void CsStubGen::GenCommonMethods(std::ofstream& stream) { - stream << CB_COMMON_METHODS; -} - -void CsStubGen::GenDisposable(std::ofstream& stream, const Interface& iface) { - stream << CB_DISPOSABLE; -} - } // namespace version2 } // namespace tidl diff --git a/idlc/gen/version2/cs_stub_generator.h b/idlc/gen/version2/cs_stub_generator.h index a4e09ea9..b74f7aef 100644 --- a/idlc/gen/version2/cs_stub_generator.h +++ b/idlc/gen/version2/cs_stub_generator.h @@ -34,24 +34,10 @@ class CsStubGen : public CsGeneratorBase { void OnFiniGen(std::ofstream& stream) override; private: - void GenNamespace(std::ofstream& stream); - void GenInterfaces(std::ofstream& stream); - void GenInterface(std::ofstream& stream, const Interface& iface); - void GenServiceBase(std::ofstream& stream, const Interface& iface); - void GenReceivedEvent(std::ofstream& stream, const Interface& iface); - void GenConnectedEvent(std::ofstream& stream); - void GenCheckPrivilege(std::ofstream& stream); - std::string GenPrivileges(const Declaration& decl, const std::string& id); - void GenDisconnectedEvent(std::ofstream& stream); - void GenCtor(std::ofstream& stream, const Interface& iface); - void GenCommonMethods(std::ofstream& stream); - void GenDeclarations(std::ofstream& stream, const Declarations& decls); - void GenInvocation(std::ofstream& stream, const Declaration& decl); - void GenDisposable(std::ofstream& stream, const Interface& iface); - std::string GetInvocationMethodParam(const Declaration& decl, bool hasRet); - std::string GetInvocationMethod(const Declaration& decl); - std::string GetWriteOutParam(const Declaration& decl); - void GenLemBase(std::ofstream& stream); + bool RepeatMethods(ReplaceAll& r, const std::unique_ptr& i); + bool RepeatInvocations(ReplaceAll& r, const std::unique_ptr& i); + std::string GetInvocationMethodParam(const Declaration& decl); + std::string GetPrivileges(const Declaration& decl, const std::string& id); }; } // namespace version2 diff --git a/idlc/gen/version2/cs_stub_generator_cb.h b/idlc/gen/version2/cs_stub_generator_cb.h index 23866a74..3477d7c5 100644 --- a/idlc/gen/version2/cs_stub_generator_cb.h +++ b/idlc/gen/version2/cs_stub_generator_cb.h @@ -17,46 +17,50 @@ #ifndef IDLC_CS_GEN_VERSION2_CS_STUB_GENRATOR_CB_H_ #define IDLC_CS_GEN_VERSION2_CS_STUB_GENRATOR_CB_H_ -/** - * Version of TIDL Compiler. +const char CB_MAIN_STUB[] = +R"__cs_cb( +/* + * Generated by tidlc . */ -const char CB_DATA_MEMBERS[] = -R"__cs_cb( private List _services = new List(); - private Type _serviceType; - private static readonly string _tidlVersion = ""; - private Dictionary> _privilege_map = new Dictionary>(); - private static Message _instance; - private bool _isListen = false; - private SynchronizationContext _async_context = TizenSynchronizationContext.Current; -)__cs_cb"; -const char CB_LEM_BASE[] = -R"__cs_cb( internal class LocalExecution +using System; +using System.Collections.Generic; +using System.Collections.Concurrent; +using System.Threading; +using System.Reflection; +using Tizen.Applications; +using Tizen.Applications.RPCPort; + +namespace RPCPort +{ + namespace { - object _instance = null; - MethodInfo _send_result = null; - MethodInfo _send_request = null; - MethodInfo _disconnect = null; + internal class LocalExecution + { + object _instance = null; + MethodInfo _send_result = null; + MethodInfo _send_request = null; + MethodInfo _disconnect = null; - internal bool _online = false; + internal bool _online = false; - internal LocalExecution (object instance) - { - Type type = instance.GetType(); + internal LocalExecution (object instance) + { + Type type = instance.GetType(); - if (type == null) - return; + if (type == null) + return; - _send_result = type.GetMethod("OnLemResultEvent", BindingFlags.NonPublic | BindingFlags.Instance); - if (_send_result == null) { return; } + _send_result = type.GetMethod("OnLemResultEvent", BindingFlags.NonPublic | BindingFlags.Instance); + if (_send_result == null) { return; } - _send_request = type.GetMethod("OnLemReceivedEvent", BindingFlags.NonPublic | BindingFlags.Instance); + _send_request = type.GetMethod("OnLemReceivedEvent", BindingFlags.NonPublic | BindingFlags.Instance); - _disconnect = type.GetMethod("OnLemDisconnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); - if (_disconnect == null) { return; } + _disconnect = type.GetMethod("OnLemDisconnectedEvent", BindingFlags.NonPublic | BindingFlags.Instance); + if (_disconnect == null) { return; } - _instance = instance; - _online = true; + _instance = instance; + _online = true; } @@ -74,366 +78,403 @@ R"__cs_cb( internal class LocalExecution internal void Disconnect() { if (_online) - _disconnect.Invoke(_instance, null); + _disconnect.Invoke(_instance, null); _online = false; } } -)__cs_cb"; -const char CB_SERVICE_BASE_FRONT[] = -R"__cs_cb( - public abstract class ServiceBase + + + namespace Stub + { + + public class : StubBase { - internal bool _is_app = true; - internal List _privileges; - internal LocalExecution _lem; - - /// - /// The client app ID - /// - public string Sender + + private List _services = new List(); + private Type _serviceType; + private static readonly string _tidlVersion = ""; + private Dictionary> _privilege_map = new Dictionary>(); + private static _instance; + private bool _isListen = false; + private SynchronizationContext _async_context = TizenSynchronizationContext.Current; + + public abstract class ServiceBase { - get; internal set; - } + internal bool _is_app = true; + internal List _privileges; + internal LocalExecution _lem; + + /// + /// The client app ID + /// + public string Sender + { + get; internal set; + } - /// - /// The client instance ID - /// - public string Instance - { - get; internal set; - } + /// + /// The client instance ID + /// + public string Instance + { + get; internal set; + } - public Port Port - { - get; - internal set; + public Port Port + { + get; + internal set; + } + + protected ServiceBase() + { + } + + internal void LoadPrivilege() + { + ApplicationInfo info = new ApplicationInfo(Sender); + if (info.PackageId == string.Empty) + { + _is_app = false; + return; + } + + Package pkg = PackageManager.GetPackage(info.PackageId); + _privileges = new List(pkg.Privileges); + } + + /// + /// Disconnects from the proxy app + /// + /// + /// Thrown when an internal IO error occurrs. + /// + public void Disconnect() + { + if(_lem != null) + { + _lem.Disconnect(); + } + else + { + Port?.Disconnect(); + Port = null; + } + } + + /// + /// This method will be called when the client is connected + /// + public abstract void OnCreate(); + + /// + /// This method will be called when the client is disconnected + /// + public abstract void OnTerminate(); + + + public abstract + + } - protected ServiceBase() + + + + + + + + + private bool CheckPrivilege(MethodId id, ServiceBase b) { + if (_privilege_map.ContainsKey(id) == false) + return true; + + foreach (var item in _privilege_map[id]) + { + if (b._privileges.Contains(item) == false) { + Tizen.Log.Error("RPC_PORT", "Permission denied. " + b.Sender + " : " + item); + return false; + } + } + + return true; } - internal void LoadPrivilege() + protected override bool OnReceivedEvent(string sender, string instance, Port port) { - ApplicationInfo info = new ApplicationInfo(Sender); - if (info.PackageId == string.Empty) + Parcel p; + + try { - _is_app = false; - return; + p = new Parcel(port); + } + catch (InvalidIOException) + { + return false; } - Package pkg = PackageManager.GetPackage(info.PackageId); - _privileges = new List(pkg.Privileges); + return OnReceivedEvent(instance, port, p); } - /// - /// Disconnects from the proxy app - /// - /// - /// Thrown when an internal IO error occurrs. - /// - public void Disconnect() + internal void OnLemReceivedEvent(string instance, Parcel p) { - if(_lem != null) + if (Thread.CurrentThread.ManagedThreadId != 1) { - _lem.Disconnect(); + _async_context.Post((data) => + { + (()data).OnReceivedEvent(instance, null, p); + }, this); } else { - Port?.Disconnect(); - Port = null; + OnReceivedEvent(instance, null, p); } } - /// - /// This method will be called when the client is connected - /// - public abstract void OnCreate(); - - /// - /// This method will be called when the client is disconnected - /// - public abstract void OnTerminate(); -)__cs_cb"; + internal bool OnReceivedEvent(string instance, Port port, Parcel p) + { + try + { + ServiceBase b = null; + foreach (var i in _services) + { + if (i.Instance.Equals(instance)) + { + b = i; + break; + } + } -const char CB_CHECK_PRIVILEGE_METHOD[] = -R"__cs_cb( - private bool CheckPrivilege(MethodId id, ServiceBase b) - { - if (_privilege_map.ContainsKey(id) == false) - return true; + if (b == null) + { + return false; + } - foreach (var item in _privilege_map[id]) - { - if (b._privileges.Contains(item) == false) { - Tizen.Log.Error("RPC_PORT", "Permission denied. " + b.Sender + " : " + item); - return false; - } - } + Port cb_port; + if (b._lem != null) + cb_port = null; + else + cb_port = GetPort(Port.Type.Callback, instance); - return true; - } -)__cs_cb"; + var result = new Parcel(); -const char CB_PRIVILEGE_MAP_BASE[] = -R"__cs_cb( - List list = new List(); - - _privilege_map[MethodId.] = list; -)__cs_cb"; + int cmd; + UnitMap map = new UnitMap(); + map.Deserialize(p); + map.Read( /*[METHOD]*/, out cmd); -const char CB_ON_RECEIVED_EVENT_FRONT[] = -R"__cs_cb( - protected override bool OnReceivedEvent(string sender, string instance, Port port) - { - Parcel p; + switch ((MethodId)cmd) + { + + case MethodId.: + { + + + map.Read( /**/, out CallbackBase del_param); + var param = new (cb_port, new WeakReference(b), del_param); + + + map.Read( /**/, out param); + + + UnitMap result_map = new UnitMap(); + if (b._is_app && CheckPrivilege((MethodId)cmd, b) == false) + { + RemoteException ex = new RemoteException("Permission denied", (int)Tizen.Internals.Errors.ErrorCode.PermissionDenied); + result_map.Write( /*[REMOTE_EXCEPTION]*/, ex); + } else { + + try + { + var retVal = b.(); + result_map.Write( /*[RESULT]*/, retVal); + + result_map.Write( /**/, param); + + } catch (RemoteException ex) + { + result_map.Write( /*[REMOTE_EXCEPTION]*/, ex); + } + + + b.(); + + } + + + ParcelHeader header = p.GetHeader(); + ParcelHeader resultHeader = result.GetHeader(); + resultHeader.SetTag(_tidlVersion); + resultHeader.SetSequenceNumber(header.GetSequenceNumber()); + int ret_cmd = (int)MethodId.__Result; + result_map.Write( /*[METHOD]*/, ret_cmd); + result_map.Serialize(result); + + if (b._lem != null) + b._lem.SendResult(result); + else + result.Send(port); + + break; + } + + + default: + return false; + } - try - { - p = new Parcel(port); + return true; + } + catch (InvalidIOException) + { + return false; + } + finally + { + p.Dispose(); + } } - catch (InvalidIOException) + + protected override void OnConnectedEvent(string sender, string instance) { - return false; + ServiceBase s = Activator.CreateInstance(_serviceType) as ServiceBase; + s.Sender = sender; + s.LoadPrivilege(); + s.Instance = instance; + s.Port = GetPort(Port.Type.Callback, instance); + _services.Add(s); + s.OnCreate(); } - return OnReceivedEvent(instance, port, p); - } - - internal void OnLemReceivedEvent(string instance, Parcel p) - { - if (Thread.CurrentThread.ManagedThreadId != 1) + internal void OnLemConnectedEvent(object proxy_instance, string sender, string instance) { - _async_context.Post((data) => + ServiceBase s = Activator.CreateInstance(_serviceType) as ServiceBase; + s.Sender = sender; + s.LoadPrivilege(); + s.Instance = instance; + s._lem = new LocalExecution(proxy_instance); + _services.Add(s); + if (Thread.CurrentThread.ManagedThreadId != 1) { - ((Message)data).OnReceivedEvent(instance, null, p); - }, this); - } - else - { - OnReceivedEvent(instance, null, p); + _async_context.Post((data) => + { + ((ServiceBase)data).OnCreate(); + }, s); + } + else + { + s.OnCreate(); + } } - } - internal bool OnReceivedEvent(string instance, Port port, Parcel p) - { - try + protected override void OnDisconnectedEvent(string sender, string instance) { - ServiceBase b = null; - - foreach (var i in _services) + foreach (var i in _services) { if (i.Instance.Equals(instance)) { - b = i; + if (i._lem != null) + i._lem._online = false; + + i.OnTerminate(); + _services.Remove(i); break; } } + } - if (b == null) + internal void OnLemDisconnectedEvent(string sender, string instance) + { + if (Thread.CurrentThread.ManagedThreadId != 1) { - return false; + _async_context.Post((data) => + { + (()data).OnDisconnectedEvent(sender, instance); + }, this); } - - var result = new Parcel(); - - int cmd; - UnitMap map = new UnitMap(); - map.Deserialize(p); - - map.Read("[METHOD]", out cmd); - - switch ((MethodId)cmd) + else { -)__cs_cb"; - -const char CB_ON_RECEIVED_EVENT_BACK[] = -R"__cs_cb( - default: - return false; + OnDisconnectedEvent(sender, instance); } - - return true; } - catch (InvalidIOException) + + internal static GetInstance() { - return false; + return _instance; } - finally + + internal bool GetListenStatus() { - p.Dispose(); + return _isListen; } - } -)__cs_cb"; -const char CB_ON_CONNECTED_EVENT[] = -R"__cs_cb( - protected override void OnConnectedEvent(string sender, string instance) - { - ServiceBase s = Activator.CreateInstance(_serviceType) as ServiceBase; - s.Sender = sender; - s.LoadPrivilege(); - s.Instance = instance; - s.Port = GetPort(Port.Type.Callback, instance); - _services.Add(s); - s.OnCreate(); - } + public () : base("") + { + _instance = this; + + + AddPrivilege(""); + + + SetTrusted(); + + + + + List list = new List(); + + _privilege_map[MethodId.] = list; + + } - internal void OnLemConnectedEvent(object proxy_instance, string sender, string instance) - { - ServiceBase s = Activator.CreateInstance(_serviceType) as ServiceBase; - s.Sender = sender; - s.LoadPrivilege(); - s.Instance = instance; - s._lem = new LocalExecution(proxy_instance); - _services.Add(s); - if (Thread.CurrentThread.ManagedThreadId != 1) + /// + /// Listens to client apps + /// + /// The type object for making service instances + /// + /// Thrown when internal I/O error happen. + /// + /// + /// Thrown when serviceType is invalid. + /// + public void Listen(Type serviceType) { - _async_context.Post((data) => - { - ((ServiceBase)data).OnCreate(); - }, s); + if (!typeof(ServiceBase).IsAssignableFrom(serviceType)) + throw new ArgumentException("Invalid type"); + _serviceType = serviceType; + Listen(); + _isListen = true; } - else + + /// + /// Gets service objects which are connected + /// + /// The enumerable service objects which are connected + public IEnumerable GetServices() { - s.OnCreate(); + return _services; } - } -)__cs_cb"; -const char CB_ON_DISCONNECTED_EVENT[] = -R"__cs_cb( - protected override void OnDisconnectedEvent(string sender, string instance) - { - foreach (var i in _services) + protected override void OnTerminatedEvent() { - if (i.Instance.Equals(instance)) + foreach (var i in _services) { if (i._lem != null) - i._lem._online = false; - + i._lem._online = false; i.OnTerminate(); - _services.Remove(i); - break; } } - } - internal void OnLemDisconnectedEvent(string sender, string instance) - { - if (Thread.CurrentThread.ManagedThreadId != 1) - { - _async_context.Post((data) => - { - ((Message)data).OnDisconnectedEvent(sender, instance); - }, this); - } - else - { - OnDisconnectedEvent(sender, instance); - } + } -)__cs_cb"; - -const char CB_CTOR_FRONT[] = -R"__cs_cb( - internal static Message GetInstance() - { - return _instance; - } - - internal bool GetListenStatus() - { - return _isListen; - } - - public $$() : base("$$") - { - _instance = this; -)__cs_cb"; - -const char CB_COMMON_METHODS[] = -R"__cs_cb( - /// - /// Listens to client apps - /// - /// The type object for making service instances - /// - /// Thrown when internal I/O error happen. - /// - /// - /// Thrown when serviceType is invalid. - /// - public void Listen(Type serviceType) - { - if (!typeof(ServiceBase).IsAssignableFrom(serviceType)) - throw new ArgumentException("Invalid type"); - _serviceType = serviceType; - Listen(); - _isListen = true; - } - - /// - /// Gets service objects which are connected - /// - /// The enumerable service objects which are connected - public IEnumerable GetServices() - { - return _services; - } -)__cs_cb"; - -const char CB_DISPOSABLE[] = -R"__cs_cb( - protected override void OnTerminatedEvent() - { - foreach (var i in _services) - { - if (i._lem != null) - i._lem._online = false; - i.OnTerminate(); - } - } -)__cs_cb"; - -constexpr const char CB_INVOCATION_CHECK_PRIVILEGE[] = -R"__cs_cb( -if (b._is_app && CheckPrivilege((MethodId)cmd, b) == false) -{ - RemoteException ex = new RemoteException("Permission denied", (int)Tizen.Internals.Errors.ErrorCode.PermissionDenied); - result_map.Write("[REMOTE_EXCEPTION]", ex); -} else { - + } + } } -)__cs_cb"; - -constexpr const char CB_INVOCATION_REMOTE_EXCEPTION[] = -R"__cs_cb(try -{ - - - -} catch (RemoteException ex) -{ - result_map.Write("[REMOTE_EXCEPTION]", ex); -})__cs_cb"; - -constexpr const char CB_INVOCATION_RESULT[] = -R"__cs_cb( -ParcelHeader header = p.GetHeader(); -ParcelHeader resultHeader = result.GetHeader(); -resultHeader.SetTag(_tidlVersion); -resultHeader.SetSequenceNumber(header.GetSequenceNumber()); -result_map.Write("[METHOD]", (int)MethodId.__Result); -result_map.Serialize(result); - -if (b._lem != null) - b._lem.SendResult(result); -else - result.Send(port); )__cs_cb";