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

namespace GLib {

	using System;
	using System.Runtime.InteropServices;

#region Autogenerated code
	public partial class AsyncInitableAdapter : GLib.GInterfaceAdapter, GLib.IAsyncInitable {

		[StructLayout (LayoutKind.Sequential)]
		struct GAsyncInitableIface {
			public InitAsyncNativeDelegate InitAsync;
			public InitFinishNativeDelegate InitFinish;
		}

		static GAsyncInitableIface iface;

		static AsyncInitableAdapter ()
		{
			GLib.GType.Register (_gtype, typeof (AsyncInitableAdapter));
			iface.InitAsync = new InitAsyncNativeDelegate (InitAsync_cb);
			iface.InitFinish = new InitFinishNativeDelegate (InitFinish_cb);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate void InitAsyncNativeDelegate (IntPtr inst, int io_priority, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data);

		static void InitAsync_cb (IntPtr inst, int io_priority, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data)
		{
			try {
				IAsyncInitableImplementor __obj = GLib.Object.GetObject (inst, false) as IAsyncInitableImplementor;
				GLibSharp.AsyncReadyCallbackInvoker cb_invoker = new GLibSharp.AsyncReadyCallbackInvoker (cb, user_data);
				__obj.InitAsync (io_priority, GLib.Object.GetObject(cancellable) as GLib.Cancellable, cb_invoker.Handler);
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate bool InitFinishNativeDelegate (IntPtr inst, IntPtr res, out IntPtr error);

		static bool InitFinish_cb (IntPtr inst, IntPtr res, out IntPtr error)
		{
			error = IntPtr.Zero;

			try {
				IAsyncInitableImplementor __obj = GLib.Object.GetObject (inst, false) as IAsyncInitableImplementor;
				bool __result;
				__result = __obj.InitFinish (GLib.AsyncResultAdapter.GetObject (res, false));
				return __result;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: above call does not return.
				throw;
			}
		}

		static int class_offset = 2 * IntPtr.Size;

		static void Initialize (IntPtr ptr, IntPtr data)
		{
			IntPtr ifaceptr = new IntPtr (ptr.ToInt64 () + class_offset);
			GAsyncInitableIface native_iface = (GAsyncInitableIface) Marshal.PtrToStructure (ifaceptr, typeof (GAsyncInitableIface));
			native_iface.InitAsync = iface.InitAsync;
			native_iface.InitFinish = iface.InitFinish;
			Marshal.StructureToPtr (native_iface, ifaceptr, false);
		}

		GLib.Object implementor;

		public AsyncInitableAdapter ()
		{
			InitHandler = new GLib.GInterfaceInitHandler (Initialize);
		}

		public AsyncInitableAdapter (IAsyncInitableImplementor implementor)
		{
			if (implementor == null)
				throw new ArgumentNullException ("implementor");
			else if (!(implementor is GLib.Object))
				throw new ArgumentException ("implementor must be a subclass of GLib.Object");
			this.implementor = implementor as GLib.Object;
		}

		public AsyncInitableAdapter (IntPtr handle)
		{
			if (!_gtype.IsInstance (handle))
				throw new ArgumentException ("The gobject doesn't implement the GInterface of this adapter", "handle");
			implementor = GLib.Object.GetObject (handle);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate IntPtr d_g_async_initable_get_type();
		static d_g_async_initable_get_type g_async_initable_get_type = FuncLoader.LoadFunction<d_g_async_initable_get_type>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_async_initable_get_type"));

		private static GLib.GType _gtype = new GLib.GType (g_async_initable_get_type ());

		public static GLib.GType GType {
			get {
				return _gtype;
			}
		}

		public override GLib.GType GInterfaceGType {
			get {
				return _gtype;
			}
		}

		public override IntPtr Handle {
			get {
				return implementor.Handle;
			}
		}

		public IntPtr OwnedHandle {
			get {
				return implementor.OwnedHandle;
			}
		}

		public static IAsyncInitable GetObject (IntPtr handle, bool owned)
		{
			GLib.Object obj = GLib.Object.GetObject (handle, owned);
			return GetObject (obj);
		}

		public static IAsyncInitable GetObject (GLib.Object obj)
		{
			if (obj == null)
				return null;
			else if (obj is IAsyncInitableImplementor)
				return new AsyncInitableAdapter (obj as IAsyncInitableImplementor);
			else if (obj as IAsyncInitable == null)
				return new AsyncInitableAdapter (obj.Handle);
			else
				return obj as IAsyncInitable;
		}

		public IAsyncInitableImplementor Implementor {
			get {
				return implementor as IAsyncInitableImplementor;
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate void d_g_async_initable_init_async(IntPtr raw, int io_priority, IntPtr cancellable, GLibSharp.AsyncReadyCallbackNative cb, IntPtr user_data);
		static d_g_async_initable_init_async g_async_initable_init_async = FuncLoader.LoadFunction<d_g_async_initable_init_async>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_async_initable_init_async"));

		public void InitAsync(int io_priority, GLib.Cancellable cancellable, GLib.AsyncReadyCallback cb) {
			GLibSharp.AsyncReadyCallbackWrapper cb_wrapper = new GLibSharp.AsyncReadyCallbackWrapper (cb);
			cb_wrapper.PersistUntilCalled ();
			g_async_initable_init_async(Handle, io_priority, cancellable == null ? IntPtr.Zero : cancellable.Handle, cb_wrapper.NativeDelegate, IntPtr.Zero);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate bool d_g_async_initable_init_finish(IntPtr raw, IntPtr res, out IntPtr error);
		static d_g_async_initable_init_finish g_async_initable_init_finish = FuncLoader.LoadFunction<d_g_async_initable_init_finish>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_async_initable_init_finish"));

		public bool InitFinish(GLib.IAsyncResult res) {
			IntPtr error = IntPtr.Zero;
			bool raw_ret = g_async_initable_init_finish(Handle, res == null ? IntPtr.Zero : ((res is GLib.Object) ? (res as GLib.Object).Handle : (res as GLib.AsyncResultAdapter).Handle), out error);
			bool ret = raw_ret;
			if (error != IntPtr.Zero) throw new GLib.GException (error);
			return ret;
		}

#endregion
	}
}
