[Kde-bindings] playground/bindings/kimono

Paolo Capriotti paolo.capriotti at gmail.com
Sun Jan 14 17:44:47 UTC 2007


SVN commit 623390 by capriotti:

Ported most of the .NET 1.1 code to .NET 2.0

CCMAIL: kde-bindings at kde.org


 M  +4 -0      ChangeLog  
 M  +64 -79    Qyoto.cs  
 M  +25 -21    SmokeInvocation.cs  


--- trunk/playground/bindings/kimono/ChangeLog #623389:623390
@@ -1,3 +1,7 @@
+2007-14-01  Paolo Capriotti  <paolo.capriotti at gmail.com>
+
+	* Ported most of the .NET 1.1 code to .NET 2.0
+	
 2007-13-01  Richard Dale  <rdale at foton.es>
 
 	* Added a generically typed QVariant.Value() method
--- trunk/playground/bindings/kimono/Qyoto.cs #623389:623390
@@ -4,6 +4,7 @@
 {
 	using System;
 	using System.Collections;
+	using System.Collections.Generic;
 	using System.Reflection;
 	using System.Text;
 	using System.Text.RegularExpressions;
@@ -29,10 +30,11 @@
 		}
 		
 		/// This Hashtable contains a list of classes with their Hashtables for slots. The class name is the key, the slot-hashtable the value.
-		public static Hashtable classes = new Hashtable();
+		public static Dictionary<string, Dictionary<string, CPPMethod>> classes = 
+			new Dictionary<string, Dictionary<string, CPPMethod>>();
     
 		/// This hashtable has classe names as keys, and QMetaObjects as values
-		static Hashtable metaObjects = new Hashtable();
+		static Dictionary<string, QMetaObject> metaObjects = new Dictionary<string, QMetaObject> ();
 		
 		public static int GetCPPEnumValue(string c, string value) {
 			Type t = Type.GetType("Qyoto." + c, false);
@@ -155,12 +157,12 @@
 				type = return_type;
 		}
 		
-		public static Hashtable GetSlotSignatures(Type t) {
+		public static Dictionary<string, CPPMethod> GetSlotSignatures(Type t) {
 			/// Remove the old object if it already exists
 			classes.Remove(t.ToString());
 			
 			/// This Hashtable contains the slots of a class. The C++ type signature is the key, the appropriate array with the MethodInfo, signature and return type the value.
-			Hashtable slots = new Hashtable();
+			Dictionary<string, CPPMethod> slots = new Dictionary<string, CPPMethod>();
 			
 			MethodInfo[] mis = t.GetMethods(	BindingFlags.Instance 
 												| BindingFlags.Static 
@@ -195,8 +197,8 @@
 		
 		
 		/// returns a Hastable with the MethodInfo as a key and the array with the MethodInfo, signature and return type the value.
-		public static Hashtable GetSignalSignatures(Type t) {
-			Hashtable signals = new Hashtable();
+		public static Dictionary<MethodInfo, CPPMethod> GetSignalSignatures(Type t) {
+			Dictionary<MethodInfo, CPPMethod> signals = new Dictionary<MethodInfo, CPPMethod>();
 			if (IsSmokeClass(t)) {
 				return signals;
 			}
@@ -241,8 +243,8 @@
 			return mi.ReturnType;
 		}
 		
-		public static Hashtable GetClassInfos(Type t) {
-			Hashtable classinfos = new Hashtable();
+		public static Dictionary<string, string> GetClassInfos(Type t) {
+			Dictionary<string, string> classinfos = new Dictionary<string, string>();
 			object[] attributes = t.GetCustomAttributes(typeof(Q_CLASSINFO), false);
 			foreach (Q_CLASSINFO attr in attributes) {
 				classinfos.Add(attr.Name, attr.Value);
@@ -251,49 +253,10 @@
 		}
 		
 		class QyotoMetaData {
-			// Keeps a hash of strings against their corresponding offsets
-			// within the qt_meta_stringdata sequence of null terminated
-			// strings.
-			class StringTableHandler {
-				Hashtable hash;
-				uint offset;
-				ArrayList data;
-			
-				public StringTableHandler() {
-					hash = new Hashtable();
-					offset = 0;
-					data = new ArrayList();
-				}
-	
-				public uint this[string str] {
-					get {
-						if (!hash.ContainsKey(str)) {
-#if DEBUG
-							Console.WriteLine("adding {0} in hash at offset {1}", str, offset);
-#endif
-							hash[str] = offset;
-							data.Add(str);
-							offset += (uint)str.Length + 1;
-						}
-						return (uint)hash[str];
-					}
-				}
-			
-				public byte[] GetStringData() {
-					ArrayList result = new ArrayList();
-					foreach(string x in data) {
-						result.AddRange(Encoding.ASCII.GetBytes(x));
-						result.Add((byte)0);
-					}
-					byte[] res = new byte[result.Count];
-					result.CopyTo(res);
-					return res;
-				}
-			}
-		
 			byte[] stringdata;
 			uint[] data;
-			StringTableHandler handler;
+			private delegate uint Handler(string str);
+			Handler handler;
 		
 			// from qt-copy/src/tools/moc/generator.cpp
 			enum MethodFlags {
@@ -308,24 +271,51 @@
 				MethodScriptable = 0x40
 			}
 			
-			void AddClassInfo(ArrayList array, string key, string value) {
-				array.Add(handler[key]);
-				array.Add(handler[value]);
+			void AddClassInfo(List<uint> array, string key, string value) {
+				array.Add(handler(key));
+				array.Add(handler(value));
 			}
 			
-			void AddMethod(ArrayList array, string method, string type, MethodFlags flags) {
-				array.Add(handler[method]);                            // signature
-				array.Add(handler[Regex.Replace(method, "[^,]", "")]); // parameters
-				array.Add(handler[type]);				// type
-				array.Add(handler[""]);                                // tag
+			void AddMethod(List<uint> array, string method, string type, MethodFlags flags) {
+				array.Add(handler(method));                            // signature
+				array.Add(handler(Regex.Replace(method, "[^,]", ""))); // parameters
+				array.Add(handler(type));				// type
+				array.Add(handler(""));                                // tag
 				array.Add((uint)flags);                                 // flags
 			}
+			
+			byte[] ComputeStringData(List<string> array) {
+				List<byte> result = new List<byte>();
+				foreach(string x in array) {
+					result.AddRange(Encoding.ASCII.GetBytes(x));
+					result.Add(0);
+				}
+				return result.ToArray();
+			}
 		
-			public QyotoMetaData(string className, ICollection signals, ICollection slots, Hashtable classinfos) {
-				handler = new StringTableHandler();
-				ArrayList tmp = new ArrayList(new uint[] {
+			public QyotoMetaData(string className, ICollection<CPPMethod> signals, 
+								ICollection<CPPMethod> slots, Dictionary<string, string> classinfos) {
+				Dictionary<string, uint> hash = new Dictionary<string, uint>();
+				uint offset = 0;
+				List<string> stringdata_tmp = new List<string>();
+
+				handler = delegate(string str) {
+						uint res;
+						if (!hash.TryGetValue(str, out res)) {
+#if DEBUG
+							Console.WriteLine("adding {0} in hash at offset {1}", str, offset);
+#endif
+							hash.Add(str, offset);
+							stringdata_tmp.Add(str);
+							res = offset;
+							offset += (uint)str.Length + 1;
+						}
+						return res;
+				};
+				
+				List<uint> tmp = new List<uint>(new uint[] {
 					1,                                  // revision
-					handler[className],                 // classname
+					handler(className),                 // classname
 					(uint)classinfos.Count, classinfos.Count > 0 ? (uint)10 : (uint)0,  // classinfo
 					(uint)(signals.Count + slots.Count),
 					(uint)(10 + (2 * classinfos.Count)),        // methods
@@ -333,8 +323,8 @@
 					0, 0
 				});
 				
-				foreach (string key in classinfos.Keys)
-					AddClassInfo(tmp, key, (string) classinfos[key]);
+				foreach (KeyValuePair<string, string> p in classinfos)
+					AddClassInfo(tmp, p.Key, p.Value);
 				
 				foreach (CPPMethod entry in signals) {
 					MethodFlags flags = MethodFlags.MethodSignal | MethodFlags.AccessProtected;
@@ -360,15 +350,10 @@
 						MethodFlags.MethodSlot | MethodFlags.AccessPublic);
 				}
 				
-				tmp.Add((uint)0);
+				tmp.Add(0);
 				
-				stringdata = handler.GetStringData();
-				data = new uint[tmp.Count];
-				tmp.CopyTo(data);
-/*				for (int i = 0; i < tmp.Count; i++) {
-					Console.WriteLine("copying {0} => {1}", i, tmp[i]);
-					data[i] = (uint)tmp[i];
-				}*/
+				stringdata = ComputeStringData(stringdata_tmp);
+				data = tmp.ToArray();
 			}
 		
 			public byte[] StringData {
@@ -384,17 +369,17 @@
 			if (t == null) return null;
 		
 			string className = t.ToString();
-			Hashtable slotTable = (Hashtable)classes[className];
-		
-			ICollection slots;
-			if (slotTable != null)
+			Dictionary<string, CPPMethod> slotTable;
+			ICollection<CPPMethod> slots;
+			
+			if (classes.TryGetValue(className, out slotTable))
 				slots = slotTable.Values;
 			else {
 				// build slot table
 				slots = GetSlotSignatures(t).Values;
 			}
 
-			Hashtable signals = GetSignalSignatures(t);
+			Dictionary<MethodInfo, CPPMethod> signals = GetSignalSignatures(t);
 			QyotoMetaData metaData = new QyotoMetaData(className, signals.Values, slots, GetClassInfos(t));
 			
 			GCHandle objHandle = GCHandle.Alloc(o);
@@ -419,11 +404,11 @@
 			Console.WriteLine("ENTER GetMetaObject t => {0}", t);
 #endif
 			
-			QMetaObject res = (QMetaObject)metaObjects[t.ToString()];
-			if (res == null) {
+			QMetaObject res;
+			if (!metaObjects.TryGetValue(t.ToString(), out res)) {
 				// create QMetaObject
 				res = MakeMetaObject(t, o);
-				metaObjects[t.ToString()] = res;
+				metaObjects.Add(t.ToString(), res);
 			}
 	
 #if DEBUG
--- trunk/playground/bindings/kimono/SmokeInvocation.cs #623389:623390
@@ -23,6 +23,7 @@
 	
 	using System;
 	using System.Collections;
+	using System.Collections.Generic;
 	using System.Text;
 	using System.Text.RegularExpressions;
 	using System.Reflection;
@@ -243,7 +244,7 @@
 
 		// The key is an IntPtr corresponding to the address of the C++ instance,
 		// and the value is a WeakReference to the C# instance.
-		static private Hashtable pointerMap = new Hashtable();
+		static private Dictionary<IntPtr, WeakReference> pointerMap = new Dictionary<IntPtr, WeakReference>();
 
 		static void MapPointer(IntPtr ptr, IntPtr instancePtr) {
 			Object instance = ((GCHandle) instancePtr).Target;
@@ -257,12 +258,12 @@
 		
 		static IntPtr GetPointerObject(IntPtr ptr) {
 //			Console.WriteLine("ENTER GetPointerObject() ptr: {0}", ptr);
-			if (pointerMap[ptr] == null) {
+			WeakReference weakRef;
+			if (!pointerMap.TryGetValue(ptr, out weakRef)) {
 //				Console.WriteLine("GetPointerObject() pointerMap[ptr] == null");
 				return (IntPtr) 0;
 			}
 
-			WeakReference weakRef = (WeakReference) pointerMap[ptr];
 			if (weakRef == null) {
 //				Console.WriteLine("GetPointerObject() weakRef zero");
 				return (IntPtr) 0;
@@ -420,7 +421,8 @@
 		// virtual methods, and the value is a Hashtable of the smoke type
 		// signatures as keys retrieving a suitable MethodInfo to invoke via 
 		// reflection.
-		static private Hashtable overridenMethods = new Hashtable();
+		static private Dictionary<string, Dictionary<string, MemberInfo>>
+			overridenMethods = new Dictionary<string, Dictionary<string, MemberInfo>>();
 		static private MethodInfo metacallMethod = typeof(Qyoto).GetMethod("QyotoMetaCall", BindingFlags.Static | BindingFlags.NonPublic);
 		static private MethodInfo metaObjectMethod = typeof(QObject).GetMethod("MetaObject", BindingFlags.Instance | BindingFlags.Public);
 		
@@ -431,13 +433,11 @@
 			}
 
 			string instanceType = klass.ToString();
-			Hashtable methodsHash = (Hashtable) overridenMethods[instanceType];
-			if (methodsHash != null) {
+			if (overridenMethods.ContainsKey(instanceType))
 				return;
-			}
 
-			methodsHash = new Hashtable();
-			overridenMethods[instanceType] = methodsHash;
+			Dictionary<string, MemberInfo> methodsHash = new Dictionary<string, MemberInfo>();
+			overridenMethods.Add(instanceType, methodsHash);
 			
 			do {
 				MemberInfo[] methods = klass.FindMembers(	MemberTypes.Method,
@@ -472,8 +472,8 @@
 						parent = parent.BaseType;
 					}
 	
-					if (signature != null && methodsHash[signature] == null) {
-						methodsHash[signature] = method;
+					if (signature != null && !methodsHash.ContainsKey(signature)) {
+						methodsHash.Add(signature, method);
 					}
 				}
 
@@ -489,13 +489,13 @@
 			if (method == "metaObject() const")
 				return (IntPtr)GCHandle.Alloc(metaObjectMethod);
 
-			Hashtable methods = (Hashtable) overridenMethods[instanceType];
-			if (methods == null) {
+			Dictionary<string, MemberInfo> methods;
+			if (!overridenMethods.TryGetValue(instanceType, out methods)) {
 				return (IntPtr) 0;
 			}
 
-			MethodInfo methodInfo = (MethodInfo) methods[method];
-			if (methodInfo == null) {
+			MemberInfo methodInfo;
+			if (!methods.TryGetValue(method, out methodInfo)) {
 				return (IntPtr) 0;
 			}
 
@@ -590,14 +590,18 @@
 #if DEBUG
 			Console.WriteLine("handling slot {0} for class {1}", slotname, qobj.GetType());
 #endif
-			Hashtable slotTable = (Hashtable)Qyoto.classes[className];
-			if (slotTable == null) {
+			Dictionary<string, Qyoto.CPPMethod> slotTable;
+			if (!Qyoto.classes.TryGetValue(className, out slotTable)) {
 				slotTable = Qyoto.GetSlotSignatures(qobj.GetType());
 			}
-			MethodInfo slot = ((Qyoto.CPPMethod)slotTable[slotname]).mi;
-			if (slot == null) {
+			MethodInfo slot;
+			try {
+				slot = (slotTable[slotname]).mi;
+			}
+			catch (KeyNotFoundException) {
 				// should not happen
 				Console.WriteLine("** Could not retrieve slot {0}::{1} info **", className, slotname);
+				return;
 			}
 		
 			ParameterInfo[] parameters = slot.GetParameters();
@@ -992,14 +996,14 @@
 			IMethodReturnMessage returnMessage = (IMethodReturnMessage) message;
 			GCHandle instanceHandle = GCHandle.Alloc(_instance);
 			string signature = "";
-			Hashtable signals = Qyoto.GetSignalSignatures(_instance.GetType());
+			Dictionary<MethodInfo, Qyoto.CPPMethod> signals = Qyoto.GetSignalSignatures(_instance.GetType());
 			
 			/// should not happen
 			if (signals == null) {
 				Console.WriteLine("** FATAL: error while retirieving signal signatures **");
 				return null;
 			}
-			signature = ((Qyoto.CPPMethod) signals[(MethodInfo) callMessage.MethodBase]).signature;
+			signature = signals[(MethodInfo) callMessage.MethodBase].signature;
 #if DEBUG
 			Console.WriteLine( "Q_SIGNAL signature: {0}", signature );
 #endif



More information about the Kde-bindings mailing list