csharp: Add conversion methods for generated types
authorLauro Moura <lauromoura@expertisesolutions.com.br>
Wed, 13 Nov 2019 13:05:19 +0000 (10:05 -0300)
committerJongmin Lee <jm105.lee@samsung.com>
Wed, 13 Nov 2019 21:13:17 +0000 (06:13 +0900)
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

src/bin/eolian_mono/eolian/mono/alias_definition.hh
src/bin/eolian_mono/eolian/mono/name_helpers.hh
src/bin/eolian_mono/eolian/mono/struct_definition.hh

index 1f17d6b..7f3f258 100644 (file)
@@ -78,6 +78,21 @@ struct alias_definition_generator
                  << scope_tab << "{\n"
                  << scope_tab << scope_tab << "return value.payload;\n"
                  << scope_tab << "}\n"
+
+                 << scope_tab << "/// <summary>Converts an instance of " << alias_type_doc << " to this struct.</summary>\n"
+                 << scope_tab << "/// <param name=\"value\">The value to be converted.</param>\n"
+                 << scope_tab << "/// <returns>A struct with the given value.</returns>\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 << "/// <summary>Converts an instance of this struct to " << alias_type_doc << ".</summary>\n"
+                 << scope_tab << "/// <returns>The actual value the alias is wrapping.</returns>\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;
index d5f68b7..2f3026d 100644 (file)
@@ -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<std::string, std::string> 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
index c733432..b3b8d71 100644 (file)
@@ -493,6 +493,26 @@ struct struct_definition_generator
             ).generate(sink, attributes::unused, context))
        return false;
 
+    if(!as_generator(
+            indent << scope_tab << "/// <summary>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 << "/// <para>Since EFL " + struct_.documentation.since + ".</para>\n"
+            ).generate(sink, attributes::unused, context))
+         return false;
+
+     if (!as_generator(
+            indent << scope_tab << "/// </summary>\n"
+            << indent << scope_tab << "/// <param name=\"ptr\">Native pointer to be converted.</param>\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;