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

namespace Gst.BaseSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate int TypeFindHelperGetRangeFunctionNative(IntPtr obj, IntPtr parent, ulong offset, uint length, out IntPtr buffer);

	internal class TypeFindHelperGetRangeFunctionInvoker {

		TypeFindHelperGetRangeFunctionNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal TypeFindHelperGetRangeFunctionInvoker (TypeFindHelperGetRangeFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal TypeFindHelperGetRangeFunctionInvoker (TypeFindHelperGetRangeFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		Gst.FlowReturn InvokeNative (Gst.Object obj, Gst.Object parent, ulong offset, uint length, out Gst.Buffer buffer)
		{
			IntPtr native_buffer;
			Gst.FlowReturn __result = (Gst.FlowReturn) native_cb (obj == null ? IntPtr.Zero : obj.Handle, parent == null ? IntPtr.Zero : parent.Handle, offset, length, out native_buffer);
			buffer = native_buffer == IntPtr.Zero ? null : (Gst.Buffer) GLib.Opaque.GetOpaque (native_buffer, typeof (Gst.Buffer), true);
			return __result;
		}
	}

	internal class TypeFindHelperGetRangeFunctionWrapper {

		public int NativeCallback (IntPtr obj, IntPtr parent, ulong offset, uint length, out IntPtr buffer)
		{
			try {
				Gst.Buffer mybuffer;

				Gst.FlowReturn __ret = managed (GLib.Object.GetObject(obj) as Gst.Object, GLib.Object.GetObject(parent) as Gst.Object, offset, length, out mybuffer);
				buffer = mybuffer == null ? IntPtr.Zero : mybuffer.Handle;

				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 TypeFindHelperGetRangeFunctionNative NativeDelegate;
		Gst.Base.TypeFindHelperGetRangeFunction managed;

		public TypeFindHelperGetRangeFunctionWrapper (Gst.Base.TypeFindHelperGetRangeFunction managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new TypeFindHelperGetRangeFunctionNative (NativeCallback);
		}

		public static Gst.Base.TypeFindHelperGetRangeFunction GetManagedDelegate (TypeFindHelperGetRangeFunctionNative native)
		{
			if (native == null)
				return null;
			TypeFindHelperGetRangeFunctionWrapper wrapper = (TypeFindHelperGetRangeFunctionWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
