blob: 0a659b027ce40e51078cb647ae71c2e5f8875f36 [file] [log] [blame]
Fred Drake8844d522001-10-05 21:52:26 +00001/* Weak references objects for Python. */
2
3#ifndef Py_WEAKREFOBJECT_H
4#define Py_WEAKREFOBJECT_H
5#ifdef __cplusplus
6extern "C" {
7#endif
8
9
10typedef struct _PyWeakReference PyWeakReference;
11
Tim Petersead8b7a2004-10-30 23:09:22 +000012/* PyWeakReference is the base struct for the Python ReferenceType, ProxyType,
13 * and CallableProxyType.
14 */
Fred Drake8844d522001-10-05 21:52:26 +000015struct _PyWeakReference {
16 PyObject_HEAD
Tim Petersead8b7a2004-10-30 23:09:22 +000017
18 /* The object to which this is a weak reference, or Py_None if none.
19 * Note that this is a stealth reference: wr_object's refcount is
20 * not incremented to reflect this pointer.
21 */
Fred Drake8844d522001-10-05 21:52:26 +000022 PyObject *wr_object;
Tim Petersead8b7a2004-10-30 23:09:22 +000023
24 /* A callable to invoke when wr_object dies, or NULL if none. */
Fred Drake8844d522001-10-05 21:52:26 +000025 PyObject *wr_callback;
Tim Petersead8b7a2004-10-30 23:09:22 +000026
27 /* A cache for wr_object's hash code. As usual for hashes, this is -1
28 * if the hash code isn't known yet.
29 */
Fred Drake8844d522001-10-05 21:52:26 +000030 long hash;
Tim Petersead8b7a2004-10-30 23:09:22 +000031
32 /* If wr_object is weakly referenced, wr_object has a doubly-linked NULL-
33 * terminated list of weak references to it. These are the list pointers.
34 * If wr_object goes away, wr_object is set to Py_None, and these pointers
35 * have no meaning then.
36 */
Fred Drake8844d522001-10-05 21:52:26 +000037 PyWeakReference *wr_prev;
38 PyWeakReference *wr_next;
39};
40
Mark Hammond91a681d2002-08-12 07:21:58 +000041PyAPI_DATA(PyTypeObject) _PyWeakref_RefType;
42PyAPI_DATA(PyTypeObject) _PyWeakref_ProxyType;
43PyAPI_DATA(PyTypeObject) _PyWeakref_CallableProxyType;
Fred Drake8844d522001-10-05 21:52:26 +000044
Fred Drake0a4dd392004-07-02 18:57:45 +000045#define PyWeakref_CheckRef(op) PyObject_TypeCheck(op, &_PyWeakref_RefType)
46#define PyWeakref_CheckRefExact(op) \
Fred Drake8844d522001-10-05 21:52:26 +000047 ((op)->ob_type == &_PyWeakref_RefType)
48#define PyWeakref_CheckProxy(op) \
49 (((op)->ob_type == &_PyWeakref_ProxyType) || \
50 ((op)->ob_type == &_PyWeakref_CallableProxyType))
Fred Drake0a4dd392004-07-02 18:57:45 +000051
52/* This macro calls PyWeakref_CheckRef() last since that can involve a
53 function call; this makes it more likely that the function call
54 will be avoided. */
Fred Drake8844d522001-10-05 21:52:26 +000055#define PyWeakref_Check(op) \
56 (PyWeakref_CheckRef(op) || PyWeakref_CheckProxy(op))
57
58
Mark Hammond91a681d2002-08-12 07:21:58 +000059PyAPI_FUNC(PyObject *) PyWeakref_NewRef(PyObject *ob,
Fred Drake8844d522001-10-05 21:52:26 +000060 PyObject *callback);
Mark Hammond91a681d2002-08-12 07:21:58 +000061PyAPI_FUNC(PyObject *) PyWeakref_NewProxy(PyObject *ob,
Fred Drake8844d522001-10-05 21:52:26 +000062 PyObject *callback);
Mark Hammond91a681d2002-08-12 07:21:58 +000063PyAPI_FUNC(PyObject *) PyWeakref_GetObject(PyObject *ref);
Fred Drake8844d522001-10-05 21:52:26 +000064
Neal Norwitzc5e060d2006-08-02 06:14:22 +000065PyAPI_FUNC(Py_ssize_t) _PyWeakref_GetWeakrefCount(PyWeakReference *head);
Fred Drake8844d522001-10-05 21:52:26 +000066
Tim Peters403a2032003-11-20 21:21:46 +000067PyAPI_FUNC(void) _PyWeakref_ClearRef(PyWeakReference *self);
68
Fred Drake8844d522001-10-05 21:52:26 +000069#define PyWeakref_GET_OBJECT(ref) (((PyWeakReference *)(ref))->wr_object)
70
71
72#ifdef __cplusplus
73}
74#endif
75#endif /* !Py_WEAKREFOBJECT_H */