[C++-sig] Passing inherited classes back to C++
Roman Yakovenko
roman.yakovenko at gmail.com
Sat May 5 20:55:42 CEST 2007
On 5/5/07, Tom Brooks <tgbrooks at gmail.com> wrote:
> I currently have these C++ classes:
>
> class Renderable
> {
> public:
> Renderable();
> virtual ~Renderable();
> virtual void Draw() = 0;
> };
>
> class Quad : public Renderable
> {
> public:
> Quad(float x, float y, float h, float w);
> ~Quad();
> void Draw();
> };
>
> And I need to expose them to Python. Then, I need to be able to make one in
> Python, and pass it back to a C++ function as a Renderable*.
>
> I made this wrapper class for Renderable:
>
> struct RenderableWrap : Renderable, wrapper<Renderable>
> {
> public:
> void Draw()
> {
> this->get_override("Draw")();
> }
> };
>
> And I have this Boost.Python code for the Python side:
>
> class_<RenderableWrap,
> boost::noncopyable>("Renderable")
> .def("Draw",pure_virtual(&Renderable::Draw))
> ;
>
> class_<Quad, bases<RenderableWrap> >("Quad",init<float, float, float,
Quad derives from Renderable, not RenderableWrap
> float>())
> .def("Draw",&Quad::Draw)
> ;
>
> But when I pass in a Quad() to my C++ function, I get an error that it can't
> convert from a Quad to a Renderable*. I have next to no clue how the base
> class should have been done, since the tutorial and anything else I can find
> online don't go over what to do for the base class (when there are virtual
> functions, at least). For example, I don't know if I inherit from
> Renderable, or RenderableWrap (or both?). I don't know whether the base
> class needs a wrapper itself (and would it if it had a virtual function?).
>
> I'd appreciate any help!
Next code was generated by Py++ GUI
(http://language-binding.net/pyplusplus/documentation/tutorials/pyplusplus_gui.html)
#include "boost/python.hpp"
#include "1.h"
namespace bp = boost::python;
struct Renderable_wrapper : Renderable, bp::wrapper< Renderable > {
Renderable_wrapper( )
: Renderable( )
, bp::wrapper< Renderable >(){
// null constructor
}
virtual void Draw( ){
bp::override func_Draw = this->get_override( "Draw" );
func_Draw( );
}
};
struct Quad_wrapper : Quad, bp::wrapper< Quad > {
Quad_wrapper(Quad const & arg )
: Quad( arg )
, bp::wrapper< Quad >(){
// copy constructor
}
Quad_wrapper(float x, float y, float h, float w )
: Quad( x, y, h, w )
, bp::wrapper< Quad >(){
// constructor
}
virtual void Draw( ) {
if( bp::override func_Draw = this->get_override( "Draw" ) )
func_Draw( );
else
this->Quad::Draw( );
}
void default_Draw( ) {
Quad::Draw( );
}
};
BOOST_PYTHON_MODULE(pyplusplus){
bp::class_< Renderable_wrapper, boost::noncopyable >( "Renderable" )
.def( bp::init< >() )
.def(
"Draw"
, bp::pure_virtual( &::Renderable::Draw ) );
bp::class_< Quad_wrapper, bp::bases< Renderable > >( "Quad",
bp::init< float, float, float, float >(( bp::arg("x"), bp::arg("y"),
bp::arg("h"), bp::arg("w") )) )
.def(
"Draw"
, &::Quad::Draw
, &Quad_wrapper::default_Draw );
}
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
More information about the Cplusplus-sig
mailing list