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