[C++-sig] Custom smart pointer progress (I think?) (original) (raw)
Alex Mohr amohr at pixar.com
Tue Dec 20 19:48:19 CET 2005
- Previous message: [C++-sig] Calling func that takes intrusive_ptr with intrusive_ptr...
- Next message: [C++-sig] Patches for pyste
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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 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 struct SmartPtrFromPython { SmartPtrFromPython() { converter::registry::insert(&convertible, &construct, type_id<SmartPtr >()); } private: static void *convertible(PyObject *p) { if (p == Py_None) return p; return converter::get_lvalue_from_python(p, converter::registered::converters); } static void construct(PyObject *source, converter:: rvalue_from_python_stage1_data *data) { void const storage = ((converter:: rvalue_from_python_storage<SmartPtr >)data)-> storage.bytes; // None case: if (data->convertible == source) new (storage) SmartPtr(); else new (storage) SmartPtr (static_cast<T*>(data->convertible)); data->convertible = storage; } }
- Previous message: [C++-sig] Calling func that takes intrusive_ptr with intrusive_ptr...
- Next message: [C++-sig] Patches for pyste
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]