[C++-sig] std::string by reference from C++ to Python

Roman Yakovenko roman.yakovenko at gmail.com
Wed Mar 28 23:15:17 CEST 2007


On 3/28/07, 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.


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/transformation/built_in/built_in.html

HTH
-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070328/55aaac93/attachment.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: function_transformations_to_be_exported.hpp
Type: text/x-c++hdr
Size: 562 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070328/55aaac93/attachment.hpp>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: function_transformations.cpp
Type: text/x-c++src
Size: 2285 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20070328/55aaac93/attachment.cpp>


More information about the Cplusplus-sig mailing list