* qt/Makfile.am:
[platform/upstream/dbus.git] / mono / DBusType / UInt16.cs
1 using System;
2 using System.Runtime.InteropServices;
3 using System.Reflection.Emit;
4
5 using DBus;
6
7 namespace DBus.DBusType
8 {
9   /// <summary>
10   /// 16-bit integer.
11   /// </summary>
12   public class UInt16 : IDBusType
13   {
14     public const char Code = 'q';
15     private System.UInt16 val;
16     
17     private UInt16()
18     {
19     }
20     
21     public UInt16(System.UInt16 val, Service service) 
22     {
23       this.val = val;
24     }
25
26     public UInt16(IntPtr iter, Service service)
27     {
28       dbus_message_iter_get_basic (iter, out this.val);
29     }
30     
31     public void Append(IntPtr iter)
32     {
33       if (!dbus_message_iter_append_basic (iter, (int) Code, ref val))
34         throw new ApplicationException("Failed to append INT16 argument:" + val);
35     }
36
37     public static bool Suits(System.Type type) 
38     {
39       if (type.IsEnum && Enum.GetUnderlyingType (type) == typeof(System.UInt16)) {
40         return true;
41       }
42       
43       switch (type.ToString()) {
44       case "System.UInt16":
45       case "System.UInt16&":
46         return true;      }
47       
48       return false;
49     }
50
51     public static void EmitMarshalIn(ILGenerator generator, Type type)
52     {
53       if (type.IsByRef) {
54         generator.Emit(OpCodes.Ldind_U2);
55       }
56     }
57
58     public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
59     {
60       generator.Emit(OpCodes.Unbox, type);
61       generator.Emit(OpCodes.Ldind_U2);
62       if (!isReturn) {
63         generator.Emit(OpCodes.Stind_I2);
64       }
65     }
66     
67     public object Get() 
68     {
69       return this.val;
70     }
71
72     public object Get(System.Type type)
73     {
74       if (type.IsEnum) {
75         return Enum.ToObject(type, this.val);
76       }
77       
78       switch (type.ToString()) {
79       case "System.UInt16":
80       case "System.UInt16&":
81         return this.val;
82       default:
83         throw new ArgumentException("Cannot cast DBus.Type.UInt16 to type '" + type.ToString() + "'");
84       }
85     }    
86
87     [DllImport("dbus-1")]
88     private extern static void dbus_message_iter_get_basic (IntPtr iter, out System.UInt16 value);
89  
90     [DllImport("dbus-1")]
91     private extern static bool dbus_message_iter_append_basic (IntPtr iter, int type, ref System.UInt16 value);
92   }
93 }