2002-08-29 21:45:07 +00:00
|
|
|
// DelegateWrapper.cs - Delegate wrapper implementation
|
|
|
|
//
|
2003-04-06 09:21:15 +00:00
|
|
|
// Authors:
|
|
|
|
// Rachel Hestilow <hestilow@ximian.com>
|
|
|
|
// Gonzalo Panigua Javier <gonzalo@ximian.com>
|
2002-08-29 21:45:07 +00:00
|
|
|
//
|
|
|
|
// (c) 2002 Rachel Hestilow
|
2003-04-06 09:21:15 +00:00
|
|
|
// (c) 2003 Ximian, Inc. (http://www.ximian.com)
|
2002-08-29 21:45:07 +00:00
|
|
|
|
|
|
|
namespace GLib {
|
|
|
|
|
|
|
|
using System;
|
|
|
|
using System.Collections;
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// DelegateWrapper Class
|
|
|
|
/// </summary>
|
|
|
|
///
|
|
|
|
/// <remarks>
|
|
|
|
/// Wrapper class for delegates.
|
|
|
|
/// </remarks>
|
|
|
|
|
2003-04-06 09:21:15 +00:00
|
|
|
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.
|
2002-08-29 21:45:07 +00:00
|
|
|
|
2003-04-06 09:21:15 +00:00
|
|
|
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);
|
2002-08-29 21:45:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-04-06 09:21:15 +00:00
|
|
|
|