|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#include <boost/python/object/life_support.hpp> |
| 7 | +#include <boost/python/detail/none.hpp> |
| 8 | + |
| 9 | +namespace boost { namespace python { namespace objects { |
| 10 | + |
| 11 | +struct life_support |
| 12 | +{ |
| 13 | + PyObject_HEAD |
| 14 | + PyObject* patient; |
| 15 | +}; |
| 16 | + |
| 17 | +extern "C" |
| 18 | +{ |
| 19 | + static void |
| 20 | + life_support_dealloc(PyObject* self) |
| 21 | + { |
| 22 | + self->ob_type->tp_free(self); |
| 23 | + } |
| 24 | + |
| 25 | + static PyObject * |
| 26 | + life_support_call(PyObject *self, PyObject *arg, PyObject *kw) |
| 27 | + { |
| 28 | + // Let the patient die now |
| 29 | + Py_XDECREF(((life_support*)self)->patient); |
| 30 | + // Also let the weak reference die. This probably kills us. |
| 31 | + Py_XDECREF(PyTuple_GET_ITEM(arg, 0)); |
| 32 | + return detail::none(); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +PyTypeObject life_support_type = { |
| 37 | + PyObject_HEAD_INIT(&PyType_Type) |
| 38 | + 0, |
| 39 | + "Boost.Python.life_support", |
| 40 | + sizeof(life_support), |
| 41 | + 0, |
| 42 | + life_support_dealloc, /* tp_dealloc */ |
| 43 | + 0, /* tp_print */ |
| 44 | + 0, /* tp_getattr */ |
| 45 | + 0, /* tp_setattr */ |
| 46 | + 0, /* tp_compare */ |
| 47 | + 0, //(reprfunc)func_repr, /* tp_repr */ |
| 48 | + 0, /* tp_as_number */ |
| 49 | + 0, /* tp_as_sequence */ |
| 50 | + 0, /* tp_as_mapping */ |
| 51 | + 0, /* tp_hash */ |
| 52 | + life_support_call, /* tp_call */ |
| 53 | + 0, /* tp_str */ |
| 54 | + 0, // PyObject_GenericGetAttr, /* tp_getattro */ |
| 55 | + 0, // PyObject_GenericSetAttr, /* tp_setattro */ |
| 56 | + 0, /* tp_as_buffer */ |
| 57 | + Py_TPFLAGS_DEFAULT /* | Py_TPFLAGS_HAVE_GC */,/* tp_flags */ |
| 58 | + 0, /* tp_doc */ |
| 59 | + 0, // (traverseproc)func_traverse, /* tp_traverse */ |
| 60 | + 0, /* tp_clear */ |
| 61 | + 0, /* tp_richcompare */ |
| 62 | + 0, //offsetof(PyLife_SupportObject, func_weakreflist), /* tp_weaklistoffset */ |
| 63 | + 0, /* tp_iter */ |
| 64 | + 0, /* tp_iternext */ |
| 65 | + 0, /* tp_methods */ |
| 66 | + 0, // func_memberlist, /* tp_members */ |
| 67 | + 0, //func_getsetlist, /* tp_getset */ |
| 68 | + 0, /* tp_base */ |
| 69 | + 0, /* tp_dict */ |
| 70 | + 0, /* tp_descr_get */ |
| 71 | + 0, /* tp_descr_set */ |
| 72 | + 0, //offsetof(PyLife_SupportObject, func_dict), /* tp_dictoffset */ |
| 73 | + 0, /* tp_init */ |
| 74 | + 0, /* tp_alloc */ |
| 75 | + 0, |
| 76 | + 0 /* tp_new */ |
| 77 | +}; |
| 78 | + |
| 79 | +int make_nurse_and_patient(PyObject* nurse, PyObject* patient) |
| 80 | +{ |
| 81 | + life_support* system = PyObject_New(life_support, &life_support_type); |
| 82 | + if (!system) |
| 83 | + return -1; |
| 84 | + |
| 85 | + // We're going to leak this reference, but don't worry; the |
| 86 | + // life_support system decrements it when the nurse dies. |
| 87 | + PyObject* weakref = PyWeakref_NewRef(nurse, (PyObject*)system); |
| 88 | + if (!weakref) |
| 89 | + { |
| 90 | + Py_XDECREF(system); |
| 91 | + return -1; |
| 92 | + } |
| 93 | + |
| 94 | + system->patient = patient; |
| 95 | + Py_XINCREF(patient); // hang on to the patient until death |
| 96 | + return 0; |
| 97 | +} |
| 98 | + |
| 99 | +}}} // namespace boost::python::objects |
0 commit comments