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;
00029
00030 dbus_message_iter_get_basic (iter, out raw);
00031
00032 this.val = Marshal.PtrToStringAnsi (raw);
00033 }
00034
00035 public void Append(IntPtr iter)
00036 {
00037 IntPtr marshalVal = Marshal.StringToHGlobalAnsi (val);
00038
00039 bool success = dbus_message_iter_append_basic (iter, (int) Code, ref marshalVal);
00040 Marshal.FreeHGlobal (marshalVal);
00041
00042 if (!success)
00043 throw new ApplicationException("Failed to append STRING argument:" + val);
00044 }
00045
00046 public static bool Suits(System.Type type)
00047 {
00048 switch (type.ToString()) {
00049 case "System.String":
00050 case "System.String&":
00051 return true;
00052 }
00053
00054 return false;
00055 }
00056
00057 public static void EmitMarshalIn(ILGenerator generator, Type type)
00058 {
00059 if (type.IsByRef) {
00060 generator.Emit(OpCodes.Ldind_Ref);
00061 }
00062 }
00063
00064 public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
00065 {
00066 generator.Emit(OpCodes.Castclass, type);
00067 if (!isReturn) {
00068 generator.Emit(OpCodes.Stind_Ref);
00069 }
00070 }
00071
00072 public object Get()
00073 {
00074 return this.val;
00075 }
00076
00077 public object Get(System.Type type)
00078 {
00079 switch (type.ToString())
00080 {
00081 case "System.String":
00082 case "System.String&":
00083 return this.val;
00084 default:
00085 throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'");
00086 }
00087 }
00088
00089 [DllImport("dbus-1")]
00090 private extern static void dbus_message_iter_get_basic (IntPtr iter, out IntPtr value);
00091
00092 [DllImport("dbus-1")]
00093 private extern static bool dbus_message_iter_append_basic (IntPtr iter, int type, ref IntPtr value);
00094 }
00095 }