]> O.S.I.I.S - jp/crow.git/commitdiff
code clean and comments
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 20 Jan 2017 15:27:38 +0000 (16:27 +0100)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Fri, 20 Jan 2017 15:27:38 +0000 (16:27 +0100)
src/CompilerServices/CompilerServices.cs
src/IML/Node.cs
src/IML/NodeAddress.cs
src/Instantiator.cs

index 86dbd66a26ffa5243534aab812bd5d7d7f39b957..c626a6e2092f007f88762c145d06c71a7f2f9f79 100644 (file)
@@ -648,7 +648,7 @@ namespace Crow
                                il.Emit (OpCodes.Ldarg_0);  //load sender ref onto the stack, the current node
 
                                if (lopParts.Length > 1) {
-                                       NodeAddress lopNA = getNodeAdressFromBindingExp (currentNode, ref operandes [0]);
+                                       NodeAddress lopNA = currentNode.ResolveExpression (ref operandes [0]);
                                        CompilerServices.emitGetInstance (il, currentNode, lopNA);
                                        lopType = lopNA.NodeType;
                                }
@@ -772,7 +772,7 @@ namespace Crow
                                il.Emit (OpCodes.Ldarg_0);  //load sender ref onto the stack
 
                                if (lopParts.Length > 1) {
-                                       NodeAddress lopNA = getNodeAdressFromBindingExp (currentNode, ref operandes [0]);
+                                       NodeAddress lopNA = currentNode.ResolveExpression (ref operandes [0]);
                                        CompilerServices.emitGetInstance (il, currentNode, lopNA);
                                        lopType = lopNA.NodeType;
                                }
@@ -876,40 +876,6 @@ namespace Crow
                                exps.Add(expression);
                        return exps.ToArray ();
                }
-
-               /// <summary>
-               /// Gets the node adress from binding expression starting at sourceAddr
-               /// and return in expression remaining part
-               /// </summary>
-               internal static NodeAddress getNodeAdressFromBindingExp(NodeAddress sourceAddr, ref string expression){
-                       int ptr = sourceAddr.Count - 1;
-                       string[] splitedExp = expression.Split ('/');
-
-                       if (splitedExp.Length < 2)//dataSource binding
-                               return null;
-
-                       if (string.IsNullOrEmpty (splitedExp [0]) || splitedExp [0] == ".") {//search template root
-                               ptr--;
-                               while (ptr >= 0) {
-                                       if (typeof(TemplatedControl).IsAssignableFrom (sourceAddr [ptr].CrowType))
-                                               break;
-                                       ptr--;
-                               }
-                       } else if (splitedExp [0] == "..") { //search starting at current node
-                               int levelUp = splitedExp.Length - 1;
-                               if (levelUp > ptr + 1)
-                                       throw new Exception ("Binding error: try to bind outside IML source");
-                               ptr -= levelUp;
-                       }
-                       expression = splitedExp [splitedExp.Length - 1];
-                       //TODO:change Template special address identified with Nodecount = 0 to something not using array count to 0,
-                       //here linq is working without limits checking in compile option
-                       //but defining a 0 capacity array with limits cheking enabled, cause 'out of memory' error
-                       return new NodeAddress (sourceAddr.Take(ptr+1).ToArray());//[ptr+1];
-                       //Array.Copy (sourceAddr.ToArray (), targetNode, ptr + 1);
-                       //return new NodeAddress (targetNode);
-               }
-
        }
 }
 
index 4ba61c03e07443b892f1208385a22e9e494261da..29f204f50c50879fa0afdef3ecd7c7719995405b 100644 (file)
@@ -30,16 +30,18 @@ namespace Crow.IML
        /// </summary>
        public struct Node
        {
-               /// <summary> Current node type</summary>
-               public Type CrowType;
-               /// <summary> Index in parent, -1 for template</summary>
-               public int Index;
-
+               #region CTOR
                public Node (Type crowType, int _index = 0)
                {
                        CrowType = crowType;
                        Index = _index;
                }
+               #endregion
+
+               /// <summary> Current node type</summary>
+               public Type CrowType;
+               /// <summary> Index in parent, -1 for template</summary>
+               public int Index;
 
                public MethodInfo GetAddMethod(int childIdx){
                        if (typeof (Group).IsAssignableFrom (CrowType))
index 460998dadd61586f06ea506e505a77b5ec11de75..c81a7f6663e7c5178b2d2523dfd785279f6d07d0 100644 (file)
@@ -26,13 +26,83 @@ using System.Reflection.Emit;
 
 namespace Crow.IML
 {
+       /// <summary>
+       /// Node address is a list of nodes from root to leaf defining a unique node
+       /// </summary>
        public class NodeAddress : List<Node>
        {
-               public NodeAddress (Node[] nodes) : base(nodes) { 
-               }
+               #region CTOR
+               public NodeAddress (Node[] nodes) : base(nodes) {}
+               #endregion
 
                public Type NodeType { get { return Count == 0 ? null : this[this.Count -1].CrowType; }}
 
+               /// <summary>
+               /// Gets the node adress from binding expression starting from this node
+               /// and return in expression remaining part
+               /// </summary>
+               public NodeAddress ResolveExpression (ref string expression){
+                       int ptr = this.Count - 1;
+                       string[] splitedExp = expression.Split ('/');
+
+                       if (splitedExp.Length < 2)//dataSource binding
+                               return null;
+
+                       if (string.IsNullOrEmpty (splitedExp [0]) || splitedExp [0] == ".") {//search template root
+                               ptr--;
+                               while (ptr >= 0) {
+                                       if (typeof(TemplatedControl).IsAssignableFrom (this [ptr].CrowType))
+                                               break;
+                                       ptr--;
+                               }
+                       } else if (splitedExp [0] == "..") { //search starting at current node
+                               int levelUp = splitedExp.Length - 1;
+                               if (levelUp > ptr + 1)
+                                       throw new Exception ("Binding error: try to bind outside IML source");
+                               ptr -= levelUp;
+                       }
+                       expression = splitedExp [splitedExp.Length - 1];
+                       //TODO:change Template special address identified with Nodecount = 0 to something not using array count to 0,
+                       //here linq is working without limits checking in compile option
+                       //but defining a 0 capacity array with limits cheking enabled, cause 'out of memory' error
+                       return new NodeAddress (this.Take(ptr+1).ToArray());//[ptr+1];
+                       //Array.Copy (sourceAddr.ToArray (), targetNode, ptr + 1);
+                       //return new NodeAddress (targetNode);
+               }
+               /// <summary>
+               /// get BindingDefinition from binding expression
+               /// </summary>
+               public BindingDefinition GetBindingDef(string sourceMember, string expression){
+                       BindingDefinition bindingDef = new BindingDefinition(this, sourceMember);
+                       if (string.IsNullOrEmpty (expression)) {
+                               return bindingDef;
+                       } else {
+                               if (expression.StartsWith ("²")) {
+                                       bindingDef.TwoWay = true;
+                                       expression = expression.Substring (1);
+                               }
+
+                               string exp = expression;
+                               bindingDef.TargetNA = this.ResolveExpression (ref exp);
+
+                               string [] bindTrg = exp.Split ('.');
+
+                               if (bindTrg.Length == 0)
+                                       throw new Exception ("invalid binding expression: " + expression);
+                               if (bindTrg.Length == 1)
+                                       bindingDef.TargetMember = bindTrg [0];
+                               else {
+                                       if (!string.IsNullOrEmpty(bindTrg[0]))//searchByName
+                                               bindingDef.TargetName = bindTrg[0];
+
+                                       bindingDef.TargetMember = exp.Substring (bindTrg[0].Length + 1);
+                               }
+                       }
+
+                       return bindingDef;
+               }
+
+               #region Object overrides
                public override bool Equals (object obj)
                {
                        if (obj == null) 
@@ -50,7 +120,6 @@ namespace Crow.IML
                                return hash;
                        }
                }
-
                public override string ToString ()
                {
                        string tmp = "";
@@ -58,6 +127,7 @@ namespace Crow.IML
                                tmp += string.Format ("{0};", n.Index);                 
                        return tmp;
                }
+               #endregion
        }
 
        public class NamedNodeAddress : NodeAddress {
index 883f31291b13ee152badd6732c93632c9e528206..6e94ae171f4582383c0721213c24b47b4a66a9d1 100644 (file)
@@ -356,7 +356,7 @@ namespace Crow
                void readPropertyBinding (Context ctx, string sourceMember, string expression)
                {
                        NodeAddress sourceNA = ctx.CurrentNodeAddress;
-                       BindingDefinition bindingDef = genBindingDef (sourceNA, sourceMember, expression);
+                       BindingDefinition bindingDef = sourceNA.GetBindingDef (sourceMember, expression);
 
                        #if DEBUG_BINDING
                        Debug.WriteLine("Property Binding: " + bindingDef.ToString());
@@ -368,39 +368,6 @@ namespace Crow
                                ctx.StorePropertyBinding (bindingDef);
                }
 
-               /// <summary>
-               /// get BindingDefinition from binding expression
-               /// </summary>
-               BindingDefinition genBindingDef(NodeAddress sourceNA, string sourceMember, string expression){
-                       BindingDefinition bindingDef = new BindingDefinition(sourceNA, sourceMember);
-                       if (string.IsNullOrEmpty (expression)) {
-                               return bindingDef;
-                       } else {
-                               if (expression.StartsWith ("²")) {
-                                       bindingDef.TwoWay = true;
-                                       expression = expression.Substring (1);
-                               }
-
-                               string exp = expression;
-                               bindingDef.TargetNA = CompilerServices.getNodeAdressFromBindingExp (sourceNA, ref exp);
-
-                               string [] bindTrg = exp.Split ('.');
-
-                               if (bindTrg.Length == 0)
-                                       throw new Exception ("invalid binding expression: " + expression);
-                               if (bindTrg.Length == 1)
-                                       bindingDef.TargetMember = bindTrg [0];
-                               else {
-                                       if (!string.IsNullOrEmpty(bindTrg[0]))//searchByName
-                                               bindingDef.TargetName = bindTrg[0];
-
-                                       bindingDef.TargetMember = exp.Substring (bindTrg[0].Length + 1);
-                               }
-                       }
-
-                       return bindingDef;
-               }
-
                #region Emit Helper
                void dataSourceChangedEmitHelper(object dscSource, object dataSource, int dynMethIdx){
                        if (dataSource is IValueChange)
@@ -424,7 +391,7 @@ namespace Crow
                /// <summary> Emits handler method bindings </summary>
                void emitHandlerBinding (Context ctx, EventInfo sourceEvent, string expression){
                        NodeAddress currentNode = ctx.CurrentNodeAddress;
-                       BindingDefinition bindingDef = genBindingDef (currentNode, sourceEvent.Name, expression);
+                       BindingDefinition bindingDef = currentNode.GetBindingDef (sourceEvent.Name, expression);
 
                        #if DEBUG_BINDING
                        Debug.WriteLine("Event Binding: " + bindingDef.ToString());