[C++-sig] beginner : call_policies

Benjamin Golinvaux Benjamin.Golinvaux at euresys.com
Thu Feb 12 12:35:26 CET 2004

Dear list members,

I need to wrap a C++ tree structure in Python, and
I'm not sure of the correct call policies to use (i'm
not even sure they can be applied to my particular 

An node of the tree holds pointers to :

- its parent
- its children

(only pointers are involved)

I have pasted the class def. and impl. at the end 
of this message.

operations are provided to attach a tree node to a 
parent, and to detach it. 

note : usually the tree nodes do not own their 
children (they  never delete them), but i might 
provide a way to tag SOME children  as 'owned' by 
their parent. I will need to express this across 
the python/cpp boundary, too...

I would like to create wrappers for this class, that 
correctly handle the various objects lifetimes.

So basically i would like an item to hold a ref 
to its children, and maybe a weak ref to its parent (?)

I have understood how to use "with_custodian_and_ward" 
to have the TreeNode::Attach(TreeNode* parent) function 
wrapped so that the node is guaranteed to live at least 
as long as its parent (== parent holding a ref ?), but 
i haven't understood how to perform the INVERSE 
operation, that is, "release" the item reference the 
parent holds when TreeNode::DetachFrom(TreeNode* parent) 
is called.

Is this possible at all ? I have read the FAQ entry called 
"how to transfer ownership across a function boundary" 
but i must admit i don't see how it fits in my problem.

I somehow feel uncomfortable being unable to control 
the way refcounts are inc'd and dec'd but i prefer 
trusting boost controlling that than myself !

in short, i would like my structure to work just like a 
python container wrt its items (if i understood things 

Thanks for taking the time to read my message.

Benjamin Golinvaux
Euresys s.a.

The code is as follows :

struct TreeNode
  typedef std::list<TreeNode*>::const_iterator ConstItor;

  TreeNode*             _parent;
  std::list<TreeNode*>  _children;

  TreeNode( ) : _parent(0) {};

  void Attach(TreeNode* parent)
    if(parent == _parent) return;
    if(_parent) Detach(_parent);
    _parent = parent;

  bool HasChild(TreeNode* node) const
     for(ConstItor itor = _children.begin(); itor != _children.end();
       if(*itor == node)
         return true;
     return false;

  void Detach(TreeNode* parent)
      throw Error_TreeNode_WrongParentSpecified();

    parent = 0;

private : // prevent generation of bad default code
  TreeNode(const TreeNode& treeNode);
  TreeNode& operator=(const TreeNode& treeNode);
 The information contained in this message or any of its attachments may be privileged and confidential and intended for the exclusive use of the addressee. If you are not the addressee any disclosure, reproduction, distribution or other dissemination or use of this communication is strictly prohibited. If you have received this transmission by error please notify the sender immediately and then delete this email.  EURESYS shall not be liable for any loss of or damage to revenues, profits, goodwill, data, information systems or other special, incidental, indirect, consequential or punitive damages of any kind arising in connection with the use of information contained in this mail or its attachments.  Email transmission cannot be guaranteed to be secure or error free as information could be intercepted, corrupted, lost, destroyed, arrive late or incomplete, or contain viruses.  The sender therefore is in no way liable for any errors or omissions in the content of this message, which may arise as a result of email transmission. If verification is required, please request a hard copy.  Please note that neither EURESYS, nor the sender accepts any responsibility for viruses and it is your responsibility to scan attachments (if any). 

More information about the Cplusplus-sig mailing list