From 4a101d97c97a304192e3d0a1a47e4e66ad6e18df Mon Sep 17 00:00:00 2001 From: Kah Wei Date: Sat, 4 Feb 2023 00:19:57 +0800 Subject: [PATCH] CallbackEvents are now able to handle situations where a CallbackAction results in adding more CallbackActions --- SHADE_CSharp/src/Events/CallbackEvent.cs | 33 ++++++++++++++++-------- SHADE_CSharp/src/Events/CallbackEvent.tt | 3 ++- 2 files changed, 24 insertions(+), 12 deletions(-) diff --git a/SHADE_CSharp/src/Events/CallbackEvent.cs b/SHADE_CSharp/src/Events/CallbackEvent.cs index 2c3cc388..44209288 100644 --- a/SHADE_CSharp/src/Events/CallbackEvent.cs +++ b/SHADE_CSharp/src/Events/CallbackEvent.cs @@ -117,10 +117,11 @@ namespace SHADE /// public void Invoke() { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(); } catch (Exception e) @@ -203,10 +204,11 @@ namespace SHADE /// public void Invoke(T1 t1) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1); } catch (Exception e) @@ -289,10 +291,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2); } catch (Exception e) @@ -375,10 +378,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3); } catch (Exception e) @@ -461,10 +465,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4); } catch (Exception e) @@ -547,10 +552,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5); } catch (Exception e) @@ -633,10 +639,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5, t6); } catch (Exception e) @@ -719,10 +726,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5, t6, t7); } catch (Exception e) @@ -805,10 +813,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5, t6, t7, t8); } catch (Exception e) @@ -891,10 +900,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5, t6, t7, t8, t9); } catch (Exception e) @@ -977,10 +987,11 @@ namespace SHADE /// public void Invoke(T1 t1, T2 t2, T3 t3, T4 t4, T5 t5, T6 t6, T7 t7, T8 t8, T9 t9, T10 t10) { - foreach (CallbackAction action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction; action.Invoke(t1, t2, t3, t4, t5, t6, t7, t8, t9, t10); } catch (Exception e) diff --git a/SHADE_CSharp/src/Events/CallbackEvent.tt b/SHADE_CSharp/src/Events/CallbackEvent.tt index 66a8b6d9..8d903d0f 100644 --- a/SHADE_CSharp/src/Events/CallbackEvent.tt +++ b/SHADE_CSharp/src/Events/CallbackEvent.tt @@ -134,10 +134,11 @@ namespace SHADE /// public void Invoke(<# if (i != 0) { for (int t = 1; t < i + 1; ++t) { #>T<#=t#> t<#=t#><# if (t != i) { #>, <# } #><# } } #>) { - foreach (CallbackAction<# if (i != 0) { #><<# for (int t = 1; t < i + 1; ++t) { #>T<#=t#><# if (t != i) { #>, <# } #><# } #>><# } #> action in actions) + for (int i = 0; i < actions.Count; ++i) { try { + var action = actions[i] as CallbackAction<# if (i != 0) { #><<# for (int t = 1; t < i + 1; ++t) { #>T<#=t#><# if (t != i) { #>, <# } #><# } #>><# } #>; action.Invoke(<# for (int t = 1; t < i + 1; ++t) { #>t<#=t#><# if (t != i) { #>, <# } #><# } #>); } catch (Exception e)