473,785 Members | 2,842 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_MO DULE(hello)
{
class_<A>("A",i nit<>())
.def("i", &A::i);
class_<B>("B",i nit<>())
.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 2583
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**********@ao l.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_MO DULE(hello)
{
class_<A>("A",i nit<>())
.def("i", &A::i);
class_<B>("B",i nit<>())
.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
1219
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 already know about boost.python dont bother reading farther cause its a kind of a technical question) anyways i embeded python in a litte c++ test app i wrote and got it working fine. eg: i could write a little python program and my app would load...
0
1778
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 implementations of a member function of A that takes as an argument the abstract class Base (see Option 1 / Option 2): - Both compile without problems - Only the second works in python (see python program below).
0
1311
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 nicely but I'm having a few issues, which I may post about later. A brief functional rundown of what I'm trying to prototype. Hopefully my explanation doesn't get too confusing! I'm embedding a python module into an application; Python will...
5
1982
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 MFC's CPtrArray. I've also exposed a public function from Entity - let's say it's called foo. This function accesses the private static data (the string array). I have multiple instances of Entity stored in a custom C++ container,
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10151
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4053
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3647
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.