public static T qobject_cast <T>(QObject obj) where T : class { if (obj == null) { return(null); } // direct cast possible? try { return((T)(object)obj); } catch {} Type t = typeof(T); if (!SmokeMarshallers.IsSmokeClass(t)) { return(null); } string className = SmokeMarshallers.SmokeClassName(t); IntPtr ret = qyoto_qt_metacast((IntPtr)GCHandle.Alloc(obj),className); if (ret == IntPtr.Zero) { return(null); } else { GCHandle handle = (GCHandle)ret; object target = handle.Target; handle.Free(); return((T)target); } }
public T FindChild <T>(string name) { string childClassName = null; IntPtr metaObject = IntPtr.Zero; if (SmokeMarshallers.IsSmokeClass(typeof(T))) { childClassName = SmokeMarshallers.SmokeClassName(typeof(T)); } else { metaObject = (IntPtr)GCHandle.Alloc(Qyoto.GetMetaObject(typeof(T))); } IntPtr child = FindQObjectChild((IntPtr)GCHandle.Alloc(this), childClassName, metaObject, name); if (child != IntPtr.Zero) { try { return((T)((GCHandle)child).Target); } catch (Exception e) { Console.WriteLine("Found child, but an error has occurred: {0}", e.Message); return(default(T)); } } else { return(default(T)); } }
public static int RegisterType <T>() { string className; Destructor dtorHandler; Constructor ctorHandler; if (SmokeMarshallers.IsSmokeClass(typeof(T))) { className = SmokeMarshallers.SmokeClassName(typeof(T)); dtorHandler = delegate(IntPtr obj) { DestroyObject(className, obj); }; ctorHandler = delegate(IntPtr copy) { return(CreateObject(className, copy)); }; } else { className = typeof(T).ToString(); dtorHandler = delegate(IntPtr obj) { ((GCHandle)obj).Free(); }; ctorHandler = delegate(IntPtr copy) { if (copy != IntPtr.Zero) { object o = (T)((GCHandle)copy).Target; // create a copy if this is a valuetype return((IntPtr)GCHandle.Alloc(o)); } return((IntPtr)GCHandle.Alloc(default(T))); }; } GCHandle.Alloc(className); // prevent collection GCHandle.Alloc(dtorHandler); GCHandle.Alloc(ctorHandler); return(QMetaTypeRegisterType(className, dtorHandler, ctorHandler)); }
public SmokeInvocation(Type klass, Object obj) { classToProxy = klass; instance = obj; className = SmokeMarshallers.SmokeClassName(klass); TryInitialize(klass.Assembly); if (!globalMethodIdCache.TryGetValue(classToProxy, out methodIdCache)) { methodIdCache = new Dictionary <string, ModuleIndex>(); globalMethodIdCache[classToProxy] = methodIdCache; } if (instance != null) { AddOverridenMethods(instance.GetType()); } }
public List <T> FindChildren <T>(QRegExp regExp) { List <T> list = new List <T>(); AddToListFn addFn = delegate(IntPtr obj) { T o = (T)((System.Runtime.InteropServices.GCHandle)obj).Target; list.Add(o); }; string childClassName = null; IntPtr metaObject = IntPtr.Zero; if (SmokeMarshallers.IsSmokeClass(typeof(T))) { childClassName = SmokeMarshallers.SmokeClassName(typeof(T)); } else { metaObject = (IntPtr)GCHandle.Alloc(Qyoto.GetMetaObject(typeof(T))); } FindQObjectChildren((IntPtr)GCHandle.Alloc(this), childClassName, metaObject, (IntPtr)GCHandle.Alloc(regExp), string.Empty, addFn); return(list); }
public T Create <T>(QWidget parentWidget, QObject parent, string keyword, List <QVariant> args) where T : class { Type type = typeof(T); if (!SmokeMarshallers.IsSmokeClass(type)) { throw new Exception("The generic type must be included in the bindings"); } QObject o = Create(SmokeMarshallers.SmokeClassName(type), parentWidget, parent, args, keyword); if (o == null) { return(null); } T t = qobject_cast <T>(o); if (t == null) { o.Dispose(); } return(t); }
static public QVariant FromValue(object value, System.Type valueType) { if (valueType == typeof(bool)) { return(new QVariant((bool)value)); } else if (valueType == typeof(double)) { return(new QVariant((double)value)); } else if (valueType == typeof(QBitArray)) { return(new QVariant((QBitArray)value)); } else if (valueType == typeof(QByteArray)) { return(new QVariant((QByteArray)value)); } else if (valueType == typeof(char)) { return(new QVariant(new QChar((char)value))); } else if (valueType == typeof(QDate)) { return(new QVariant((QDate)value)); } else if (valueType == typeof(QDateTime)) { return(new QVariant((QDateTime)value)); } else if (valueType == typeof(int)) { return(new QVariant((int)value)); } else if (valueType == typeof(QLine)) { return(new QVariant((QLine)value)); } else if (valueType == typeof(QLineF)) { return(new QVariant((QLineF)value)); } else if (valueType == typeof(QLocale)) { return(new QVariant((QLocale)value)); } else if (valueType == typeof(QPoint)) { return(new QVariant((QPoint)value)); } else if (valueType == typeof(QPointF)) { return(new QVariant((QPointF)value)); } else if (valueType == typeof(QRect)) { return(new QVariant((QRect)value)); } else if (valueType == typeof(QRectF)) { return(new QVariant((QRectF)value)); } else if (valueType == typeof(QRegExp)) { return(new QVariant((QRegExp)value)); } else if (valueType == typeof(QSize)) { return(new QVariant((QSize)value)); } else if (valueType == typeof(QSizeF)) { return(new QVariant((QSizeF)value)); } else if (valueType == typeof(string)) { return(new QVariant((string)value)); } else if (valueType == typeof(List <string>)) { return(new QVariant((List <string>)value)); } else if (valueType == typeof(List <QVariant>)) { return(new QVariant((List <QVariant>)value)); } else if (valueType == typeof(Dictionary <string, QVariant>)) { return(new QVariant((Dictionary <string, QVariant>)value)); } else if (valueType == typeof(QTime)) { return(new QVariant((QTime)value)); } else if (valueType == typeof(uint)) { return(new QVariant((uint)value)); } else if (valueType == typeof(QUrl)) { return(new QVariant((QUrl)value)); } else if (valueType == typeof(QVariant)) { return(new QVariant((QVariant)value)); } else if (valueType.IsEnum) { return(new QVariant((int)value)); } else { string typeName; if (SmokeMarshallers.IsSmokeClass(valueType)) { typeName = SmokeMarshallers.SmokeClassName(valueType); } else { typeName = valueType.ToString(); } Type type = NameToType(typeName); if (type == Type.Invalid) { return(FromValue <object>(value)); } else if (type > Type.LastCoreType) { IntPtr valueHandle = IntPtr.Zero; if (value != null) { valueHandle = (IntPtr)GCHandle.Alloc(value); } GCHandle handle = (GCHandle)QVariantFromValue(QMetaType.type(typeName), valueHandle); QVariant v = (QVariant)handle.Target; handle.Free(); return(v); } return(new QVariant()); } }
public object Value(System.Type valueType) { if (valueType == typeof(bool)) { return(ToBool()); } else if (valueType == typeof(double)) { return(ToDouble()); } else if (valueType == typeof(QBitArray)) { return(ToBitArray()); } else if (valueType == typeof(QByteArray)) { return(ToByteArray()); } else if (valueType == typeof(char)) { return(ToChar()); } else if (valueType == typeof(QDate)) { return(ToDate()); } else if (valueType == typeof(QDateTime)) { return(ToDateTime()); } else if (valueType == typeof(int)) { return(ToInt()); } else if (valueType == typeof(QLine)) { return(ToLine()); } else if (valueType == typeof(QLineF)) { return(ToLineF()); } else if (valueType == typeof(QLocale)) { return(ToLocale()); } else if (valueType == typeof(QPoint)) { return(ToPoint()); } else if (valueType == typeof(QPointF)) { return(ToPointF()); } else if (valueType == typeof(QRect)) { return(ToRect()); } else if (valueType == typeof(QRectF)) { return(ToRectF()); } else if (valueType == typeof(QRegExp)) { return(ToRegExp()); } else if (valueType == typeof(QSize)) { return(ToSize()); } else if (valueType == typeof(QSizeF)) { return(ToSizeF()); } else if (valueType == typeof(string)) { return(ToString()); } else if (valueType == typeof(List <string>)) { return(ToStringList()); } else if (valueType == typeof(List <QVariant>)) { return(ToList()); } else if (valueType == typeof(Dictionary <string, QVariant>)) { object o = ToMap(); if (o == null) { o = ToHash(); } return(o); } else if (valueType == typeof(QTime)) { return(ToTime()); } else if (valueType == typeof(uint)) { return(ToUInt()); } else if (valueType == typeof(QUrl)) { return(ToUrl()); } else if (valueType == typeof(QVariant)) { return(this); } else if (valueType.IsEnum) { return(Enum.ToObject(valueType, ToLongLong())); } else { string typeName; if (SmokeMarshallers.IsSmokeClass(valueType)) { typeName = SmokeMarshallers.SmokeClassName(valueType); } else { typeName = valueType.ToString(); } Type type = NameToType(typeName); if (type > Type.LastCoreType) { IntPtr instancePtr = QVariantValue(typeName, (IntPtr)GCHandle.Alloc(this)); return(((GCHandle)instancePtr).Target); } else if (type == Type.Invalid) { Console.WriteLine("QVariant.Value(): invalid type: {0}", valueType); } return(null); } }
public static QMetaObject MakeMetaObject(Type t) { if (t == null) { return(null); } QMetaObject parentMeta = null; string parentClassName = null; if (!SmokeMarshallers.IsSmokeClass(t.BaseType) && !metaObjects.TryGetValue(t.BaseType, out parentMeta)) { // create QMetaObject parentMeta = MakeMetaObject(t.BaseType); } else { parentClassName = SmokeMarshallers.SmokeClassName(t.BaseType); } ICollection <CPPMethod> slots; // build slot table slots = GetSlotSignatures(t).Values; PropertyInfo pi = t.GetProperty("Emit", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.DeclaredOnly); ICollection <CPPMethod> signals = null; if (pi == null) { signals = new List <CPPMethod>(); } else { emitInterfaceCache[t] = pi.PropertyType; signals = GetSignalSignatures(pi.PropertyType).Values; } ICollection <CPPProperty> properties = GetProperties(t).Values; QyotoMetaData metaData = new QyotoMetaData(t.Name, signals, slots, GetClassInfos(t), properties); IntPtr metaObject; IntPtr parentMetaPtr = (IntPtr)0; unsafe { fixed(byte *stringdata = metaData.StringData) fixed(uint *data = metaData.Data) { if (parentMeta != null) { #if DEBUG parentMetaPtr = (IntPtr)DebugGCHandle.Alloc(parentMeta); #else parentMetaPtr = (IntPtr)GCHandle.Alloc(parentMeta); #endif } metaObject = qyoto_make_metaObject(parentClassName, parentMetaPtr, (IntPtr)stringdata, metaData.StringData.Length, (IntPtr)data, metaData.Data.Length); } } QMetaObject res = (QMetaObject)((GCHandle)metaObject).Target; #if DEBUG DebugGCHandle.Free((GCHandle)metaObject); #else ((GCHandle)metaObject).Free(); #endif metaObjects.Add(t, res); return(res); }
public static string GetPrimitiveString(Type type) { string typeString = type.ToString(); switch (typeString) { case "System.Void": return(""); case "System.Boolean": return("bool"); case "System.Int32": return("int"); case "System.Int64": return("long"); case "System.UInt32": return("uint"); case "System.UInt64": return("ulong"); case "System.Int16": return("short"); case "System.UInt16": return("ushort"); case "System.Byte": return("uchar"); case "System.SByte": return("sbyte"); case "System.String": return("QString"); case "System.Double": return("double"); case "System.Single": return("float"); case "System.Char": return("char"); } if (type.IsGenericType) { Type[] args = type.GetGenericArguments(); if (type.FullName.StartsWith("System.Collections.Generic.List`1")) { return("QList<" + GetPrimitiveString(args[0]) + ">"); } else if (type.FullName.StartsWith("System.Collections.Generic.Dictionary`2")) { return("QMap<" + GetPrimitiveString(args[0]) + ", " + GetPrimitiveString(args[1]) + ">"); } } if (SmokeMarshallers.IsSmokeClass(type)) { typeString = SmokeMarshallers.SmokeClassName(type); } List <string> nonQObjectClasses = new List <string> { // paint devices "QCustomRasterPaintDevice", "QGLFramebufferObject", "QGLPixelBuffer", "QImage", "QPicture", "QPixmap", "QPrinter", "QSvgGenerator", // painters "QStylePainter", "Q3Painter", "QPainter", // model items "QListWidgetItem", "QTreeWidgetItem", "QTableWidgetItem", "QStandardItem" }; // pointer types if (type.IsSubclassOf(typeof(QObject)) || nonQObjectClasses.Contains(typeString)) { typeString += "*"; } return(typeString); }
public static string GetPrimitiveString(Type type) { string typeString = type.ToString(); switch (typeString) { case "System.Void": return(""); case "System.Boolean": return("bool"); case "System.Int32": return("int"); case "System.Int64": return("long"); case "System.UInt32": return("uint"); case "System.UInt64": return("ulong"); case "System.Int16": return("short"); case "System.UInt16": return("ushort"); case "System.Byte": return("uchar"); case "System.SByte": return("sbyte"); case "System.String": return("QString"); case "System.Double": return("double"); case "System.Single": return("float"); case "System.Char": return("char"); } if (type.IsGenericType) { Type[] args = type.GetGenericArguments(); if (type.FullName.StartsWith("System.Collections.Generic.List`1")) { return("QList<" + GetPrimitiveString(args[0]) + ">"); } else if (type.FullName.StartsWith("System.Collections.Generic.Dictionary`2")) { return("QMap<" + GetPrimitiveString(args[0]) + ", " + GetPrimitiveString(args[1]) + ">"); } } if (SmokeMarshallers.IsSmokeClass(type)) { typeString = SmokeMarshallers.SmokeClassName(type); } // pointer types if (type.IsSubclassOf(typeof(QObject)) || typeString == "QListWidgetItem" || typeString == "QTreeWidgetItem" || typeString == "QTableWidgetItem" || typeString == "QStandardItem") { typeString += "*"; } return(typeString); }