Welcome to mirror list, hosted at ThFree Co, Russian Federation.

PropertyValueImpl.cs « WindowsRuntime « InteropServices « Runtime « System « src « System.Private.Interop « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d4df5c9ddd1f8859c1e4eaf1404251a5c7943896 (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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
// 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.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.WindowsRuntime;

namespace System.Runtime.InteropServices.WindowsRuntime
{
    [System.Runtime.CompilerServices.DependencyReductionRootAttribute]
    [McgInternalTypeAttribute]
    public class PropertyValueImpl : BoxedValue, IPropertyValue
    {
        internal PropertyValueImpl(object val, int type) : base(val, type)
        {
        }

        public PropertyType get_Type()
        {
            return (PropertyType)m_type;
        }

        public bool IsNumericScalar
        {
            get
            {
                return IsNumericScalarImpl((PropertyType)m_type, m_data);
            }
        }

        public byte GetUInt8()
        {
            return CoerceScalarValue<byte>(PropertyType.UInt8);
        }

        public short GetInt16()
        {
            return CoerceScalarValue<short>(PropertyType.Int16);
        }

        public ushort GetUInt16()
        {
            return CoerceScalarValue<ushort>(PropertyType.UInt16);
        }

        public int GetInt32()
        {
            return CoerceScalarValue<int>(PropertyType.Int32);
        }

        public uint GetUInt32()
        {
            return CoerceScalarValue<uint>(PropertyType.UInt32);
        }

        public long GetInt64()
        {
            return CoerceScalarValue<long>(PropertyType.Int64);
        }

        public ulong GetUInt64()
        {
            return CoerceScalarValue<ulong>(PropertyType.UInt64);
        }

        public float GetSingle()
        {
            return CoerceScalarValue<float>(PropertyType.Single);
        }

        public double GetDouble()
        {
            return CoerceScalarValue<double>(PropertyType.Double);
        }

        public char GetChar16()
        {
            CheckType(PropertyType.Char16);
            return (char)m_data;
        }

        public bool GetBoolean()
        {
            CheckType(PropertyType.Boolean);
            return (bool)m_data;
        }

        public string GetString()
        {
            return CoerceScalarValue<string>(PropertyType.String);
        }

        public object GetInspectable()
        {
            CheckType(PropertyType.Inspectable);
            return m_data;
        }

        public System.Guid GetGuid()
        {
            return CoerceScalarValue<System.Guid>(PropertyType.Guid);
        }

        public System.DateTimeOffset GetDateTime()
        {
            CheckType(PropertyType.DateTime);
            return (System.DateTimeOffset)m_data;
        }

        public System.TimeSpan GetTimeSpan()
        {
            CheckType(PropertyType.TimeSpan);
            return (System.TimeSpan)m_data;
        }

        public global::Windows.Foundation.Point GetPoint()
        {
            CheckType(PropertyType.Point);
            return (global::Windows.Foundation.Point)m_data;
        }

        public global::Windows.Foundation.Size GetSize()
        {
            CheckType(PropertyType.Size);
            return (global::Windows.Foundation.Size)m_data;
        }

        public global::Windows.Foundation.Rect GetRect()
        {
            CheckType(PropertyType.Rect);
            return (global::Windows.Foundation.Rect)m_data;
        }

        public void GetUInt8Array(out byte[] array)
        {
            array = CoerceArrayValue<byte>(PropertyType.UInt8Array);
        }

        public void GetInt16Array(out short[] array)
        {
            array = CoerceArrayValue<short>(PropertyType.Int16Array);
        }

        public void GetUInt16Array(out ushort[] array)
        {
            array = CoerceArrayValue<ushort>(PropertyType.UInt16Array);
        }

        public void GetInt32Array(out int[] array)
        {
            array = CoerceArrayValue<int>(PropertyType.Int32Array);
        }

        public void GetUInt32Array(out uint[] array)
        {
            array = CoerceArrayValue<uint>(PropertyType.UInt32Array);
        }

        public void GetInt64Array(out long[] array)
        {
            array = CoerceArrayValue<long>(PropertyType.Int64Array);
        }

        public void GetUInt64Array(out ulong[] array)
        {
            array = CoerceArrayValue<ulong>(PropertyType.UInt64Array);
        }

        public void GetSingleArray(out float[] array)
        {
            array = CoerceArrayValue<float>(PropertyType.SingleArray);
        }

        public void GetDoubleArray(out double[] array)
        {
            array = CoerceArrayValue<double>(PropertyType.DoubleArray);
        }

        public void GetChar16Array(out char[] array)
        {
            CheckType(PropertyType.Char16Array);
            array = (char[])m_data;
        }

        public void GetBooleanArray(out bool[] array)
        {
            CheckType(PropertyType.BooleanArray);
            array = (bool[])m_data;
        }

        public void GetStringArray(out string[] array)
        {
            array = CoerceArrayValue<string>(PropertyType.StringArray);
        }

        public void GetInspectableArray(out object[] array)
        {
            CheckType(PropertyType.InspectableArray);
            array = (object[])m_data;
        }

        public void GetGuidArray(out System.Guid[] array)
        {
            array = CoerceArrayValue<System.Guid>(PropertyType.GuidArray);
        }

        public void GetDateTimeArray(out System.DateTimeOffset[] array)
        {
            CheckType(PropertyType.DateTimeArray);
            array = (System.DateTimeOffset[])m_data;
        }

        public void GetTimeSpanArray(out System.TimeSpan[] array)
        {
            CheckType(PropertyType.TimeSpanArray);
            array = (System.TimeSpan[])m_data;
        }

        public void GetPointArray(out global::Windows.Foundation.Point[] array)
        {
            CheckType(PropertyType.PointArray);
            array = (global::Windows.Foundation.Point[])m_data;
        }

        public void GetSizeArray(out global::Windows.Foundation.Size[] array)
        {
            CheckType(PropertyType.SizeArray);
            array = (global::Windows.Foundation.Size[])m_data;
        }

        public void GetRectArray(out global::Windows.Foundation.Rect[] array)
        {
            CheckType(PropertyType.RectArray);
            array = (global::Windows.Foundation.Rect[])m_data;
        }

        private T[] CoerceArrayValue<T>(PropertyType unboxType)
        {
            // If we contain the type being looked for directly, then take the fast-path
            if (m_type == (int)unboxType)
            {
                return (T[])m_data;
            }

            // Make sure we have an array to begin with
            System.Array dataArray = m_data as System.Array;

            if (dataArray == null)
            {
                throw CreateExceptionForInvalidCast((PropertyType)m_type, unboxType);
            }

            // Array types are 1024 larger than their equivilent scalar counterpart
            if ((m_type <= 1024) || ((int)unboxType <= 1024))
            {
                throw CreateExceptionForInvalidCast((PropertyType)m_type, unboxType);
            }

            PropertyType scalarType = (PropertyType)(m_type - 1024);
            PropertyType unboxTypeT = unboxType - 1024;

            // If we do not have the correct array type, then we need to convert the array element-by-element
            // to a new array of the requested type
            T[] coercedArray = new T[dataArray.Length];

            for (int i = 0; i < dataArray.Length; ++i)
            {
                coercedArray[i] = (T)CoerceScalarValue(scalarType, dataArray.GetValue(i), unboxTypeT);
            }

            return coercedArray;
        }

        [System.Runtime.CompilerServices.MethodImplAttribute(System.Runtime.CompilerServices.MethodImplOptions.AggressiveInlining)]
        private T CoerceScalarValue<T>(PropertyType unboxType)
        {
            object result = m_data;

            // If we are just a boxed version of the requested type, then take the fast path out
            if (m_type != (int)unboxType)
            {
                result = CoerceScalarValue((PropertyType)m_type, result, unboxType);
            }

            return (T)result;
        }

        static private object CoerceScalarValue(PropertyType type, object value, PropertyType unboxType)
        {
            // If the property type is neither one of the coercable numeric types nor IInspectable, we
            // should not attempt coersion, even if the underlying value is technically convertable
            if ((type == PropertyType.Guid) && (unboxType == PropertyType.String))
            {
                // String <--> Guid is allowed
                return ((System.Guid)value).ToString();
            }
            else if ((type == PropertyType.String) && (unboxType == PropertyType.Guid))
            {
                System.Guid result;

                if (System.Guid.TryParse((string)value, out result))
                {
                    return result;
                }
            }
            else if (type == PropertyType.Inspectable)
            {
                // If the property type is IInspectable, and we have a nested IPropertyValue, then we need
                // to pass along the request to coerce the value.
                IPropertyValue ipv = value as IPropertyValue;

                if (ipv != null)
                {
                    object result = ReferenceUtility.GetWellKnownScalar(ipv, unboxType);

                    if (result != null)
                    {
                        return result;
                    }

                    Debug.Assert(
                        false,
                        "T in coersion function wasn't understood as a type that can be coerced - make sure that CoerceScalarValue and NumericScalarTypes are in sync"
                    );
                }
            }
            else if (type == PropertyType.Boolean || type == PropertyType.Char16)
            {
                throw CreateExceptionForInvalidCoersion(type, value, unboxType, Interop.COM.TYPE_E_TYPEMISMATCH);
            }

            //
            // Let Convert handle all possible conversions - this include 
            // 1. string - which desktop code accidentally allowed
            // 2. object (IInspectable)
            // 
            try
            {
                switch (unboxType)
                {
                    case PropertyType.UInt8:
                        return System.Convert.ToByte(value);

                    case PropertyType.Int16:
                        return System.Convert.ToInt16(value);

                    case PropertyType.UInt16:
                        return System.Convert.ToUInt16(value);

                    case PropertyType.Int32:
                        return System.Convert.ToInt32(value);

                    case PropertyType.UInt32:
                        return System.Convert.ToUInt32(value);

                    case PropertyType.Int64:
                        return System.Convert.ToInt64(value);

                    case PropertyType.UInt64:
                        return System.Convert.ToUInt64(value);

                    case PropertyType.Single:
                        return System.Convert.ToSingle(value);

                    case PropertyType.Double:
                        return System.Convert.ToDouble(value);

                    default:
                        break;
                }
            }
            catch (System.FormatException)
            {
                throw CreateExceptionForInvalidCoersion(type, value, unboxType, Interop.COM.TYPE_E_TYPEMISMATCH);
            }
            catch (System.InvalidCastException)
            {
                throw CreateExceptionForInvalidCoersion(type, value, unboxType, Interop.COM.TYPE_E_TYPEMISMATCH);
            }
            catch (System.OverflowException)
            {
                throw CreateExceptionForInvalidCoersion(type, value, unboxType, Interop.COM.DISP_E_OVERFLOW);
            }

            throw CreateExceptionForInvalidCast(type, unboxType);
        }

        private static bool IsNumericScalarImpl(PropertyType type, object data)
        {
            switch (type)
            {
                case PropertyType.UInt8:
                case PropertyType.Int16:
                case PropertyType.UInt16:
                case PropertyType.Int32:
                case PropertyType.UInt32:
                case PropertyType.Int64:
                case PropertyType.UInt64:
                case PropertyType.Single:
                case PropertyType.Double:
                    return true;

                default:
                    return McgMarshal.IsEnum(data);
            }
        }

        private void CheckType(PropertyType unboxType)
        {
            if (this.get_Type() != unboxType)
            {
                throw CreateExceptionForInvalidCast(this.get_Type(), unboxType);
            }
        }

        private static System.InvalidCastException CreateExceptionForInvalidCast(
            PropertyType type,
            PropertyType unboxType)
        {
            System.InvalidCastException ex = new System.InvalidCastException(SR.Format(SR.PropertyValue_InvalidCast, type, unboxType));
            McgMarshal.SetExceptionErrorCode(ex, Interop.COM.TYPE_E_TYPEMISMATCH);
            return ex;
        }

        private static System.InvalidCastException CreateExceptionForInvalidCoersion(
            PropertyType type,
            object value,
            PropertyType unboxType,
            int hr)
        {
            InvalidCastException ex = new InvalidCastException(SR.Format(SR.PropertyValue_InvalidCoersion, type, value, unboxType));
            McgMarshal.SetExceptionErrorCode(ex, hr);
            return ex;
        }
    }

    internal class ReferenceUtility
    {
        internal static object GetWellKnownScalar(IPropertyValue ipv, PropertyType type)
        {
            switch (type)
            {
                case PropertyType.UInt8:
                    return ipv.GetUInt8();

                case PropertyType.Int16:
                    return ipv.GetInt16();

                case PropertyType.UInt16:
                    return ipv.GetUInt16();

                case PropertyType.Int32:
                    return ipv.GetInt32();

                case PropertyType.UInt32:
                    return ipv.GetUInt32();

                case PropertyType.Int64:
                    return ipv.GetInt64();

                case PropertyType.UInt64:
                    return ipv.GetUInt64();

                case PropertyType.Single:
                    return ipv.GetSingle();

                case PropertyType.Double:
                    return ipv.GetDouble();
            }

            Debug.Assert(false);
            return null;
        }
    }
}