1 /** 2 Mirror _weakrefobject.h 3 4 Weak references objects for Python. 5 */ 6 module deimos.python.weakrefobject; 7 8 import deimos.python.pyport; 9 import deimos.python.object; 10 11 extern(C): 12 // Python-header-file: Include/weakrefobject.h: 13 14 /** PyWeakReference is the base struct for the Python ReferenceType, ProxyType, 15 * and CallableProxyType. 16 * 17 * subclass of PyObject 18 */ 19 struct PyWeakReference { 20 mixin PyObject_HEAD; 21 22 /**The object to which this is a weak reference, or Py_None if none. 23 * Note that this is a stealth reference: wr_object's refcount is 24 * not incremented to reflect this pointer. 25 */ 26 PyObject* wr_object; 27 /** A callable to invoke when wr_object dies, or NULL if none. */ 28 PyObject* wr_callback; 29 /** A cache for wr_object's hash code. As usual for hashes, this is -1 30 * if the hash code isn't known yet. 31 */ 32 version(Python_3_2_Or_Later) { 33 Py_hash_t hash; 34 }else{ 35 C_long hash; 36 } 37 /** If wr_object is weakly referenced, wr_object has a doubly-linked NULL- 38 * terminated list of weak references to it. These are the list pointers. 39 * If wr_object goes away, wr_object is set to Py_None, and these pointers 40 * have no meaning then. 41 */ 42 PyWeakReference* wr_prev; 43 /// ditto 44 PyWeakReference* wr_next; 45 } 46 47 /// _ 48 mixin(PyAPI_DATA!"PyTypeObject _PyWeakref_RefType"); 49 /// _ 50 mixin(PyAPI_DATA!"PyTypeObject _PyWeakref_ProxyType"); 51 /// _ 52 mixin(PyAPI_DATA!"PyTypeObject _PyWeakref_CallableProxyType"); 53 54 // D translations of C macros: 55 /// _ 56 int PyWeakref_CheckRef()(PyObject* op) { 57 return PyObject_TypeCheck(op, &_PyWeakref_RefType); 58 } 59 /// _ 60 int PyWeakref_CheckRefExact()(PyObject* op) { 61 return Py_TYPE(op) == &_PyWeakref_RefType; 62 } 63 /// _ 64 int PyWeakref_CheckProxy()(PyObject* op) { 65 return Py_TYPE(op) == &_PyWeakref_ProxyType 66 || Py_TYPE(op) == &_PyWeakref_CallableProxyType; 67 } 68 /** This macro calls PyWeakref_CheckRef() last since that can involve a 69 function call; this makes it more likely that the function call 70 will be avoided. */ 71 int PyWeakref_Check()(PyObject* op) { 72 return PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op); 73 } 74 75 /// _ 76 PyObject* PyWeakref_NewRef(PyObject* ob, PyObject* callback); 77 /// _ 78 PyObject* PyWeakref_NewProxy(PyObject* ob, PyObject* callback); 79 /// _ 80 PyObject_BorrowedRef* PyWeakref_GetObject(PyObject* _ref); 81 82 version(Python_2_5_Or_Later){ 83 /// _ 84 Py_ssize_t _PyWeakref_GetWeakrefCount(PyWeakReference* head); 85 }else{ 86 /// _ 87 C_long _PyWeakref_GetWeakrefCount(PyWeakReference *head); 88 } 89 /// _ 90 void _PyWeakref_ClearRef(PyWeakReference *self); 91 92 /// _ 93 PyObject_BorrowedRef* PyWeakref_GET_OBJECT()(PyObject* _ref) { 94 return (cast(PyWeakReference *) _ref).wr_object; 95 } 96