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

namespace Gst
{

Object::Object(const Glib::ConstructParams& construct_params)
:
  Glib::Object(construct_params)
{
  // Get rid of the floating reference for newly constructed Gst::Objects.
  gst_object_ref(gobj());
  gst_object_sink(gobj());
}

Object::Object(GstObject* castitem)
:
  Glib::Object(reinterpret_cast<GObject*>(castitem))
{
  if(G_LIKELY(GST_OBJECT_IS_FLOATING(castitem)))
  {
    //The floating reference is convenience for C,
    //but is useless and difficult for us:
    gst_object_ref(gobj());
    gst_object_sink(gobj());
  }
}

xmlpp::Node* Object::save(xmlpp::Node* parent)
{
  gst_object_save_thyself(gobj(), parent->cobj());

  // gst_object_save_thyself() returns parent node so do so here also.
  return parent;
}

const xmlpp::Node* Object::save(const xmlpp::Node* parent) const
{
  gst_object_save_thyself(const_cast<GstObject*>(gobj()),
    const_cast<xmlNodePtr>(parent->cobj()));

  // gst_object_save_thyself() returns parent node so do so here also.
  return parent;
}

GstXmlNodePtr Object_Class::save_thyself_vfunc_callback(GstObject* self, GstXmlNodePtr parent)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        xmlpp::Node xmlpp_parent(parent);
        // Call the virtual member method, which derived classes might override.
        (void) obj->save_vfunc(&xmlpp_parent);
        return parent;
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
  );

  // Call the original underlying C function:
  if(base && base->save_thyself)
    return (*base->save_thyself)(self, parent);


  typedef GstXmlNodePtr RType;
  return RType();
}
void Object_Class::restore_thyself_vfunc_callback(GstObject* self, GstXmlNodePtr self_node)
{
  Glib::ObjectBase *const obj_base = static_cast<Glib::ObjectBase*>(
      Glib::ObjectBase::_get_current_wrapper((GObject*)self));

  // Non-gtkmmproc-generated custom classes implicitly call the default
  // Glib::ObjectBase constructor, which sets is_derived_. But gtkmmproc-
  // generated classes can use this optimisation, which avoids the unnecessary
  // parameter conversions if there is no possibility of the virtual function
  // being overridden:
  if(obj_base && obj_base->is_derived_())
  {
    CppObjectType *const obj = dynamic_cast<CppObjectType* const>(obj_base);
    if(obj) // This can be NULL during destruction.
    {
      try // Trap C++ exceptions which would normally be lost because this is a C callback.
      {
        // Call the virtual member method, which derived classes might override.
        xmlpp::Node xmlpp_node(self_node);
        obj->restore_vfunc(&xmlpp_node);
        return;
      }
      catch(...)
      {
        Glib::exception_handlers_invoke();
      }
    }
  }
  
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(self)) // Get the parent class of the object class (The original underlying C class).
  );

  // Call the original underlying C function:
  if(base && base->restore_thyself)
    (*base->restore_thyself)(self, self_node);

}
xmlpp::Node* Gst::Object::save_vfunc(xmlpp::Node* parent) 
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
  );

  if(base && base->save_thyself)
  {
    (*base->save_thyself)(gobj(),parent->cobj());
    return parent;
  }

  return 0;
}
void Gst::Object::restore_vfunc(xmlpp::Node* self_node) 
{
  BaseClassType *const base = static_cast<BaseClassType*>(
      g_type_class_peek_parent(G_OBJECT_GET_CLASS(gobject_)) // Get the parent class of the object class (The original underlying C class).
  );

  if(base && base->restore_thyself)
    (*base->restore_thyself)(gobj(),self_node->cobj());
}

} // namespace Gst
