Skip to main content
gamedev.net
🔒 Locked

questions about boost.python

Started by Tonni Oct 31, 2006 at 8:50 AM 0 replies 1.2k views
Original Post
Tonni
Tonni
hi, i would like to embed a scripting language in my (planned) c++ apps. i've found boost.python now, and it seems to be what i need. nevertheless, i'm still unsure whether i can do the follwoing with boost.python: (note that the python-related part is just pseudo-code :> )

class someNumber
{
public:
  float x;
 
  someNumber() : x(0) {}
};
class myClass
{
public:
  int a;
  someNumber* number;
  
  myClass() : a(0) {number=new someNumber();}
  ~myClass(){delete number;}
  
  void add(const int x) {a+=x;}
};

void printHelloPython()
{
  std::cout<<"hello python";
}

int main()
{
  myClass foobar();

  revealFunctionToPython(&printHelloPython);
  revealClassToPython("someNumber");
  revealClassToPython("myClass");
  revealInstanceToPython(&foobar);
  runThatPythonScrip();

  return foobar.a+foobar.number.x;
}

Where the python code sets a to 5 directly (something like foobar.a=5), calls printHelloPython(), sets the someNumber instance inside foobar to 1330 and then calls foobar.add(2). I'd expect an output of "hello python" and the returned result from main to be 1337. so, can i manipulate the c++ instance from within a python script? if i can't, is there a scripting language that i can embed in my application that is able to do the job?
mikeman
mikeman
Yes, of course it can. Because you're passing around pointers though, you need to do 2 things(at least to my knowledge):

1)When calling a Python function passsing a C++ pointer as a parameter, normally the pointee gets copied. You need to use ptr(&foobar) to wrap it up so the Python function will operate on foobar and not on a copy of it.
2)Declare the pointer "number" as private, and have a number() function that returns the pointer, informing python that it is an internal reference.

Here's how the C++ code would look:

#include <iostream>#include <boost/python.hpp>#include <boost/python/ptr.hpp>#include <string>class someNumber{public:  float x;   someNumber() : x(0) {}};class myClass{private:  someNumber* _number;public:  int a;          myClass() : a(0) {_number=new someNumber();}  ~myClass(){delete _number;}    void add(const int x) {a+=x;}  someNumber* number(){return _number;}};void printHelloPython(){	std::cout<<"hello python"<<std::endl;}//Expose C++ classes and functions to Pythonusing namespace boost::python;BOOST_PYTHON_MODULE(cpp_module){	class_<someNumber>("someNumber")        		.def_readwrite("x",&someNumber::x);	class_<myClass>("myClass")		.def_readwrite("a",&myClass::a)						.def("add",&myClass::add)		.def("number", &myClass::number, return_internal_reference <> ());	def("printHelloPython",&printHelloPython);}void InitInterpreter(){	try	{		Py_Initialize();	}	catch(error_already_set)	{		PyErr_Print();		throw;	}}void FinalizeInterpreter(){	Py_Finalize();	}object ImportModule(std::string name){	object _name=str(name.c_str());	object module=object(handle<>(borrowed(PyImport_Import(_name.ptr())))); 		return module;}int _tmain(int argc, _TCHAR* argv[]){	InitInterpreter();	       	initcpp_module();	myClass foobar;//create the instance		object script=ImportModule("script");//import the script module(a file named "script.py")	object func=script.attr("script_func");//obtain the function		func(ptr(&foobar));	//call the python function,passing the foobar as a parameter	int result=foobar.a+foobar.number()->x;	std::cout<<"The result returned by main is: "<<result<<std::endl;			FinalizeInterpreter();	return 0;}


The script is contained in a file called "script.py" which is in the same folder as the .exe:

script.py:
import cpp_moduledef script_func(foobar):	foobar.a=5	cpp_module.printHelloPython();	foobar.number().x=1330	foobar.add(2)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.