summaryrefslogtreecommitdiff
path: root/src/mscorlib/src/System/Runtime/InteropServices/WindowsRuntime/IReadOnlyDictionaryToIMapViewAdapter.cs
blob: b185b41be03ced2a1d54ead2e7a0958804d435d0 (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
// 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 IMapView`2 interface on managed
    // objects that implement IReadOnlyDictionary`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 IReadOnlyDictionaryToIMapViewAdapter objects. Rather, they are of type
    // IReadOnlyDictionary<K, V>. No actual IReadOnlyDictionaryToIMapViewAdapter object is ever instantiated. Thus, you will
    // see a lot of expressions that cast "this" to "IReadOnlyDictionary<K, V>". 
    [DebuggerDisplay("Size = {Size}")]
    internal sealed class IReadOnlyDictionaryToIMapViewAdapter
    {
        private IReadOnlyDictionaryToIMapViewAdapter()
        {
            Debug.Assert(false, "This class is never instantiated");
        }

        // V Lookup(K key)
        internal V Lookup<K, V>(K key)
        {
            IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
            V value;
            bool keyFound = _this.TryGetValue(key, out value);

            if (!keyFound)
            {
                Exception e = new KeyNotFoundException(Environment.GetResourceString("Arg_KeyNotFound"));
                e.SetErrorCode(__HResults.E_BOUNDS);
                throw e;
            }

            return value;
        }

        // uint Size { get }
        internal uint Size<K, V>()
        {
            IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
            return (uint)_this.Count;
        }
        
        // bool HasKey(K key)
        internal bool HasKey<K, V>(K key)
        {
            IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);
            return _this.ContainsKey(key);
        }

        // void Split(out IMapView<K, V> first, out IMapView<K, V> second)
        internal void Split<K, V>(out IMapView<K, V> first, out IMapView<K, V> second)
        {
            IReadOnlyDictionary<K, V> _this = JitHelpers.UnsafeCast<IReadOnlyDictionary<K, V>>(this);

            if (_this.Count < 2) {
                first = null;
                second = null;
                return;
            }

            ConstantSplittableMap<K, V> splittableMap = _this as ConstantSplittableMap<K, V>;

            if (splittableMap == null)
                splittableMap = new ConstantSplittableMap<K, V>(_this);

            splittableMap.Split(out first, out second);
        }
    }
}