using System; using System.IO; namespace Hast.Transformer.Abstractions.SimpleMemory { /// /// Facilitates read and overwrite of the for outside users (like DMA). /// public class SimpleMemoryAccessor { private readonly SimpleMemory _simpleMemory; public SimpleMemoryAccessor(SimpleMemory simpleMemory) => _simpleMemory = simpleMemory; /// /// Gets the memory contents without an additional prefix. /// /// /// Here besides so for simpler cases there's no branching necessary. /// public Memory Get() => _simpleMemory.Memory; /// /// Gets the memory contents with additional prefix. /// /// /// The length of the prefix in cells. It must not be greater than . /// On what this means see the remarks on /// public Memory Get(int prefixCellCount) { if (prefixCellCount < 0) throw new ArgumentOutOfRangeException($"{nameof(prefixCellCount)} must be positive!"); if (prefixCellCount == 0) return Get(); // If we need more prefix than what is available. if (prefixCellCount > _simpleMemory.PrefixCellCount) { int missingBytes = prefixCellCount - _simpleMemory.PrefixCellCount * SimpleMemory.MemoryCellSizeBytes; Memory newMemory = new byte[_simpleMemory.PrefixedMemory.Length + missingBytes]; _simpleMemory.PrefixedMemory.CopyTo(newMemory.Slice(missingBytes)); _simpleMemory.PrefixCellCount = prefixCellCount; return _simpleMemory.PrefixedMemory = newMemory; } return _simpleMemory.PrefixedMemory.Slice((_simpleMemory.PrefixCellCount - prefixCellCount) * SimpleMemory.MemoryCellSizeBytes); } /// /// Sets the internal value of the SimpleMemory with the first prefixCellCount cells hidden. /// /// The new data. /// /// The number of cells to be used as the value for the underlying /// . /// /// /// Using prefixCellCount allows you to set the communication headers during Get without an extra copy, but you /// must use at least as many prefixCellCount for Set as for Get if the is reused, /// otherwise the memory has to be copied and that incurs a performance hit. /// public void Set(Memory data, int prefixCellCount = 0) { _simpleMemory.PrefixedMemory = data; _simpleMemory.PrefixCellCount = prefixCellCount; } /// /// Saves the underlying to a binary storage format. /// /// The stream to write the storage data to. public void Store(Stream stream) { var segment = Get().GetUnderlyingArray(); stream.Write(segment.Array, segment.Offset, _simpleMemory.ByteCount); } /// /// Saves the underlying to a binary storage format to a file. Overwrites the file /// if it exists, creates it otherwise. /// /// The path under to write the storage data file to. public void Store(string filePath) { using (var fileStream = File.Create(filePath)) Store(fileStream); } /// /// Loads a binary storage format into the underlying with the first prefixCellCount /// cells hidden. /// /// The stream to read the storage data from. /// /// The number of cells to be used as the value for the underlying /// . /// public void Load(Stream stream, int prefixCellCount = 0) { int prefixBytesCount = prefixCellCount * SimpleMemory.MemoryCellSizeBytes; var data = new byte[stream.Length + prefixBytesCount]; stream.Read(data, prefixBytesCount, (int)stream.Length); Set(data, prefixCellCount); } /// /// Loads a binary storage format into the underlying with the first prefixCellCount /// cells hidden. /// /// The path of the file to read the storage data from. /// /// The number of cells to be used as the value for the underlying /// . /// public void Load(string filePath, int prefixCellCount = 0) { using (var fileStream = File.OpenRead(filePath)) Load(fileStream, prefixCellCount); } } }