<commit_before>//
//
// (C) 2006 Novell Inc.
//
// This class implements the functionalities to bind a callback
// function to a signal dynamically.  
// 
//

using GLib;
using System;
using System.Runtime.InteropServices;
using System.Collections;

namespace GLib {
	
	public delegate void DynamicSignalHandler(object o, SignalArgs args);

	delegate void GClosureMarshal (IntPtr closure, ref GLib.Value retval, uint argc, IntPtr argsPtr, 
		IntPtr invocation_hint, IntPtr data);

	public class DynamicSignal {
		
		private static readonly int gvalue_struct_size = Marshal.SizeOf(typeof(GLib.Value));

		class Key {
			object o;
			string signal_name;

			public Key (object o, string name) {
				this.o = o;
				signal_name = name;
			}

			public override bool Equals(object o) {
				if(o is Key) {
					Key k = (Key) o;
					return k.o.Equals(this.o) && signal_name.Equals(k.signal_name);
				}
				return base.Equals(o);
			}

			public override int GetHashCode() {
				return o.GetHashCode() ^ signal_name.GetHashCode();
			}
		}

		class SignalInfo {
			uint handlerId;
			IntPtr closure;
			Delegate registeredHandler;

			public IntPtr Closure {
				get { return closure; }
				set { closure = value; }
			}

			public uint HandlerId {
				get { return handlerId; }
				set { handlerId = value; }			
			}

			public Delegate RegisteredHandler {
				get { return registeredHandler; }
				set { registeredHandler = value; }
			}

			public SignalInfo(uint handlerId, IntPtr closure, Delegate registeredHandler) {
				this.handlerId = handlerId;
				this.closure = closure;
				this.registeredHandler = registeredHandler;
			}
		}

		static Hashtable SignalHandlers = new Hashtable();

		static GClosureMarshal marshalHandler = new GClosureMarshal(OnMarshal);

		public static void Connect(GLib.Object o, string name, DynamicSignalHandler handler) {
			Connect(o, name, false, handler);
		}

		public static void Connect(GLib.Object o, string name, 
			bool after, DynamicSignalHandler handler)
		{
			Delegate newHandler;

			Key k = new Key(o, name);
						
			if(SignalHandlers[k] != null) {
				SignalInfo si = (SignalInfo) SignalHandlers[k];
				newHandler = Delegate.Combine(si.RegisteredHandler, handler);
				si.RegisteredHandler = newHandler;
			}
			else {
				IntPtr closure = g_closure_new_simple(16, IntPtr.Zero);
				g_closure_set_meta_marshal(closure, (IntPtr) GCHandle.Alloc(k),  marshalHandler);<commit_msg>Fix dynamic signals on 64 bit architectures<commit_after>//
//
// (C) 2006 Novell Inc.
//
// This class implements the functionalities to bind a callback
// function to a signal dynamically.  
// 
//

using GLib;
using System;
using System.Runtime.InteropServices;
using System.Collections;

namespace GLib {
	
	public delegate void DynamicSignalHandler(object o, SignalArgs args);

	delegate void GClosureMarshal (IntPtr closure, ref GLib.Value retval, uint argc, IntPtr argsPtr, 
		IntPtr invocation_hint, IntPtr data);

	public class DynamicSignal {
		
		private static readonly int gvalue_struct_size = Marshal.SizeOf(typeof(GLib.Value));

		class Key {
			object o;
			string signal_name;

			public Key (object o, string name) {
				this.o = o;
				signal_name = name;
			}

			public override bool Equals(object o) {
				if(o is Key) {
					Key k = (Key) o;
					return k.o.Equals(this.o) && signal_name.Equals(k.signal_name);
				}
				return base.Equals(o);
			}

			public override int GetHashCode() {
				return o.GetHashCode() ^ signal_name.GetHashCode();
			}
		}

		class SignalInfo {
			uint handlerId;
			IntPtr closure;
			Delegate registeredHandler;

			public IntPtr Closure {
				get { return closure; }
				set { closure = value; }
			}

			public uint HandlerId {
				get { return handlerId; }
				set { handlerId = value; }			
			}

			public Delegate RegisteredHandler {
				get { return registeredHandler; }
				set { registeredHandler = value; }
			}

			public SignalInfo(uint handlerId, IntPtr closure, Delegate registeredHandler) {
				this.handlerId = handlerId;
				this.closure = closure;
				this.registeredHandler = registeredHandler;
			}
		}

		static Hashtable SignalHandlers = new Hashtable();

		static GClosureMarshal marshalHandler = new GClosureMarshal(OnMarshal);

		public static void Connect(GLib.Object o, string name, DynamicSignalHandler handler) {
			Connect(o, name, false, handler);
		}

		public static void Connect(GLib.Object o, string name, 
			bool after, DynamicSignalHandler handler)
		{
			Delegate newHandler;

			Key k = new Key(o, name);
						
			if(SignalHandlers[k] != null) {
				SignalInfo si = (SignalInfo) SignalHandlers[k];
				newHandler = Delegate.Combine(si.RegisteredHandler, handler);
				si.RegisteredHandler = newHandler;
			}
			else {
				/* We use 32 as this is correct for 32 bit and 64 bit architectures */
				IntPtr closure = g_closure_new_simple(32, IntPtr.Zero);
				g_closure_set_meta_marshal(closure, (IntPtr) GCHandle.Alloc(k),  marshalHandler);
{'fraction_non_alphanumeric': 0.0930888575458392, 'fraction_numerical': 0.004029820672980053, 'mean_word_length': 3.373568281938326, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6865658', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1486, 'n_words': 439}