GtkSharp/glib/DelegateWrapper.cs
Gonzalo Paniagua Javier 3821938764 2003-04-03 Gonzalo Paniagua Javier <gonzalo@ximian.com>
* generator/CallbackGen.cs: the new generated wrappers have:
		-(optional) Field of the same type returned by the callback.
		-A call to RemoveIfNotAlive at the beginning. It returns true,
		return the dummy field.
		-Added an object to the ctor signature and pass it to the base
		class.

	* generator/Ctor.cs: added a Params property.

	* generator/Method.cs: set Static property in Parameters if the method
	is static.

	* generator/Parameters.cs: added Static property. The call creation of
	the delegate wrapper (if applicable) uses the new signature. Pass a null
	as object is the method is static.

	* generator/StructBase.cs: set Static for the parameters of the ctors.

	* glib/DelegateWrapper.cs: the ctor takes an object (the one creating
	the wrapper or null) and creates a weak reference to it. Store it in
	a static Hashtable (this way the wrapper itself is not garbage
	collected).
	(RemoveIfNotAlive): called from the native delegate callbacks. If the
	target of the weak reference has been garbage collected, removes itself
	from the hashtable to let the GC dispose this instance and returns true.

	* gdk/Pixbuf.custom:
	* gtk/Clipboard.custom:
	* gtk/GtkSharp.GtkClipboardClearFuncNative.cs:
	* gtk/GtkSharp.GtkClipboardGetFuncNative.cs:
	* glade/XML.custom: changed delegate wrappers to match the new
	signature.

svn path=/trunk/gtk-sharp/; revision=13237
2003-04-06 09:21:15 +00:00

59 lines
1.7 KiB
C#

// DelegateWrapper.cs - Delegate wrapper implementation
//
// Authors:
// Rachel Hestilow <hestilow@ximian.com>
// Gonzalo Panigua Javier <gonzalo@ximian.com>
//
// (c) 2002 Rachel Hestilow
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
namespace GLib {
using System;
using System.Collections;
/// <summary>
/// DelegateWrapper Class
/// </summary>
///
/// <remarks>
/// Wrapper class for delegates.
/// </remarks>
public class DelegateWrapper
{
// Keys in the hashtable are instances of classes derived from this one.
// Values are WeakReference instances to the object that creates the
// delegate or instances of derived classes (if created from static methods).
static Hashtable weakReferences = new Hashtable ();
// The object 'o' is the object that creates the instance of the DelegateWrapper
// derived class or null if created from a static method.
// Note that the instances will never be disposed if they are created in a static
// method.
protected DelegateWrapper (object o)
{
if (o == null)
o = this; // Never expires. Used in static methods.
weakReferences [this] = new WeakReference (o);
}
// IMPORTANT: this method must be the first one called from the callback methods that
// are invoked from unmanaged code.
// If this method returns true, the object that created the delegate wrapper no longer
// exists and the instance of the delegate itself is removed from the hash table.
protected bool RemoveIfNotAlive ()
{
WeakReference r = null;
r = weakReferences [this] as WeakReference;
if (r != null && !r.IsAlive) {
weakReferences.Remove (this);
r = null;
}
return (r == null);
}
}
}