Ejemplo n.º 1
0
        public void HandleGetSize(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 exists = context.Variables.Declare(VariableKeys.NotNull);

            context.Builder.AppendFormat("var {0} = {1} != null;", exists, context.TypeOwnerName);
            TypeHandlers.Boolean.HandleGetSize(new TypeHandlingContext(context)
            {
                TypeOwnerName = exists
            });

            context.Builder.AppendFormat("if({0})", exists).Append("{");
            TypeHandlers.Int32.HandleGetSize(new TypeHandlingContext(context)
            {
                TypeOwnerName = String.Format("{0}.Length", context.TypeOwnerName)
            });
            context.Builder.AppendFormat("if({0}.Length > 0)", context.TypeOwnerName);
            context.Builder.Append("{");
            if (valueHandler.BinarySizePersistent)
            {
                var size = context.Variables.Declare(VariableKeys.ValueSize);

                context.Builder.AppendFormat("var {0} = 0;", size);

                valueHandler.HandleGetSize(new TypeHandlingContext(context)
                {
                    SizeVariableKey = VariableKeys.ValueSize
                });

                context.Builder.AppendFormat("{0} += {1}*{2}.Length;", context.GetSizeVariableName(), size, context.TypeOwnerName);
            }
            else
            {
                var indexName = context.Variables.Declare(Index);
                context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}.Length; {0}++)", indexName, context.TypeOwnerName);
                context.Builder.Append("{");
                valueHandler.HandleGetSize(new TypeHandlingContext(context)
                {
                    TypeOwnerName = String.Format("{0}[{1}]", context.TypeOwnerName, indexName)
                });
                context.Builder.Append("}");
            }
            context.Builder.Append("}}");
        }
        public void HandleGetSize(TypeHandlingContext context)
        {
            IType valueType = context.Type.GetNullableUnderlyingType();

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

            var valueHandlingContext = new TypeHandlingContext(context)
            {
                Type          = valueType,
                TypeName      = valueType.GetPresentableName(context.PresentationLanguage),
                TypeOwnerName = String.Format("{0}.Value", context.TypeOwnerName),
            };
            ITypeHandler valueHandler = TypeHandlers.All.FirstOrDefault(h => h.CanHandle(valueHandlingContext));

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

            context.Builder.AppendFormat("{0} += sizeof(Boolean);", context.GetSizeVariableName());
            valueHandler.HandleGetSize(context);
        }
        private static 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);

            var size = ctx.GetSizeVariableName();

            ctx.Builder.Append("{");
            ctx.Builder.AppendFormat("var {0} = 0;", size);
            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.HandleGetSize(ctx);
                    }
                }
            }
            ctx.Builder.AppendFormat("return {0};", size);
            ctx.Builder.Append("}");

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

            declaration.SetBody(body);
        }