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

namespace GESSharp {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
	internal delegate void MetaForeachFuncNative(IntPtr container, IntPtr key, IntPtr value, IntPtr user_data);

	internal class MetaForeachFuncInvoker {

		MetaForeachFuncNative native_cb;
		IntPtr __data;
		GLib.DestroyNotify __notify;

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

		internal MetaForeachFuncInvoker (MetaForeachFuncNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

		internal MetaForeachFuncInvoker (MetaForeachFuncNative native_cb, IntPtr data) : this (native_cb, data, null) {}

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

		internal GES.MetaForeachFunc Handler {
			get {
				return new GES.MetaForeachFunc(InvokeNative);
			}
		}

		void InvokeNative (GES.IMetaContainer container, string key, GLib.Value value)
		{
			IntPtr native_key = GLib.Marshaller.StringToPtrGStrdup (key);
			IntPtr native_value = GLib.Marshaller.StructureToPtrAlloc (value);
			native_cb (container == null ? IntPtr.Zero : ((container is GLib.Object) ? (container as GLib.Object).Handle : (container as GES.MetaContainerAdapter).Handle), native_key, native_value, __data);
			GLib.Marshaller.Free (native_key);
			Marshal.FreeHGlobal (native_value);
		}
	}

	internal class MetaForeachFuncWrapper {

		public void NativeCallback (IntPtr container, IntPtr key, IntPtr value, IntPtr user_data)
		{
			try {
				managed (GES.MetaContainerAdapter.GetObject (container, false), GLib.Marshaller.Utf8PtrToString (key), (GLib.Value) Marshal.PtrToStructure (value, typeof (GLib.Value)));
				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 MetaForeachFuncNative NativeDelegate;
		GES.MetaForeachFunc managed;

		public MetaForeachFuncWrapper (GES.MetaForeachFunc managed)
		{
			this.managed = managed;
			if (managed != null)
				NativeDelegate = new MetaForeachFuncNative (NativeCallback);
		}

		public static GES.MetaForeachFunc GetManagedDelegate (MetaForeachFuncNative native)
		{
			if (native == null)
				return null;
			MetaForeachFuncWrapper wrapper = (MetaForeachFuncWrapper) native.Target;
			if (wrapper == null)
				return null;
			return wrapper.managed;
		}
	}
#endregion
}
