Skip to content

Commit

Permalink
Fixed the marshalling of enums in signals.
Browse files Browse the repository at this point in the history
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
ddobrev committed Jun 25, 2016
1 parent 2df4fc1 commit 4538187
Showing 1 changed file with 56 additions and 25 deletions.
81 changes: 56 additions & 25 deletions QtSharp/QObject.cs
Original file line number Diff line number Diff line change
@@ -54,34 +54,65 @@ protected int HandleQtMetacall(int index, QMetaObject.Call call, void** argument
{
System.Reflection.ParameterInfo parameter = @params[i];
var arg = new IntPtr(arguments[1 + i]);
object value;
if (parameter.ParameterType.IsValueType)
{
value = Marshal.PtrToStructure(arg, parameter.ParameterType);
}
else
{
if (parameter.ParameterType.IsAssignableFrom(typeof(string)))
{
var metaMethod = this.Sender.MetaObject.Method(handler.SignalId);
if (metaMethod.ParameterType(i) == (int) QMetaType.Type.QString)
{
value = Marshal.PtrToStringUni(new IntPtr(QtCore.QString.__CreateInstance(arg).Utf16));
}
else
{
value = Marshal.PtrToStringUni(arg);
}
}
else
{
value = Activator.CreateInstance(parameter.ParameterType, arg);
}
}
parameters[i] = value;
parameters[i] = GetParameterValue(handler, i, parameter, arg);
}
handler.Delegate.DynamicInvoke(parameters);
return -1;
}

private object GetParameterValue(Handler handler, int i, System.Reflection.ParameterInfo parameter, IntPtr arg)
{
if (arg == IntPtr.Zero)
{
return null;
}
var type = parameter.ParameterType.IsEnum ? parameter.ParameterType.GetEnumUnderlyingType() : parameter.ParameterType;
switch (Type.GetTypeCode(type))
{
case TypeCode.Empty:
return null;
case TypeCode.Object:
return Activator.CreateInstance(type, arg);
case TypeCode.DBNull:
return DBNull.Value;
case TypeCode.Boolean:
return *(bool*) arg;
case TypeCode.Char:
return *(char*) arg;
case TypeCode.SByte:
return *(sbyte*) arg;
case TypeCode.Byte:
return *(byte*) arg;
case TypeCode.Int16:
return *(short*) arg;
case TypeCode.UInt16:
return *(ushort*) arg;
case TypeCode.Int32:
return *(int*) arg;
case TypeCode.UInt32:
return *(uint*) arg;
case TypeCode.Int64:
return *(long*) arg;
case TypeCode.UInt64:
return *(ulong*) arg;
case TypeCode.Single:
return *(float*) arg;
case TypeCode.Double:
return *(double*) arg;
case TypeCode.Decimal:
return *(decimal*) arg;
case TypeCode.DateTime:
return *(DateTime*) arg;
case TypeCode.String:
var metaMethod = this.Sender.MetaObject.Method(handler.SignalId);
if (metaMethod.ParameterType(i) == (int) QMetaType.Type.QString)
{
return Marshal.PtrToStringUni(new IntPtr(QtCore.QString.__CreateInstance(arg).Utf16));
}
return Marshal.PtrToStringUni(arg);
default:
throw new ArgumentOutOfRangeException(parameter.Name, "Parameter type with invalid type code.");
}
}
}
}

0 comments on commit 4538187

Please sign in to comment.