From 9015042f06059cef3f8880bf1dea19be310ec286 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jean-Philippe=20Bruy=C3=A8re?= Date: Mon, 29 Jan 2018 04:24:36 +0100 Subject: [PATCH] :bulb: :lipstick: :bug: template attribute fetch when no default template exist. --- Crow.csproj | 2 +- Tests/Tests.csproj | 2 +- Tests/ui/showcase.crow | 3 +- src/CompilerServices/CompilerServices.cs | 17 +++++ src/GraphicObjects/TemplatedGroup.cs | 14 ++-- src/IML/Node.cs | 5 ++ src/Instantiator.cs | 82 +++++++++++------------- src/ItemTemplate.cs | 11 +--- 8 files changed, 69 insertions(+), 67 deletions(-) diff --git a/Crow.csproj b/Crow.csproj index a78add0b..c556bc79 100644 --- a/Crow.csproj +++ b/Crow.csproj @@ -31,7 +31,7 @@ true full true - DEBUG_UPDATE0;DEBUG_FOCUS0;DEBUG_LAYOUTING0;TRACE0;DEBUG;__linux__;MEASURE_TIME0;DEBUG_LOAD0;DEBUG_BINDING0;DEBUG_CLIP_RECTANGLE0 + DEBUG_UPDATE0;DEBUG_FOCUS0;DEBUG_LAYOUTING0;TRACE0;DEBUG;MEASURE_TIME;DEBUG_LOAD0;DEBUG_BINDING0;DEBUG_CLIP_RECTANGLE0 false diff --git a/Tests/Tests.csproj b/Tests/Tests.csproj index 21cb740d..2adcdb16 100644 --- a/Tests/Tests.csproj +++ b/Tests/Tests.csproj @@ -8,7 +8,7 @@ Exe Tests Tests - Tests.BasicTests + Tests.Showcase v4.5 AnyCPU 0.5 diff --git a/Tests/ui/showcase.crow b/Tests/ui/showcase.crow index a511a100..24b038d6 100755 --- a/Tests/ui/showcase.crow +++ b/Tests/ui/showcase.crow @@ -1,7 +1,6 @@ - + diff --git a/src/CompilerServices/CompilerServices.cs b/src/CompilerServices/CompilerServices.cs index 827ee617..b119b3e7 100644 --- a/src/CompilerServices/CompilerServices.cs +++ b/src/CompilerServices/CompilerServices.cs @@ -846,6 +846,23 @@ namespace Crow exps.Add(expression); return exps.ToArray (); } + /// + /// Try to get the type named strDataType, search first in crow assembly then in + /// entry assembly. + /// + /// the corresponding type object if found + /// type name + internal static Type tryGetType (string strDataType){ + Type dataType = Type.GetType(strDataType); + if (dataType != null) + return dataType; + Assembly a = Assembly.GetEntryAssembly (); + foreach (Type expT in a.GetExportedTypes ()) { + if (expT.Name == strDataType) + return dataType; + } + return null; + } } } diff --git a/src/GraphicObjects/TemplatedGroup.cs b/src/GraphicObjects/TemplatedGroup.cs index 60f18994..3d5b7e83 100644 --- a/src/GraphicObjects/TemplatedGroup.cs +++ b/src/GraphicObjects/TemplatedGroup.cs @@ -266,6 +266,9 @@ namespace Crow // } #endregion + /// + /// Items loading thread + /// void loading(){ if (ItemTemplates == null) ItemTemplates = new Dictionary (); @@ -359,16 +362,7 @@ namespace Crow iTemp = ItemTemplates [itempKey]; else { foreach (string it in ItemTemplates.Keys) { - Type t = Type.GetType (it); - if (t == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == it) { - t = expT; - break; - } - } - } + Type t = CompilerServices.tryGetType (it); if (t == null) continue; if (t.IsAssignableFrom (dataType)) {//TODO:types could be cached diff --git a/src/IML/Node.cs b/src/IML/Node.cs index 2b63cf4b..8b85388b 100644 --- a/src/IML/Node.cs +++ b/src/IML/Node.cs @@ -49,6 +49,11 @@ namespace Crow.IML /// Index in parent, -1 for template public int Index; + /// + /// retrieve the child addition method depending on the type of this node + /// + /// The child addition method + /// child index or, template root node has index == -1 public MethodInfo GetAddMethod(int childIdx){ if (typeof (Group).IsAssignableFrom (CrowType)) return CompilerServices.miAddChild; diff --git a/src/Instantiator.cs b/src/Instantiator.cs index 4c236cd4..3520ce83 100644 --- a/src/Instantiator.cs +++ b/src/Instantiator.cs @@ -93,7 +93,7 @@ namespace Crow return new Instantiator (s); } } catch (Exception ex) { - throw new Exception ("Error loading fragment:\n" + fragment + "\n", ex); + throw new Exception ("Error loading IML fragment:\n" + fragment + "\n", ex); } } #endregion @@ -145,17 +145,7 @@ namespace Crow break; } } - - Type t = Type.GetType ("Crow." + root); - if (t == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == root) { - t = expT; - break; - } - } - } + Type t = tryGetGOType (root); if (t == null) throw new Exception ("IML parsing error: undefined root type (" + root + ")"); return t; @@ -177,9 +167,9 @@ namespace Crow List itemTemplateIds = new List (); bool inlineTemplate = false; + reader.Read (); string templatePath = reader.GetAttribute ("Template"); - reader.Read (); int depth = reader.Depth + 1; while (reader.Read ()) { if (!reader.IsStartElement () || reader.Depth > depth) @@ -310,7 +300,6 @@ namespace Crow ctx.nodesStack.ResetCurrentNodeIndex (); } } - /// /// Parse child node an generate corresponding msil /// @@ -336,16 +325,7 @@ namespace Crow ctx.il.Emit (OpCodes.Ldloc_0); ctx.il.Emit (OpCodes.Ldloc_0); - Type t = Type.GetType ("Crow." + reader.Name); - if (t == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == reader.Name) { - t = expT; - break; - } - } - } + Type t = tryGetGOType (reader.Name); if (t == null) throw new Exception (reader.Name + " type not found"); @@ -392,6 +372,25 @@ namespace Crow (dataSource as IValueChange).ValueChanged += (EventHandler)dsValueChangedDynMeths [dynMethIdx].CreateDelegate (typeof(EventHandler), dscSource); } + /// Emits remove old data source event handler./summary> + void emitRemoveOldDataSourceHandler(ILGenerator il, string eventName, string delegateName, bool DSSide = true){ + System.Reflection.Emit.Label cancel = il.DefineLabel (); + + il.Emit (OpCodes.Ldarg_2);//load old parent + il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCOldDS); + il.Emit (OpCodes.Brfalse, cancel);//old parent is null + + //remove handler + if (DSSide){//event is defined in the dataSource instance + il.Emit (OpCodes.Ldarg_2);//1st arg load old datasource + il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCOldDS); + }else//the event is in the source + il.Emit (OpCodes.Ldarg_1);//1st arg load old datasource + il.Emit (OpCodes.Ldstr, eventName);//2nd arg event name + il.Emit (OpCodes.Ldstr, delegateName);//3d arg: delegate name + il.Emit (OpCodes.Call, CompilerServices.miRemEvtHdlByName); + il.MarkLabel(cancel); + } #endregion #region Event Bindings @@ -970,26 +969,23 @@ namespace Crow } #endregion - /// Emits remove old data source event handler./summary> - void emitRemoveOldDataSourceHandler(ILGenerator il, string eventName, string delegateName, bool DSSide = true){ - System.Reflection.Emit.Label cancel = il.DefineLabel (); - - il.Emit (OpCodes.Ldarg_2);//load old parent - il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCOldDS); - il.Emit (OpCodes.Brfalse, cancel);//old parent is null - - //remove handler - if (DSSide){//event is defined in the dataSource instance - il.Emit (OpCodes.Ldarg_2);//1st arg load old datasource - il.Emit (OpCodes.Ldfld, CompilerServices.fiDSCOldDS); - }else//the event is in the source - il.Emit (OpCodes.Ldarg_1);//1st arg load old datasource - il.Emit (OpCodes.Ldstr, eventName);//2nd arg event name - il.Emit (OpCodes.Ldstr, delegateName);//3d arg: delegate name - il.Emit (OpCodes.Call, CompilerServices.miRemEvtHdlByName); - il.MarkLabel(cancel); + /// + /// search for graphic object type in crow assembly, if not found, + /// search for type independently of namespace in entry assembly + /// + /// the corresponding type object + /// graphic object type name without its namespace + Type tryGetGOType (string typeName){ + Type t = Type.GetType ("Crow." + typeName); + if (t != null) + return t; + Assembly a = Assembly.GetEntryAssembly (); + foreach (Type expT in a.GetExportedTypes ()) { + if (expT.Name == typeName) + return expT; + } + return null; } - } } diff --git a/src/ItemTemplate.cs b/src/ItemTemplate.cs index 53619042..fc7d9063 100644 --- a/src/ItemTemplate.cs +++ b/src/ItemTemplate.cs @@ -65,16 +65,7 @@ namespace Crow #endregion public void CreateExpandDelegate (TemplatedGroup host){ - Type dataType = Type.GetType(strDataType); - if (dataType == null) { - Assembly a = Assembly.GetEntryAssembly (); - foreach (Type expT in a.GetExportedTypes ()) { - if (expT.Name == strDataType) { - dataType = expT; - break; - } - } - } + Type dataType = CompilerServices.tryGetType(strDataType); if (dataType == null) { Debug.WriteLine ("ItemTemplate error: DataType not found: {0}.", strDataType); return; -- 2.47.3