// This file was generated by the Gtk# code generator.
// Any changes made will be lost if regenerated.

namespace GLibSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	internal delegate void Win32RegistryKeyWatchCallbackFuncNative(IntPtr key, IntPtr user_data);

	internal class Win32RegistryKeyWatchCallbackFuncInvoker {

		Win32RegistryKeyWatchCallbackFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

		~Win32RegistryKeyWatchCallbackFuncInvoker ()
		{
			if (__notify == null)
				return;
			__notify (__data);
		}

		internal Win32RegistryKeyWatchCallbackFuncInvoker (Win32RegistryKeyWatchCallbackFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal Win32RegistryKeyWatchCallbackFuncInvoker (Win32RegistryKeyWatchCallbackFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

		internal Win32RegistryKeyWatchCallbackFuncInvoker (Win32RegistryKeyWatchCallbackFuncNative native_cb, IntPtr data, GLib.DestroyNotify notify)
		{
			this.native_cb = native_cb;
			__data = data;
			__notify = notify;
		}

		internal GLib.Win32RegistryKeyWatchCallbackFunc Handler {
			get {
				return new GLib.Win32RegistryKeyWatchCallbackFunc(InvokeNative);
			}
		}

		void InvokeNative (GLib.Win32RegistryKey key)
		{
			native_cb (key == null ? IntPtr.Zero : key.Handle, __data);
		}
	}

	internal class Win32RegistryKeyWatchCallbackFuncWrapper {

		public void NativeCallback (IntPtr key, IntPtr user_data)
		{
			try {
				managed (GLib.Object.GetObject(key) as GLib.Win32RegistryKey);
				if (release_on_call)
					gch.Free ();
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		bool release_on_call = false;
		GCHandle gch;

		public void PersistUntilCalled ()
		{
			release_on_call = true;
			gch = GCHandle.Alloc (this);
		}

		internal Win32RegistryKeyWatchCallbackFuncNative NativeDelegate;
		GLib.Win32RegistryKeyWatchCallbackFunc managed;

		public Win32RegistryKeyWatchCallbackFuncWrapper (GLib.Win32RegistryKeyWatchCallbackFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new Win32RegistryKeyWatchCallbackFuncNative (NativeCallback);
		}

		public static GLib.Win32RegistryKeyWatchCallbackFunc GetManagedDelegate (Win32RegistryKeyWatchCallbackFuncNative native)
		{
			if (native == null)
				return null;
			Win32RegistryKeyWatchCallbackFuncWrapper wrapper = (Win32RegistryKeyWatchCallbackFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
