471,897 Members | 1,850 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,897 software developers and data experts.

boost::python> exposing instances from c++

I'm writing a game in C++ which calls out to python for scripting. I'd like to
expose the instance of my Game class (a singleton) to python (so that the
instances of Clock, Camera, etc are available to the scripts).

I ran into trouble trying to expose a specific instance (or even a function
returning the instance). Here is a simplification of my problem in code (c++):

<code>
class A{
public:
int i;
};
class B{
public:
A a;
};
B b;
B getb() {return b;} //return the instance

int main() {
getb().a.i = 42;
return 0;}
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
class_<A>("A",init<>())
.def("i", &A::i);
class_<B>("B",init<>())
.def("a", &B::a);
def("getb", getb);
}
</code>

Of course, I would be calling any python script using these classes from within
main().

This doesn't compile (using bjam with msvc). If I return by reference or
pointers in getb it gets ugly (like pages and pages of error messages). Am I
doing this the right way? How do I expose an instance to python?
Jul 18 '05 #1
1 2417
Dear Mr. Handheld Vacuum,

You will probably get better answers to your Boost.Python questions
if you post them to the C++-sig. See
http://www.boost.org/more/mailing_lists.htm#Cplussig.

th**********@aol.com (TheDustbustr) writes:
I'm writing a game in C++ which calls out to python for scripting. I'd like to
expose the instance of my Game class (a singleton) to python (so that the
instances of Clock, Camera, etc are available to the scripts).

I ran into trouble trying to expose a specific instance (or even a function
returning the instance). Here is a simplification of my problem in code (c++):

<code>
class A{
public:
int i;
};
class B{
public:
A a;
};
B b;
B getb() {return b;} //return the instance

int main() {
getb().a.i = 42;
return 0;}
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/def.hpp>
using namespace boost::python;
BOOST_PYTHON_MODULE(hello)
{
class_<A>("A",init<>())
.def("i", &A::i);
class_<B>("B",init<>())
.def("a", &B::a);
def("getb", getb);
}
</code>

Of course, I would be calling any python script using these classes from within
main().

This doesn't compile (using bjam with msvc). If I return by reference or
pointers in getb it gets ugly (like pages and pages of error messages). Am I
doing this the right way? How do I expose an instance to python?


--
Dave Abrahams
Boost Consulting
www.boost-consulting.com
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.