Exemple #1
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            if (!String.IsNullOrEmpty(To))
                MappedProperty = TypeReflector.GetProperty(ctx.MappedObjectType, To);

            if (MappedProperty != null)
            {
                if (MapNotBoolProperty)
                {
                    // type value = bitReader.ReadBits(length);
                    method.Statements.Add(new CodeVariableDeclarationStatement(MappedValueType, "value",
                        new CodeMethodInvokeExpression(ctx.DataReader, "ReadBits", new CodePrimitiveExpression(Length))));
                }
                else
                {
                    method.Statements.Add(
                        new CodeVariableDeclarationStatement(MappedValueType, "value",
                            new CodeBinaryOperatorExpression(
                                new CodeMethodInvokeExpression(ctx.DataReader, "ReadBits", new CodePrimitiveExpression(Length)),
                                CodeBinaryOperatorType.GreaterThan, new CodePrimitiveExpression(0))));
                }

                // add operations code
                CodeVariableReferenceExpression value = new CodeVariableReferenceExpression("value");
                foreach (IOperation op in Operations)
                    method.Statements.AddRange(op.BuildOperation(ctx, this, value));

                method.Statements.AddRange(GenerateSetMappedPropertyCode(ctx.MappedObject, value));
            }
            else
            {   // just read
                method.Statements.Add(
                    new CodeMethodInvokeExpression(ctx.DataReader, "ReadBits", new CodePrimitiveExpression(Length)));
            }
        }
Exemple #2
0
 public override CodeStatementCollection BuildOperation(CodeGenerationContext ctx, ICodeGeneratorNode element, CodeVariableReferenceExpression value)
 {
     CodeStatementCollection statemets = new CodeStatementCollection();
     statemets.Add(new CodeSnippetExpression(
         string.Format("{0} = ({2})((int){0} >> {1})", value.VariableName, Value, GetValueType(element.MappedProperty).FullName)));
     return statemets;
 }
Exemple #3
0
        public CodeExpression GetCondition(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            if (Scope == ValueScope.Segment)
                return new CodePropertyReferenceExpression(ctx.MappedObject, _value);

            if (Scope == ValueScope.Global)
                return new CodePropertyReferenceExpression(ctx.ResultObject, _value);

            return new CodeSnippetExpression(Preprocess(ctx, _value));
        }
Exemple #4
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            if (!string.IsNullOrEmpty(_key))
            {
                ICodeGeneratorNode element = ctx.MapInfo.GetNamedMapping(_key);

                Guard.NotNull(element, "Named mapping '{0}' not found for {1}", _key, ctx.MappedObjectType.FullName);

                element.To = To;
                element.GenerateMappingCode(ctx, method);
            }
        }
        public CodeExpression GetCondition(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            Guard.IsTrue(Next > 0, "Next property of NextBytesCondition can't be 0");
            Guard.IsTrue(!String.IsNullOrEmpty(Bytes), "You must specify BytesAre or BytesAreNot property of NextBytesCondition");

            CodeMemberMethod conditionMethod = ctx.Builder.AddNewMethod(typeof(bool));
            conditionMethod.Statements.Add(new CodeSnippetStatement(String.Format(@"
                System.IO.MemoryStream stream = new System.IO.MemoryStream({0});
                System.IO.BinaryReader r = new System.IO.BinaryReader(stream);
                r.BaseStream.Position = {1}.BaseStream.Position;
                byte[] data = r.ReadBytes({2});
                r.Close();
                stream.Close();
                byte[] defined = AnjLab.FX.Devices.Convert.HexStringToBytes(""{3}"");
                {4}", ctx.DataArray.VariableName, ctx.DataReader.VariableName, Next, Bytes, CompareCode)));

            return new CodeMethodInvokeExpression(new CodeThisReferenceExpression(), conditionMethod.Name);
        }
Exemple #6
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            Guard.NotNull(_while, "While property can't be null in Do mapping");
            //bool conditionResult = true;
            //do
            //{
            //    // code
            //    conditionResult = (1 > 2);
            //} while (condition);
            method.Statements.Add(new CodeSnippetStatement("bool conditionResult = true;"));
            CodeVariableReferenceExpression conditionResult = new CodeVariableReferenceExpression("conditionResult");
            method.Statements.Add(new CodeSnippetStatement("do {"));

            base.GenerateMappingCode(ctx, method);

            method.Statements.Add(new CodeAssignStatement(conditionResult, _while.GetCondition(ctx, method)));
            method.Statements.Add(new CodeSnippetStatement("} while (conditionResult);"));
        }
Exemple #7
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            Guard.NotNull(_condition, "'Condition' property can't be null in If mapping");
            Guard.NotNull(_true, "'True' property can't be null in If mapping");

            CodeMemberMethod trueMethod = ctx.Builder.NewElementMappingMethod(ctx, _true);

            if (_false != null)
            {
                CodeMemberMethod falseMethod = ctx.Builder.NewElementMappingMethod(ctx, _false);
                method.Statements.Add(new CodeConditionStatement(_condition.GetCondition(ctx, method),
                    new CodeStatement[] { new CodeSnippetStatement(String.Format("this.{0}();", trueMethod.Name)) },
                    new CodeStatement[] { new CodeSnippetStatement(String.Format("this.{0}();", falseMethod.Name)) }));
            }
            else
            {
                method.Statements.Add(new CodeConditionStatement(_condition.GetCondition(ctx, method),
                    new CodeStatement[] { new CodeSnippetStatement(String.Format("this.{0}();", trueMethod.Name)) }));
            }

        }
Exemple #8
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            Guard.NotNull(_condition, "While property can't be null in Do mapping");
            //bool conditionResult = condition;
            //while(condition)
            //{
            //    // code
            //    conditionResult = condition;
            //}
            CodeExpression condition = _condition.GetCondition(ctx, method);
            method.Statements.Add(new CodeVariableDeclarationStatement(
                typeof(bool), "conditionResult", condition));
            CodeVariableReferenceExpression conditionResult = new CodeVariableReferenceExpression("conditionResult");

            method.Statements.Add(new CodeSnippetStatement("while (conditionResult) {"));
            
            base.GenerateMappingCode(ctx, method);

            method.Statements.Add(new CodeAssignStatement(conditionResult, condition));
            method.Statements.Add(new CodeSnippetStatement("}"));
        }
Exemple #9
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            if (!String.IsNullOrEmpty(To))
                MappedProperty = TypeReflector.GetProperty(ctx.MappedObjectType, To);

            // byte[] bytes = reader.ReadBytes(length);
            method.Statements.Add(new CodeVariableDeclarationStatement(typeof(byte[]), "bytes",
                new CodeMethodInvokeExpression(ctx.DataReader, "ReadBytes", new CodePrimitiveExpression(Length))));

            if (MappedProperty != null)
            {
                // MemoryStream stream2 = new MemoryStream(bytes);
                CodeVariableReferenceExpression bytes = new CodeVariableReferenceExpression("bytes");
                method.Statements.Add(new CodeVariableDeclarationStatement(
                    typeof(MemoryStream), "stream2", new CodeObjectCreateExpression(typeof(MemoryStream), bytes)));
                CodeVariableReferenceExpression stream2 = new CodeVariableReferenceExpression("stream2");

                // BinaryReader newReader = new BinarryReader(stream2);
                
                method.Statements.Add(new CodeVariableDeclarationStatement(
                    typeof(BinaryReader), "reader2", new CodeObjectCreateExpression(typeof(BinaryReader), stream2)));
                CodeVariableReferenceExpression reader2 = new CodeVariableReferenceExpression("reader2");

                //Type value = ?
                method.Statements.Add(new CodeVariableDeclarationStatement(
                    MappedValueType, "value", GetPropertyValueExpression(MappedValueType, reader2)));
                CodeVariableReferenceExpression value = new CodeVariableReferenceExpression("value");

                // add operations code
                foreach (IOperation op in Operations)
                    method.Statements.AddRange(op.BuildOperation(ctx, this, value));
                
                method.Statements.AddRange(GenerateSetMappedPropertyCode(ctx.MappedObject, value));

                // newReader.Dispose();
                method.Statements.Add(new CodeMethodInvokeExpression(reader2, "Close"));
                // newStream.Dispose();
                method.Statements.Add(new CodeMethodInvokeExpression(stream2, "Close"));
            }
        }
Exemple #10
0
        public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
        {
            if (!String.IsNullOrEmpty(To))
                MappedProperty = TypeReflector.GetProperty(ctx.MappedObjectType, To);

            CodeExpression segmentObj;
            if (MappedProperty != null)
            {
                // mappedObj.PropName = new SegmentType();
                method.Statements.AddRange(GenerateSetMappedPropertyCode(ctx.MappedObject, new CodeObjectCreateExpression(_segmentType)));
                segmentObj = GetMappedProperty(ctx.MappedObject);
            }
            else
            {
                // this._segmentField = new SegmentType();
                segmentObj = ctx.Builder.AddNewField(SegmentType);
                method.Statements.Add(new CodeAssignStatement(segmentObj, new CodeObjectCreateExpression(_segmentType)));
            }

            CodeGenerationContext segmentCtx = ctx.Clone();
            segmentCtx.MappedObject = segmentObj;
            segmentCtx.MappedObjectType = _segmentType;
            method.Statements.AddRange(ctx.Builder.NewElementsMappingCode(segmentCtx, Nodes));
        }
Exemple #11
0
 private string Preprocess(CodeGenerationContext ctx, string value)
 {
     string preprocessed = value.Replace("$result", ctx.ResultObjectName);
     return preprocessed;
 }
 public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
 {
     method.Statements.AddRange(ctx.Builder.NewElementsMappingCode(ctx, Nodes));
 }
Exemple #13
0
 public abstract void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method);