Re: Casting from a wrapped class to a possible implemented interface



Murray Cumming wrote:
On Sun, 2008-03-02 at 01:57 -0500, José Alburquerque wrote:
Is it possible to cast a class that in its declaration does not by default implement a certain interface and then cast it's RefPtr to the interface (assuming that the C object implements the interface)?

Generally, no.

In gstreamer, the GstImplementsInterface <http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstImplementsInterface.html> has functionality to test if an Element implements an interface (gst_element_implements_interface <http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstImplementsInterface.html#gst-element-implements-interface>) and to cast to that interface if necessary (gst_implements_interface_cast() <http://gstreamer.freedesktop.org/data/doc/gstreamer/head/gstreamer/html/GstImplementsInterface.html#gst-implements-interface-cast>) and I thought maybe this functionality can be used to cast from a wrapped Gst::Element to a RefPtr of the interface. Would this make
sense?

This would cause us to have two C++ instances with the same underlying C
instance. I wonder if that would have any bad side-effects. You would
probably have to avoid using Glib::wrap(), because it assumes a 1-to-1
mapping.

I think I may have found a way to do this, as you said, not using Glib::wrap(). It may be a bit intricate (and not ideal), but I think it may work. What I did is that I implemented a templated Gst::Element derived class (called ElementInterfaced) which implements the template argument (which would be the interface to be cast to). The Gst::Interface::cast() method (templated with the interface to be cast) creates a new templated Gst::ElementInterfaced class with the target interface and returns a RefPtr to the Gst::ElementInterfaced templated class. Working with the returned cast is successful so this is positive (see the test-create-element.cc for example).

My concern, however, is that because the newly created Gst::ElementInterfaced class is not "wrapped", it doesn't look like it will be destroyed when all references to the underlying gobject are gone. As a thought, I'm thinking that maybe the destruction of the underlying gobject might be "sensed" from the Gst::ElementInterfaced instance and then destroyed, but I'd have to look a bit into this. In the meantime, I've checked in what I've done so far. If it turns out not to be useful, it can be modified or reverted.

--
José Alburquerque
jaalburquerque cox net



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