std::string by reference from C++ to Python
Hi, I would like to know what is the best way to pass a std::string by reference from C++ to Python. I know this should not work directly since string are immutable in Python. Let's say that I have this C++ base class: class State { virtual bool processEvent(const Event& in_event, std::string& out_strNextState); } And this C++ base class could be derived from Python, then Python would be able to decide what is the next state by setting out_strNextState function parameter. I know this could be much easier by changing the signature of the processEvent function (as retourning a std::string), but it's important for me to keep this signature. Thanks, Francis
On 3/28/07, Francis Moreau <francis_moreau@hotmail.com> wrote:
Hi,
I would like to know what is the best way to pass a std::string by reference from C++ to Python. I know this should not work directly since string are immutable in Python.
Let's say that I have this C++ base class:
class State { virtual bool processEvent(const Event& in_event, std::string& out_strNextState); }
And this C++ base class could be derived from Python, then Python would be able to decide what is the next state by setting out_strNextState function parameter. I know this could be much easier by changing the signature of the processEvent function (as retourning a std::string), but it's important for me to keep this signature.
Well you cannot do this, but you can find acceptable solution - Py++ :-) In general the solution to have different interfaces for C++ and Python code. I attached few files that shows you how you can achieve this. All relevant source code is in Py++ SVN. Here is the source code you need to write if you use Py++: from pyplusplus.module_builder import module_builder_t from pyplusplus import function_transformers as ft mb = module_builder_t( ... ) mb.mem_fun( 'processEvent' ).add_transformation( ft.out( 'out_strNextState' ) ) mb.write_module( your file name ) That's all. Here you will find list of transformations Py++ supports: http://language-binding.net/pyplusplus/documentation/functions/transformatio... HTH -- Roman Yakovenko C++ Python language binding http://www.language-binding.net/
on Wed Mar 28 2007, "Francis Moreau" <francis_moreau-AT-hotmail.com> wrote:
Hi,
I would like to know what is the best way to pass a std::string by reference from C++ to Python. I know this should not work directly since string are immutable in Python.
Let's say that I have this C++ base class:
class State { virtual bool processEvent(const Event& in_event, std::string& out_strNextState); }
And this C++ base class could be derived from Python, then Python would be able to decide what is the next state by setting out_strNextState function parameter. I know this could be much easier by changing the signature of the processEvent function (as retourning a std::string), but it's important for me to keep this signature.
You can declare a C++ class: struct mystring { std::string x; }; Then expose mystring to class as usual and then register an lvalue from_python converter to std::string&. Naturally, in that case, a wrapped mystring instance is not a Python string. -- Dave Abrahams Boost Consulting www.boost-consulting.com Don't Miss BoostCon 2007! ==> http://www.boostcon.com
participants (3)
-
David Abrahams -
Francis Moreau -
Roman Yakovenko