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

namespace Gnome.VfsSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate void AsyncCreateAsChannelCallbackNative(IntPtr handle, IntPtr channel, int result, IntPtr callback_data);

	internal class AsyncCreateAsChannelCallbackInvoker {

		AsyncCreateAsChannelCallbackNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal AsyncCreateAsChannelCallbackInvoker (AsyncCreateAsChannelCallbackNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal AsyncCreateAsChannelCallbackInvoker (AsyncCreateAsChannelCallbackNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal Gnome.Vfs.AsyncCreateAsChannelCallback Handler {
			get {
				return new Gnome.Vfs.AsyncCreateAsChannelCallback(InvokeNative);
			}
		}

		void InvokeNative (Gnome.Vfs.AsyncHandle handle, IntPtr channel, Gnome.Vfs.Result result)
		{
			native_cb (handle == null ? IntPtr.Zero : handle.Handle, channel, (int) result, __data);
		}
	}

	internal class AsyncCreateAsChannelCallbackWrapper {

		public void NativeCallback (IntPtr handle, IntPtr channel, int result, IntPtr callback_data)
		{
			try {
				managed (handle == IntPtr.Zero ? null : (Gnome.Vfs.AsyncHandle) GLib.Opaque.GetOpaque (handle, typeof (Gnome.Vfs.AsyncHandle), false), channel, (Gnome.Vfs.Result) result);
				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 AsyncCreateAsChannelCallbackNative NativeDelegate;
		Gnome.Vfs.AsyncCreateAsChannelCallback managed;

		public AsyncCreateAsChannelCallbackWrapper (Gnome.Vfs.AsyncCreateAsChannelCallback managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new AsyncCreateAsChannelCallbackNative (NativeCallback);
		}

		public static Gnome.Vfs.AsyncCreateAsChannelCallback GetManagedDelegate (AsyncCreateAsChannelCallbackNative native)
		{
			if (native == null)
				return null;
			AsyncCreateAsChannelCallbackWrapper wrapper = (AsyncCreateAsChannelCallbackWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
