コード例 #1
0
ファイル: MapBits.cs プロジェクト: anddudek/anjlab.fx
        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)));
            }
        }
コード例 #2
0
ファイル: RightShift.cs プロジェクト: anddudek/anjlab.fx
 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;
 }
コード例 #3
0
ファイル: ValueCondition.cs プロジェクト: anddudek/anjlab.fx
        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));
        }
コード例 #4
0
ファイル: MapByName.cs プロジェクト: anddudek/anjlab.fx
        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);
            }
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: Do.cs プロジェクト: anddudek/anjlab.fx
        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);"));
        }
コード例 #7
0
ファイル: If.cs プロジェクト: anddudek/anjlab.fx
        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)) }));
            }

        }
コード例 #8
0
ファイル: While.cs プロジェクト: anddudek/anjlab.fx
        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("}"));
        }
コード例 #9
0
ファイル: MapBytes.cs プロジェクト: anddudek/anjlab.fx
        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"));
            }
        }
コード例 #10
0
ファイル: MapBytesSegment.cs プロジェクト: anddudek/anjlab.fx
        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));
        }
コード例 #11
0
ファイル: ValueCondition.cs プロジェクト: anddudek/anjlab.fx
 private string Preprocess(CodeGenerationContext ctx, string value)
 {
     string preprocessed = value.Replace("$result", ctx.ResultObjectName);
     return preprocessed;
 }
コード例 #12
0
 public override void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method)
 {
     method.Statements.AddRange(ctx.Builder.NewElementsMappingCode(ctx, Nodes));
 }
コード例 #13
0
ファイル: MapElement.cs プロジェクト: anddudek/anjlab.fx
 public abstract void GenerateMappingCode(CodeGenerationContext ctx, CodeMemberMethod method);