BufferWithAge.cs 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. namespace OpenVIII
  5. {
  6. public class BufferWithAge : IReadOnlyList<byte>
  7. {
  8. #region Fields
  9. private readonly byte[] _buffer;
  10. #endregion Fields
  11. #region Constructors
  12. public BufferWithAge(byte[] buffer)
  13. {
  14. this._buffer = buffer;
  15. Used = Created = DateTime.Now;
  16. }
  17. #endregion Constructors
  18. #region Properties
  19. public int Count => ((IReadOnlyList<byte>)_buffer).Count;
  20. public DateTime Created { get; }
  21. public DateTime Used { get; set; }
  22. #endregion Properties
  23. #region Indexers
  24. public byte this[int index] => ((IReadOnlyList<byte>)_buffer)[index];
  25. #endregion Indexers
  26. #region Methods
  27. public static implicit operator BufferWithAge(byte[] @in) => new BufferWithAge(@in);
  28. public static implicit operator byte[] (BufferWithAge @in) => @in._buffer;
  29. public IEnumerator<byte> GetEnumerator() => ((IReadOnlyList<byte>)_buffer).GetEnumerator();
  30. IEnumerator IEnumerable.GetEnumerator() => ((IReadOnlyList<byte>)_buffer).GetEnumerator();
  31. public DateTime Poke() => Used = DateTime.Now;
  32. public override string ToString() => $"{{{nameof(Created)}: {Created}, {nameof(Used)}: {Used}, Size: {_buffer?.Length ?? 0}}}";
  33. #endregion Methods
  34. }
  35. }