简体   繁体   中英

Extending an embedded python interpreter with global instance of a c++ object

I have a class

class A {
  A(SomeClass* ptr);
  do_something();
};

Which I want to use in an embedded python interpreter using boost-python

I have gotten so far that I have managed to create an python module through BOOST_PYTHON_MODULE and created a class_<A> with an constructor that accept a SomeClass pointer.

Now I want extend the interpreter so that there is an instance of this class (named an_a ) whenever some python code is invoked so that the following python code is valid:

#preferably no imports here.
an_a.do_something()

My problem is two fold, I need to construct this object either in python or in C++ before the interpreter is used, and I need to make the object available for the writer of the script. I am having some problems finding exactly how to do this in the documentation I can find.

I recently needed to do this exact thing. I also considered (and used) Boost.Python but personally (as much as I love Boost) I find it overkill to have to drag in half the Boost library to get this one feature.

So, if you're interested, I recently implemented embedded Python scripting for C++ from first principles in a Python wrapper library called ECS:Python. ECS:Python (Embedded C++ Scripting with Python) is designed specifically for C++ developers that wish to expose objects from a C++ application to an embedded Python interpreter for interactive scripting.

Its free (BSD) and open source: http://sourceforge.net/projects/ecspython

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM