Commit f3345389 authored by Owen Fraser-Green's avatar Owen Fraser-Green

Made all DBusTypes take Service in the constructor because Array also needed...

Made all DBusTypes take Service in the constructor because Array also needed it in the case of an array of OBJECT_PATH objects.
parent 93aa6203
......@@ -74,17 +74,15 @@ namespace DBus
// Append an argument
public void Append(DBusType.IDBusType dbusType)
{
if (dbusType.GetType() == typeof(DBusType.ObjectPath)) {
((DBusType.ObjectPath) dbusType).SetService(message.Service);
}
dbusType.Append(appenderIter);
}
// Append an argument of the specified type
private void AppendType(Type type, object val)
{
object [] pars = new Object[1];
object [] pars = new Object[2];
pars[0] = val;
pars[1] = message.Service;
DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(MatchType(type), pars);
Append(dbusType);
}
......@@ -163,7 +161,7 @@ namespace DBus
// Get the appropriate constructor for a D-BUS type
public static ConstructorInfo GetDBusTypeConstructor(Type dbusType, Type type)
{
ConstructorInfo constructor = dbusType.GetConstructor(new Type[] {type.UnderlyingSystemType});
ConstructorInfo constructor = dbusType.GetConstructor(new Type[] {type.UnderlyingSystemType, typeof(Service)});
if (constructor == null)
throw new ArgumentException("There is no valid constructor for '" + dbusType + "' from type '" + type + "'");
......@@ -254,17 +252,13 @@ namespace DBus
{
get
{
object [] pars = new Object[1];
object [] pars = new Object[2];
pars[0] = iter;
pars[1] = arguments.message.Service;
Type type = (Type) DBusTypes[(char) dbus_message_iter_get_arg_type(iter)];
DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(type, pars);
// Special case for ObjectPath
if (type == typeof(DBusType.ObjectPath)) {
((DBusType.ObjectPath) dbusType).SetService(arguments.message.Service);
}
return dbusType;
}
}
......
......@@ -16,19 +16,23 @@ namespace DBus.DBusType
private System.Array val;
private ArrayList elements;
private Type elementType;
private Service service = null;
private Array()
{
}
public Array(System.Array val)
public Array(System.Array val, Service service)
{
this.val = val;
this.elementType = Arguments.MatchType(val.GetType().UnderlyingSystemType);
this.service = service;
}
public Array(IntPtr iter)
public Array(IntPtr iter, Service service)
{
this.service = service;
IntPtr arrayIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
int elementTypeCode;
......@@ -38,8 +42,9 @@ namespace DBus.DBusType
elements = new ArrayList();
do {
object [] pars = new Object[1];
object [] pars = new Object[2];
pars[0] = arrayIter;
pars[1] = service;
DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
elements.Add(dbusType);
} while (dbus_message_iter_next(arrayIter));
......@@ -58,8 +63,9 @@ namespace DBus.DBusType
}
foreach (object element in this.val) {
object [] pars = new Object[1];
object [] pars = new Object[2];
pars[0] = element;
pars[1] = this.service;
DBusType.IDBusType dbusType = (DBusType.IDBusType) Activator.CreateInstance(elementType, pars);
dbusType.Append(arrayIter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Boolean(System.Boolean val)
public Boolean(System.Boolean val, Service service)
{
this.val = val;
}
public Boolean(IntPtr iter)
public Boolean(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_boolean(iter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Byte(System.Byte val)
public Byte(System.Byte val, Service service)
{
this.val = val;
}
public Byte(IntPtr iter)
public Byte(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_byte(iter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Custom(DBus.Custom val)
public Custom(DBus.Custom val, Service service)
{
this.val = val;
}
public Custom(IntPtr iter)
public Custom(IntPtr iter, Service service)
{
string name;
IntPtr value;
......
......@@ -19,7 +19,7 @@ namespace DBus.DBusType
{
}
public Dict(IDictionary val)
public Dict(IDictionary val, Service service)
{
this.val = new Hashtable();
foreach (DictionaryEntry entry in val) {
......@@ -27,7 +27,7 @@ namespace DBus.DBusType
}
}
public Dict(IntPtr iter)
public Dict(IntPtr iter, Service service)
{
IntPtr dictIter = Marshal.AllocCoTaskMem(Arguments.DBusMessageIterSize);
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Double(System.Double val)
public Double(System.Double val, Service service)
{
this.val = val;
}
public Double(IntPtr iter)
public Double(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_double(iter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Int32(System.Int32 val)
public Int32(System.Int32 val, Service service)
{
this.val = val;
}
public Int32(IntPtr iter)
public Int32(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_int32(iter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public Int64(System.Int64 val)
public Int64(System.Int64 val, Service service)
{
this.val = val;
}
public Int64(IntPtr iter)
public Int64(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_int64(iter);
}
......
......@@ -17,11 +17,11 @@ namespace DBus.DBusType
{
}
public Nil(object nil)
public Nil(object nil, Service service)
{
}
public Nil(IntPtr iter)
public Nil(IntPtr iter, Service service)
{
}
......
......@@ -20,19 +20,16 @@ namespace DBus.DBusType
{
}
public ObjectPath(object val)
public ObjectPath(object val, Service service)
{
this.val = val;
this.service = service;
}
public ObjectPath(IntPtr iter)
public ObjectPath(IntPtr iter, Service service)
{
this.pathName = Marshal.PtrToStringAnsi(dbus_message_iter_get_object_path(iter));
}
public void SetService(Service service)
{
this.service = service;
}
......@@ -43,17 +40,13 @@ namespace DBus.DBusType
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);
}
......@@ -90,10 +83,6 @@ namespace DBus.DBusType
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) {
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public String(string val)
public String(string val, Service service)
{
this.val = val;
}
public String(IntPtr iter)
public String(IntPtr iter, Service service)
{
this.val = Marshal.PtrToStringAnsi(dbus_message_iter_get_string(iter));
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public UInt32(System.UInt32 val)
public UInt32(System.UInt32 val, Service service)
{
this.val = val;
}
public UInt32(IntPtr iter)
public UInt32(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_uint32(iter);
}
......
......@@ -18,12 +18,12 @@ namespace DBus.DBusType
{
}
public UInt64(System.UInt64 val)
public UInt64(System.UInt64 val, Service service)
{
this.val = val;
}
public UInt64(IntPtr iter)
public UInt64(IntPtr iter, Service service)
{
this.val = dbus_message_iter_get_uint64(iter);
}
......
......@@ -108,7 +108,7 @@ namespace DBus
for (int parN = 0; parN < pars.Length; parN++) {
ParameterInfo par = pars[parN];
if (!par.IsOut) {
EmitIn(generator, par.ParameterType, parN);
EmitIn(generator, par.ParameterType, parN, serviceF);
}
}
......@@ -145,7 +145,7 @@ namespace DBus
typeB.DefineMethodOverride(methodBuilder, method);
}
private void EmitIn(ILGenerator generator, Type parType, int parN)
private void EmitIn(ILGenerator generator, Type parType, int parN, FieldInfo serviceF)
{
Type inParType = Arguments.MatchType(parType);
//generator.EmitWriteLine("methodCall.Arguments.Append(...)");
......@@ -157,6 +157,7 @@ namespace DBus
object[] pars = new object[] {generator, parType};
inParType.InvokeMember("EmitMarshalIn", BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod, null, null, pars, null);
generator.Emit(OpCodes.Ldsfld, serviceF);
generator.Emit(OpCodes.Newobj, Arguments.GetDBusTypeConstructor(inParType, parType));
generator.EmitCall(OpCodes.Callvirt, Arguments_AppendMI, null);
}
......
......@@ -75,6 +75,10 @@ namespace DBus
internal Handler GetHandler(object handledObject)
{
if (!registeredHandlers.Contains(handledObject)) {
throw new ArgumentException("No handler registered for object: " + handledObject);
}
return (Handler) registeredHandlers[handledObject];
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment