summaryrefslogtreecommitdiff
path: root/gstreamer-sharp/glib-sharp/SignalCallback.cs
blob: 933a6296e761099e41443bb210470d77405ce43b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// GLib.SignalCallback.cs - Signal callback base class implementation
//
// Authors: Mike Kestner <mkestner@ximian.com>
//
// Copyright (c) 2001 Mike Kestner
// Copyright (c) 2004 Novell, Inc.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of version 2 of the Lesser GNU General 
// Public License as published by the Free Software Foundation.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the
// Free Software Foundation, Inc., 59 Temple Place - Suite 330,
// Boston, MA 02111-1307, USA.


namespace Gst.GLib {
	using System;
	using System.Collections;
	using System.Runtime.InteropServices;

	[Obsolete ("Replaced by GLib.Signal.")]
	public abstract class SignalCallback : IDisposable {

		// A counter used to produce unique keys for instances.
		protected static int _NextKey = 0;

		// Hashtable containing refs to all current instances.
		protected static Hashtable _Instances = new Hashtable ();

		// protected instance members
		protected GLib.Object _obj;
		protected Delegate _handler;
		protected int _key;
		protected System.Type _argstype;
		protected uint _HandlerID;

		protected SignalCallback (GLib.Object obj, Delegate eh, System.Type argstype)
		{
			_key = _NextKey++;
			_obj = obj;
			_handler = eh;
			_argstype = argstype;
			_Instances [_key] = this;
		}

		public void AddDelegate (Delegate d)
		{
			_handler = Delegate.Combine (_handler, d);
		}

		public void RemoveDelegate (Delegate d)
		{
			_handler = Delegate.Remove (_handler, d);
		}

		[DllImport("libgobject-2.0-0.dll")]
		static extern uint g_signal_connect_data(IntPtr obj, IntPtr name, Delegate cb, int key, IntPtr p, int flags);

		protected void Connect (string name, Delegate cb, int flags)
		{
			IntPtr native_name = Marshaller.StringToPtrGStrdup (name);
			_HandlerID = g_signal_connect_data(_obj.Handle, native_name, cb, _key, new IntPtr(0), flags);
			Marshaller.Free (native_name);
		}

		[DllImport("libgobject-2.0-0.dll")]
		static extern void g_signal_handler_disconnect (IntPtr instance, uint handler);

		[DllImport("libgobject-2.0-0.dll")]
		static extern bool g_signal_handler_is_connected (IntPtr instance, uint handler);

		protected void Disconnect ()
		{
			if (g_signal_handler_is_connected (_obj.Handle, _HandlerID))
				g_signal_handler_disconnect (_obj.Handle, _HandlerID);
		}

		public void Dispose ()
		{
			Dispose (true);
			GC.SuppressFinalize (this);
		}

		protected virtual void Dispose (bool disposing)
		{
			if (disposing) {
				_obj = null;
				_handler = null;
				_argstype = null;
			}
		}

		~SignalCallback ()
		{
			Dispose (false);
		}
	}
}