// 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 bool IteratorFoldFunctionNative(IntPtr item, IntPtr _ret, IntPtr user_data);

	internal class IteratorFoldFunctionInvoker {

		IteratorFoldFunctionNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal IteratorFoldFunctionInvoker (IteratorFoldFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal IteratorFoldFunctionInvoker (IteratorFoldFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		bool InvokeNative (GLib.Value item, GLib.Value _ret)
		{
			IntPtr native_item = GLib.Marshaller.StructureToPtrAlloc (item);
			IntPtr native__ret = GLib.Marshaller.StructureToPtrAlloc (_ret);
			bool __result = native_cb (native_item, native__ret, __data);
			Marshal.FreeHGlobal (native_item);
			Marshal.FreeHGlobal (native__ret);
			return __result;
		}
	}

	internal class IteratorFoldFunctionWrapper {

		public bool NativeCallback (IntPtr item, IntPtr _ret, IntPtr user_data)
		{
			try {
				bool __ret = managed ((GLib.Value) Marshal.PtrToStructure (item, typeof (GLib.Value)), (GLib.Value) Marshal.PtrToStructure (_ret, typeof (GLib.Value)));
				if (release_on_call)
					gch.Free ();
				return __ret;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
				return false;
			}
		}

		bool release_on_call = false;
		GCHandle gch;

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

		internal IteratorFoldFunctionNative NativeDelegate;
		Gst.IteratorFoldFunction managed;

		public IteratorFoldFunctionWrapper (Gst.IteratorFoldFunction managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new IteratorFoldFunctionNative (NativeCallback);
		}

		public static Gst.IteratorFoldFunction GetManagedDelegate (IteratorFoldFunctionNative native)
		{
			if (native == null)
				return null;
			IteratorFoldFunctionWrapper wrapper = (IteratorFoldFunctionWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}