[C++-sig] beginner : call_policies

Benjamin Golinvaux Benjamin.Golinvaux at euresys.com
Mon Feb 16 09:13:33 CET 2004


Dear list members,

I would like to know if it's possible to write my own 
call policies, in order to be able to handle the reference
counting of objets dynamically, based on some return values
of my wrapped C++ functions. For example

struct A {};

struct B 
{
	bool foo(A& someA);
	A _a;
}

where i would like B::foo(someA) to increment someA reference count
if it returns "true", and don't do anything otherwise.
(so basically i would like to tie someA's lifetime to the instance
of B ONLY IF the function returns "true".

is this possible with some precall/postcall magic ?

Thank you.

Benjamin Golinvaux
Euresys s.a.
www.euresys.com



 

-----Original Message-----
From: c++-sig-bounces at python.org [mailto:c++-sig-bounces at python.org] On Behalf Of Benjamin Golinvaux
Sent: jeudi 12 février 2004 12:35
To: Development of Python/C++ integration
Cc: Pierre Joskin; Philippe Latour
Subject: [C++-sig] beginner : call_policies


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 case).

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 correctly).

Thanks for taking the time to read my message.

Benjamin Golinvaux
Euresys s.a.
www.euresys.com


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;
    parent->_children.push_back(this);
  }

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

  void Detach(TreeNode* parent)
  {
    if(!parent->HasChild(this))
      throw Error_TreeNode_WrongParentSpecified();

    parent->_children.remove(this);
    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). 
  
--------------------------------------------------------

_______________________________________________
C++-sig mailing list
C++-sig at python.org
http://mail.python.org/mailman/listinfo/c++-sig 
--------------------------------------------------------
 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