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

List:       kde-bindings
Subject:    Re: [Kde-bindings] Qyoto: SIGNALS/SLOTS
From:       Adam Treat <treat () kde ! org>
Date:       2005-12-14 19:22:00
Message-ID: 200512141422.01400.treat () kde ! org
[Download RAW message or body]

On Wednesday December 14 2005 1:43 pm, you wrote:
> Hi,
>
> I found the following in you code:
> IntPtr s = (this.Instance as QtSharp).RawObject;
>
> does 'RawObject' point to the underlying C++ instance?
> If it does, a second question: How do I get the pointer to the
> C++-instance? It tried it with 'GCHandle.Alloc' but that doesn't seem to
> work.

Yes, it does.  You can find the code for RawObject and the handling of C++ 
pointer interaction in the attached file.

Here is the location for all the behind-the-scenes mechanisms for the 
unreleased version of Qt#.  This is the latest code that I worked on and it 
did work up to the point you are looking to get, but I think you are on the 
right track with the smoke bindings.

http://cvs.sourceforge.net/viewcvs.py/qtcsharp/qtsharp/src/newbindings/static/

As soon as you get beyond this... ie, get virtuals working and the example 
applications, i'll point everything towards Qyoto as the way forward.

Adam


["QtSharp.cs" (text/x-c++src)]

namespace Qt {

	using System;	        
	using System.Reflection;
	using System.Collections;
	using System.Runtime.InteropServices;

	public interface IQPaintDevice
	{
	}

	[StructLayout(LayoutKind.Sequential)]
	public class QtSharp: ICustomMarshaler
	{
		internal static Hashtable typedDelegateCache = new Hashtable ();	
		private bool isAllocated, isRegistered = false;
		private GCHandle gch;
		public IntPtr rawObject;
                
		internal VirtualCollection overrides;
		
		internal delegate void freeCallback ( IntPtr ptr );
		internal delegate IntPtr factoryCallback ( IntPtr ptr, string nsname, string \
basename, string derived );  internal static freeCallback free_ = new freeCallback \
(QtSharp.FreeManagedObject);  internal static factoryCallback callback = new \
factoryCallback (QtSharp.SharpFactory);  
		internal QtSharp () {}

		internal QtSharp (QtNull dummy) 
		{
			if (!isRegistered)
			{
				registerFreeCallback (free_);
				registerFactoryCallback (callback);
				isRegistered = true;
			}
		}
                
		internal QtSharp (IntPtr ptr)
		{
			RawObject = ptr;
		}

		public IntPtr RawObject
		{
			get { return rawObject; }
			set { rawObject = value; }
		}

		public GCHandle GCHandle
		{
			get
			{
				if (!this.isAllocated)
				{
					this.gch = System.Runtime.InteropServices.GCHandle.Alloc (this);
					this.isAllocated = true;
				}
				return this.gch;
			}
		}

		public static explicit operator IntPtr (QtSharp obj)
		{
			if (obj != null || (obj as QtSharp).RawObject == IntPtr.Zero)
			{
				if ((obj as QtSharp).RawObject == IntPtr.Zero)
				{
// 					Console.WriteLine(
// 					"HELP!!!: operator QtSharp <--> RawObject is IntPtr.Zero");
				}
				return obj.RawObject;
			}
			else
			{
// 				Console.WriteLine("Help: operator QtSharp <--> IntPtr obj is null");
				return IntPtr.Zero;
			}
		}
		
		public static QtSharp GetManagedObject 
			(IntPtr ptr, string nsname, string basetype, bool polymorphic)
		{	
			//Retrieve or wrap the object
			ptr = getManagedObject (ptr, nsname, basetype, polymorphic);

			GCHandle obj = (GCHandle) ptr;
			return obj.Target as QtSharp;
		}
		
		public static explicit operator QtSharp (IntPtr ptr)
		{	
			GCHandle obj = (GCHandle) ptr;
			return obj.Target as QtSharp;
		}

		public IntPtr MarshalManagedToNative (object obj)
		{
			//Converts the managed data to unmanaged data.
			if (obj != null)
			{
				if ((obj as QtSharp).RawObject == IntPtr.Zero)
				{
// 					Console.WriteLine(
// 					"HELP!!!: ManagedToNative QtSharp <--> RawObject is IntPtr.Zero");
				}
				return (obj as QtSharp).RawObject;
			}
			else
			{
// 				Console.WriteLine("Help: ManagedToNative QtSharp <--> IntPtr obj is null");
				return IntPtr.Zero;
			}
		}

		public object MarshalNativeToManaged (IntPtr ptr)
		{
			//Converts the unmanaged data to managed data.
			//Console.WriteLine("ManagedObject: " + (IntPtr)obj + " " + ptr);
			GCHandle obj = (GCHandle) ptr;
			return obj.Target as QtSharp;
		}
                
		public void CleanUpManagedData (object obj)
		{
			//Performs necessary cleanup of the managed data when it is no longer needed.
		}

		public void CleanUpNativeData (IntPtr ptr)
		{
			//Performs necessary cleanup of the unmanaged data when it is no longer needed.
		}

		public int GetNativeDataSize ()
		{
			//Returns the size of the native data to be marshaled.
			return -1;
		}

		private static readonly QtSharp sharp = new QtSharp(QtNull.Instance);
		public static ICustomMarshaler GetInstance (string s)
		{
			return sharp;
		}

		internal virtual IntPtr GetDelegateList ()
		{
			return IntPtr.Zero;
		}

		internal static void FreeManagedObject ( IntPtr ptr )
		{
			//Console.WriteLine ("FreeManagedOject: '{0}'", ((GCHandle)ptr).Target.GetType());
			((GCHandle)ptr).Free();
		}
		
		internal static IntPtr SharpFactory 
			( IntPtr ptr, string nsname, string basename, string derived )
		{
			Type type;
			QtSharp obj;
			
			if (derived != String.Empty)
				type = Type.GetType ( nsname+"."+derived );
			else
				type = Type.GetType ( nsname+"."+basename );
			
			if (type == null)
				type = typeof (QtSharp);
				
			try
			{
				obj = (QtSharp)Activator.CreateInstance (
						type,
						BindingFlags.NonPublic | BindingFlags.Instance,
						null,
						new object[] {QtNull.Instance},
						null);
			}
			catch(Exception e)
			{
				Console.Error.WriteLine 
					("Error wrapping unwrapped native object '{0}'!", basename);
				
				throw e;
			}

			obj.RawObject = ptr;
			return (IntPtr)obj.GCHandle;
		}
		
		[DllImport ("libnewqtsharp.so", EntryPoint="registerFreeCallback", \
                CharSet=CharSet.Ansi)]
                internal static extern void registerFreeCallback (Delegate \
freeCallback);  
		[DllImport ("libnewqtsharp.so", EntryPoint="registerFactoryCallback", \
                CharSet=CharSet.Ansi)]
                internal static extern void registerFactoryCallback (Delegate \
callback);  
		[DllImport ("libnewqtsharp.so", EntryPoint="getManagedObject", \
CharSet=CharSet.Ansi)]  internal static extern IntPtr getManagedObject (IntPtr ptr, \
string nsname, string basename, bool polymorphic);  }
}



_______________________________________________
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