Wrapping a class whose constructor takes a pointer to an abstract class

View: New views
3 Messages — Rating Filter:   Alert me  

Wrapping a class whose constructor takes a pointer to an abstract class

by Jean-Sébastien Guay-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

I've just started trying to use boost.python on a personal project of
mine, and I decided to start with the hardest part :-)  I'm trying to
wrap OpenSceneGraph... It's going really well for the most part, and I
do mean really well. In little time I've got a good number of classes
working and tested. But I've hit a roadblock.

I've done a lot of searching and have found lots of posts on this list
dealing with pure virtual methods, but never anything about a method
taking a pointer to an abstract class as a parameter.

I have to wrap the following structure:

     class osg::Shape : public osg::Object
     {
         // has some pure virtual methods, none of which I need to
         // expose to python at this point
     };

     class osg::Sphere : public osg::Shape
     {
         // fully concrete
     };

     class osg::ShapeDrawable
     {
         ShapeDrawable(osg::Shape*, TessellationHints) { /* ... */ }
     };

So the constructor to ShapeDrawable takes a pointer to the abstract base
class. My attempt to wrap this (leaving out some details) is:

     // Abstract class, so noncopyable and no_init.
     class_<Shape, bases<Object>, ref_ptr<Shape>,
            boost::noncopyable >("Shape", no_init);

     // Concrete derived class.
     class_<Sphere, bases<Shape>, ref_ptr<Sphere> >("Sphere");

     // Details of this class not important for now.
     class_<TessellationHints, bases<Object>,
            ref_ptr<TessellationHints> >("TessellationHints");

     class_<ShapeDrawable, bases<Drawable>,
            ref_ptr<ShapeDrawable> >("ShapeDrawable")
         .def(init<Shape, TessellationHints>())
     ;

The first three classes wrap correctly, but the fourth fails with a
compiler error saying it cannot instantiate abstract class Shape. OK, I
understand that, but what can I do about it?

Thanks in advance for any help,

J-S
--
______________________________________________________
Jean-Sebastien Guay    jean-sebastien.guay@...
                                http://www.cm-labs.com/
                         http://whitestar02.webhop.org/
_______________________________________________
C++-sig mailing list
C++-sig@...
http://mail.python.org/mailman/listinfo/c++-sig

Re: Wrapping a class whose constructor takes a pointer to an abstract class

by David Abrahams-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


on Fri Sep 19 2008, Jean-Sébastien Guay <jean-sebastien.guay-AT-cm-labs.com> wrote:

>     class_<ShapeDrawable, bases<Drawable>,
>            ref_ptr<ShapeDrawable> >("ShapeDrawable")
>         .def(init<Shape, TessellationHints>())
>     ;
>
> The first three classes wrap correctly, but the fourth fails with a compiler
> error saying it cannot instantiate abstract class Shape. OK, I understand that,
> but what can I do about it?

I can't promise that this is enough to make everything work, but you
could start by making the init signature match your constructor
signature:

     class_<ShapeDrawable, bases<Drawable>,
            ref_ptr<ShapeDrawable> >("ShapeDrawable")
         .def(init<Shape*, TessellationHints>())
                        ^
here--------------------^

It will at least compile, then!

--
Dave Abrahams
BoostPro Computing
http://www.boostpro.com
_______________________________________________
C++-sig mailing list
C++-sig@...
http://mail.python.org/mailman/listinfo/c++-sig

Re: Wrapping a class whose constructor takes a pointer to an abstract class

by Jean-Sébastien Guay-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi David,

> I can't promise that this is enough to make everything work, but you
> could start by making the init signature match your constructor
> signature:
[...]
> It will at least compile, then!

Hmmm, you're right of course. The only thing I can say in my defense is
that I'm still confused as to when I need to use "python" syntax and
when I need to use "C++" syntax... I bet there are a lot more places
where I made the same mistake. :-)

Thanks a lot, and I'm sure I'll have many more such newbie questions in
the near future.

J-S
--
______________________________________________________
Jean-Sebastien Guay    jean-sebastien.guay@...
                                http://www.cm-labs.com/
                         http://whitestar02.webhop.org/
_______________________________________________
C++-sig mailing list
C++-sig@...
http://mail.python.org/mailman/listinfo/c++-sig
LightInTheBox - Buy quality products at wholesale price!