473,421 Members | 1,714 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,421 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 2554
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.