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
This commit is contained in:
Lauro Moura 2019-11-13 10:05:19 -03:00
parent 790fa0e04b
commit bdf4396dfe
3 changed files with 61 additions and 0 deletions

View 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;

View 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

View 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;