[C++-sig] take ownership in a call policy

Renato Araujo renatox at gmail.com
Sat Dec 13 03:52:37 CET 2008


Thanks David I will try explain more about my problems with ownership:

I already expose this problem before here the ideas not fit yet.


This is my problem.
attention: I can't change the c++ code;


struct object
{

    object(object *parent=0)
    {
       //if (parent): append myself in parent child list (parent take
ownership of object, parent will delete this object)
    }

    void set_parent(object *parent)
    {
       //the same case of constructor: if parent is null remove old parent
    }

    void add_child(object *child)
    {
       //add child in my children list get ownership
    }

    void remove_child(object *child)
    {
       //remove child from my list, give back the ownership to the child object
    }

    ~object()
    { // destroy all children; }

private:
     list<object*> children;
}

this is a simple example how c++ code works, we have allot of others
functions to get ownership of a object, then is impossible rewrite all
functions like in the boost python FAQ example:
(http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#ownership)

I thinking about create a call policy to do this for me. One for take
ownership and another to restore ownership.

I'm trying do something like that, I don't know if this is the best
idea, sugestions are welcome:

class<object_wrapper>("object", init< optional<object*>
>()[parent_child_policy_add<2,1>()])
	.def("set_parent", &object::set_parent,
parent_child_policy_add<2,1>()) //this case (index 2 = arg1) = parent
object, (index 1 = self) = child object
        .def("remove_child", &object::remove_child,
parent_child_policy_remove<1,2>() //this case self is the parent and
arg1 is the child


I know how implement a call policy and get access to PyObject from
child and parent. What I don't know is how inform to boost to not
delete c++ object during the python object destructor (in
parent_child_policy_add) because this will be done by the parent, or
inform to boost to delete the c++ object (in
parent_child_policy_remove) because the object no have parent.


On Fri, Dec 12, 2008 at 8:08 PM, David Abrahams <dave at boostpro.com> wrote:
>
> on Fri Dec 12 2008, "Renato Araujo" <renatox-AT-gmail.com> wrote:
>
>> Hi guys,
>>
>> I'm creating a 2 new call_policy to my functions where I need take and
>> give back the ownership of my object. I would like reproduce this in
>> my precall or postcall function policy functions.
>>
>> http://www.boost.org/doc/libs/1_36_0/libs/python/doc/v2/faq.html#ownership
>>
>> How I can do this? How I can get a ref to  auto_ptr or shared_ptr from
>> my PyObject?
>
> I'm sorry, I don't understand the question.  Could you please show the
> function signature you're trying to wrap and describe the ownership
> semantics required?
>
> Thanks,
>
> --
> Dave Abrahams
> BoostPro Computing
> http://www.boostpro.com
> _______________________________________________
> Cplusplus-sig mailing list
> Cplusplus-sig at python.org
> http://mail.python.org/mailman/listinfo/cplusplus-sig
>



-- 
Renato Araujo Oliveira Filho


More information about the Cplusplus-sig mailing list