[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-bindings
Subject:    [Kde-bindings] Qyoto: dynamically create a QMetaObject
From:       "Paolo Capriotti" <p.capriotti () gmail ! com>
Date:       2006-05-26 20:55:41
Message-ID: 87b37d60605261355t115c91b1v5d58edc37266fdb0 () mail ! gmail ! com
[Download RAW message or body]

I've translated from ruby to C# the code to create a QMetaObject.
Attached is a patch for quoto/Qyoto.cs.

Paolo

["meta.diff" (text/x-patch)]

Index: qyoto/Qyoto.cs
===================================================================
--- qyoto/Qyoto.cs	(revision 545029)
+++ qyoto/Qyoto.cs	(working copy)
@@ -3,6 +3,8 @@
 	using System;
 	using System.Collections;
 	using System.Reflection;
+  using System.Text;
+  using System.Text.RegularExpressions;
 	using System.Runtime.InteropServices;
 	
 	public class Qyoto : System.Object
@@ -67,6 +69,111 @@
 			MethodInfo mi = t.GetMethod("Emit", BindingFlags.Instance | BindingFlags.NonPublic);
 			return mi.ReturnType;
 		}
+    
+    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;
+        int offset;
+        ArrayList data;
+        
+        public StringTableHandler() {
+          hash = new Hashtable();
+          offset = 0;
+          data = new ArrayList();
+        }
+
+        public int this[string str] {
+          get {
+            if (!hash.ContainsKey(str)) {
+              hash[str] = offset;
+              data.Add(str);
+              offset += str.Length + 1;
+            }
+            return (int)hash[str];
+          }
+        }
+        
+        public byte[] GetStringData() {
+          ArrayList result = new ArrayList();
+          foreach(string x in data) {
+            result.AddRange(Encoding.ASCII.GetBytes(x));
+            result.Add(0);
+          }
+          byte[] res = new byte[result.Count];
+          result.CopyTo(res);
+          return res;
+        }
+      }
+    
+      byte[] stringdata;
+      int[] data;
+      StringTableHandler handler;
+      
+      // from qt-copy/src/tools/moc/generator.cpp
+      enum MethodFlags {
+        AccessPrivate = 0x00,
+        AccessProtected = 0x01,
+        AccessPublic = 0x02,
+        MethodMethod = 0x00,
+        MethodSignal = 0x04,
+        MethodSlot = 0x08,
+        MethodCompatibility = 0x10,
+        MethodCloned = 0x20,
+        MethodScriptable = 0x40
+      }
+      
+      void AddMethod(ArrayList array, string method, MethodFlags flags) {
+        array.Add(handler[method]);                            // signature
+        array.Add(handler[Regex.Replace(method, "[^,]", "")]); // parameters
+        array.Add(handler[""]);                                // type
+        array.Add(handler[""]);                                // tag
+        array.Add(flags);                                      // flags
+      }
+      
+      public QyotoMetaData(string className, ICollection signals, ICollection slots) {
+        handler = new StringTableHandler();
+        ArrayList tmp = new ArrayList(new int[] { 
+            1,                                  // revision
+            handler[className],                 // classname
+            0, 0,                               // classinfo
+            signals.Count + slots.Count, 10,  // methods
+            0, 0,                               // properties
+            0, 0
+        });
+        
+        foreach (string entry in signals)
+          AddMethod(tmp, entry, MethodFlags.MethodSignal | MethodFlags.AccessProtected);
+        
+        foreach (string entry in signals)
+          AddMethod(tmp, entry, MethodFlags.MethodSlot | MethodFlags.AccessPublic);
+          
+        tmp.Add(0);
+        
+        stringdata = handler.GetStringData();
+        data = new int[tmp.Count];
+        tmp.CopyTo(data);
+      }
+      
+      public byte[] StringData {
+        get { return stringdata; }
+      }
+      
+      public int[] Data {
+        get { return data; }
+      }
+    }
+    
+    public static QMetaObject MakeMetaObject(QObject o) {
+      string className = o.GetType().ToString();
+      ICollection slots = ((Hashtable)classes[className]).Values;
+      string[] signals = GetSignalSignatures(o);
+      QyotoMetaData metaData = new QyotoMetaData(className, signals, slots);
+      QMetaObject meta = null; //= make_metaObject(o, metaData.StringData, metaData.Data);
+      return meta;
+    }
 	}
 	
 	[AttributeUsage( AttributeTargets.Class )]
@@ -108,7 +215,7 @@
 	}
 
 	[AttributeUsage( AttributeTargets.Method )]
-	class Q_SIGNAL : Attribute
+	public class Q_SIGNAL : Attribute
 	{
 		public string signature;
 	
@@ -127,7 +234,7 @@
 	}
 
 	[AttributeUsage( AttributeTargets.Method )]
-	class Q_SLOT : Attribute
+	public class Q_SLOT : Attribute
 	{
 		public string signature;
 	


_______________________________________________
Kde-bindings mailing list
Kde-bindings@kde.org
https://mail.kde.org/mailman/listinfo/kde-bindings


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic