]> O.S.I.I.S - jp/crow.git/commitdiff
Code simplification, only 1 cachedDelegates list.
authorjpbruyere <jp.bruyere@hotmail.com>
Fri, 16 Dec 2016 11:42:59 +0000 (12:42 +0100)
committerjpbruyere <jp.bruyere@hotmail.com>
Fri, 16 Dec 2016 11:42:59 +0000 (12:42 +0100)
modifié :         src/IML/Context.cs
modifié :         src/Instantiator.cs

src/IML/Context.cs
src/Instantiator.cs

index 79ab8a31c3314907dbd6db92c88d74382e74e6da..8679dd4cd5b797b3036912fd14037ff6c40b62da 100644 (file)
@@ -83,13 +83,13 @@ namespace Crow.IML
                        CompilerServices.emitSetCurInterface (il);
                }
 
-               public void emitDataSourceChangedHandlerAddition(int index){
+               public void emitCachedDelegateHandlerAddition(int index, EventInfo evt){
                        il.Emit(OpCodes.Ldloc_0);//load ref to current graphic object
                        il.Emit(OpCodes.Ldarg_0);//load ref to this instanciator onto the stack
-                       il.Emit(OpCodes.Ldfld, typeof(Instantiator).GetField("dataSourceChangedDelegates", BindingFlags.Instance | BindingFlags.NonPublic));
+                       il.Emit(OpCodes.Ldfld, typeof(Instantiator).GetField("cachedDelegates", BindingFlags.Instance | BindingFlags.NonPublic));
                        il.Emit(OpCodes.Ldc_I4, index);//load delegate index
                        il.Emit(OpCodes.Callvirt, typeof(List<Delegate>).GetMethod("get_Item", new Type[] { typeof(Int32) }));
-                       il.Emit(OpCodes.Callvirt, typeof(GraphicObject).GetEvent("DataSourceChanged").AddMethod);//call add event
+                       il.Emit(OpCodes.Callvirt, evt.AddMethod);//call add event
                }
        }
 }
\ No newline at end of file
index dcf53259e56cbe59410b906d91fa1d9884097a07..74c0da5375ffc069968812ada0b00eb83a690a50 100644 (file)
@@ -84,11 +84,9 @@ namespace Crow
                }
 
                List<DynamicMethod> dsValueChangedDynMeths = new List<DynamicMethod>();
-               List<Delegate> dataSourceChangedDelegates = new List<Delegate>();
-               List<Delegate> templateValueChangedDelegates = new List<Delegate>();
+               List<Delegate> cachedDelegates = new List<Delegate>();
                Dictionary<string, Delegate> bindingDelegates = new Dictionary<string, Delegate>();//valuechanged del
                Dictionary<string, Delegate> bindingInitializer = new Dictionary<string, Delegate>();//initialize with actual values of binding origine
-               List<Delegate> eventDynHandlers = new List<Delegate>();
                Delegate templateBinding;
 
                #region IML parsing
@@ -256,7 +254,7 @@ namespace Crow
                                                        foreach (string exp in splitOnSemiColumnOutsideAccolades(reader.Value)) {
                                                                string trimed = exp.Trim();
                                                                if (trimed.StartsWith ("{", StringComparison.OrdinalIgnoreCase))
-                                                                       emitEventHandler (ctx, mi as EventInfo, trimed.Substring (1, trimed.Length - 2));
+                                                                       compileAndStoreDynHandler (ctx, mi as EventInfo, trimed.Substring (1, trimed.Length - 2));
                                                                else
                                                                        emitHandlerBinding (ctx, mi as EventInfo, trimed);
                                                        }
@@ -586,12 +584,11 @@ namespace Crow
                        il.Emit (OpCodes.Ret);
 
                        //store dschange delegate in instatiator instance for access while instancing graphic object
-                       int delDSIndex = dataSourceChangedDelegates.Count;
-                       dataSourceChangedDelegates.Add(dm.CreateDelegate (CompilerServices.ehTypeDSChange, this));
+                       int delDSIndex = cachedDelegates.Count;
+                       cachedDelegates.Add(dm.CreateDelegate (CompilerServices.ehTypeDSChange, this));
                        #endregion
 
-                       //Emit datasourcechanged handler binding in the loader context
-                       ctx.emitDataSourceChangedHandlerAddition(delDSIndex);
+                       ctx.emitCachedDelegateHandlerAddition(delDSIndex, typeof(GraphicObject).GetEvent("DataSourceChanged"));
                }
 
                #region Emit Helper
@@ -606,7 +603,7 @@ namespace Crow
                /// Compile events expression in IML attributes, and store the result in the instanciator
                /// Those handlers will be bound when instatiing
                /// </summary>
-               void emitEventHandler (Context ctx, EventInfo sourceEvent, string expression)
+               void compileAndStoreDynHandler (Context ctx, EventInfo sourceEvent, string expression)
                {
                        #if DEBUG_BINDING
                        Debug.WriteLine ("\tCompile Event Source ");
@@ -730,17 +727,9 @@ namespace Crow
                        #endregion
 
                        //store event handler dynamic method in instanciator
-                       int dmIdx = eventDynHandlers.Count;
-                       eventDynHandlers.Add (dm.CreateDelegate (sourceEvent.EventHandlerType));
-
-                       #region Emit event handler binding in the loader context
-                       ctx.il.Emit(OpCodes.Ldloc_0);//load ref to current graphic object
-                       ctx.il.Emit(OpCodes.Ldarg_0);//load ref to this instanciator onto the stack
-                       ctx.il.Emit(OpCodes.Ldfld, typeof(Instantiator).GetField("eventDynHandlers", BindingFlags.Instance | BindingFlags.NonPublic));
-                       ctx.il.Emit(OpCodes.Ldc_I4, dmIdx);//load delegate index
-                       ctx.il.Emit(OpCodes.Callvirt, typeof(List<Delegate>).GetMethod("get_Item", new Type[] { typeof(Int32) }));
-                       ctx.il.Emit(OpCodes.Callvirt, sourceEvent.AddMethod);//call add event
-                       #endregion
+                       int dmIdx = cachedDelegates.Count;
+                       cachedDelegates.Add (dm.CreateDelegate (sourceEvent.EventHandlerType));
+                       ctx.emitCachedDelegateHandlerAddition(dmIdx, sourceEvent);
                }
 
                void emitHandlerBinding (Context ctx, EventInfo sourceEvent, string expression){
@@ -786,11 +775,10 @@ namespace Crow
                                il.Emit (OpCodes.Ret);
 
                                //store dschange delegate in instatiator instance for access while instancing graphic object
-                               int delDSIndex = dataSourceChangedDelegates.Count;
-                               dataSourceChangedDelegates.Add(dm.CreateDelegate (CompilerServices.ehTypeDSChange, this));
+                               int delDSIndex = cachedDelegates.Count;
+                               cachedDelegates.Add(dm.CreateDelegate (CompilerServices.ehTypeDSChange, this));
 
-                               //Emit datasourcechanged handler binding in the loader context
-                               ctx.emitDataSourceChangedHandlerAddition(delDSIndex);
+                               ctx.emitCachedDelegateHandlerAddition(delDSIndex, typeof(GraphicObject).GetEvent("DataSourceChanged"));
                                return;
                        }
                }
@@ -926,19 +914,11 @@ namespace Crow
                        ilPC.Emit (OpCodes.Ret);
 
                        //store dschange delegate in instatiator instance for access while instancing graphic object
-                       int delDSIndex = dataSourceChangedDelegates.Count;
-                       dataSourceChangedDelegates.Add(dmPC.CreateDelegate (CompilerServices.ehTypeDSChange, this));
-                       #endregion
-
-                       #region Emit datasourcechanged handler binding in the loader context
-                       ctx.il.Emit(OpCodes.Ldloc_0);//load ref to current graphic object
-                       ctx.il.Emit(OpCodes.Ldarg_0);//load ref to this instanciator onto the stack
-                       ctx.il.Emit(OpCodes.Ldfld, typeof(Instantiator).GetField("dataSourceChangedDelegates", BindingFlags.Instance | BindingFlags.NonPublic));
-                       ctx.il.Emit(OpCodes.Ldc_I4, delDSIndex);//load delegate index
-                       ctx.il.Emit(OpCodes.Callvirt, typeof(List<Delegate>).GetMethod("get_Item", new Type[] { typeof(Int32) }));
-                       ctx.il.Emit(OpCodes.Callvirt, typeof(GraphicObject).GetEvent("ParentChanged").AddMethod);//call add event
+                       int delDSIndex = cachedDelegates.Count;
+                       cachedDelegates.Add(dmPC.CreateDelegate (CompilerServices.ehTypeDSChange, this));
                        #endregion
 
+                       ctx.emitCachedDelegateHandlerAddition(delDSIndex, typeof(GraphicObject).GetEvent("ParentChanged"));
                }
                void emitBindingDelegate(NodeAddress origine, Dictionary<string, List<MemberAddress>> bindings){
                        Type origineNodeType = origine.NodeType;
@@ -1014,9 +994,9 @@ namespace Crow
 
                                i++;
                        }
-                       //il.Emit (OpCodes.Pop);
-                       il.MarkLabel (endMethod);
+
                        ilInit.Emit (OpCodes.Ret);
+                       il.MarkLabel (endMethod);
                        il.Emit (OpCodes.Ret);
 
                        bindingDelegates [origine.ToString()] = dm.CreateDelegate (typeof(EventHandler<ValueChangeEventArgs>));