示例#1
0
        /// <summary>
        /// Reads a list of nodes from the underlying stream, where the number of
        /// nodes is determined by the LEB128 encoded integer prefix.
        /// </summary>
        /// <param name="reader">Reader used for reading from the underlying stream.</param>
        public override void ReadValue(ExtendedBinaryReader reader)
        {
            var count = reader.Read7BitEncodedInt();

            for (var i = 0; i < count; i++)
            {
                var type = (BsfType)reader.ReadByte();

                if (type == BsfType.Null)
                {
                    _list.Add(null);
                }
                else
                {
                    if (ElementType == BsfType.Null)
                    {
                        ElementType = type;
                    }

                    if (type != ElementType)
                    {
                        throw new InvalidOperationException($"List contains elements of both type '{ElementType}' and '{type}");
                    }

                    var node = Create(ElementType);
                    node.ReadValue(reader);
                    _list.Add(node);
                }
            }
        }
        /// <summary>
        /// Parameterized factory method used to create a node of the given type.
        /// </summary>
        /// <param name="type">Type of the node that needs to be created.</param>
        /// <returns>A newly created node of provided type.</returns>
        protected static BsfNode Create(BsfType type)
        {
            return(type switch
            {
                BsfType.Struct => (BsfNode) new BsfStruct(),
                BsfType.List => new BsfList(),
                BsfType.Null => null,

                BsfType.Byte => new BsfByte(),
                BsfType.Short => new BsfShort(),
                BsfType.Int => new BsfInt(),
                BsfType.Long => new BsfLong(),
                BsfType.Float => new BsfFloat(),
                BsfType.Double => new BsfDouble(),
                BsfType.Bool => new BsfBool(),
                BsfType.Char => new BsfChar(),
                BsfType.String => new BsfString(),

                BsfType.ByteArray => new BsfByteArray(),
                BsfType.ShortArray => new BsfShortArray(),
                BsfType.IntArray => new BsfIntArray(),
                BsfType.LongArray => new BsfLongArray(),
                BsfType.FloatArray => new BsfFloatArray(),
                BsfType.DoubleArray => new BsfDoubleArray(),
                BsfType.BoolArray => new BsfBoolArray(),
                BsfType.CharArray => new BsfCharArray(),
                BsfType.StringArray => new BsfStringArray(),

                _ => throw new ArgumentOutOfRangeException(nameof(type), type, null)
            });