[C++-sig] exposing pointers using boost
Roman Yakovenko
roman.yakovenko at gmail.com
Tue Sep 4 14:03:58 CEST 2007
On 9/4/07, Furkan Kuru <furkankuru at gmail.com> wrote:
>
> Hello,
>
> Is there a way to expose structs containing pointers of their types?
There is another problem here
Let's say, I have a very simple doubly-linked list.
>
> struct Node {
> Node* next;
> Node* prev;
> int value;
> };
>
> I expose it like this:
>
> class_<Node>("Node")
> .def_readwrite("value", &Node::value)
> .def_readwrite("next", &Node::next)
> .def_readwrite("prev", &Node::prev);
>
> When I try to access the next and prev attributes of a Node instance in
> python,
> I get this error:
>
> TypeError: No to_python (by-value) converter found for C++ type: struct
> Node *
If you would have access to Py++, it would generate the right code for you.
Here is cut and paste of the relevant code from the unittests:
struct tree_node_t{
...
tree_node_t *left;
...
};
struct tree_node_t_wrapper : tree_node_t, bp::wrapper< tree_node_t > {
...
static tree_node_t * get_right(tree_node_t const & inst ){
return inst.right;
}
static void set_right( tree_node_t & inst, tree_node_t * new_value ){
inst.right = new_value;
}
};
{ //::tree_node_t
typedef bp::class_< tree_node_t_wrapper > tree_node_t_exposer_t;
tree_node_t_exposer_t tree_node_t_exposer = tree_node_t_exposer_t(
"tree_node_t", "documentation", bp::init< bp::optional<
member_variables::pointers::tree_node_t const * > >((
bp::arg("parent")=bp::object() ), "documentation") );
bp::scope tree_node_t_scope( tree_node_t_exposer );
tree_node_t_exposer.add_property( "right"
, bp::make_function( (::tree_node_t * (*)( ::tree_node_t
const & ))(&tree_node_t_wrapper::get_right), bp::return_internal_reference<
>() )
, bp::make_function( (void (*)( ::tree_node_t
&,::tree_node_t * ))(&tree_node_t_wrapper::set_right),
bp::with_custodian_and_ward_postcall< 1, 2 >() ));
}
Pay attention to used call policies. They are very important.
HTH
--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070904/82c6b682/attachment.htm>
More information about the Cplusplus-sig
mailing list