summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/MapToCollectionAdapter.cs
blob: f11260eb4a89cb26c48269b5d9ae54a4c9dbb3af (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
// 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
{
    // These stubs will be used when a call via ICollection<KeyValuePair<K, V>> is made in managed code.
    // This can mean two things - either the underlying unmanaged object implements IMap<K, V> or it
    // implements IVector<IKeyValuePair<K, V>> and we cannot determine this statically in the general
    // case so we have to cast at run-time. 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 MapToCollectionAdapter objects. Rather, they are of type
    // IVector<KeyValuePair<K, V>> or IMap<K, V>. No actual MapToCollectionAdapter object is ever
    // instantiated. Thus, you will see a lot of expressions that cast "this" to "IVector<KeyValuePair<K, V>>"
    // or "IMap<K, V>".
    internal sealed class MapToCollectionAdapter
    {
        private MapToCollectionAdapter()
        {
            Debug.Assert(false, "This class is never instantiated");
        }

        // int Count { get }
        [Pure]
        internal int Count<K, V>()
        {
            object _this = JitHelpers.UnsafeCast<object>(this);

            IMap<K, V> _this_map = _this as IMap<K, V>;
            if (_this_map != null)
            {
                uint size = _this_map.Size;

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

                return (int)size;
            }
            else
            {
                IVector<KeyValuePair<K, V>> _this_vector = JitHelpers.UnsafeCast<IVector<KeyValuePair<K, V>>>(this);
                uint size = _this_vector.Size;

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

                return (int)size;
            }
        }

        // bool IsReadOnly { get }
        internal bool IsReadOnly<K, V>()
        {
            return false;
        }

        // void Add(T item)
        internal void Add<K, V>(KeyValuePair<K, V> item)
        {
            object _this = JitHelpers.UnsafeCast<object>(this);

            IDictionary<K, V> _this_dictionary = _this as IDictionary<K, V>;
            if (_this_dictionary != null)
            {
                _this_dictionary.Add(item.Key, item.Value);
            }
            else
            {
                IVector<KeyValuePair<K, V>> _this_vector = JitHelpers.UnsafeCast<IVector<KeyValuePair<K, V>>>(this);
                _this_vector.Append(item);
            }
        }

        // void Clear()
        internal void Clear<K, V>()
        {
            object _this = JitHelpers.UnsafeCast<object>(this);

            IMap<K, V> _this_map = _this as IMap<K, V>;
            if (_this_map != null)
            {
                _this_map.Clear();
            }
            else
            {
                IVector<KeyValuePair<K, V>> _this_vector = JitHelpers.UnsafeCast<IVector<KeyValuePair<K, V>>>(this);
                _this_vector.Clear();
            }
        }

        // bool Contains(T item)
        internal bool Contains<K, V>(KeyValuePair<K, V> item)
        {
            object _this = JitHelpers.UnsafeCast<object>(this);

            IDictionary<K, V> _this_dictionary = _this as IDictionary<K, V>;
            if (_this_dictionary != null)
            {
                V value;
                bool hasKey = _this_dictionary.TryGetValue(item.Key, out value);

                if (!hasKey)
                    return false;

                return EqualityComparer<V>.Default.Equals(value, item.Value);
            }
            else
            {
                IVector<KeyValuePair<K, V>> _this_vector = JitHelpers.UnsafeCast<IVector<KeyValuePair<K, V>>>(this);

                uint index;
                return _this_vector.IndexOf(item, out index);
            }
        }

        // void CopyTo(T[] array, int arrayIndex)
        internal void CopyTo<K, V>(KeyValuePair<K, V>[] array, int arrayIndex)
        {
            if (array == null)
                throw new ArgumentNullException(nameof(array));

            if (arrayIndex < 0)
                throw new ArgumentOutOfRangeException(nameof(arrayIndex));

            if (array.Length <= arrayIndex && Count<K, V>() > 0)
                throw new ArgumentException(Environment.GetResourceString("Argument_IndexOutOfArrayBounds"));

            if (array.Length - arrayIndex < Count<K, V>())
                throw new ArgumentException(Environment.GetResourceString("Argument_InsufficientSpaceToCopyCollection"));

            Contract.EndContractBlock();

            IIterable<KeyValuePair<K, V>> _this = JitHelpers.UnsafeCast<IIterable<KeyValuePair<K, V>>>(this);
            foreach (KeyValuePair<K, V> mapping in _this)
            {
                array[arrayIndex++] = mapping;
            }
        }

        // bool Remove(T item)
        internal bool Remove<K, V>(KeyValuePair<K, V> item)
        {
            object _this = JitHelpers.UnsafeCast<object>(this);

            IDictionary<K, V> _this_dictionary = _this as IDictionary<K, V>;
            if (_this_dictionary != null)
            {
                return _this_dictionary.Remove(item.Key);
            }
            else
            {
                IVector<KeyValuePair<K, V>> _this_vector = JitHelpers.UnsafeCast<IVector<KeyValuePair<K, V>>>(this);
                uint index;
                bool exists = _this_vector.IndexOf(item, out index);

                if (!exists)
                    return false;

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

                VectorToListAdapter.RemoveAtHelper<KeyValuePair<K, V>>(_this_vector, index);
                return true;
            }
        }
    }
}