Updates referencesource to .NET 4.7
[mono-project.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / windowsruntime / vectortocollectionadapter.cs
blob0443ed2d1428452a4171aaaaf78433097080dfc3
1 // ==++==
2 //
3 // Copyright (c) Microsoft Corporation. All rights reserved.
4 //
5 // ==--==
6 //
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>Microsoft</OWNER>
10 using System;
11 using System.Security;
12 using System.Collections;
13 using System.Collections.Generic;
14 using System.Diagnostics.Contracts;
15 using System.Runtime.InteropServices;
16 using System.Runtime.CompilerServices;
18 namespace System.Runtime.InteropServices.WindowsRuntime
20 // This is a set of stub methods implementing the support for the ICollection`1 interface on WinRT
21 // objects that support IVector`1. Used by the interop mashaling infrastructure.
23 // The methods on this class must be written VERY carefully to avoid introducing security holes.
24 // That's because they are invoked with special "this"! The "this" object
25 // for all of these methods are not VectorToCollectionAdapter objects. Rather, they are of type
26 // IVector<T>. No actual VectorToCollectionAdapter object is ever instantiated. Thus, you will see
27 // a lot of expressions that cast "this" to "IVector<T>".
28 internal sealed class VectorToCollectionAdapter
30 private VectorToCollectionAdapter()
32 Contract.Assert(false, "This class is never instantiated");
35 // int Count { get }
36 [Pure]
37 [SecurityCritical]
38 internal int Count<T>()
40 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
41 uint size = _this.Size;
42 if (((uint)Int32.MaxValue) < size)
44 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
47 return (int)size;
50 // bool IsReadOnly { get }
51 [SecurityCritical]
52 internal bool IsReadOnly<T>()
54 return false;
57 // void Add(T item)
58 [SecurityCritical]
59 internal void Add<T>(T item)
61 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
62 _this.Append(item);
65 // void Clear()
66 [SecurityCritical]
67 internal void Clear<T>()
69 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
70 _this.Clear();
73 // bool Contains(T item)
74 [SecurityCritical]
75 internal bool Contains<T>(T item)
77 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
79 uint index;
80 return _this.IndexOf(item, out index);
83 // void CopyTo(T[] array, int arrayIndex)
84 [SecurityCritical]
85 internal void CopyTo<T>(T[] array, int arrayIndex)
87 if (array == null)
88 throw new ArgumentNullException("array");
90 if (arrayIndex < 0)
91 throw new ArgumentOutOfRangeException("arrayIndex");
93 if (array.Length <= arrayIndex && Count<T>() > 0)
94 throw new ArgumentException(Environment.GetResourceString("Argument_IndexOutOfArrayBounds"));
96 if (array.Length - arrayIndex < Count<T>())
97 throw new ArgumentException(Environment.GetResourceString("Argument_InsufficientSpaceToCopyCollection"));
99 Contract.EndContractBlock();
101 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
102 int count = Count<T>();
103 for (int i = 0; i < count; i++)
105 array[i + arrayIndex] = VectorToListAdapter.GetAt<T>(_this, (uint)i);
109 // bool Remove(T item)
110 [SecurityCritical]
111 internal bool Remove<T>(T item)
113 IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
115 uint index;
116 bool exists = _this.IndexOf(item, out index);
118 if (!exists)
119 return false;
121 if (((uint)Int32.MaxValue) < index)
123 throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
126 VectorToListAdapter.RemoveAtHelper<T>(_this, index);
127 return true;