Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- APIs that take Actions/delegates/callbacks only accept ones that take 'typed' forms of events
- as callbacks, even though generally, in code, they are kept typeless as the type is not actually
- useful in context.
- Thus, when delegates are provided as callbacks to Node object event handlers, they need to be wrapped
- in an Action that performs a simple cast, even if the cast is implicit.
- Take into note that 'LibFoo.Node' also has derived types, like 'LibFoo.NodeA', 'LibFoo.NodeB', and such, which inherit from it.
- Events are expected to be specialized off of this.
- That is, an object of type LibFoo.NodeB's mouse callback would be expecting an Action<LibFoo.MouseEvent<LibFoo.NodeB>>, but generally
- the actual callback would be an Action<LibFoo.MouseEvent>.
- As LibFoo.NodeB is derived from LibFoo.Node, LibFoo.Node is not implicitly castable to it.
- Ergo, LibFoo.Event<LibFoo.Node> nor LibFoo.Event are implicitly castable to LibFoo.Event<LibFoo.NodeB>.
- Ergo, Action<...> and so on.
- I would love a way to solve this without making hundreds of generic overloads as seen below.
- */
- // LibFoo.Event<T> inherits from LibFoo.Event
- // T is constrained to LibFoo.Node in Event<T>
- public static Action<LibFoo.Event> Base<T>(this Action<LibFoo.Event<T>> action) where T : LibFoo.Node {
- return new Action<LibFoo.Event>(ev => action.Invoke(ev as LibFoo.Event<T>));
- }
- // LibFoo.MouseEvent<T> inherits from LibFoo.MouseEvent
- // LibFoo.MouseEvent inherits from LibFoo.Event
- // LibFoo.MouseEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and MouseEvent<T>
- public static Action<LibFoo.MouseEvent> Base<T>(this Action<LibFoo.MouseEvent<T>> action) where T : LibFoo.Node {
- return new Action<LibFoo.MouseEvent>(ev => action.Invoke(ev as LibFoo.MouseEvent<T>));
- }
- // LibFoo.KeyboardEvent<T> inherits from LibFoo.KeyboardEvent
- // LibFoo.KeyboardEvent inherits from LibFoo.Event
- // LibFoo.KeyboardEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and KeyboardEvent
- public static Action<LibFoo.KeyboardEvent> Base<T>(this Action<LibFo<T>o.KeyboardEvent<T>> action) where T : LibFoo.Node {
- return new Action<LibFoo.KeyboardEvent>(ev => action.Invoke(ev as LibFoo.KeyboardEvent<T>));
- }
- // LibFoo.WheelEvent<T> inherits from LibFoo.WheelEvent
- // LibFoo.WheelEvent inherits from LibFoo.Event
- // LibFoo.WheelEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and WheelEvent<T>
- public static Action<LibFoo.WheelEvent> Base<T>(this Action<LibFoo.WheelEvent<T>> action) where T : LibFoo.Node {
- return new Action<LibFoo.WheelEvent>(ev => action.Invoke(ev as LibFoo.WheelEvent<T>));
- }
- // LibFoo.Event<T> inherits from LibFoo.Event
- // T is constrained to LibFoo.Node in Event<T>
- public static Action<LibFoo.Event<T>> Typed<T>(this Action<LibFoo.Event> action) where T : LibFoo.Node {
- return new Action<LibFoo.Event<T>>(ev => action.Invoke(ev));
- }
- // LibFoo.MouseEvent<T> inherits from LibFoo.MouseEvent
- // LibFoo.MouseEvent inherits from LibFoo.Event
- // LibFoo.MouseEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and MouseEvent<T>
- public static Action<LibFoo.MouseEvent<T>> Typed<T>(this Action<LibFoo.MouseEvent> action) where T : LibFoo.Node {
- return new Action<LibFoo.MouseEvent<T>>(ev => action.Invoke(ev));
- }
- // LibFoo.KeyboardEvent<T> inherits from LibFoo.KeyboardEvent
- // LibFoo.KeyboardEvent inherits from LibFoo.Event
- // LibFoo.KeyboardEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and KeyboardEvent<T>
- public static Action<LibFoo.KeyboardEvent<T>> Typed<T>(this Action<LibFoo.KeyboardEvent> action) where T : LibFoo.Node {
- return new Action<LibFoo.KeyboardEvent<T>>(ev => action.Invoke(ev));
- }
- // LibFoo.WheelEvent<T> inherits from LibFoo.WheelEvent
- // LibFoo.WheelEvent inherits from LibFoo.Event
- // LibFoo.WheelEvent<T> does _not_ inherit from LibFoo.Event<T>
- // T is constrained to LibFoo.Node in Event<T> and WheelEvent<T>
- public static Action<LibFoo.WheelEvent<T>> Typed<T>(this Action<LibFoo.WheelEvent> action) where T : LibFoo.Node {
- return new Action<LibFoo.WheelEvent<T>>(ev => action.Invoke(ev));
- }
Advertisement
Add Comment
Please, Sign In to add comment