Made all DBusTypes take Service in the constructor because Array also needed it in...
[platform/upstream/dbus.git] / mono / DBusType / String.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   /// A string.
11   /// </summary>
12   public class String : IDBusType
13   {
14     public const char Code = 's';
15     private string val;
16     
17     private String()
18     {
19     }
20     
21     public String(string val, Service service) 
22     {
23       this.val = val;
24     }
25     
26     public String(IntPtr iter, Service service)
27     {
28       this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter));
29     }
30
31     public void Append(IntPtr iter) 
32     {
33       if (!dbus_message_iter_append_string(iter, Marshal.StringToHGlobalAnsi(val)))
34         throw new ApplicationException("Failed to append STRING argument:" + val);
35     }
36
37     public static bool Suits(System.Type type) 
38     {
39       switch (type.ToString()) {
40       case "System.String":
41       case "System.String&":
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_Ref);
52       }
53     }
54
55     public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
56     {
57       generator.Emit(OpCodes.Castclass, type);
58       if (!isReturn) {
59         generator.Emit(OpCodes.Stind_Ref);
60       }
61     }
62
63     public object Get() 
64     {
65       return this.val;
66     }
67
68     public object Get(System.Type type)
69     {
70       switch (type.ToString()) 
71         {
72         case "System.String":
73         case "System.String&":
74           return this.val;
75         default:
76           throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'");
77         }
78     }    
79
80     [DllImport("dbus-1")]
81     private extern static IntPtr dbus_message_iter_get_string(IntPtr iter);
82  
83     [DllImport("dbus-1")]
84     private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value);
85   }
86 }