summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/VectorToListAdapter.cs
blob: 3e3324864da25655c09c88677e80c8d879ce03a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

//

using System;
using System.Security;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Contracts;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;

namespace System.Runtime.InteropServices.WindowsRuntime
{
    // This is a set of stub methods implementing the support for the IList`1 interface on WinRT
    // objects that support IVector`1. Used by the interop mashaling infrastructure.
    //
    // The methods on this class must be written VERY carefully to avoid introducing security holes.
    // That's because they are invoked with special "this"! The "this" object
    // for all of these methods are not VectorToListAdapter objects. Rather, they are of type
    // IVector<T>. No actual VectorToListAdapter object is ever instantiated. Thus, you will see
    // a lot of expressions that cast "this" to "IVector<T>".
    internal sealed class VectorToListAdapter
    {
        private VectorToListAdapter()
        {
            Debug.Assert(false, "This class is never instantiated");
        }

        // T this[int index] { get }
        internal T Indexer_Get<T>(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
            return GetAt(_this, (uint)index);
        }

        // T this[int index] { set }
        internal void Indexer_Set<T>(int index, T value)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
            SetAt(_this, (uint)index, value);
        }

        // int IndexOf(T item)
        internal int IndexOf<T>(T item)
        {
            IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);

            uint index;
            bool exists = _this.IndexOf(item, out index);

            if (!exists)
                return -1;

            if (((uint)Int32.MaxValue) < index)
            {
                throw new InvalidOperationException(Environment.GetResourceString("InvalidOperation_CollectionBackingListTooLarge"));
            }

            return (int)index;
        }

        // void Insert(int index, T item)
        internal void Insert<T>(int index, T item)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
            InsertAtHelper<T>(_this, (uint)index, item);
        }

        // void RemoveAt(int index)
        internal void RemoveAt<T>(int index)
        {
            if (index < 0)
                throw new ArgumentOutOfRangeException(nameof(index));

            IVector<T> _this = JitHelpers.UnsafeCast<IVector<T>>(this);
            RemoveAtHelper<T>(_this, (uint)index);
        }

        // Helpers:

        internal static T GetAt<T>(IVector<T> _this, uint index)
        {
            try
            {
                return _this.GetAt(index);

                // We delegate bounds checking to the underlying collection and if it detected a fault,
                // we translate it to the right exception:
            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new ArgumentOutOfRangeException(nameof(index));

                throw;
            }
        }

        private static void SetAt<T>(IVector<T> _this, UInt32 index, T value)
        {
            try
            {
                _this.SetAt(index, value);

                // We deligate bounds checking to the underlying collection and if it detected a fault,
                // we translate it to the right exception:
            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new ArgumentOutOfRangeException(nameof(index));

                throw;
            }
        }

        private static void InsertAtHelper<T>(IVector<T> _this, uint index, T item)
        {
            try
            {
                _this.InsertAt(index, item);

                // We delegate bounds checking to the underlying collection and if it detected a fault,
                // we translate it to the right exception:
            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new ArgumentOutOfRangeException(nameof(index));

                throw;
            }
        }

        internal static void RemoveAtHelper<T>(IVector<T> _this, uint index)
        {
            try
            {
                _this.RemoveAt(index);

                // We delegate bounds checking to the underlying collection and if it detected a fault,
                // we translate it to the right exception:
            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new ArgumentOutOfRangeException(nameof(index));

                throw;
            }
        }
    }
}