[C++-sig] Custom smart pointer progress (I think?)
Alex Mohr
amohr at pixar.com
Tue Dec 20 19:48:19 CET 2005
I have come up with what seems to be an initial solution to the problems
I posted yesterday regarding smart pointers that are not shared_ptr.
Digging into boost python, I found converter/shared_ptr_from_python.hpp.
I followed its lead and did something like the code below.
If I instantiate one of these SmartPtrFromPython<T> for each T that has
SmartPtr as the held type, then the broken examples I posted yesterday
seem to work as I would expect, just like in the shared_ptr case.
Of course this seems a bit questionable. I think I'm dealing with
private API, and I don't really know if registering these converters is
potentially dangerous. Is this a reasonable approach?
Thanks,
Alex
using namespace boost::python;
template <typename T>
struct SmartPtrFromPython {
SmartPtrFromPython() {
converter::registry::insert(&convertible, &construct,
type_id<SmartPtr<T> >());
}
private:
static void *convertible(PyObject *p) {
if (p == Py_None)
return p;
return converter::get_lvalue_from_python(p,
converter::registered<T>::converters);
}
static void construct(PyObject *source, converter::
rvalue_from_python_stage1_data *data) {
void *const storage = ((converter::
rvalue_from_python_storage<SmartPtr<T> >*)data)->
storage.bytes;
// None case:
if (data->convertible == source)
new (storage) SmartPtr<T>();
else
new (storage) SmartPtr<T>
(static_cast<T*>(data->convertible));
data->convertible = storage;
}
}
More information about the Cplusplus-sig
mailing list