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); }
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) { var valueType = context.Type.GetGenericUnderlyingType(_listTypeElement); var valueHandler = TypeHandlers.All.First(h => h.CanHandle(context.WithType(valueType))); TypeHandlers.Boolean.HandleGetSize(context); context.Builder.AppendFormat("if({0} != null)", context.TypeOwnerName); context.Builder.Append("{"); TypeHandlers.Int32.HandleGetSize(new TypeHandlingContext(context) { TypeOwnerName = String.Format("{0}.Length", context.TypeOwnerName) }); if (valueHandler.BinarySizePersistent) { var valueSize = context.Variables.Declare(VariableKeys.ValueSize); context.Builder.AppendFormat("var {0} = 0;", valueSize); valueHandler.HandleGetSize(new TypeHandlingContext(context) { SizeVariableKey = VariableKeys.ValueSize }); context.Builder.AppendFormat("{0} += {1}*{2}.Count;", context.GetSizeVariableName(), valueSize, context.TypeOwnerName); } else { var i = context.Variables.Declare("i"); var v = context.Variables.Declare("v"); context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}.Count; {0}++)", i, context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("var {0} = {1}[{2}];", v, context.TypeOwnerName, i); valueHandler.HandleGetSize(new TypeHandlingContext(context) { TypeOwnerName = v }); context.Builder.Append("}"); context.Variables.Dispose("v"); context.Variables.Dispose("i"); } context.Builder.Append("}"); }
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); }
public void HandleGetSize(TypeHandlingContext context) { context.Builder.AppendFormat("{0} += sizeof(Boolean);", context.GetSizeVariableName()); }
public void HandleGetSize(TypeHandlingContext context) { TypeHandlers.Boolean.HandleGetSize(context); context.Builder.AppendFormat("if({0} != null)", context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("{2} += {0}.{1}();", context.TypeOwnerName, GetBinarySizeMethodGenerator.MethodName, context.GetSizeVariableName()); context.Builder.Append("}"); }
public void HandleGetSize(TypeHandlingContext context) { TypeHandlers.Boolean.HandleGetSize(context); context.Builder.AppendFormat("if({0} != null)", context.TypeOwnerName); context.Builder.Append("{"); TypeHandlers.Int32.HandleGetSize(context); context.Builder.AppendFormat("if(!String.IsNullOrEmpty({0}))", context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("{1} += System.Text.Encoding.UTF8.GetByteCount({0});", context.TypeOwnerName, context.GetSizeVariableName()); context.Builder.Append("}"); context.Builder.Append("}"); }
public void HandleGetSize(TypeHandlingContext context) { IEnum enumType = context.Type.GetEnumType(); if (enumType == null) { throw new NotSupportedException(); } IType baseType = enumType.GetUnderlyingType(); context.Builder.AppendFormat("{1} += sizeof({0});", baseType.GetPresentableName(context.PresentationLanguage), context.GetSizeVariableName()); }
public void HandleGetSize(TypeHandlingContext context) { context.Builder.AppendFormat("{1} += sizeof({0});", _typeName, context.GetSizeVariableName()); }
public void HandleGetSize(TypeHandlingContext context) { context.Builder.AppendFormat("{2} += {0}.{1}();", context.TypeOwnerName, GetBinarySizeMethodGenerator.MethodName, context.GetSizeVariableName()); }