Esempio n. 1
0
        private static SubItemToken StartSubItem(object instance, ProtoWriter writer, bool allowFixed)
        {
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }
            if (++writer.depth > RecursionCheckDepth)
            {
                writer.CheckRecursionStackAndPush(instance);
            }
            if (writer.packedFieldNumber != 0)
            {
                throw new InvalidOperationException("Cannot begin a sub-item while performing packed encoding");
            }
            switch (writer.wireType)
            {
            case WireType.StartGroup:
                writer.wireType = WireType.None;
                return(new SubItemToken(-writer.fieldNumber));

            case WireType.String:
#if DEBUG
                if (writer.model != null && writer.model.ForwardsOnly)
                {
                    throw new ProtoException("Should not be buffering data");
                }
#endif
                writer.wireType = WireType.None;
                DemandSpace(32, writer);     // make some space in anticipation...
                writer.flushLock++;
                writer.position++;
                return(new SubItemToken(writer.ioIndex++));    // leave 1 space (optimistic) for length

            case WireType.Fixed32:
            {
                if (!allowFixed)
                {
                    throw CreateException(writer);
                }
                DemandSpace(32, writer);         // make some space in anticipation...
                writer.flushLock++;
                SubItemToken token = new SubItemToken(writer.ioIndex);
                ProtoWriter.IncrementedAndReset(4, writer);         // leave 4 space (rigid) for length
                return(token);
            }

            default:
                throw CreateException(writer);
            }
        }