public void HandleWrite(TypeHandlingContext context) { var valueType = context.Type.GetGenericUnderlyingType(_listTypeElement); var valueHandler = TypeHandlers.All.First(h => h.CanHandle(context.WithType(valueType))); var exists = context.Variables.Declare("exists"); var index = context.Variables.Declare("index"); context.Builder.AppendFormat("var {0} = {1} != null;", exists, context.TypeOwnerName); TypeHandlers.Boolean.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = exists, }); context.Builder.AppendFormat("if({0})", exists); context.Builder.Append("{"); TypeHandlers.Int32.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = String.Format("{0}.Count", context.TypeOwnerName) }); context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}.Count; {0}++)", index, context.TypeOwnerName); context.Builder.Append("{"); valueHandler.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = String.Format("{0}[{1}]", context.TypeOwnerName, index) }); context.Builder.Append("}"); context.Builder.Append("}"); context.Variables.Dispose("index"); }
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 HandleWrite(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("tmp = BitConverter.GetBytes({0}.HasValue);", context.TypeOwnerName); context.Builder.Append("Array.Copy(tmp, 0, bytes, index, tmp.Length);"); context.Builder.Append("index += tmp.Length;"); context.Builder.AppendFormat("if({0}.HasValue)", context.TypeOwnerName).Append("{"); valueHandler.HandleWrite(valueHandlingContext); context.Builder.Append("}"); }
public bool CanHandle(TypeHandlingContext context) { var classType = context.Type.GetClassType(); if (classType != null) { if (classType.Constructors.Any(c => c.Parameters.Count == 0)) { var generators = MethodGenerators.All; var found = new Boolean[generators.Length]; foreach (var method in classType.Methods) { for (Int32 i = 0, c = generators.Length; i < c; i++) { if (!found[i]) { found[i] = generators[i].IsGenerationTarget(method); } } } if (found.All(any => any)) { return(true); } } } return(false); }
public void HandleRead(TypeHandlingContext context) { context.Builder.Append("tmp = new Byte[16];"); context.Builder.Append("Array.Copy(bytes, index, tmp, 0, tmp.Length);"); context.Builder.AppendFormat("{0} = new Guid(tmp);", context.TypeOwnerName); context.Builder.Append("index += 16;"); }
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("}"); }
public void HandleRead(TypeHandlingContext context) { var length = 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("{"); TypeHandlers.Int32.HandleRead(new TypeHandlingContext(context) { TypeOwnerName = String.Format("var {0}", length) }); context.Builder.AppendFormat("if({0} > 0)", length); context.Builder.Append("{"); context.Builder.AppendFormat("{0} = System.Text.Encoding.UTF8.GetString(bytes, index, {1});", context.TypeOwnerName, length); context.Builder.AppendFormat("index += System.Text.Encoding.UTF8.GetByteCount({0});", context.TypeOwnerName); context.Builder.Append("}"); context.Builder.Append("else{"); context.Builder.AppendFormat("{0} = String.Empty;", context.TypeOwnerName); context.Builder.Append("}"); context.Builder.Append("}"); context.Variables.Dispose("length"); }
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) { 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 override void Populate(CSharpGeneratorContext context) { // use context.ProvidedElements.AddRange to add new // generator elements (e.g., GeneratorDeclaredElement<T>) IClassLikeDeclaration typeDeclaration = context.ClassDeclaration; ITypeElement typeElement = typeDeclaration.DeclaredElement; ListHandler.Initialize(context); MethodGeneratorBase.Initialize(context); GetBytesMethodGenerator.Initialize(context); SetBytesMethodGenerator.Initialize(context); if (typeElement is IClass || typeElement is IStruct) { var ctx = new TypeHandlingContext(context); foreach (ITypeMember member in typeElement.GetMembers()) { ITypeOwner owner = null; var field = member as IField; if (field != null) { if (field.GetAccessRights() != AccessRights.PRIVATE && !field.IsConstant && !field.IsReadonly && !field.IsStatic) { owner = field; } } var property = member as IProperty; if (property != null) { if (property.IsReadable && property.IsWritable && !property.IsStatic) { owner = property; } } if (owner != null) { ctx.Resolve(owner); if (TypeHandlers.All.Any(h => h.CanHandle(ctx))) { context.ProvidedElements.Add(new GeneratorDeclaredElement <ITypeOwner>(owner)); } } } } }
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 bool CanHandle(TypeHandlingContext context) { var arrayType = context.Type as IArrayType; if (arrayType != null && arrayType.Rank == 1) { var scalarType = context.Type.GetScalarType(); if (scalarType != null) { return(TypeHandlers.All.Any(h => h.CanHandle(context.WithType(scalarType)))); } } return(false); }
public void HandleWrite(TypeHandlingContext context) { var haveValue = context.Variables.Declare("haveValue"); context.Builder.Append("{"); context.Builder.AppendFormat("var {0} = {1} != null;", haveValue, context.TypeOwnerName); TypeHandlers.Boolean.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = haveValue }); context.Builder.AppendFormat("if({0} != null)", context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("index += {0}.{1}(bytes, index);", context.TypeOwnerName, WriteMethodGenerator.Instance.MethodName); context.Builder.Append("}}"); context.Variables.Dispose("haveValue"); }
public void HandleRead(TypeHandlingContext context) { var haveValue = context.Variables.Declare("haveValue"); context.Builder.Append("{"); TypeHandlers.Boolean.HandleRead(new TypeHandlingContext(context) { TypeOwnerName = String.Format("var {0}", haveValue) }); context.Builder.AppendFormat("if({0})", haveValue); context.Builder.Append("{"); context.Builder.AppendFormat("{0} = new {1}();", context.TypeOwnerName, context.TypeName); context.Builder.AppendFormat("index += {0}.{1}(bytes, index);", context.TypeOwnerName, ReadMethodGenerator.Instance.MethodName); context.Builder.Append("}}"); context.Variables.Dispose("haveValue"); }
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); }
public void HandleWrite(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(); } String indexName = context.Variables.Declare(Index); var exists = context.Variables.Declare(VariableKeys.NotNull); context.Builder.AppendFormat("var {0} = {1} != null;", exists, context.TypeOwnerName); TypeHandlers.Boolean.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = exists }); context.Builder.AppendFormat("if({0})", exists); context.Builder.Append("{"); TypeHandlers.Int32.HandleWrite(new TypeHandlingContext(context) { TypeName = TypeHandlers.Int32.TypeName, TypeOwnerName = String.Format("{0}.Length", context.TypeOwnerName) }); context.Builder.AppendFormat("if({0}.Length > 0)", context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("for(Int32 {0} = 0; {0} < {1}.Length; {0}++)", indexName, context.TypeOwnerName); context.Builder.Append("{"); valueHandler.HandleWrite(new TypeHandlingContext(context) { TypeName = valueType.GetPresentableName(context.PresentationLanguage), TypeOwnerName = String.Format("{0}[{1}]", context.TypeOwnerName, indexName) }); context.Builder.Append("}}}"); context.Variables.Dispose(Index); }
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("}"); }
public void HandleWrite(TypeHandlingContext context) { IEnum enumType = context.Type.GetEnumType(); if (enumType == null) { throw new NotSupportedException(); } IType baseType = enumType.GetUnderlyingType(); IEnumBaseTypeHandler handler = TypeHandlers.EnumBaseTypeHandlers.SingleOrDefault(h => h.CanHandle(context.WithType(baseType))); if (handler == null) { throw new NotImplementedException(); } handler.HandleEnumWrite(context.TypeOwner, context.Builder, context.Args); }
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 HandleRead(TypeHandlingContext context) { var valueType = context.Type.GetGenericUnderlyingType(_listTypeElement); var valueHandler = TypeHandlers.All.First(h => h.CanHandle(context.WithType(valueType))); var count = context.Variables.Declare("count"); var existed = context.Variables.Declare("existed"); context.Builder.Append("{"); TypeHandlers.Boolean.HandleRead(new TypeHandlingContext(context) { TypeOwnerName = "var " + existed }); context.Builder.AppendFormat("if({0})", existed); context.Builder.Append("{"); TypeHandlers.Int32.HandleRead(new TypeHandlingContext(context) { TypeOwnerName = String.Format("var {0} ", count) }); context.Builder.AppendFormat("if({0} > 0)", count); context.Builder.Append("{"); context.Builder.AppendFormat("{0} = new List<{2}>({1});", context.TypeOwnerName, count, valueType); context.Builder.Append("} else {"); context.Builder.AppendFormat("{0} = new List<{1}>();", context.TypeOwnerName, valueType); context.Builder.Append("}"); context.Builder.AppendFormat("for(Int32 i = 0; i < {0}; i++)", count); context.Builder.Append("{"); context.Builder.AppendFormat("var {0} = default({1});", VariableKeys.Value, valueType.GetPresentableName(context.PresentationLanguage)); valueHandler.HandleRead(new TypeHandlingContext(context) { TypeOwnerName = VariableKeys.Value, TypeName = valueType.GetPresentableName(context.PresentationLanguage) }); context.Builder.AppendFormat("{0}.Add(value);", context.TypeOwnerName); context.Builder.Append("}}}"); context.Variables.Dispose("count"); context.Variables.Dispose("existed"); }
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); }
public void HandleWrite(TypeHandlingContext context) { var exists = context.Variables.Declare(VariableKeys.NotNull); context.Builder.AppendFormat("var {0} = {1} != null;", exists, context.TypeOwnerName); TypeHandlers.Boolean.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = exists }); context.Builder.AppendFormat("if({0})", exists); context.Builder.Append("{"); TypeHandlers.Int32.HandleWrite(new TypeHandlingContext(context) { TypeOwnerName = String.Format("{0}.Length", context.TypeOwnerName) }); context.Builder.AppendFormat("if({0}.Length > 0)", context.TypeOwnerName); context.Builder.Append("{"); context.Builder.AppendFormat("tmp = System.Text.Encoding.UTF8.GetBytes({0});", context.TypeOwnerName); context.Builder.Append("Array.Copy(tmp, 0, bytes, index, tmp.Length);"); context.Builder.Append("index += tmp.Length;"); context.Builder.Append("}"); context.Builder.Append("}"); }
public void HandleGetSize(TypeHandlingContext context) { context.Builder.AppendFormat("{0} += sizeof(Boolean);", context.GetSizeVariableName()); }
public void HandleRead(TypeHandlingContext context) { context.Builder.AppendFormat("{0} = BitConverter.ToBoolean(bytes, index);", context.TypeOwnerName); context.Builder.AppendFormat("index += sizeof(Boolean);"); }
public void HandleWrite(TypeHandlingContext context) { context.Builder.AppendFormat("tmp = BitConverter.GetBytes({0});", context.TypeOwnerName); context.Builder.Append("Array.Copy(tmp, 0, bytes, index, tmp.Length);"); context.Builder.Append("index += tmp.Length;"); }
public bool CanHandle(TypeHandlingContext context) { return(context.Type.IsBool()); }
public void HandleGetSize(TypeHandlingContext context) { context.Builder.AppendFormat("{1} += sizeof({0});", _typeName, context.GetSizeVariableName()); }
public bool CanHandle(TypeHandlingContext context) { var valueType = context.Type.GetGenericUnderlyingType(_listTypeElement); return(valueType != null && TypeHandlers.All.Any(h => h.CanHandle(context.WithType(valueType)))); }
public void HandleRead(TypeHandlingContext context) { context.Builder.AppendFormat("{0} = BitConverter.To{1}(bytes, index);", context.TypeOwnerName, _typeName); context.Builder.AppendFormat("index += sizeof({0});", _typeName); }