forked from enlightenment/efl
mono: reduce duplicated event code
Summary: Introduction GetInternalEventCallback function to reduce duplicated event code. Test Plan: ninja test Reviewers: lauromoura Reviewed By: lauromoura Subscribers: cedric, #reviewers, #committers Tags: #efl Differential Revision: https://phab.enlightenment.org/D10638
This commit is contained in:
parent
73494aa71c
commit
77c1777ca2
|
@ -37,7 +37,7 @@ struct unpack_event_args_visitor
|
|||
typedef bool result_type;
|
||||
bool operator()(grammar::attributes::regular_type_def const& regular) const
|
||||
{
|
||||
std::string const& arg = "evt.Info";
|
||||
std::string const& arg = "info";
|
||||
std::string arg_type = name_helpers::type_full_managed_name(regular);
|
||||
|
||||
if (regular.is_struct())
|
||||
|
@ -45,7 +45,7 @@ struct unpack_event_args_visitor
|
|||
// Structs are usually passed by pointer to events, like having a ptr<> modifier
|
||||
// Uses implicit conversion from IntPtr
|
||||
return as_generator(
|
||||
" evt.Info"
|
||||
" info"
|
||||
).generate(sink, attributes::unused, *context);
|
||||
}
|
||||
else if (type.is_ptr)
|
||||
|
@ -121,17 +121,17 @@ struct unpack_event_args_visitor
|
|||
// Type defined in Eo is passed here. (e.g. enum type defined in Eo)
|
||||
// Uses conversion from IntPtr with type casting to the given type.
|
||||
return as_generator(
|
||||
" (" << arg_type << ")evt.Info"
|
||||
" (" << arg_type << ")info"
|
||||
).generate(sink, attributes::unused, *context);
|
||||
}
|
||||
}
|
||||
bool operator()(grammar::attributes::klass_name const& cls) const
|
||||
{
|
||||
return as_generator("(Efl.Eo.Globals.CreateWrapperFor(evt.Info) as " + name_helpers::klass_full_concrete_name(cls) + ")").generate(sink, attributes::unused, *context);
|
||||
return as_generator("(Efl.Eo.Globals.CreateWrapperFor(info) as " + name_helpers::klass_full_concrete_name(cls) + ")").generate(sink, attributes::unused, *context);
|
||||
}
|
||||
bool operator()(attributes::complex_type_def const&) const
|
||||
{
|
||||
return as_generator("new " << eolian_mono::type << "(evt.Info, false, false)").generate(sink, type, *context);
|
||||
return as_generator("new " << eolian_mono::type << "(info, false, false)").generate(sink, type, *context);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -411,8 +411,8 @@ struct event_definition_generator
|
|||
|
||||
auto sub_context = change_indentation(indent.inc().inc(), context);
|
||||
|
||||
if (!as_generator(wrapper_args_type << " args = new " << wrapper_args_type << "();\n"
|
||||
<< scope_tab(6) << "args.arg = ").generate(arg_initializer_sink, attributes::unused, context))
|
||||
if (!as_generator(", info => new " << wrapper_args_type << "{ "
|
||||
<< "arg = ").generate(arg_initializer_sink, attributes::unused, context))
|
||||
return false;
|
||||
if (!(*etype).original_type.visit(unpack_event_args_visitor<decltype(arg_initializer_sink), decltype(sub_context)>{arg_initializer_sink, &sub_context, *etype}))
|
||||
return false;
|
||||
|
@ -420,7 +420,7 @@ struct event_definition_generator
|
|||
if (!(*etype).original_type.visit(pack_event_info_and_call_visitor<decltype(event_call_site_sink), decltype(sub_context)>{event_call_site_sink, &sub_context, *etype}))
|
||||
return false;
|
||||
|
||||
arg_initializer += ";\n";
|
||||
arg_initializer += " }";
|
||||
|
||||
event_args = arg_initializer;
|
||||
}
|
||||
|
@ -540,24 +540,9 @@ struct event_definition_generator
|
|||
return as_generator(
|
||||
scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << "add\n"
|
||||
<< scope_tab << scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << scope_tab << "Efl.EventCb callerCb = (IntPtr data, ref Efl.Event.NativeStruct evt) =>\n"
|
||||
<< scope_tab << scope_tab << scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << "var obj = Efl.Eo.Globals.WrapperSupervisorPtrToManaged(data).Target;\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << "if (obj != null)\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << event_args
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "try\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "value?.Invoke(obj, args);\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "}\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "catch (Exception e)\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "{\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "Eina.Log.Error(e.ToString());\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << scope_tab << "}\n"
|
||||
<< scope_tab << scope_tab << scope_tab << scope_tab << "}\n"
|
||||
<< scope_tab << scope_tab << scope_tab << "};\n\n"
|
||||
<< scope_tab << scope_tab << "{\n"//evt.type.is_engaged()
|
||||
<< scope_tab << scope_tab << scope_tab << "Efl.EventCb callerCb = GetInternalEventCallback(value"
|
||||
<< (evt.type.is_engaged() ? event_args : "") << ");\n"
|
||||
<< scope_tab << scope_tab << scope_tab << "string key = \"_" << upper_c_name << "\";\n"
|
||||
<< scope_tab << scope_tab << scope_tab << "AddNativeEventHandler(" << library_name << ", key, callerCb, value);\n"
|
||||
<< scope_tab << scope_tab << "}\n\n"
|
||||
|
|
|
@ -325,6 +325,44 @@ public abstract class EoWrapper : IWrapper, IDisposable
|
|||
}
|
||||
}
|
||||
}
|
||||
internal Efl.EventCb GetInternalEventCallback<T>(EventHandler<T> handler, Func<IntPtr, T> createArgsInstance) where T:EventArgs
|
||||
{
|
||||
return (IntPtr data, ref Efl.Event.NativeStruct evt) =>
|
||||
{
|
||||
var obj = Efl.Eo.Globals.WrapperSupervisorPtrToManaged(data).Target;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
handler?.Invoke(obj, createArgsInstance(evt.Info));
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Eina.Log.Error(e.ToString());
|
||||
Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
internal Efl.EventCb GetInternalEventCallback(EventHandler handler)
|
||||
{
|
||||
return (IntPtr data, ref Efl.Event.NativeStruct evt) =>
|
||||
{
|
||||
var obj = Efl.Eo.Globals.WrapperSupervisorPtrToManaged(data).Target;
|
||||
if (obj != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
handler?.Invoke(obj, EventArgs.Empty);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Eina.Log.Error(e.ToString());
|
||||
Eina.Error.Set(Eina.Error.UNHANDLED_EXCEPTION);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private static void OwnershipUniqueCallback(IntPtr data, ref Efl.Event.NativeStruct evt)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue