[C++-sig] Wrapping a non-copyiable static instance in bp::object

Michael Wild themiwi at users.sourceforge.net
Sun Jan 20 19:48:46 CET 2013


I knew (hoped, really) there would be a simple solution!

Thanks, it works beautifully.

Michael

PS: Sorry, I posted earlier, but didn't notice that I got rejected
because I used the wrong sender address...

On 01/16/2013 10:29 PM, Wojciech Mamrak wrote:
> Hi,
> 
> bp::object static_instance()
> {
>   static bp::object* result = new bp::object(boost::ref(A::static_a));
>   return *result;
> }
> 
> Instead of exposing a function static_instance in your module, you can
> alternatively add a variable to your module's scope:
> 
> scope sc;
> sc.attr("static_a") = boost::ref(A::static_a); //no copy here
> 
> regards
> 
> 
> 2013/1/16 Michael Wild <themiwi at users.sourceforge.net>:
>> Dear all
>>
>> I struggle to find a way in which I can wrap a static instance in a
>> bp::object without copying it. The goal is to have object identity for
>> this static instance:
>>
>> struct A {
>>   /*...*/
>>   static A static_a;
>> };
>>
>> a A::a;
>>
>> bp::object static_instance()
>> {
>>   static bp::object* result = new bp::object(A::static_a); // [1]
>>   return *result;
>> }
>>
>> Wrapping A and static_instance with Boost.Python is no problem, and
>> everything works as expected, however the A::static_a that is being
>> passed to bp::object() at [1] is being copied, which defeats the whole
>> purpose.
>>
>> Anybody has an idea how achieve this? Thanks!
>>
>> Cheers
>>
>> Michael
> 



More information about the Cplusplus-sig mailing list