summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/MapToDictionaryAdapter.cs
blob: 981972ca9f668dfc4e8c2795a20bb558ac68f18c (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
// 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 IDictionary`2 interface on WinRT
    // objects that support IMap`2. 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 MapToDictionaryAdapter objects. Rather, they are of type
    // IMap<K, V>. No actual MapToDictionaryAdapter object is ever instantiated. Thus, you will see
    // a lot of expressions that cast "this" to "IMap<K, V>".
    internal sealed class MapToDictionaryAdapter
    {
        private MapToDictionaryAdapter()
        {
            Debug.Assert(false, "This class is never instantiated");
        }

        // V this[K key] { get }
        internal V Indexer_Get<K, V>(K key)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            Contract.EndContractBlock();

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            return Lookup(_this, key);
        }

        // V this[K key] { set }
        internal void Indexer_Set<K, V>(K key, V value)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            Contract.EndContractBlock();

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            Insert(_this, key, value);
        }

        // ICollection<K> Keys { get }
        internal ICollection<K> Keys<K, V>()
        {
            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            IDictionary<K, V> dictionary = (IDictionary<K, V>)_this;
            return new DictionaryKeyCollection<K, V>(dictionary);
        }

        // ICollection<V> Values { get }
        internal ICollection<V> Values<K, V>()
        {
            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            IDictionary<K, V> dictionary = (IDictionary<K, V>)_this;
            return new DictionaryValueCollection<K, V>(dictionary);
        }

        // bool ContainsKey(K key)
        [Pure]
        internal bool ContainsKey<K, V>(K key)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            return _this.HasKey(key);
        }

        // void Add(K key, V value)
        internal void Add<K, V>(K key, V value)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            if (ContainsKey<K, V>(key))
                throw new ArgumentException(Environment.GetResourceString("Argument_AddingDuplicate"));

            Contract.EndContractBlock();

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            Insert(_this, key, value);
        }

        // bool Remove(TKey key)
        internal bool Remove<K, V>(K key)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            if (!_this.HasKey(key))
                return false;

            try
            {
                _this.Remove(key);
                return true;

            }
            catch (Exception ex)
            {
                if (__HResults.E_BOUNDS == ex._HResult)
                    return false;

                throw;
            }
        }

        // bool TryGetValue(TKey key, out TValue value)
        internal bool TryGetValue<K, V>(K key, out V value)
        {
            if (key == null)
                throw new ArgumentNullException(nameof(key));

            IMap<K, V> _this = JitHelpers.UnsafeCast<IMap<K, V>>(this);
            if (!_this.HasKey(key))
            {
                value = default(V);
                return false;
            }

            try
            {
                value = Lookup(_this, key);
                return true;
            }
            catch (KeyNotFoundException)
            {
                value = default(V);
                return false;
            }
        }

        // Helpers:

        private static V Lookup<K, V>(IMap<K, V> _this, K key)
        {
            Contract.Requires(null != key);

            try
            {
                return _this.Lookup(key);
            }
            catch (Exception ex)
            {

                if (__HResults.E_BOUNDS == ex._HResult)
                    throw new KeyNotFoundException(Environment.GetResourceString("Arg_KeyNotFound"));
                throw;
            }
        }

        private static bool Insert<K, V>(IMap<K, V> _this, K key, V value)
        {
            Contract.Requires(null != key);

            bool replaced = _this.Insert(key, value);
            return replaced;
        }
    }
}