Initial commit
[mono.git] / mcs / class / referencesource / mscorlib / system / runtime / interopservices / windowsruntime / listtobindablevectorviewadapter.cs
1 // ==++==
2 // 
3 //   Copyright (c) Microsoft Corporation.  All rights reserved.
4 // 
5 // ==--==
6 //
7 // <OWNER>GPaperin</OWNER>
8 // <OWNER>[....]</OWNER>
9
10 using System;
11 using System.Security;
12 using System.Reflection;
13 using System.Collections;
14 using System.Collections.Generic;
15 using System.Collections.ObjectModel;
16 using System.Diagnostics.Contracts;
17 using System.Runtime.InteropServices;
18 using System.Runtime.CompilerServices;
19
20 namespace System.Runtime.InteropServices.WindowsRuntime
21 {
22     /// A Windows Runtime IBindableVectorView implementation that wraps around a managed IList exposing
23     /// it to Windows runtime interop.
24     internal sealed class ListToBindableVectorViewAdapter : IBindableVectorView
25     {
26         private readonly IList list;
27
28         internal ListToBindableVectorViewAdapter(IList list)
29         {
30             if (list == null)
31                 throw new ArgumentNullException("list");
32
33             Contract.EndContractBlock();
34
35             this.list = list;
36         }
37
38         private static void EnsureIndexInt32(uint index, int listCapacity)
39         {
40             // We use '<=' and not '<' becasue Int32.MaxValue == index would imply
41             // that Size > Int32.MaxValue:
42             if (((uint)Int32.MaxValue) <= index || index >= (uint)listCapacity)
43             {
44                 Exception e = new ArgumentOutOfRangeException("index", Environment.GetResourceString("ArgumentOutOfRange_IndexLargerThanMaxValue"));
45                 e.SetErrorCode(__HResults.E_BOUNDS);
46                 throw e;
47             }
48         }
49
50         // IBindableIterable implementation:
51
52         public IBindableIterator First()
53         {
54             IEnumerator enumerator = list.GetEnumerator();
55             return new EnumeratorToIteratorAdapter<object>(new EnumerableToBindableIterableAdapter.NonGenericToGenericEnumerator(enumerator));
56         }
57
58         // IBindableVectorView implementation:
59
60         public object GetAt(uint index)
61         {
62             EnsureIndexInt32(index, list.Count);
63
64             try
65             {
66                 return list[(int)index];
67
68             }
69             catch (ArgumentOutOfRangeException ex)
70             {
71                 throw WindowsRuntimeMarshal.GetExceptionForHR(__HResults.E_BOUNDS, ex, "ArgumentOutOfRange_IndexOutOfRange");
72             }
73         }
74
75         public uint Size
76         {
77             get
78             {
79                 return (uint)list.Count;
80             }
81         }
82
83         public bool IndexOf(object value, out uint index)
84         {
85             int ind = list.IndexOf(value);
86
87             if (-1 == ind)
88             {
89                 index = 0;
90                 return false;
91             }
92
93             index = (uint)ind;
94             return true;
95         }
96     }
97 }