summaryrefslogtreecommitdiffstats
path: root/mono/DBusType/Custom.cs
diff options
context:
space:
mode:
Diffstat (limited to 'mono/DBusType/Custom.cs')
-rw-r--r--mono/DBusType/Custom.cs109
1 files changed, 109 insertions, 0 deletions
diff --git a/mono/DBusType/Custom.cs b/mono/DBusType/Custom.cs
new file mode 100644
index 00000000..d3eb7629
--- /dev/null
+++ b/mono/DBusType/Custom.cs
@@ -0,0 +1,109 @@
+using System;
+using System.Runtime.InteropServices;
+using System.Reflection.Emit;
+
+using DBus;
+
+namespace DBus.DBusType
+{
+ /// <summary>
+ /// A named byte array, used for custom types.
+ /// </summary>
+ public class Custom : IDBusType
+ {
+ public const char Code = 'c';
+ private DBus.Custom val;
+
+ private Custom()
+ {
+ }
+
+ public Custom(DBus.Custom val)
+ {
+ this.val = val;
+ }
+
+ public Custom(IntPtr iter)
+ {
+ string name;
+ IntPtr value;
+ int len;
+
+ if (!dbus_message_iter_get_custom(iter, out name, out value, out len)) {
+ throw new ApplicationException("Failed to get CUSTOM argument.");
+ }
+
+ this.val.Name = name;
+ this.val.Data = new byte[len];
+ Marshal.Copy(value, this.val.Data, 0, len);
+ }
+
+ public void Append(IntPtr iter)
+ {
+ IntPtr data = Marshal.AllocCoTaskMem(this.val.Data.Length);
+ try {
+ Marshal.Copy(this.val.Data, 0, data, this.val.Data.Length);
+ if (!dbus_message_iter_append_custom(iter, this.val.Name, data, this.val.Data.Length)) {
+ throw new ApplicationException("Failed to append CUSTOM argument:" + val);
+ }
+ } finally {
+ Marshal.FreeCoTaskMem(data);
+ }
+ }
+
+ public static bool Suits(System.Type type)
+ {
+ switch (type.ToString()) {
+ case "DBus.Custom":
+ case "DBus.Custom&":
+ return true;
+ }
+
+ return false;
+ }
+
+ public static void EmitMarshalIn(ILGenerator generator, Type type)
+ {
+ if (type.IsByRef) {
+ generator.Emit(OpCodes.Ldobj, type);
+ }
+ }
+
+ public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn)
+ {
+ generator.Emit(OpCodes.Unbox, type);
+ generator.Emit(OpCodes.Ldobj, type);
+ if (!isReturn) {
+ generator.Emit(OpCodes.Stobj, type);
+ }
+ }
+
+ public object Get()
+ {
+ return this.val;
+ }
+
+ public object Get(System.Type type)
+ {
+ switch (type.ToString()) {
+ case "DBus.Custom":
+ case "DBus.Custom&":
+ return this.val;
+ default:
+ throw new ArgumentException("Cannot cast DBus.Type.Custom to type '" + type.ToString() + "'");
+ }
+ }
+
+ [DllImport("dbus-1")]
+ private extern static bool dbus_message_iter_get_custom(IntPtr iter,
+ out string name,
+ out IntPtr value,
+ out int len);
+
+ [DllImport("dbus-1")]
+ private extern static bool dbus_message_iter_append_custom(IntPtr iter,
+ string name,
+ IntPtr data,
+ int len);
+ }
+}