Boost logo

Boost Users :

Subject: [Boost-users] [Boost.Python] Exposing protected virtual member functions
From: Vitaly Budovski (vbudovski+news_at_[hidden])
Date: 2013-11-09 06:33:32


Hi,

I'd like to expose some protected virtual member functions to Python, but
I've been unable to find a practical way to do so.
I've created a wrapper making the protected functions public, but have not
been able to make further progress due to either compile errors or Python
runtime exceptions.
Is there something I've overlooked in the documentation? Please see code
below.

Thanks,

Vitaly

#include <boost/python.hpp>
#include <string>
using namespace boost::python;

class base
{
//protected:
public:
    virtual std::string f()
    {
        return "base::f";
    }
public:
    virtual std::string g() = 0;
};

class derived: public base
{
public:
    std::string g()
    {
        return "derived::g";
    }
};

struct base_wrapper: base, wrapper<base>
{
    std::string f()
    {
        if(override f = this->get_override("f"))
        {
            return f();
        }
        return base::f();
    }
    std::string default_f()
    {
        return base::f();
    }
    std::string g()
    {
        return this->get_override("g")();
    }
};

BOOST_PYTHON_MODULE(protected)
{
    class_<base_wrapper, boost::noncopyable>("base")
        .def("f", &base::f, &base_wrapper::default_f)
        .def("g", pure_virtual(&base_wrapper::g))
    ;

    class_<derived, bases<base>, boost::noncopyable>("derived")
        .def("g", &derived::g)
    ;
}



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net