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 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 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); }
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"); }
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 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 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 bool CanHandle(TypeHandlingContext context) { var valueType = context.Type.GetGenericUnderlyingType(_listTypeElement); return(valueType != null && TypeHandlers.All.Any(h => h.CanHandle(context.WithType(valueType)))); }