// 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;
  using System.Collections;

  #region Autogenerated code
  [Gst.GLib.CDeclCallback]
  internal delegate IntPtr PadQueryTypeFunctionNative (IntPtr pad);

  internal class PadQueryTypeFunctionInvoker {

    PadQueryTypeFunctionNative native_cb;
    IntPtr __data;
    Gst.GLib.DestroyNotify __notify;

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

    internal PadQueryTypeFunctionInvoker (PadQueryTypeFunctionNative native_cb) : this (native_cb, IntPtr.Zero, null) {}

    internal PadQueryTypeFunctionInvoker (PadQueryTypeFunctionNative native_cb, IntPtr data) : this (native_cb, data, null) {}

    internal PadQueryTypeFunctionInvoker (PadQueryTypeFunctionNative native_cb, IntPtr data, Gst.GLib.DestroyNotify notify) {
      this.native_cb = native_cb;
      __data = data;
      __notify = notify;
    }

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

    Gst.QueryType[] InvokeNative (Gst.Pad pad) {
      IntPtr raw_ret = native_cb (pad == null ? IntPtr.Zero : pad.Handle);
      if (raw_ret == IntPtr.Zero)
        return new Gst.QueryType[] {};

      ArrayList result = new ArrayList ();
      bool term = false;
      int ofs = 0;
      while (!term) {
        Gst.QueryType t = (Gst.QueryType) Marshal.ReadInt32 (raw_ret, ofs);
        if (t == Gst.QueryType.None) {
          term = true;
        } else {
          result.Add (t);
          ofs += 4;
        }
      }

      return (Gst.QueryType[]) result.ToArray (typeof (Gst.QueryType));
    }
  }

  internal class PadQueryTypeFunctionWrapper {
    IntPtr raw_ret = IntPtr.Zero;

    public IntPtr NativeCallback (IntPtr pad) {
      try {
        if (raw_ret != IntPtr.Zero)
          return raw_ret;

        Gst.QueryType[] __ret = managed (Gst.GLib.Object.GetObject (pad) as Gst.Pad);
        if (release_on_call)
          gch.Free ();
        raw_ret = Marshal.AllocHGlobal (4 * __ret.Length + 4);
        for (int i = 0; i < __ret.Length; i++)
          Marshal.WriteInt32 (raw_ret, i * 4, (int) __ret[i]);
        Marshal.WriteInt32 (raw_ret, __ret.Length * 4, 0);

        return raw_ret;
      } catch (Exception e) {
        Gst.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 PadQueryTypeFunctionNative NativeDelegate;
    Gst.PadQueryTypeFunction managed;

    public PadQueryTypeFunctionWrapper (Gst.PadQueryTypeFunction managed) {
      this.managed = managed;
      if (managed != null)
        NativeDelegate = new PadQueryTypeFunctionNative (NativeCallback);
    }

    public static Gst.PadQueryTypeFunction GetManagedDelegate (PadQueryTypeFunctionNative native) {
      if (native == null)
        return null;
      PadQueryTypeFunctionWrapper wrapper = (PadQueryTypeFunctionWrapper) native.Target;
      if (wrapper == null)
        return null;
      return wrapper.managed;
    }
  }
  #endregion
}