[C++-sig] problem w/intermodule communication
Neal Becker
ndbecker2 at gmail.com
Tue Nov 25 01:15:33 CET 2008
Here is a simple example.
---------------
test1.cc
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
using namespace boost::python;
struct A {
A (int _x) : x (_x) {}
int x;
};
BOOST_PYTHON_MODULE(test1) {
class_<A> ("A", init<int>());
}
----------------------
test2.cc
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
#include <boost/shared_ptr.hpp>
using namespace boost::python;
struct A {
A (int _x) : x (_x) {}
int x;
};
struct B {
B (boost::shared_ptr<A> _a) : a (_a) {}
boost::shared_ptr<A> a;
};
BOOST_PYTHON_MODULE(test2) {
class_<B> ("B", init<boost::shared_ptr<A> >())
.def_readonly ("a", &B::a)
;
}
-------------------------
test12.py
from test1 import A
from test2 import B
a = A(1)
b = B(a)
print b.a
---------------------
TypeError: No to_python (by-value) converter found for C++ type: boost::shared_ptr<A>
How can I solve this? Now consider:
------------------------
test3.cc
#include <boost/python/module.hpp>
#include <boost/python/def.hpp>
#include <boost/python/class.hpp>
#include <boost/python/init.hpp>
using namespace boost::python;
struct A {
A (int _x) : x (_x) {}
int x;
};
struct B {
B (boost::shared_ptr<A> _a) : a (_a) {}
boost::shared_ptr<A> a;
};
BOOST_PYTHON_MODULE(test3) {
class_<A> ("A", init<int>());
class_<B> ("B", init<boost::shared_ptr<A> >())
.def_readonly ("a", &B::a)
;
}
-------------------------
test13.py
from test3 import A, B
a = A(1)
b = B(a)
print b.a
---------------------------
<test3.A object at 0x2167208>
So if both are in same module, no problem. If they are in different modules it doesn't work. Any ideas?
More information about the Cplusplus-sig
mailing list