Made all DBusTypes take Service in the constructor because Array also needed it in...
[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(IntPtr iter, Service service)
27     {
28       this.val = dbus_message_iter_get_byte(iter);
29     }
30     
31     public void Append(IntPtr iter)
32     {
33       if (!dbus_message_iter_append_byte(iter, val))
34         throw new ApplicationException("Failed to append BYTE argument:" + val);
35     }
36
37     public static bool Suits(System.Type type) 
38     {
39       switch (type.ToString()) {
40       case "System.Byte":
41       case "System.Byte&":
42         return true;
43       }
44       
45       return false;
46     }
47
48     public static void EmitMarshalIn(ILGenerator generator, Type type)
49     {
50       if (type.IsByRef) {
51         generator.Emit(OpCodes.Ldind_U1);
52       }
53     }
54
55     public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
56     {
57       generator.Emit(OpCodes.Unbox, type);
58       generator.Emit(OpCodes.Ldind_U1);
59       if (!isReturn) {
60         generator.Emit(OpCodes.Stind_I1);
61       }
62     }
63     
64     public object Get() 
65     {
66       return this.val;
67     }
68
69     public object Get(System.Type type)
70     {
71       switch (type.ToString()) {
72       case "System.Byte":
73       case "System.Byte&":
74         return this.val;
75       default:
76         throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'");
77       }
78     }
79
80     [DllImport("dbus-1")]
81     private extern static System.Byte dbus_message_iter_get_byte(IntPtr iter);
82  
83     [DllImport("dbus-1")]
84     private extern static bool dbus_message_iter_append_byte(IntPtr iter, System.Byte value);
85   }
86 }