Re: Fixing the Gst::Message classes



Murray Cumming wrote:
On Tue, 2007-12-11 at 09:31 -0500, José Alburquerque wrote:
If you feel like it. :-) I've looked a bit and started to make some modifications, but if you'll look into it you'll probably have a better sense as to what has to be done. Would you like me to send you the modifications I've made so far?

Sure. That could be useful. Thanks.

I managed to look at wrap.cc and wrap.h. I haven't made modifications to Gst::MiniObject yet because it was a work in progress. I didn't get a chance to compile, but the logic may be ok so here are wrap.h and wrap.cc.

-Jose
// -*- c++ -*-
#ifndef _GSTMM_WRAP_H
#define _GSTMM_WRAP_H

/* $Id: wrap.h 447 2007-10-03 09:51:41Z murrayc $ */

/* Copyright (C) 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library 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/gstminiobject.h>
#include <glibmm/refptr.h>
#include <gstmm/miniobject.h>

namespace Gst
{

#ifndef DOXYGEN_SHOULD_SKIP_THIS

class MiniObject;

// Type of the per-class wrap_new() functions.
typedef Gst::MiniObject* (*WrapNewFunction) (GstMiniObject*);

// Setup and free the structures used by wrap_register().
// Both functions might be called more than once.
void wrap_register_init();
void wrap_register_cleanup();

// Register a new type for auto allocation.
void wrap_register(GType type, WrapNewFunction func);

// Return the current C++ wrapper instance of the GObject,
// or automatically generate a new wrapper if there's none.
Gst::MiniObject* wrap_auto(GstMiniObject* object, bool take_copy = false);

/** Create a C++ instance of a known C++ type that is mostly closely associated with the GType of the C object.
 * @param object The C object which should be placed in a new C++ instance.
 * @param interface_gtype The returned instance will implement this interface. Otherwise it will be NULL.
 */
Glib::MiniObject* wrap_create_new_wrapper_for_interface(GstMiniObject* object, GType interface_gtype);

// Return the current C++ wrapper instance of the GObject,
// or automatically generate a new wrapper if there's none.
template<class TInterface>
TInterface* wrap_auto_interface(GstMiniObject* object, bool take_copy = false)
{
  if(!object)
    return 0;

  // Look up current C++ wrapper instance:
  MiniObject* pCppObject = MiniObject::_get_current_wrapper(object);

  if(!pCppObject)
  {
    // There's not already a wrapper: generate a new C++ instance.
    // We use exact_type_only=true avoid creating Glib::Object for interfaces of unknown implementation,
    // because we do not want a C++ object that does not dynamic_cast to the expected interface type.
    pCppObject = wrap_create_new_wrapper_for_interface(object, TInterface::get_base_type());
  }

  //If no exact wrapper was created, 
  //create an instance of the interface, 
  //so we at least get the expected type:
  TInterface* result = 0;
  if(pCppObject)
     result = dynamic_cast<TInterface*>(pCppObject);
  else
     result = new TInterface((typename TInterface::BaseObjectType*)object);

  // take_copy=true is used where the GTK+ function doesn't do
  // an extra ref for us, and always for plain struct members.
  if(take_copy && result)
    result->reference();

  return result;
}

#endif //DOXYGEN_SHOULD_SKIP_THIS

// Get a C++ instance that wraps the C instance.
// This always returns the same C++ instance for the same C instance.
// Each wrapper has it's own override of Glib::wrap().
// use take_copy = true when wrapping a struct member.
// TODO: move to object.h ?
/** @relates Glib::Object */
Glib::RefPtr<Glib::Object> wrap(GstMiniObject* object, bool take_copy = false);


/** Get the underlying C instance from the C++ instance.  This is just
 * like calling gobj(), but it does its own check for a NULL pointer.
 */
template <class T> inline
typename T::BaseObjectType* unwrap(T* ptr)
{
  return (ptr) ? ptr->gobj() : 0;
}

/** Get the underlying C instance from the C++ instance.  This is just
 * like calling gobj(), but it does its own check for a NULL pointer.
 */
template <class T> inline
const typename T::BaseObjectType* unwrap(const T* ptr)
{
  return (ptr) ? ptr->gobj() : 0;
}

/** Get the underlying C instance from the C++ instance.  This is just
 * like calling gobj(), but it does its own check for a NULL pointer.
 */
template <class T> inline
typename T::BaseObjectType* unwrap(const Glib::RefPtr<T>& ptr)
{
  return (ptr) ? ptr->gobj() : 0;
}

/** Get the underlying C instance from the C++ instance.  This is just
 * like calling gobj(), but it does its own check for a NULL pointer.
 */
template <class T> inline
const typename T::BaseObjectType* unwrap(const Glib::RefPtr<const T>& ptr)
{
  return (ptr) ? ptr->gobj() : 0;
}

/** Get the underlying C instance from the C++ instance and acquire a
 * reference.  This is just like calling gobj_copy(), but it does its own
 * check for a NULL pointer.
 */
template <class T> inline
typename T::BaseObjectType* unwrap_copy(const Glib::RefPtr<T>& ptr)
{
  return (ptr) ? ptr->gobj_copy() : 0;
}

/** Get the underlying C instance from the C++ instance and acquire a
 * reference.  This is just like calling gobj_copy(), but it does its own
 * check for a NULL pointer.
 */
template <class T> inline
const typename T::BaseObjectType* unwrap_copy(const Glib::RefPtr<const T>& ptr)
{
  return (ptr) ? ptr->gobj_copy() : 0;
}

extern GLIBMM_API GQuark quark_;
extern GLIBMM_API GQuark quark_cpp_wrapper_deleted_;

} // namespace Gst


#endif /* _GSTMM_WRAP_H */

// -*- c++ -*-
/* $Id: wrap.cc 447 2007-10-03 09:51:41Z murrayc $ */

/* wrap.cc
 *
 * Copyright (C) 1998-2002 The gtkmm Development Team
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <glib/gtypes.h>
#include <glib/gmacros.h>

#include <vector>
#include <gstmm/miniobject.h>
#include <gstmm/wrap.h>

#include <glibmmconfig.h>
GLIBMM_USING_STD(vector)


namespace
{

// Although the new g_type_set_qdata() interface is used now, we still need
// a table because we cannot assume that a function pointer fits into void*
// on any platform.  Nevertheless, indexing a vector costs almost nothing
// compared to a map lookup.

typedef std::vector<Gst::WrapNewFunction> WrapFuncTable;

static WrapFuncTable* wrap_func_table = 0;

} // anonymous namespace


namespace Gst
{

void wrap_register_init()
{
  g_type_init();

  if(!Gst::quark_)
  {
    Gst::quark_ = g_quark_from_static_string("gstmm__Gst::quark_");
    Gst::quark_cpp_wrapper_deleted_ = g_quark_from_static_string("gstmm__Gst::quark_cpp_wrapper_deleted_");
  }

  if(!wrap_func_table)
  {
    // Make the first element a dummy so we can detect unregistered types.
    // g_type_get_qdata() returns NULL if no data has been set up.
    wrap_func_table = new WrapFuncTable(1);
  }
}

void wrap_register_cleanup()
{
  if(wrap_func_table)
  {
    delete wrap_func_table;
    wrap_func_table = 0;
  }
}

// Register the unique wrap_new() function of a new C++ wrapper type.
// The GType argument specifies the parent C type to wrap from.
//
void wrap_register(GType type, WrapNewFunction func)
{
  const guint idx = wrap_func_table->size();
  wrap_func_table->push_back(func);

  // Store the table index in the type's static data.
  g_type_set_qdata(type, Gst::quark_, GUINT_TO_POINTER(idx));
}


static Gst::MiniObject* wrap_create_new_wrapper(GstMiniObject* object)
{
  g_return_val_if_fail(wrap_func_table != 0, 0);
  g_return_val_if_fail (object != 0, 0);

  const bool gstmm_wrapper_already_deleted = (bool)g_type_get_qdata(G_TYPE_FROM_INSTANCE(object->instance), Gst::quark_cpp_wrapper_deleted_);
  if(gstmm_wrapper_already_deleted)
  {
    g_warning("Gst::wrap_create_new_wrapper: Attempted to create a 2nd C++ wrapper for a C instance whose C++ wrapper has been deleted.");
    return 0;
  }

  // Traverse upwards through the inheritance hierarchy
  // to find the most-specialized wrap_new() for this GType.
  //
  for(GType type = G_TYPE_FROM_INSTANCE(object->instance); type != 0; type = g_type_parent(type))
  {
    // Look up the wrap table index stored in the type's static data.
    // If a wrap_new() has been registered for the type then call it.
    //
    if(const gpointer idx = g_type_get_qdata(type, Gst::quark_))
    {
      const Gst:WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
      return (*func)(object);
    }
  }

  return 0;
}

static gboolean gtype_wraps_interface(GType implementer_type, GType interface_type)
{
  guint n_ifaces = 0;
  GType *ifaces = g_type_interfaces (implementer_type, &n_ifaces);

  gboolean found = FALSE;
  while (n_ifaces-- && !found)
  {
    found = (ifaces[n_ifaces] == interface_type);
  }
      
  g_free (ifaces);

  return found;
}

Gst::MinObject* wrap_create_new_wrapper_for_interface(GstMiniObject* object, GType interface_gtype)
{
  g_return_val_if_fail(wrap_func_table != 0, 0);
  g_return_val_if_fail (object != 0, 0);

  const bool gstmm_wrapper_already_deleted = (bool)g_object_get_qdata(G_TYPE_FROM_INSTANCE(object->instance), Glib::quark_cpp_wrapper_deleted_);
  if(gstmm_wrapper_already_deleted)
  {
    g_warning("Gst::wrap_create_new_wrapper: Attempted to create a 2nd C++ wrapper for a C instance whose C++ wrapper has been deleted.");
    return 0;
  }

  // Traverse upwards through the inheritance hierarchy
  // to find the most-specialized wrap_new() for this GType.
  //
  for(GType type = G_TYPE_FROM_INSTANCE(object->instance); type != 0; type = g_type_parent(type))
  {
    // Look up the wrap table index stored in the type's static data.
    // If a wrap_new() has been registered for the type then call it.
    // But only if the type implements the interface, 
    // so that the C++ instance is likely to inherit from the appropriate class too.
    //
    const gpointer idx = g_type_get_qdata(type, Glib::quark_);
    if(idx && gtype_wraps_interface(type, interface_gtype))
    {
      const Gst::WrapNewFunction func = (*wrap_func_table)[GPOINTER_TO_UINT(idx)];
      return (*func)(object);
    }
  }

  return 0;
}


// This is a factory function that converts any type to
// its C++ wrapper instance by looking up a wrap_new() function in a map.
//
MiniObject* wrap_auto(GstMiniObject* object, bool take_copy)
{
  if(!object)
    return 0;

  // Look up current C++ wrapper instance:
  GstMiniObject* pCppObject = GstMiniObject::_get_current_wrapper(object);

  if(!pCppObject)
  {
    // There's not already a wrapper: generate a new C++ instance.
    pCppObject = wrap_create_new_wrapper(object);

    if(!pCppObject)
    {
      g_warning("failed to wrap type of '%s'", g_type_name(G_TYPE_FROM_INSTANCE(object->instance)));
      return 0;
    }
  }

  // take_copy=true is used where the GTK+ function doesn't do
  // an extra ref for us, and always for plain struct members.
  if(take_copy)
    pCppObject->reference();

  return pCppObject;
}

Glib::RefPtr<MiniObject> wrap(GstMiniObject* object, bool take_copy /* = false */)
{
  return Glib::RefPtr<MiniObject>(dynamic_cast<MiniObject*>(wrap_auto(object, take_copy)));
}

GQuark quark_ = 0;
GQuark quark_cpp_wrapper_deleted_ = 0;


} /* namespace Gst */



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]