// 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 ConverterAdapter : GLib.GInterfaceAdapter, GLib.IConverter {

		[StructLayout (LayoutKind.Sequential)]
		struct GConverterIface {
			public ConvertNativeDelegate Convert;
			public ResetNativeDelegate Reset;
		}

		static GConverterIface iface;

		static ConverterAdapter ()
		{
			GLib.GType.Register (_gtype, typeof (ConverterAdapter));
			iface.Convert = new ConvertNativeDelegate (Convert_cb);
			iface.Reset = new ResetNativeDelegate (Reset_cb);
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate int ConvertNativeDelegate (IntPtr inst, IntPtr inbuf, UIntPtr inbuf_size, IntPtr outbuf, UIntPtr outbuf_size, int flags, out UIntPtr bytes_read, out UIntPtr bytes_written, out IntPtr error);

		static int Convert_cb (IntPtr inst, IntPtr inbuf, UIntPtr inbuf_size, IntPtr outbuf, UIntPtr outbuf_size, int flags, out UIntPtr bytes_read, out UIntPtr bytes_written, out IntPtr error)
		{
			error = IntPtr.Zero;

			try {
				IConverterImplementor __obj = GLib.Object.GetObject (inst, false) as IConverterImplementor;
				GLib.ConverterResult __result;
				ulong mybytes_read;
				ulong mybytes_written;
				__result = __obj.Convert (inbuf, (ulong) inbuf_size, outbuf, (ulong) outbuf_size, (GLib.ConverterFlags) flags, out mybytes_read, out mybytes_written);
				bytes_read = new UIntPtr (mybytes_read);
				bytes_written = new UIntPtr (mybytes_written);
				return (int) __result;
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, true);
				// NOTREACHED: above call does not return.
				throw;
			}
		}

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate void ResetNativeDelegate (IntPtr inst);

		static void Reset_cb (IntPtr inst)
		{
			try {
				IConverterImplementor __obj = GLib.Object.GetObject (inst, false) as IConverterImplementor;
				__obj.Reset ();
			} catch (Exception e) {
				GLib.ExceptionManager.RaiseUnhandledException (e, false);
			}
		}

		static int class_offset = 2 * IntPtr.Size;

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

		GLib.Object implementor;

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

		public ConverterAdapter (IConverterImplementor 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 ConverterAdapter (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_converter_get_type();
		static d_g_converter_get_type g_converter_get_type = FuncLoader.LoadFunction<d_g_converter_get_type>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_converter_get_type"));

		private static GLib.GType _gtype = new GLib.GType (g_converter_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 IConverter GetObject (IntPtr handle, bool owned)
		{
			GLib.Object obj = GLib.Object.GetObject (handle, owned);
			return GetObject (obj);
		}

		public static IConverter GetObject (GLib.Object obj)
		{
			if (obj == null)
				return null;
			else if (obj is IConverterImplementor)
				return new ConverterAdapter (obj as IConverterImplementor);
			else if (obj as IConverter == null)
				return new ConverterAdapter (obj.Handle);
			else
				return obj as IConverter;
		}

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

		[UnmanagedFunctionPointer (CallingConvention.Cdecl)]
		delegate int d_g_converter_convert(IntPtr raw, IntPtr inbuf, UIntPtr inbuf_size, IntPtr outbuf, UIntPtr outbuf_size, int flags, out UIntPtr bytes_read, out UIntPtr bytes_written, out IntPtr error);
		static d_g_converter_convert g_converter_convert = FuncLoader.LoadFunction<d_g_converter_convert>(FuncLoader.GetProcAddress(GLibrary.Load(Library.Gio), "g_converter_convert"));

		public GLib.ConverterResult Convert(IntPtr inbuf, ulong inbuf_size, IntPtr outbuf, ulong outbuf_size, GLib.ConverterFlags flags, out ulong bytes_read, out ulong bytes_written) {
			UIntPtr native_bytes_read;
			UIntPtr native_bytes_written;
			IntPtr error = IntPtr.Zero;
			int raw_ret = g_converter_convert(Handle, inbuf, new UIntPtr (inbuf_size), outbuf, new UIntPtr (outbuf_size), (int) flags, out native_bytes_read, out native_bytes_written, out error);
			GLib.ConverterResult ret = (GLib.ConverterResult) raw_ret;
			bytes_read = (ulong) native_bytes_read;
			bytes_written = (ulong) native_bytes_written;
			if (error != IntPtr.Zero) throw new GLib.GException (error);
			return ret;
		}

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

		public void Reset() {
			g_converter_reset(Handle);
		}

#endregion
	}
}
