コード例 #1
0
        internal MetaType FindWithoutAdd(Type type)
        {
            BasicList.NodeEnumerator enumerator = this.types.GetEnumerator();
            while (enumerator.MoveNext())
            {
                MetaType metaType = (MetaType)enumerator.Current;
                if (metaType.Type == type)
                {
                    if (metaType.Pending)
                    {
                        this.WaitOnLock(metaType);
                    }
                    return(metaType);
                }
            }
            Type underlyingType = TypeModel.ResolveProxies(type);

            if (!(underlyingType == null))
            {
                return(this.FindWithoutAdd(underlyingType));
            }
            return(null);
        }
コード例 #2
0
ファイル: EnumSerializer.cs プロジェクト: radtek/work-1
        void IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom)
        {
            ProtoTypeCode typeCode = this.GetTypeCode();

            if (this.map == null)
            {
                ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
                ctx.ConvertFromInt32(typeCode, false);
            }
            else
            {
                int[]    keys   = new int[this.map.Length];
                object[] values = new object[this.map.Length];
                for (int i = 0; i < this.map.Length; i++)
                {
                    keys[i]   = this.map[i].WireValue;
                    values[i] = this.map[i].RawValue;
                }
                using (Local local = new Local(ctx, this.ExpectedType))
                {
                    using (Local local2 = new Local(ctx, ctx.MapType(typeof(int))))
                    {
                        ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
                        ctx.StoreValue(local2);
                        CodeLabel label = ctx.DefineLabel();
                        BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(keys, values).GetEnumerator();
                        while (enumerator.MoveNext())
                        {
                            BasicList.Group current = (BasicList.Group)enumerator.Current;
                            CodeLabel       label2  = ctx.DefineLabel();
                            int             count   = current.Items.Count;
                            if (count == 1)
                            {
                                ctx.LoadValue(local2);
                                ctx.LoadValue(current.First);
                                CodeLabel label3 = ctx.DefineLabel();
                                ctx.BranchIfEqual(label3, true);
                                ctx.Branch(label2, false);
                                WriteEnumValue(ctx, typeCode, label3, label, current.Items[0], local);
                            }
                            else
                            {
                                ctx.LoadValue(local2);
                                ctx.LoadValue(current.First);
                                ctx.Subtract();
                                CodeLabel[] jumpTable = new CodeLabel[count];
                                for (int j = 0; j < count; j++)
                                {
                                    jumpTable[j] = ctx.DefineLabel();
                                }
                                ctx.Switch(jumpTable);
                                ctx.Branch(label2, false);
                                for (int k = 0; k < count; k++)
                                {
                                    WriteEnumValue(ctx, typeCode, jumpTable[k], label, current.Items[k], local);
                                }
                            }
                            ctx.MarkLabel(label2);
                        }
                        ctx.LoadReaderWriter();
                        ctx.LoadValue(this.ExpectedType);
                        ctx.LoadValue(local2);
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException"));
                        ctx.MarkLabel(label);
                        ctx.LoadValue(local);
                    }
                }
            }
        }
コード例 #3
0
        void IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom)
        {
            Type expectedType = this.ExpectedType;

            using (Local local = ctx.GetLocalWithValue(expectedType, valueFrom))
            {
                using (Local local2 = new Local(ctx, ctx.MapType(typeof(int))))
                {
                    if (this.HasCallbacks(TypeModel.CallbackType.BeforeDeserialize))
                    {
                        if (this.ExpectedType.IsValueType)
                        {
                            this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.BeforeDeserialize);
                        }
                        else
                        {
                            CodeLabel label = ctx.DefineLabel();
                            ctx.LoadValue(local);
                            ctx.BranchIfFalse(label, false);
                            this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.BeforeDeserialize);
                            ctx.MarkLabel(label);
                        }
                    }
                    CodeLabel label2 = ctx.DefineLabel();
                    CodeLabel label3 = ctx.DefineLabel();
                    ctx.Branch(label2, false);
                    ctx.MarkLabel(label3);
                    BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(this.fieldNumbers, this.serializers).GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        BasicList.Group current = (BasicList.Group)enumerator.Current;
                        CodeLabel       label4  = ctx.DefineLabel();
                        int             count   = current.Items.Count;
                        if (count == 1)
                        {
                            ctx.LoadValue(local2);
                            ctx.LoadValue(current.First);
                            CodeLabel label5 = ctx.DefineLabel();
                            ctx.BranchIfEqual(label5, true);
                            ctx.Branch(label4, false);
                            this.WriteFieldHandler(ctx, expectedType, local, label5, label2, (IProtoSerializer)current.Items[0]);
                        }
                        else
                        {
                            ctx.LoadValue(local2);
                            ctx.LoadValue(current.First);
                            ctx.Subtract();
                            CodeLabel[] jumpTable = new CodeLabel[count];
                            for (int i = 0; i < count; i++)
                            {
                                jumpTable[i] = ctx.DefineLabel();
                            }
                            ctx.Switch(jumpTable);
                            ctx.Branch(label4, false);
                            for (int j = 0; j < count; j++)
                            {
                                this.WriteFieldHandler(ctx, expectedType, local, jumpTable[j], label2, (IProtoSerializer)current.Items[j]);
                            }
                        }
                        ctx.MarkLabel(label4);
                    }
                    this.EmitCreateIfNull(ctx, local);
                    ctx.LoadReaderWriter();
                    if (this.isExtensible)
                    {
                        ctx.LoadValue(local);
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("AppendExtensionData"));
                    }
                    else
                    {
                        ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("SkipField"));
                    }
                    ctx.MarkLabel(label2);
                    ctx.EmitBasicRead("ReadFieldHeader", ctx.MapType(typeof(int)));
                    ctx.CopyValue();
                    ctx.StoreValue(local2);
                    ctx.LoadValue(0);
                    ctx.BranchIfGreater(label3, false);
                    this.EmitCreateIfNull(ctx, local);
                    this.EmitCallbackIfNeeded(ctx, local, TypeModel.CallbackType.AfterDeserialize);
                    if ((valueFrom != null) && !local.IsSame(valueFrom))
                    {
                        ctx.LoadValue(local);
                        ctx.Cast(valueFrom.Type);
                        ctx.StoreValue(valueFrom);
                    }
                }
            }
        }
コード例 #4
0
        void ProtoBuf.Serializers.IProtoSerializer.EmitRead(CompilerContext ctx, Local valueFrom)
        {
            ProtoTypeCode typeCode = this.GetTypeCode();

            if (this.map == null)
            {
                ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
                ctx.ConvertFromInt32(typeCode, false);
                return;
            }
            int[]    wireValue = new int[(int)this.map.Length];
            object[] rawValue  = new object[(int)this.map.Length];
            for (int i = 0; i < (int)this.map.Length; i++)
            {
                wireValue[i] = this.map[i].WireValue;
                rawValue[i]  = this.map[i].RawValue;
            }
            using (Local local = new Local(ctx, this.ExpectedType))
            {
                using (Local local1 = new Local(ctx, ctx.MapType(typeof(int))))
                {
                    ctx.EmitBasicRead("ReadInt32", ctx.MapType(typeof(int)));
                    ctx.StoreValue(local1);
                    CodeLabel codeLabel = ctx.DefineLabel();
                    BasicList.NodeEnumerator enumerator = BasicList.GetContiguousGroups(wireValue, rawValue).GetEnumerator();
                    while (enumerator.MoveNext())
                    {
                        BasicList.Group current    = (BasicList.Group)enumerator.Current;
                        CodeLabel       codeLabel1 = ctx.DefineLabel();
                        int             count      = current.Items.Count;
                        if (count != 1)
                        {
                            ctx.LoadValue(local1);
                            ctx.LoadValue(current.First);
                            ctx.Subtract();
                            CodeLabel[] codeLabelArray = new CodeLabel[count];
                            for (int j = 0; j < count; j++)
                            {
                                codeLabelArray[j] = ctx.DefineLabel();
                            }
                            ctx.Switch(codeLabelArray);
                            ctx.Branch(codeLabel1, false);
                            for (int k = 0; k < count; k++)
                            {
                                EnumSerializer.WriteEnumValue(ctx, typeCode, codeLabelArray[k], codeLabel, current.Items[k], local);
                            }
                        }
                        else
                        {
                            ctx.LoadValue(local1);
                            ctx.LoadValue(current.First);
                            CodeLabel codeLabel2 = ctx.DefineLabel();
                            ctx.BranchIfEqual(codeLabel2, true);
                            ctx.Branch(codeLabel1, false);
                            EnumSerializer.WriteEnumValue(ctx, typeCode, codeLabel2, codeLabel, current.Items[0], local);
                        }
                        ctx.MarkLabel(codeLabel1);
                    }
                    ctx.LoadReaderWriter();
                    ctx.LoadValue(this.ExpectedType);
                    ctx.LoadValue(local1);
                    ctx.EmitCall(ctx.MapType(typeof(ProtoReader)).GetMethod("ThrowEnumException"));
                    ctx.MarkLabel(codeLabel);
                    ctx.LoadValue(local);
                }
            }
        }
コード例 #5
0
        public override string GetSchema(Type type)
        {
            BasicList requiredTypes = new BasicList();
            MetaType  primaryType   = null;
            bool      isInbuiltType = false;

            if (type == null)
            {
                BasicList.NodeEnumerator enumerator = this.types.GetEnumerator();
                while (enumerator.MoveNext())
                {
                    MetaType meta = (MetaType)enumerator.Current;
                    MetaType tmp  = meta.GetSurrogateOrBaseOrSelf(false);
                    if (!requiredTypes.Contains(tmp))
                    {
                        requiredTypes.Add(tmp);
                        this.CascadeDependents(requiredTypes, tmp);
                    }
                }
            }
            else
            {
                Type tmp2 = Helpers.GetUnderlyingType(type);
                if (tmp2 != null)
                {
                    type = tmp2;
                }
                WireType defaultWireType;
                isInbuiltType = (ValueMember.TryGetCoreSerializer(this, DataFormat.Default, type, out defaultWireType, false, false, false, false) != null);
                if (!isInbuiltType)
                {
                    int index = this.FindOrAddAuto(type, false, false, false);
                    if (index < 0)
                    {
                        throw new ArgumentException("The type specified is not a contract-type", "type");
                    }
                    primaryType = ((MetaType)this.types[index]).GetSurrogateOrBaseOrSelf(false);
                    requiredTypes.Add(primaryType);
                    this.CascadeDependents(requiredTypes, primaryType);
                }
            }
            StringBuilder headerBuilder = new StringBuilder();
            string        package       = null;

            if (!isInbuiltType)
            {
                IEnumerable typesForNamespace = (primaryType == null) ? this.types : requiredTypes;
                foreach (MetaType meta2 in typesForNamespace)
                {
                    if (!meta2.IsList)
                    {
                        string tmp3 = meta2.Type.Namespace;
                        if (!Helpers.IsNullOrEmpty(tmp3) && !tmp3.StartsWith("System."))
                        {
                            if (package == null)
                            {
                                package = tmp3;
                            }
                            else
                            {
                                if (!(package == tmp3))
                                {
                                    package = null;
                                    break;
                                }
                            }
                        }
                    }
                }
            }
            if (!Helpers.IsNullOrEmpty(package))
            {
                headerBuilder.Append("package ").Append(package).Append(';');
                Helpers.AppendLine(headerBuilder);
            }
            bool          requiresBclImport = false;
            StringBuilder bodyBuilder       = new StringBuilder();

            MetaType[] metaTypesArr = new MetaType[requiredTypes.Count];
            requiredTypes.CopyTo(metaTypesArr, 0);
            Array.Sort <MetaType>(metaTypesArr, MetaType.Comparer.Default);
            if (isInbuiltType)
            {
                Helpers.AppendLine(bodyBuilder).Append("message ").Append(type.Name).Append(" {");
                MetaType.NewLine(bodyBuilder, 1).Append("optional ").Append(this.GetSchemaTypeName(type, DataFormat.Default, false, false, ref requiresBclImport)).Append(" value = 1;");
                Helpers.AppendLine(bodyBuilder).Append('}');
            }
            else
            {
                for (int i = 0; i < metaTypesArr.Length; i++)
                {
                    MetaType tmp4 = metaTypesArr[i];
                    if (!tmp4.IsList || tmp4 == primaryType)
                    {
                        tmp4.WriteSchema(bodyBuilder, 0, ref requiresBclImport);
                    }
                }
            }
            if (requiresBclImport)
            {
                headerBuilder.Append("import \"bcl.proto\"; // schema for protobuf-net's handling of core .NET types");
                Helpers.AppendLine(headerBuilder);
            }
            return(Helpers.AppendLine(headerBuilder.Append(bodyBuilder)).ToString());
        }