473,395 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 2552
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Gouda Man | last post by:
I'm planning to write a large program that will have advanced scripting in python in which the scripter will subclass c++ objects to add functionality. this leads me to boost.python (if you dont...
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
0
by: Stodge | last post by:
Hi folks, new to Boost Python and struggling to build a prototype at work. I thought I'd start with a conceptual question to help clarify my understanding. I already have a basic prototype working...
5
by: Stodge | last post by:
I've exposed a C++ class to Python using Boost Python. The class, let's say it's called Entity, contains private static data, which is an array of strings. Though I think it implements it using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.