00001 using System;
00002 using System.Runtime.InteropServices;
00003 using System.Reflection.Emit;
00004
00005 using DBus;
00006
00007 namespace DBus.DBusType
00008 {
00012 public class String : IDBusType
00013 {
00014 public const char Code = 's';
00015 private string val;
00016
00017 private String()
00018 {
00019 }
00020
00021 public String(string val, Service service)
00022 {
00023 this.val = val;
00024 }
00025
00026 public String(IntPtr iter, Service service)
00027 {
00028 IntPtr raw_str = dbus_message_iter_get_string (iter);
00029 this.val = Marshal.PtrToStringAnsi (raw_str);
00030 dbus_free (raw_str);
00031 }
00032
00033 public void Append(IntPtr iter)
00034 {
00035 IntPtr raw_str = Marshal.StringToHGlobalAnsi (val);
00036
00037 bool success = dbus_message_iter_append_string (iter, raw_str);
00038 Marshal.FreeHGlobal (raw_str);
00039
00040 if (!success)
00041 throw new ApplicationException("Failed to append STRING argument:" + val);
00042 }
00043
00044 public static bool Suits(System.Type type)
00045 {
00046 switch (type.ToString()) {
00047 case "System.String":
00048 case "System.String&":
00049 return true;
00050 }
00051
00052 return false;
00053 }
00054
00055 public static void EmitMarshalIn(ILGenerator generator, Type type)
00056 {
00057 if (type.IsByRef) {
00058 generator.Emit(OpCodes.Ldind_Ref);
00059 }
00060 }
00061
00062 public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
00063 {
00064 generator.Emit(OpCodes.Castclass, type);
00065 if (!isReturn) {
00066 generator.Emit(OpCodes.Stind_Ref);
00067 }
00068 }
00069
00070 public object Get()
00071 {
00072 return this.val;
00073 }
00074
00075 public object Get(System.Type type)
00076 {
00077 switch (type.ToString())
00078 {
00079 case "System.String":
00080 case "System.String&":
00081 return this.val;
00082 default:
00083 throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'");
00084 }
00085 }
00086
00087 [DllImport("dbus-1")]
00088 private extern static IntPtr dbus_message_iter_get_string(IntPtr iter);
00089
00090 [DllImport("dbus-1")]
00091 private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value);
00092
00093 [DllImport("dbus-1")]
00094 private extern static void dbus_free (IntPtr raw);
00095 }
00096 }