Ameisen

Untitled

Feb 23rd, 2021
844
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     APIs that take Actions/delegates/callbacks only accept ones that take 'typed' forms of events
  3.     as callbacks, even though generally, in code, they are kept typeless as the type is not actually
  4.     useful in context.
  5.    
  6.     Thus, when delegates are provided as callbacks to Node object event handlers, they need to be wrapped
  7.     in an Action that performs a simple cast, even if the cast is implicit.
  8.    
  9.    
  10.     Take into note that 'LibFoo.Node' also has derived types, like 'LibFoo.NodeA', 'LibFoo.NodeB', and such, which inherit from it.
  11.     Events are expected to be specialized off of this.
  12.    
  13.     That is, an object of type LibFoo.NodeB's mouse callback would be expecting an Action<LibFoo.MouseEvent<LibFoo.NodeB>>, but generally
  14.     the actual callback would be an Action<LibFoo.MouseEvent>.
  15.    
  16.     As LibFoo.NodeB is derived from LibFoo.Node, LibFoo.Node is not implicitly castable to it.
  17.     Ergo, LibFoo.Event<LibFoo.Node> nor LibFoo.Event are implicitly castable to LibFoo.Event<LibFoo.NodeB>.
  18.     Ergo, Action<...> and so on.
  19.    
  20.     I would love a way to solve this without making hundreds of generic overloads as seen below.
  21. */
  22.  
  23. // LibFoo.Event<T> inherits from LibFoo.Event
  24. // T is constrained to LibFoo.Node in Event<T>
  25. public static Action<LibFoo.Event> Base<T>(this Action<LibFoo.Event<T>> action) where T : LibFoo.Node {
  26.     return new Action<LibFoo.Event>(ev => action.Invoke(ev as LibFoo.Event<T>));
  27. }
  28.  
  29. // LibFoo.MouseEvent<T> inherits from LibFoo.MouseEvent
  30. // LibFoo.MouseEvent inherits from LibFoo.Event
  31. // LibFoo.MouseEvent<T> does _not_ inherit from LibFoo.Event<T>
  32. // T is constrained to LibFoo.Node in Event<T> and MouseEvent<T>
  33. public static Action<LibFoo.MouseEvent> Base<T>(this Action<LibFoo.MouseEvent<T>> action) where T : LibFoo.Node {
  34.     return new Action<LibFoo.MouseEvent>(ev => action.Invoke(ev as LibFoo.MouseEvent<T>));
  35. }
  36.  
  37. // LibFoo.KeyboardEvent<T> inherits from LibFoo.KeyboardEvent
  38. // LibFoo.KeyboardEvent inherits from LibFoo.Event
  39. // LibFoo.KeyboardEvent<T> does _not_ inherit from LibFoo.Event<T>
  40. // T is constrained to LibFoo.Node in Event<T> and KeyboardEvent
  41. public static Action<LibFoo.KeyboardEvent> Base<T>(this Action<LibFo<T>o.KeyboardEvent<T>> action) where T : LibFoo.Node {
  42.     return new Action<LibFoo.KeyboardEvent>(ev => action.Invoke(ev as LibFoo.KeyboardEvent<T>));
  43. }
  44.  
  45. // LibFoo.WheelEvent<T> inherits from LibFoo.WheelEvent
  46. // LibFoo.WheelEvent inherits from LibFoo.Event
  47. // LibFoo.WheelEvent<T> does _not_ inherit from LibFoo.Event<T>
  48. // T is constrained to LibFoo.Node in Event<T> and WheelEvent<T>
  49. public static Action<LibFoo.WheelEvent> Base<T>(this Action<LibFoo.WheelEvent<T>> action) where T : LibFoo.Node {
  50.     return new Action<LibFoo.WheelEvent>(ev => action.Invoke(ev as LibFoo.WheelEvent<T>));
  51. }
  52.  
  53. // LibFoo.Event<T> inherits from LibFoo.Event
  54. // T is constrained to LibFoo.Node in Event<T>
  55. public static Action<LibFoo.Event<T>> Typed<T>(this Action<LibFoo.Event> action) where T : LibFoo.Node {
  56.     return new Action<LibFoo.Event<T>>(ev => action.Invoke(ev));
  57. }
  58.  
  59. // LibFoo.MouseEvent<T> inherits from LibFoo.MouseEvent
  60. // LibFoo.MouseEvent inherits from LibFoo.Event
  61. // LibFoo.MouseEvent<T> does _not_ inherit from LibFoo.Event<T>
  62. // T is constrained to LibFoo.Node in Event<T> and MouseEvent<T>
  63. public static Action<LibFoo.MouseEvent<T>> Typed<T>(this Action<LibFoo.MouseEvent> action) where T : LibFoo.Node {
  64.     return new Action<LibFoo.MouseEvent<T>>(ev => action.Invoke(ev));
  65. }
  66.  
  67. // LibFoo.KeyboardEvent<T> inherits from LibFoo.KeyboardEvent
  68. // LibFoo.KeyboardEvent inherits from LibFoo.Event
  69. // LibFoo.KeyboardEvent<T> does _not_ inherit from LibFoo.Event<T>
  70. // T is constrained to LibFoo.Node in Event<T> and KeyboardEvent<T>
  71. public static Action<LibFoo.KeyboardEvent<T>> Typed<T>(this Action<LibFoo.KeyboardEvent> action) where T : LibFoo.Node {
  72.     return new Action<LibFoo.KeyboardEvent<T>>(ev => action.Invoke(ev));
  73. }
  74.  
  75. // LibFoo.WheelEvent<T> inherits from LibFoo.WheelEvent
  76. // LibFoo.WheelEvent inherits from LibFoo.Event
  77. // LibFoo.WheelEvent<T> does _not_ inherit from LibFoo.Event<T>
  78. // T is constrained to LibFoo.Node in Event<T> and WheelEvent<T>
  79. public static Action<LibFoo.WheelEvent<T>> Typed<T>(this Action<LibFoo.WheelEvent> action) where T : LibFoo.Node {
  80.     return new Action<LibFoo.WheelEvent<T>>(ev => action.Invoke(ev));
  81. }
RAW Paste Data