public void HandleRead(TypeHandlingContext context)
        {
            IType type = context.Type;

            IType valueType = type.GetNullableUnderlyingType();

            if (valueType == null)
            {
                throw new NotSupportedException();
            }

            var valueHandlingContext = new TypeHandlingContext(context)
            {
                Type          = valueType,
                TypeName      = valueType.GetPresentableName(context.PresentationLanguage),
                TypeOwnerName = String.Format("{0}", context.TypeOwnerName)
            };

            ITypeHandler valueHandler = TypeHandlers.All.FirstOrDefault(h => h.CanHandle(valueHandlingContext));

            if (valueHandler == null)
            {
                throw new NotSupportedException();
            }

            context.Builder.Append("if(BitConverter.ToBoolean(bytes, index)){");
            context.Builder.Append("index += sizeof(Boolean);");
            valueHandler.HandleRead(valueHandlingContext);
            context.Builder.Append("}");
            context.Builder.Append("else{");
            context.Builder.Append("index += sizeof(Boolean);");
            context.Builder.Append("}");
        }
示例#2
0
        public void HandleRead(TypeHandlingContext context)
        {
            IDeclaredType valueType = context.Type.GetScalarType();

            if (valueType == null)
            {
                throw new NotSupportedException();
            }

            ITypeHandler valueHandler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(context.WithType(valueType)));

            if (valueHandler == null)
            {
                throw new NotSupportedException();
            }

            var indexName  = context.Variables.Declare(Index);
            var lengthName = context.Variables.Declare(Length);
            var exists     = context.Variables.Declare(VariableKeys.NotNull);

            TypeHandlers.Boolean.HandleRead(new TypeHandlingContext(context)
            {
                TypeOwnerName = String.Format("var {0}", exists)
            });
            context.Builder.AppendFormat("if({0})", exists);
            context.Builder.Append("{");
            context.Builder.AppendFormat("var {0} = BitConverter.ToInt32(bytes, index);", lengthName);
            context.Builder.Append("index += sizeof(Int32);");
            context.Builder.AppendFormat("{0} = new {1}[{2}];", context.TypeOwnerName, valueType.GetPresentableName(context.PresentationLanguage), lengthName);
            context.Builder.AppendFormat("if({0} > 0)", lengthName);
            context.Builder.Append("{");
            context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}; {0}++)", indexName, lengthName);
            context.Builder.Append("{");
            valueHandler.HandleRead(new TypeHandlingContext(context)
            {
                TypeOwnerName = String.Format("{0}[{1}]", context.TypeOwnerName, indexName),
                Type          = valueType,
                TypeName      = valueType.GetPresentableName(context.PresentationLanguage),
                TypeOwner     = context.TypeOwner
            });
            context.Builder.Append("}}}");

            context.Variables.Dispose(Index);
            context.Variables.Dispose(Length);
        }
        protected override void Generate(CSharpGeneratorContext context,
                                         IMethodDeclaration declaration,
                                         IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                         CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            ctx.Builder.Append("{");
            ctx.Builder.Append("var index = startIndex;");
            ctx.Builder.Append("Byte[] tmp;");
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleRead(ctx);
                    }
                }
            }
            ctx.Builder.Append("return index;");
            ctx.Builder.Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }