csharp: Refactor annotation selection function

Summary:
Better names to convey their intent and formatting.

Removing the hardcoded `return` from them will also free the return list
to be used for `@out` parameters as they have similar semantics to
`return`, different from regular `@in` parameters. This change in
behavior will come in a future commit, and explains why are are keeping
both lists for now despite being essentially the same.

Ref T8515

Test Plan: Run existing tests

Reviewers: felipealmeida, brunobelo, jptiz, YOhoho

Reviewed By: jptiz

Subscribers: cedric, #reviewers, #committers

Tags: #efl

Maniphest Tasks: T8515

Differential Revision: https://phab.enlightenment.org/D10827
This commit is contained in:
Lauro Moura 2019-12-09 16:40:13 -03:00
parent 06c2bbf798
commit f793939315
1 changed files with 70 additions and 65 deletions

View File

@ -28,15 +28,15 @@ namespace eina = efl::eina;
namespace detail { namespace detail {
template <typename Array, typename F, int N, typename A> template <typename Array, typename SelectionPredicate, int N, typename AcceptFunc>
eina::optional<bool> call_annotation_match(Array const (&array)[N], F f, A a) eina::optional<bool> call_annotation_match(Array const (&array)[N], SelectionPredicate predicate, AcceptFunc acceptFunc)
{ {
typedef Array const* iterator_type; typedef Array const* iterator_type;
iterator_type match_iterator = &array[0], match_last = match_iterator + N; iterator_type match_iterator = &array[0], match_last = match_iterator + N;
match_iterator = std::find_if(match_iterator, match_last, f); match_iterator = std::find_if(match_iterator, match_last, predicate);
if(match_iterator != match_last) if(match_iterator != match_last)
{ {
return a(match_iterator->function()); return acceptFunc(match_iterator->function());
} }
return {nullptr}; return {nullptr};
} }
@ -63,103 +63,108 @@ struct marshall_annotation_visitor_generate
eina::optional<bool> has_own; eina::optional<bool> has_own;
std::function<std::string()> function; std::function<std::string()> function;
}; };
// These two tables are currently the same but will hold different marshallers
// for @in and @out/return semantics in a future commit.
match const parameter_match_table[] = match const parameter_match_table[] =
{ {
// signed primitives // signed primitives
{"bool", nullptr, [&] { return "[MarshalAs(UnmanagedType.U1)]"; }}, {"bool", nullptr, [] { return "MarshalAs(UnmanagedType.U1)"; }},
{"string", true, [&] { {"string", true, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))";
}}, }},
{"string", false, [&] { {"string", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))";
}}, }},
{"mstring", true, [&] { {"mstring", true, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))";
}}, }},
{"mstring", false, [&] { {"mstring", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))";
}}, }},
{"stringshare", true, [&] { {"stringshare", true, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringsharePassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringsharePassOwnershipMarshaler))";
}}, }},
{"stringshare", false, [&] { {"stringshare", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringshareKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringshareKeepOwnershipMarshaler))";
}}, }},
{"any_value_ref", true, [&] { {"any_value_ref", true, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshalerOwn))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshalerOwn))";
}}, }},
{"any_value_ref", false, [&] { {"any_value_ref", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshaler))";
}}, }},
{"strbuf", true, [&] { {"strbuf", true, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufPassOwnershipMarshaler))";
}}, }},
{"strbuf", false, [&] { {"strbuf", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufKeepOwnershipMarshaler))";
}}, }},
{"Value_Type", false, [&] { {"Value_Type", false, [] {
return "[MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueTypeMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueTypeMarshaler))";
}}, }},
}; };
match const return_match_table[] = match const return_match_table[] =
{ {
// signed primitives // signed primitives
{"bool", nullptr, [&] { return "[return: MarshalAs(UnmanagedType.U1)]"; }}, {"bool", nullptr, [] { return "MarshalAs(UnmanagedType.U1)"; }},
{"string", true, [&] { {"string", true, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))";
}}, }},
{"string", nullptr, [&] { {"string", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))";
}}, }},
{"mstring", true, [&] { {"mstring", true, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringPassOwnershipMarshaler))";
}}, }},
{"mstring", false, [&] { {"mstring", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringKeepOwnershipMarshaler))";
}}, }},
{"stringshare", true, [&] { {"stringshare", true, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringsharePassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringsharePassOwnershipMarshaler))";
}}, }},
{"stringshare", false, [&] { {"stringshare", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringshareKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StringshareKeepOwnershipMarshaler))";
}}, }},
{"any_value_ref", true, [&] { {"any_value_ref", true, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshalerOwn))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshalerOwn))";
}}, }},
{"any_value_ref", false, [&] { {"any_value_ref", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueMarshaler))";
}}, }},
{"strbuf", true, [&] { {"strbuf", true, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufPassOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufPassOwnershipMarshaler))";
}}, }},
{"strbuf", false, [&] { {"strbuf", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufKeepOwnershipMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Efl.Eo.StrbufKeepOwnershipMarshaler))";
}}, }},
{"Value_Type", false, [&] { {"Value_Type", false, [] {
return "[return: MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueTypeMarshaler))]"; return "MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef=typeof(Eina.ValueTypeMarshaler))";
}}, }},
}; };
if(eina::optional<bool> b = call_annotation_match auto predicate = [&regular] (match const& m)
((is_return ? return_match_table : parameter_match_table)
, [&] (match const& m)
{ {
return (!m.name || *m.name == regular.base_type) return (!m.name || *m.name == regular.base_type)
&& (!m.has_own || *m.has_own == (bool)(regular.base_qualifier & qualifier_info::is_own)) && (!m.has_own || *m.has_own == (bool)(regular.base_qualifier & qualifier_info::is_own))
; ;
} };
, [&] (std::string const& string)
auto acceptCb = [this] (std::string const& marshalTag)
{ {
std::copy(string.begin(), string.end(), sink); std::string prefix = is_return ? "return: " : "";
return true; return as_generator("[" << prefix << marshalTag << "]").generate(sink, nullptr, *context);
})) };
{
return *b; const auto& match_table = is_return ? return_match_table : parameter_match_table;
}
else if(eina::optional<bool> b = call_annotation_match(match_table, predicate, acceptCb))
{ {
return true; return *b;
} }
else
{
return true;
}
} }
bool operator()(attributes::klass_name const& klass_name) const bool operator()(attributes::klass_name const& klass_name) const
{ {