Exemplo n.º 1
0
        public void WriteValues <T>(Memory <T> memory)
        {
            var values      = memory.Span;
            var buffer      = _bufferOwner.Memory;
            var isPrimitive = PrimitiveTypes.Primitives.Contains(typeof(T));
            var sizeOfElem  = isPrimitive ? _sizing.SizeOf <T>() : 0;

            if (!isPrimitive) // no size check bc it's checked on write calls from cache
            {
                foreach (var value in values)
                {
                    BinaryCache <T> .WriteValue(this, value);
                }
                return;
            }

            var sizeOf = sizeOfElem * values.Length;

            if (Remaining < sizeOf)
            {
                throw new ArgumentOutOfRangeException(nameof(Remaining),
                                                      $"Not enough bytes remaining in the buffer to write {typeof(T).Name} array. " +
                                                      $"Position : {Position}, Length : {Count}, Remaining : {Remaining}, SizeOf : {sizeOf}.");
            }

            var offset = Position;

            for (var i = 0; i < values.Length; i++, offset += sizeOfElem)
            {
                var dst = buffer.Slice(offset, sizeOfElem);
                PrimitiveBinaryCache <T> .WriteValue(dst.Span, values[i]);
            }
            Advance(sizeOf);
        }
Exemplo n.º 2
0
        public void WriteValue <T>(T value)
        {
            var isPrimitive = PrimitiveTypes.Primitives.Contains(typeof(T));
            var sizeOf      = isPrimitive ? _sizing.SizeOf <T>() : _sizing.SizeOf(value);

            if (Remaining < sizeOf)
            {
                throw new ArgumentOutOfRangeException(nameof(Remaining),
                                                      $"Not enough bytes remaining in the buffer to write {typeof(T).Name}. " +
                                                      $"Position : {Position}, Length : {Count}, Remaining : {Remaining}, SizeOf : {sizeOf}.");
            }

            if (!isPrimitive)
            {
                BinaryCache <T> .WriteValue(this, value);

                return;
            }

            var dst = _bufferOwner.Memory.Slice(Position, sizeOf);

            PrimitiveBinaryCache <T> .WriteValue(dst.Span, value);

            Advance(sizeOf);
        }