dbus/mono/DBusType Array.cs, NONE, 1.1 Boolean.cs, NONE, 1.1 Byte.cs, NONE, 1.1 Custom.cs, NONE, 1.1 Dict.cs, NONE, 1.1 Double.cs, NONE, 1.1 IDBusType.cs, NONE, 1.1 Int32.cs, NONE, 1.1 Int64.cs, NONE, 1.1 Nil.cs, NONE, 1.1 ObjectPath.cs, NONE, 1.1 String.cs, NONE, 1.1 UInt32.cs, NONE, 1.1 UInt64.cs, NONE, 1.1

Owen Fraser-Green ow3n at pdx.freedesktop.org
Tue Mar 23 04:10:34 PST 2004


Update of /cvs/dbus/dbus/mono/DBusType
In directory pdx:/tmp/cvs-serv17190/mono/DBusType

Added Files:
	Array.cs Boolean.cs Byte.cs Custom.cs Dict.cs Double.cs 
	IDBusType.cs Int32.cs Int64.cs Nil.cs ObjectPath.cs String.cs 
	UInt32.cs UInt64.cs 
Log Message:
First checkin of the Mono bindings.


--- NEW FILE: Array.cs ---
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// Array.
  /// </summary>
  public class Array : IDBusType
  {
    public const char Code = 'a';
    private System.Array val;
    private ArrayList elements;
    private Type elementType;
    
    private Array()
    {
    }
    
    public Array(System.Array val) 
    {
      this.val = val;
      this.elementType = Arguments.MatchType(val.GetType().UnderlyingSystemType);
    }

    public Array(IntPtr iter)
    {
      IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
      
      int elementTypeCode;
      dbus_message_iter_init_array_iterator(iter, arrayIter, out elementTypeCode);
      this.elementType = (Type) Arguments.DBusTypes[(char) elementTypeCode];

      elements = new ArrayList();

      do {
	object [] pars = new Object[1];
	pars[0] = arrayIter;
	DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
	elements.Add(dbusType);
      } while (dbus_message_iter_next(arrayIter));
      
      Marshal.FreeCoTaskMem(arrayIter);
    }
    
    public void Append(IntPtr iter)
    {
      IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);

      if (!dbus_message_iter_append_array(iter,
					  arrayIter,
					  (int) Arguments.GetCode(this.elementType))) {
	throw new ApplicationException("Failed to append INT32 argument:" + val);
      }

      foreach (object element in this.val) {
	object [] pars = new Object[1];
	pars[0] = element;
	DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
	dbusType.Append(arrayIter);
      }

      Marshal.FreeCoTaskMem(arrayIter);
    }    

    public static bool Suits(System.Type type) 
    {
      if (type.IsArray) {
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_Ref);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Castclass, type);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_Ref);
      }
    }
    
    public object Get() 
    {
      throw new ArgumentException("Cannot call Get on an Array without specifying type.");
    }

    public object Get(System.Type type)
    {
      if (Arguments.Suits(elementType, type.UnderlyingSystemType)) {
	this.val = System.Array.CreateInstance(type.UnderlyingSystemType, elements.Count);
	int i = 0;
	foreach (DBusType.IDBusType element in elements) {
	  this.val.SetValue(element.Get(type.UnderlyingSystemType), i++);
	}	
      } else {
	throw new ArgumentException("Cannot cast DBus.Type.Array to type '" + type.ToString() + "'");
      }
	
	return this.val;
    }    

    [DllImport("dbus-1")]
    private extern static void dbus_message_iter_init_array_iterator(IntPtr iter,
								     IntPtr arrayIter,
								     out int elementType);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_array(IntPtr iter, 
							      IntPtr arrayIter,
							      int elementType);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_has_next(IntPtr iter);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_next(IntPtr iter);
  }
}

--- NEW FILE: Boolean.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// Boolean
  /// </summary>
  public class Boolean : IDBusType
  {
    public const char Code = 'b';
    private System.Boolean val;
    
    private Boolean()
    {
    }
    
    public Boolean(System.Boolean val) 
    {
      this.val = val;
    }

    public Boolean(IntPtr iter)
    {
      this.val = dbus_message_iter_get_boolean(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_boolean(iter, val))
	throw new ApplicationException("Failed to append BOOLEAN argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.Boolean":
      case "System.Boolean&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_I1);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_I1);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I1);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "System.Boolean":
      case "System.Boolean&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Boolean to type '" + type.ToString() + "'");
      }
    }

    [DllImport("dbus-1")]
    private extern static System.Boolean dbus_message_iter_get_boolean(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_boolean(IntPtr iter, System.Boolean value);
  }
}

--- NEW FILE: Byte.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// Byte
  /// </summary>
  public class Byte : IDBusType
  {
    public const char Code = 'y';
    private System.Byte val;
    
    private Byte()
    {
    }
    
    public Byte(System.Byte val) 
    {
      this.val = val;
    }

    public Byte(IntPtr iter)
    {
      this.val = dbus_message_iter_get_byte(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_byte(iter, val))
	throw new ApplicationException("Failed to append BYTE argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.Byte":
      case "System.Byte&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_U1);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_U1);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I1);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "System.Byte":
      case "System.Byte&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Byte to type '" + type.ToString() + "'");
      }
    }

    [DllImport("dbus-1")]
    private extern static System.Byte dbus_message_iter_get_byte(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_byte(IntPtr iter, System.Byte value);
  }
}

--- NEW FILE: Custom.cs ---
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);
  }
}

--- NEW FILE: Dict.cs ---
using System;
using System.Collections;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// Dict.
  /// </summary>
  public class Dict : IDBusType
  {
    public const char Code = 'm';
    private Hashtable val;
    
    private Dict()
    {
    }
    
    public Dict(IDictionary val)
    {
      this.val = new Hashtable();
      foreach (DictionaryEntry entry in val) {
	this.val.Add(entry.Key, entry.Value);
      }
    }

    public Dict(IntPtr iter)
    {
      IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
      
      dbus_message_iter_init_dict_iterator(iter, dictIter);

      this.val = new Hashtable();

      do {
	string key = dbus_message_iter_get_dict_key(dictIter);

	// Get the argument type and get the value
	Type elementType = (Type) DBus.Arguments.DBusTypes[(char) dbus_message_iter_get_arg_type(dictIter)];
	object [] pars = new Object[1];
	pars[0] = dictIter;
	DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
	this.val.Add(key, dbusType);
      } while (dbus_message_iter_next(dictIter));
      
      Marshal.FreeCoTaskMem(dictIter);
    }
    
    public void Append(IntPtr iter)
    {
      IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);

      if (!dbus_message_iter_append_dict(iter,
					 dictIter)) {
	throw new ApplicationException("Failed to append DICT argument:" + val);
      }

      foreach (DictionaryEntry entry in this.val) {
	if (!dbus_message_iter_append_dict_key(dictIter, (string) entry.Key)) {
	  throw new ApplicationException("Failed to append DICT key:" + entry.Key);
	}
	
	// Get the element type
	Type elementType = Arguments.MatchType(entry.Value.GetType());
	object [] pars = new Object[1];
	pars[0] = entry.Value;
	DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
	dbusType.Append(dictIter);
      }

      Marshal.FreeCoTaskMem(dictIter);
    }    

    public static bool Suits(System.Type type) 
    {
      if (typeof(IDictionary).IsAssignableFrom(type)) {
	return true;
      }
            
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_Ref);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Castclass, type);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_Ref);
      }
    }
    
    public object Get() 
    {
      return Get(typeof(Hashtable));
    }

    public object Get(System.Type type)
    {
      IDictionary retVal;

      if (Suits(type)) {
	retVal = (IDictionary) Activator.CreateInstance(type, new object[0]);
	foreach (DictionaryEntry entry in this.val) {
	  retVal.Add(entry.Key, ((IDBusType) entry.Value).Get());
	}
      } else {
	throw new ArgumentException("Cannot cast DBus.Type.Dict to type '" + type.ToString() + "'");
      }
	
      return retVal;
    }    

    [DllImport("dbus-1")]
    private extern static void dbus_message_iter_init_dict_iterator(IntPtr iter,
								    IntPtr dictIter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_dict(IntPtr iter, 
							     IntPtr dictIter);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_has_next(IntPtr iter);

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_next(IntPtr iter);

    [DllImport("dbus-1")]
    private extern static string dbus_message_iter_get_dict_key (IntPtr dictIter);  

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_dict_key (IntPtr dictIter,
								  string value);
    [DllImport("dbus-1")]
    private extern static int dbus_message_iter_get_arg_type(IntPtr iter);
  }
}

--- NEW FILE: Double.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// IEEE 754 double
  /// </summary>
  public class Double : IDBusType
  {
    public const char Code = 'd';
    private System.Double val;
    
    private Double()
    {
    }
    
    public Double(System.Double val) 
    {
      this.val = val;
    }

    public Double(IntPtr iter)
    {
      this.val = dbus_message_iter_get_double(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_double(iter, val))
	throw new ApplicationException("Failed to append DOUBLE argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.Double":
      case "System.Double&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_R8);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_R8);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_R8);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "System.Double":
      case "System.Double&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Double to type '" + type.ToString() + "'");
      }
    }

    [DllImport("dbus-1")]
    private extern static System.Double dbus_message_iter_get_double(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_double(IntPtr iter, System.Double value);
  }
}

--- NEW FILE: IDBusType.cs ---
using System;

namespace DBus.DBusType
{
  /// <summary>
  /// Base class for DBusTypes
  /// </summary>
  public interface IDBusType
  {
    object Get();
    
    object Get(System.Type type);  

    void Append(IntPtr iter);
  }
}

--- NEW FILE: Int32.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// 32-bit integer.
  /// </summary>
  public class Int32 : IDBusType
  {
    public const char Code = 'i';
    private System.Int32 val;
    
    private Int32()
    {
    }
    
    public Int32(System.Int32 val) 
    {
      this.val = val;
    }

    public Int32(IntPtr iter)
    {
      this.val = dbus_message_iter_get_int32(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_int32(iter, val))
	throw new ApplicationException("Failed to append INT32 argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.Int32":
      case "System.Int32&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_I4);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_I4);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I4);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "System.Int32":
      case "System.Int32&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Int32 to type '" + type.ToString() + "'");
      }
    }    

    [DllImport("dbus-1")]
    private extern static System.Int32 dbus_message_iter_get_int32(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_int32(IntPtr iter, System.Int32 value);
  }
}

--- NEW FILE: Int64.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// 64-bit integer.
  /// </summary>
  public class Int64 : IDBusType
  {
    public const char Code = 'x';
    private System.Int64 val;
    
    private Int64()
    {
    }
    
    public Int64(System.Int64 val) 
    {
      this.val = val;
    }

    public Int64(IntPtr iter)
    {
      this.val = dbus_message_iter_get_int64(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_int64(iter, val))
	throw new ApplicationException("Failed to append INT64 argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.Int64":
      case "System.Int64&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_I8);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_I8);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I8);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) {
      case "System.Int64":
      case "System.Int64&":
	return this.val;
      default:
	throw new ArgumentException("Cannot cast DBus.Type.Int64 to type '" + type.ToString() + "'");
      }
    }    

    [DllImport("dbus-1")]
    private extern static System.Int64 dbus_message_iter_get_int64(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_int64(IntPtr iter, System.Int64 value);
  }
}

--- NEW FILE: Nil.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// Marks a "void"/"unset"/"nonexistent"/"null" argument.
  /// </summary>
  public class Nil : IDBusType
  {
    public const char Code = 'v';
    
    private Nil()
    {
    }
    
    public Nil(object nil) 
    {
    }

    public Nil(IntPtr iter)
    {
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_nil(iter))
	throw new ApplicationException("Failed to append NIL argument");
    }

    public static bool Suits(System.Type type) 
    {
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_I1);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_I1);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I1);
      }
    }
    
    public object Get() 
    {
      return null;
    }

    public object Get(System.Type type)
    {
      throw new ArgumentException("Cannot cast DBus.Type.Nil to type '" + type.ToString() + "'");
    }

    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_nil(IntPtr iter);
  }
}

--- NEW FILE: ObjectPath.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// An object path.
  /// </summary>
  public class ObjectPath : IDBusType
  {
    public const char Code = 'o';
    private string pathName = null;
    private object val = null;
    private Service service = null;
    
    private ObjectPath()
    {
    }
    
    public ObjectPath(object val) 
    {
      this.val = val;
    }
    
    public ObjectPath(IntPtr iter)
    {
      
      this.pathName = Marshal.PtrToStringAnsi(dbus_message_iter_get_object_path(iter));
    }

    public void SetService(Service service) 
    {
      this.service = service;
    }

    private string PathName 
    {
      get {
	if (this.pathName == null && this.val != null) {
	  Handler handler = this.service.GetHandler(this.val);
	  this.pathName = handler.PathName;
	}
	
	return this.pathName;
      }
    }

    public void Append(IntPtr iter) 
    {
      if (PathName == null) {
	throw new ApplicationException("Unable to append ObjectPath before calling SetService()");
      }
      
      if (!dbus_message_iter_append_object_path(iter, Marshal.StringToHGlobalAnsi(PathName)))
	throw new ApplicationException("Failed to append OBJECT_PATH argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      object[] attributes = type.GetCustomAttributes(typeof(InterfaceAttribute), true);
      if (attributes.Length == 1) {
	return true;
      } else {
	return false;
      }
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_Ref);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Castclass, type);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_Ref);
      }
    }

    public object Get() 
    {
      throw new ArgumentException("Cannot call Get on an ObjectPath without specifying type.");
    }

    public object Get(System.Type type)
    {
      if (this.service == null) {
	throw new ApplicationException("Unable to get ObjectPath before calling SetService()");
      }
      
      try {
	return this.service.GetObject(type, PathName);
      } catch(Exception ex) {
	throw new ArgumentException("Cannot cast object pointed to by Object Path to type '" + type.ToString() + "': " + ex);
      }
    }

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_iter_get_object_path(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_object_path(IntPtr iter, IntPtr pathName);
  }
}

--- NEW FILE: String.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// A string.
  /// </summary>
  public class String : IDBusType
  {
    public const char Code = 's';
    private string val;
    
    private String()
    {
    }
    
    public String(string val) 
    {
      this.val = val;
    }
    
    public String(IntPtr iter)
    {
      this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter));
    }

    public void Append(IntPtr iter) 
    {
      if (!dbus_message_iter_append_string(iter, Marshal.StringToHGlobalAnsi(val)))
	throw new ApplicationException("Failed to append STRING argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.String":
      case "System.String&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_Ref);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Castclass, type);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_Ref);
      }
    }

    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) 
	{
	case "System.String":
	case "System.String&":
	  return this.val;
	default:
	  throw new ArgumentException("Cannot cast DBus.Type.String to type '" + type.ToString() + "'");
	}
    }    

    [DllImport("dbus-1")]
    private extern static IntPtr dbus_message_iter_get_string(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_string(IntPtr iter, IntPtr value);
  }
}

--- NEW FILE: UInt32.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// 32-bit unsigned integer.
  /// </summary>
  public class UInt32 : IDBusType
  {
    public const char Code = 'u';
    private System.UInt32 val;
    
    private UInt32()
    {
    }
    
    public UInt32(System.UInt32 val) 
    {
      this.val = val;
    }

    public UInt32(IntPtr iter)
    {
      this.val = dbus_message_iter_get_uint32(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_uint32(iter, val))
	throw new ApplicationException("Failed to append UINT32 argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.UInt32":
      case "System.UInt32&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_U4);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_U4);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I4);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) 
	{
	case "System.UInt32":
	case "System.UInt32&":
	  return this.val;
	default:
	  throw new ArgumentException("Cannot cast DBus.Type.UInt32 to type '" + type.ToString() + "'");
	}
    }    

    [DllImport("dbus-1")]
    private extern static System.UInt32 dbus_message_iter_get_uint32(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_uint32(IntPtr iter, System.UInt32 value);
  }
}

--- NEW FILE: UInt64.cs ---
using System;
using System.Runtime.InteropServices;
using System.Reflection.Emit;

using DBus;

namespace DBus.DBusType
{
  /// <summary>
  /// 64-bit unsigned integer.
  /// </summary>
  public class UInt64 : IDBusType
  {
    public const char Code = 't';
    private System.UInt64 val;
    
    private UInt64()
    {
    }
    
    public UInt64(System.UInt64 val) 
    {
      this.val = val;
    }

    public UInt64(IntPtr iter)
    {
      this.val = dbus_message_iter_get_uint64(iter);
    }
    
    public void Append(IntPtr iter)
    {
      if (!dbus_message_iter_append_uint64(iter, val))
	throw new ApplicationException("Failed to append UINT64 argument:" + val);
    }

    public static bool Suits(System.Type type) 
    {
      switch (type.ToString()) {
      case "System.UInt64":
      case "System.UInt64&":
	return true;
      }
      
      return false;
    }

    public static void EmitMarshalIn(ILGenerator generator, Type type)
    {
      if (type.IsByRef) {
	generator.Emit(OpCodes.Ldind_I8);
      }
    }

    public static void EmitMarshalOut(ILGenerator generator, Type type, bool isReturn) 
    {
      generator.Emit(OpCodes.Unbox, type);
      generator.Emit(OpCodes.Ldind_I8);
      if (!isReturn) {
	generator.Emit(OpCodes.Stind_I8);
      }
    }
    
    public object Get() 
    {
      return this.val;
    }

    public object Get(System.Type type)
    {
      switch (type.ToString()) 
	{
	case "System.UInt64":
	case "System.UInt64&":
	  return this.val;
	default:
	  throw new ArgumentException("Cannot cast DBus.Type.UInt64 to type '" + type.ToString() + "'");
	}
    }    

    [DllImport("dbus-1")]
    private extern static System.UInt64 dbus_message_iter_get_uint64(IntPtr iter);
 
    [DllImport("dbus-1")]
    private extern static bool dbus_message_iter_append_uint64(IntPtr iter, System.UInt64 value);
  }
}




More information about the dbus-commit mailing list