A game framework written with osu! in mind.
1// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
2// See the LICENCE file in the repository root for full licence text.
3
4#nullable enable
5
6using System;
7using System.Collections;
8using System.Collections.Generic;
9using osu.Framework.Extensions.ListExtensions;
10
11namespace osu.Framework.Lists
12{
13 /// <summary>
14 /// A wrapper that provides a read-only view into a <see cref="List{T}"/>.
15 /// This can be instantiated via the <see cref="ListExtensions.AsSlimReadOnly{T}"/> extension method.
16 /// </summary>
17 /// <typeparam name="T">The type of elements contained by the list.</typeparam>
18 public readonly struct SlimReadOnlyListWrapper<T> : IList<T>, IList, IReadOnlyList<T>
19 {
20 private readonly List<T> list;
21
22 public SlimReadOnlyListWrapper(List<T> list)
23 {
24 this.list = list;
25 }
26
27 public List<T>.Enumerator GetEnumerator() => list.GetEnumerator();
28
29 IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
30
31 IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
32
33 public void Add(T item) => throw new NotImplementedException();
34
35 public int Add(object? value) => throw new NotImplementedException();
36
37 void IList.Clear() => throw new NotImplementedException();
38
39 public bool Contains(object? value) => ((IList)list).Contains(value);
40
41 public int IndexOf(object? value) => ((IList)list).IndexOf(value);
42
43 public void Insert(int index, object? value) => ((IList)list).Insert(index, value);
44
45 public void Remove(object? value) => throw new NotImplementedException();
46
47 void IList.RemoveAt(int index) => throw new NotImplementedException();
48
49 public bool IsFixedSize => ((IList)list).IsFixedSize;
50
51 bool IList.IsReadOnly => true;
52
53 object? IList.this[int index]
54 {
55 get => ((IList)list)[index];
56 set => throw new NotImplementedException();
57 }
58
59 void ICollection<T>.Clear() => throw new NotImplementedException();
60
61 public bool Contains(T item) => list.Contains(item);
62
63 public void CopyTo(T[] array, int arrayIndex) => list.CopyTo(array, arrayIndex);
64
65 public bool Remove(T item) => throw new NotImplementedException();
66
67 public void CopyTo(Array array, int index) => ((ICollection)list).CopyTo(array, index);
68
69 int ICollection.Count => list.Count;
70
71 public bool IsSynchronized => ((ICollection)list).IsSynchronized;
72
73 public object SyncRoot => ((ICollection)list).SyncRoot;
74
75 int ICollection<T>.Count => list.Count;
76
77 bool ICollection<T>.IsReadOnly => true;
78
79 public int IndexOf(T item) => list.IndexOf(item);
80
81 public void Insert(int index, T item) => throw new NotImplementedException();
82
83 void IList<T>.RemoveAt(int index) => throw new NotImplementedException();
84
85 public T this[int index]
86 {
87 get => list[index];
88 set => throw new NotImplementedException();
89 }
90
91 int IReadOnlyCollection<T>.Count => list.Count;
92 }
93}