// 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 MetaClearFunctionNative(IntPtr buffer, IntPtr meta);

	internal class MetaClearFunctionInvoker {

		MetaClearFunctionNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal MetaClearFunctionInvoker (MetaClearFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal MetaClearFunctionInvoker (MetaClearFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		void InvokeNative (Gst.Buffer buffer, Gst.Meta meta)
		{
			IntPtr native_meta = GLib.Marshaller.StructureToPtrAlloc (meta);
			native_cb (buffer == null ? IntPtr.Zero : buffer.Handle, native_meta);
			Marshal.FreeHGlobal (native_meta);
		}
	}

	internal class MetaClearFunctionWrapper {

		public void NativeCallback (IntPtr buffer, IntPtr meta)
		{
			try {
				managed (buffer == IntPtr.Zero ? null : (Gst.Buffer) GLib.Opaque.GetOpaque (buffer, typeof (Gst.Buffer), false), Gst.Meta.New (meta));
				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 MetaClearFunctionNative NativeDelegate;
		Gst.MetaClearFunction managed;

		public MetaClearFunctionWrapper (Gst.MetaClearFunction managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new MetaClearFunctionNative (NativeCallback);
		}

		public static Gst.MetaClearFunction GetManagedDelegate (MetaClearFunctionNative native)
		{
			if (native == null)
				return null;
			MetaClearFunctionWrapper wrapper = (MetaClearFunctionWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
