For a BPL wrapped object to be a python string, it simply needs to 
provide the same API as a Python string.

Replicate the magic underscore methods of python's string object and 
your wrapped std::string becomes a python string.

It used to be the case that inheriting your BPL object from python's 
string was problematic. I'd assume recent Pythons have fixed that.

Niall 

On 25 Dec 2012 at 14:22, simon zhang wrote:

> If Jim is right,string's memory allocated is different in python and
> c++.However,before python
> handle some strings from c++,it must convert string from c++'s memory to
> python's memory.
> 
> So smart pointers have not changed anything.It must keep a separate copy of
> a string in c++ memory and python memory.
> 
> My idea.In the future, I will be write a wrapped class. When the c++
> interface, it store the data by std :: string.When the python
> interface,it store the data by  the Python str object. And perhaps this is
> a good way.
> 
> 
> 2012/12/25 Niall Douglas <s_sourcefo...@nedprod.com>
> 
> > Traditionally, the proper solution to avoid deep copies is to wrap a
> > std::shared_ptr<std::string> instead of a std::string directly.
> >
> > If your strings are short of course, it may well be faster to leave
> > it as is. std::shared_ptr is not lightweight and may use atomic
> > instructions, the bandwidth for which in a system is always limited.
> >
> > Niall
> >
> > On 23 Dec 2012 at 16:16, simon zhang wrote:
> >
> > > ..so,It seems I can only convert std::string and make a deep copy.I can
> > > only avoid it as much as possible.Thank you.
> > >
> > >
> > > 2012/12/22 Jim Bosch <tallji...@gmail.com>
> > >
> > > > On 12/21/2012 03:52 AM, simon zhang wrote:
> > > >
> > > >> How to converter std::string* in boost.python?I have to handle some
> > data
> > > >> of c++ in python.The data may be big.So I return a pointer to python.
> > > >> But there are some errors.
> > > >>
> > > >>
> > > > If the data is big, and you really want to avoid a deep copy, the only
> > way
> > > > to use it is if you manually allocate the memory as a Python str object
> > > > using the Python C API or return it as something else (like a NumPy
> > array
> > > > or buffer object, or a custom Boost.Python-wrapped class) that can hold
> > > > C++-allocated memory.  A Python str object always owns its own memory,
> > and
> > > > so does std::string, so you can't convert from one to the other
> > without a
> > > > deep copy.
> > > >
> > > > Jim
> > > >
> > > > ______________________________**_________________
> > > > Cplusplus-sig mailing list
> > > > Cplusplus-sig@python.org
> > > > http://mail.python.org/**mailman/listinfo/cplusplus-sig<
> > http://mail.python.org/mailman/listinfo/cplusplus-sig>
> > > >
> > >
> >
> >
> > --
> > Any opinions or advice expressed here do NOT reflect those
> > of my employer Research In Motion Inc.
> > Work Portfolio: http://careers.stackoverflow.com/nialldouglas/
> >
> >
> >
> >
> > _______________________________________________
> > Cplusplus-sig mailing list
> > Cplusplus-sig@python.org
> > http://mail.python.org/mailman/listinfo/cplusplus-sig
> >
> 


-- 
Any opinions or advice expressed here do NOT reflect those
of my employer Research In Motion Inc.
Work Portfolio: http://careers.stackoverflow.com/nialldouglas/



Attachment: SMime.p7s
Description: S/MIME cryptographic signature

_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig

Reply via email to