From bdf4396dfefb0462605b741df674de764c9afaac Mon Sep 17 00:00:00 2001 From: Lauro Moura Date: Wed, 13 Nov 2019 10:05:19 -0300 Subject: [PATCH] csharp: Add conversion methods for generated types Summary: This also adds a helper method to convert from a value type name to the reference type name. (e.g. int to Int32). Ref T8430 Reviewers: felipealmeida, brunobelo, YOhoho Reviewed By: brunobelo Subscribers: cedric, #reviewers, #committers Tags: #efl Maniphest Tasks: T8430 Differential Revision: https://phab.enlightenment.org/D10653 --- .../eolian/mono/alias_definition.hh | 15 +++++++++++ .../eolian_mono/eolian/mono/name_helpers.hh | 26 +++++++++++++++++++ .../eolian/mono/struct_definition.hh | 20 ++++++++++++++ 3 files changed, 61 insertions(+) diff --git a/src/bin/eolian_mono/eolian/mono/alias_definition.hh b/src/bin/eolian_mono/eolian/mono/alias_definition.hh index 1f17d6b368..7f3f2588f3 100644 --- a/src/bin/eolian_mono/eolian/mono/alias_definition.hh +++ b/src/bin/eolian_mono/eolian/mono/alias_definition.hh @@ -78,6 +78,21 @@ struct alias_definition_generator << scope_tab << "{\n" << scope_tab << scope_tab << "return value.payload;\n" << scope_tab << "}\n" + + << scope_tab << "/// Converts an instance of " << alias_type_doc << " to this struct.\n" + << scope_tab << "/// The value to be converted.\n" + << scope_tab << "/// A struct with the given value.\n" + << scope_tab << "public static " << alias_name << " From" << name_helpers::translate_value_type(alias_type) << "(" << alias_type << " value)\n" + << scope_tab << "{\n" + << scope_tab << scope_tab << "return value;\n" + << scope_tab << "}\n\n" + + << scope_tab << "/// Converts an instance of this struct to " << alias_type_doc << ".\n" + << scope_tab << "/// The actual value the alias is wrapping.\n" + << scope_tab << "public " << alias_type << " To" << name_helpers::translate_value_type(alias_type) << "()\n" + << scope_tab << "{\n" + << scope_tab << scope_tab << "return this;\n" + << scope_tab << "}\n" << "}\n" ).generate(sink, alias, context)) return false; diff --git a/src/bin/eolian_mono/eolian/mono/name_helpers.hh b/src/bin/eolian_mono/eolian/mono/name_helpers.hh index d5f68b7331..2f3026dfdc 100644 --- a/src/bin/eolian_mono/eolian/mono/name_helpers.hh +++ b/src/bin/eolian_mono/eolian/mono/name_helpers.hh @@ -537,6 +537,32 @@ std::string constructor_managed_name(std::string full_name) return managed_name(tokens.at(tokens.size()-1)); } +std::string translate_value_type(std::string const& name) +{ + static std::map table = { + {"sbyte", "SByte"}, + {"byte","Byte"}, + {"short","Int16"}, + {"ushort","UInt16"}, + {"int", "Int32"}, + {"uint","UInt32"}, + {"long","Int64"}, + {"ulong","UInt64"}, + {"char","Char"}, + {"float","Single"}, + {"double","Double"}, + {"bool","Boolean"}, + {"decimal","Decimal"}, + }; + + auto found = table.find(name); + + if (found != table.end()) + return found->second; + + return name; +} + } // namespace name_helpers } // namespace eolian_mono diff --git a/src/bin/eolian_mono/eolian/mono/struct_definition.hh b/src/bin/eolian_mono/eolian/mono/struct_definition.hh index c733432465..b3b8d717f6 100644 --- a/src/bin/eolian_mono/eolian/mono/struct_definition.hh +++ b/src/bin/eolian_mono/eolian/mono/struct_definition.hh @@ -493,6 +493,26 @@ struct struct_definition_generator ).generate(sink, attributes::unused, context)) return false; + if(!as_generator( + indent << scope_tab << "/// Conversion to the managed representation from a native pointer.\n" + ).generate(sink, attributes::unused, context)) + return false; + + if (!struct_.documentation.since.empty()) + if (!as_generator(indent << scope_tab << "/// Since EFL " + struct_.documentation.since + ".\n" + ).generate(sink, attributes::unused, context)) + return false; + + if (!as_generator( + indent << scope_tab << "/// \n" + << indent << scope_tab << "/// Native pointer to be converted.\n" + << indent << scope_tab << "public static " << struct_name << " FromIntPtr(IntPtr ptr)\n" + << indent << scope_tab << "{\n" + << indent << scope_tab << scope_tab << "return ptr;\n" + << indent << scope_tab << "}\n\n" + ).generate(sink, attributes::unused, context)) + return false; + if (!struct_internal_definition.generate(sink, struct_, change_indentation(indent.inc(), context))) return false;