[C++-sig] Problem with inheritance
Håkon Groven
hgroven at emgs.com
Fri Jul 27 07:39:50 CEST 2007
Hi!
In one of my cpp-functions I return a vector with items of type
channel*.
My channel class is extended by some channel_xxx classes.
The instances returned in the vector are actually of type channel_xxx*.
In Python I do however only get objects of the super type.
One of my other functions return a channel* and here I am able to get
objects of the subclasses in Python.
I'd be glad if anyone could help!
I have pasted parts of my wrapper code below.
The two interesting functions are:
1) get_available_channels (inheritance does not work for the returned
value)
2) get_channel (inheritance do work for the returned value)
Code:
// Boost Includes
============================================================
#include <boost/python.hpp>
#include <boost/cstdint.hpp>
// Includes
==================================================================
#include "../include/channel.h"
#include "../include/channel_group.h"
.
.
.
// Using
=====================================================================
using namespace boost::python;
// Declarations
==============================================================
namespace {
template < typename ContainerType>
struct to_tuple
{
static PyObject* convert ( ContainerType const& a)
{
using boost::python::incref;
using boost::python::list;
list result;
for(std::size_t i=0;i<a.size();i++) {
result.append(object(a[i]));
}
return incref(tuple(result).ptr());
}
};
template < typename T >
struct std_vector_to_tuple
{
std_vector_to_tuple ()
{
to_python_converter < std::vector < T >,
to_tuple < std::vector < T > > > ();
}
};
}
// Module
====================================================================
BOOST_PYTHON_MODULE(pyelio)
{
std_vector_to_tuple < std::string > ();
std_vector_to_tuple < float > ();
std_vector_to_tuple < channel_group* > ();
std_vector_to_tuple < channel* > ();
std_vector_to_tuple < sample* > ();
std_vector_to_tuple < std::complex<float> > ();
class_< channel_group >("channel_group", init< const channel_group& >())
.def(init< boost::shared_ptr<elio>, channel_group_type >())
.def("get_available_channels", &channel_group::get_available_channels)
.def("get_channel", (channel* (channel_group::*)(channel_type) const)
&channel_group::get_channel,
return_value_policy<reference_existing_object>())
.def("get_channel", (channel* (channel_group::*)(channel_type, int)
const)
&channel_group::get_channel,
return_value_policy<reference_existing_object>())
.def("get_type", &channel_group::get_type)
;
class_< channel >("channel", init< >())
.def(init< const channel& >())
.def("init", (void (channel::*)(boost::shared_ptr<elio>,
channel_group_type, channel_type, int) )
&channel::init)
.def("init", (void (channel::*)(boost::shared_ptr<elio>,
channel_group_type,
channel_type, int, int) )&channel::init)
;
class_< channel_emf, bases< channel > >("channel_emf", init< >())
.def(init< const channel_emf& >())
;
}
Best wishes Hakon
More information about the Cplusplus-sig
mailing list