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