weakrefobject.h 2.37 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
/* Weak references objects for Python. */

#ifndef Py_WEAKREFOBJECT_H
#define Py_WEAKREFOBJECT_H
#ifdef __cplusplus
extern "C" {
#endif


typedef struct _PyWeakReference PyWeakReference;

12 13 14
/* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
 * and CallableProxyType.
 */
15 16
struct _PyWeakReference {
    PyObject_HEAD
17 18 19 20 21

    /* The object to which this is a weak reference, or Py_None if none.
     * Note that this is a stealth reference:  wr_object's refcount is
     * not incremented to reflect this pointer.
     */
22
    PyObject *wr_object;
23 24

    /* A callable to invoke when wr_object dies, or NULL if none. */
25
    PyObject *wr_callback;
26 27 28 29

    /* A cache for wr_object's hash code.  As usual for hashes, this is -1
     * if the hash code isn't known yet.
     */
30
    long hash;
31 32 33 34 35 36

    /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
     * terminated list of weak references to it.  These are the list pointers.
     * If wr_object goes away, wr_object is set to Py_None, and these pointers
     * have no meaning then.
     */
37 38 39 40
    PyWeakReference *wr_prev;
    PyWeakReference *wr_next;
};

41 42 43
PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;
PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;
PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;
44

45 46
#define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
#define PyWeakref_CheckRefExact(op) \
47
        (Py_TYPE(op) == &_PyWeakref_RefType)
48
#define PyWeakref_CheckProxy(op) \
49 50
        ((Py_TYPE(op) == &_PyWeakref_ProxyType) || \
         (Py_TYPE(op) == &_PyWeakref_CallableProxyType))
51 52 53 54

/* This macro calls PyWeakref_CheckRef() last since that can involve a
   function call; this makes it more likely that the function call
   will be avoided. */
55 56 57 58
#define PyWeakref_Check(op) \
        (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))


59
PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
60
                                              PyObject *callback);
61
PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
62
                                                PyObject *callback);
63
PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
64

65
PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
66

67 68
PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);

69 70 71 72 73 74 75
#define PyWeakref_GET_OBJECT(ref) (((PyWeakReference *)(ref))->wr_object)


#ifdef __cplusplus
}
#endif
#endif /* !Py_WEAKREFOBJECT_H */