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

namespace GstSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate void ElementCallAsyncFuncNative(IntPtr element, IntPtr user_data);

	internal class ElementCallAsyncFuncInvoker {

		ElementCallAsyncFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal ElementCallAsyncFuncInvoker (ElementCallAsyncFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal ElementCallAsyncFuncInvoker (ElementCallAsyncFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal Gst.ElementCallAsyncFunc Handler {
			get {
				return new Gst.ElementCallAsyncFunc(InvokeNative);
			}
		}

		void InvokeNative (Gst.Element element)
		{
			native_cb (element == null ? IntPtr.Zero : element.Handle, __data);
		}
	}

	internal class ElementCallAsyncFuncWrapper {

		public void NativeCallback (IntPtr element, IntPtr user_data)
		{
			try {
				managed (GLib.Object.GetObject(element) as Gst.Element);
				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 ElementCallAsyncFuncNative NativeDelegate;
		Gst.ElementCallAsyncFunc managed;

		public ElementCallAsyncFuncWrapper (Gst.ElementCallAsyncFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new ElementCallAsyncFuncNative (NativeCallback);
		}

		public static Gst.ElementCallAsyncFunc GetManagedDelegate (ElementCallAsyncFuncNative native)
		{
			if (native == null)
				return null;
			ElementCallAsyncFuncWrapper wrapper = (ElementCallAsyncFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
