// 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 MetaSerializeFunctionNative(IntPtr meta, IntPtr data, out byte version);

	internal class MetaSerializeFunctionInvoker {

		MetaSerializeFunctionNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal MetaSerializeFunctionInvoker (MetaSerializeFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal MetaSerializeFunctionInvoker (MetaSerializeFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

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

		bool InvokeNative (Gst.Meta meta, Gst.ByteArrayInterface data, out byte version)
		{
			IntPtr native_meta = GLib.Marshaller.StructureToPtrAlloc (meta);
			IntPtr native_data = GLib.Marshaller.StructureToPtrAlloc (data);
			bool __result = native_cb (native_meta, native_data, out version);
			Marshal.FreeHGlobal (native_meta);
			Marshal.FreeHGlobal (native_data);
			return __result;
		}
	}

	internal class MetaSerializeFunctionWrapper {

		public bool NativeCallback (IntPtr meta, IntPtr data, out byte version)
		{
			try {
				bool __ret = managed (Gst.Meta.New (meta), Gst.ByteArrayInterface.New (data), out version);
				if (release_on_call)
					gch.Free ();
				return __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 MetaSerializeFunctionNative NativeDelegate;
		Gst.MetaSerializeFunction managed;

		public MetaSerializeFunctionWrapper (Gst.MetaSerializeFunction managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new MetaSerializeFunctionNative (NativeCallback);
		}

		public static Gst.MetaSerializeFunction GetManagedDelegate (MetaSerializeFunctionNative native)
		{
			if (native == null)
				return null;
			MetaSerializeFunctionWrapper wrapper = (MetaSerializeFunctionWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
