// 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 int IteratorNextFunctionNative(IntPtr it, IntPtr _result);

	internal class IteratorNextFunctionInvoker {

		IteratorNextFunctionNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal IteratorNextFunctionInvoker (IteratorNextFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal IteratorNextFunctionInvoker (IteratorNextFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		Gst.IteratorResult InvokeNative (Gst.Iterator it, GLib.Value _result)
		{
			IntPtr native__result = GLib.Marshaller.StructureToPtrAlloc (_result);
			Gst.IteratorResult __result = (Gst.IteratorResult) native_cb (it == null ? IntPtr.Zero : it.Handle, native__result);
			Marshal.FreeHGlobal (native__result);
			return __result;
		}
	}

	internal class IteratorNextFunctionWrapper {

		public int NativeCallback (IntPtr it, IntPtr _result)
		{
			try {
				Gst.IteratorResult __ret = managed (it == IntPtr.Zero ? null : (Gst.Iterator) GLib.Opaque.GetOpaque (it, typeof (Gst.Iterator), false), (GLib.Value) Marshal.PtrToStructure (_result, typeof (GLib.Value)));
				if (release_on_call)
					gch.Free ();
				return (int) __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: Above call does not return.
				throw e;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

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

		internal IteratorNextFunctionNative NativeDelegate;
		Gst.IteratorNextFunction managed;

		public IteratorNextFunctionWrapper (Gst.IteratorNextFunction managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new IteratorNextFunctionNative (NativeCallback);
		}

		public static Gst.IteratorNextFunction GetManagedDelegate (IteratorNextFunctionNative native)
		{
			if (native == null)
				return null;
			IteratorNextFunctionWrapper wrapper = (IteratorNextFunctionWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
