/* gstreamermm - a C++ wrapper for gstreamer
 *
 * Copyright 2008 The gstreamermm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <gst/gst.h>
#include <glibmm/value.h>
#include <sigc++/sigc++.h>

#include <map>

_DEFS(gstreamermm,gst)

namespace Gst
{

_WRAP_ENUM(MiniObjectFlags, GstMiniObjectFlags)
_WRAP_ENUM(LockFlags, GstLockFlags)

/** This is a base class for data which represents qdata.
 */
class QuarkData
{
public:
  virtual ~QuarkData(){}
};

/** This is a base class for some gstreamermm objects.
 * It is similar to Glib::Object but has no GObject property or signal support.
 */
class MiniObject
{
  _CLASS_OPAQUE_REFCOUNTED(MiniObject, GstMiniObject, NONE, gst_mini_object_ref, gst_mini_object_unref)
  _IGNORE(gst_mini_object_ref, gst_mini_object_unref)

public:
  typedef sigc::slot< void > SlotFinalizer;

  ~MiniObject();
  _MEMBER_GET(flags, flags, guint, guint)
  _MEMBER_SET(flags, flags, guint, guint)

  _MEMBER_GET(refcount, refcount, gint, gint)

  _MEMBER_GET(lockstate, lockstate, gint, gint)

  _WRAP_METHOD(bool lock(LockFlags flags), gst_mini_object_lock)
  _WRAP_METHOD(void unlock(LockFlags flags), gst_mini_object_unlock)
  _WRAP_METHOD(bool is_writable() const, gst_mini_object_is_writable)

  _IGNORE(gst_mini_object_make_writable)
  /** Checks if a mini-object is writable.  If not, a writable copy is made and
   * returned.  This gives away the reference to the original mini object,
   * and returns a reference to the new object.
   *
   * MT safe
   * @return A mini-object (possibly the same pointer) that
   * is writable.
   */
  Glib::RefPtr<MiniObject> create_writable();

  /** This sets an opaque, named pointer on a miniobject.
   * The name is specified through a #GQuark (retrived e.g. via
   * g_quark_from_static_string()), and the pointer
   * can be gotten back with get_qdata().
   * Setting a previously set user data pointer, overrides (frees)
   * the old pointer set, using NULL as pointer essentially
   * removes the data stored.
   * @param quark a GQuark, naming the user data pointer.
   * @param data an opaque user data pointer.
   */
  void set_qdata(GQuark quark, QuarkData *data);
  _IGNORE(gst_mini_object_set_qdata)

  /** This function gets back user data pointers stored via
   * set_qdata().
   * @param quark a GQuark, naming the user data pointer.
   * @return the user data pointer set, or NULL.
   */
  QuarkData* get_qdata(GQuark quark) const;
  _IGNORE(gst_mini_object_get_qdata)

#m4 _CONVERSION(`gpointer',`QuarkData*',`static_cast<QuarkData*>($3)')
  _WRAP_METHOD(QuarkData* steal_qdata(GQuark quark), gst_mini_object_steal_qdata)

  // Copying a mini object can be achieved by assignment.
  _IGNORE(gst_mini_object_copy)

  /** Adds notifier when mini object is finalized
   * @param slot notifier.
   */
  void add_finalize_notifier(const SlotFinalizer& slot);

  /** Remove finalize notifier
   */
  void remove_finalize_notifier();

  _IGNORE(gst_mini_object_weak_ref)
  _IGNORE(gst_mini_object_weak_unref)

private:
  // In this class non-static member cannot exists, so there is a map
  // with key = gobj() and value = handler.
  static std::map<GstMiniObject*, SlotFinalizer*> finalizers;

  static void destroy_qdata(gpointer qdata);

  static void MiniObject_Finalizer_gstreamermm_callback(gpointer userdata, GstMiniObject *obj);

};

} // namespace Gst
