From e083b392ba4d04a42d2728ea40ca610709163271 Mon Sep 17 00:00:00 2001 From: Pavlos Touboulidis Date: Thu, 8 May 2014 17:33:04 +0300 Subject: [PATCH] Add IReadOnlyList --- OpenRA.Game/OpenRA.Game.csproj | 1 + OpenRA.Game/Primitives/ReadOnlyList.cs | 71 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 OpenRA.Game/Primitives/ReadOnlyList.cs diff --git a/OpenRA.Game/OpenRA.Game.csproj b/OpenRA.Game/OpenRA.Game.csproj index bcfb422fe3..6e1e58beaa 100644 --- a/OpenRA.Game/OpenRA.Game.csproj +++ b/OpenRA.Game/OpenRA.Game.csproj @@ -244,6 +244,7 @@ + diff --git a/OpenRA.Game/Primitives/ReadOnlyList.cs b/OpenRA.Game/Primitives/ReadOnlyList.cs new file mode 100644 index 0000000000..38dcd13246 --- /dev/null +++ b/OpenRA.Game/Primitives/ReadOnlyList.cs @@ -0,0 +1,71 @@ +#region Copyright & License Information +/* + * Copyright 2007-2014 The OpenRA Developers (see AUTHORS) + * This file is part of OpenRA, which is free software. It is made + * available to you under the terms of the GNU General Public License + * as published by the Free Software Foundation. For more information, + * see COPYING. + */ +#endregion + +using System; +using System.Collections.Generic; + +namespace OpenRA +{ + /// + /// A minimal read only list interface for .NET 4 + /// + /// + /// .NET 4.5 has an implementation built-in, this code is not meant to + /// duplicate it but provide a compatible interface that can be replaced + /// when we switch to .NET 4.5 or higher. + /// + public interface IReadOnlyList : IEnumerable + { + int Count { get; } + T this[int index] { get; } + } + + /// + /// A minimal read only list for .NET 4 implemented as a wrapper + /// around an IList. + /// + public class ReadOnlyList : IReadOnlyList + { + private readonly IList list; + + public ReadOnlyList() + : this(new List()) + { + } + + public ReadOnlyList(IList list) + { + if (list == null) + throw new ArgumentNullException("list"); + + this.list = list; + } + + #region IEnumerable implementation + public IEnumerator GetEnumerator() + { + return list.GetEnumerator(); + } + #endregion + + #region IEnumerable implementation + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return list.GetEnumerator(); + } + #endregion + + #region IReadOnlyList implementation + public int Count { get { return list.Count; } } + + public T this[int index] { get { return list[index]; } } + #endregion + } +}