/* 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/gstenumtypes.h>
_PINCLUDE(gstreamermm/private/object_p.h)

static void Task_Task_gstreamermm_callback(void* data)
{
  Gst::Task::SlotTask* the_slot = static_cast<Gst::Task::SlotTask*>(data);

  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  try
  {
  #endif //GLIBMM_EXCEPTIONS_ENABLED
    (*the_slot)();
  #ifdef GLIBMM_EXCEPTIONS_ENABLED
  }
  catch(...)
  {
    Glib::exception_handlers_invoke();
  }
  #endif //GLIBMM_EXCEPTIONS_ENABLED
}

namespace Gst
{

Task::Task(const SlotTask& task_slot)
: _CONSTRUCT()
{
  //Create a copy of the slot.  A pointer to this copy will be passed through
  //the call back's data parameter.  It will be destroyed in the Task's
  //destructor
  slot = new SlotTask(task_slot);
  _slot_set(true);

  // These lines are taken verbatim from gst_task_create() after calling
  // g_object_new().
  gobj()->func = &Task_Task_gstreamermm_callback;
  gobj()->data = &slot;
}

bool Task::_slot_set(bool mark_set) const
{
  static bool slot_set = false;

  if (mark_set)
    slot_set = true;

  return slot_set;
}

Task::~Task()
{
  // Delete task's slot upon destruction
  if (_slot_set())
    delete slot;
}

} //namespace Gst
