[C++-sig] r-value converter

Piotr Jaroszyński p.jaroszynski at gmail.com
Sun Jun 17 20:56:08 CEST 2007


Hello,

for one of the classes I had to write PythonFoo equivalent and hence I need a 
r-value converter from PythonFoo to Foo as implicit conversion is not 
possible.

This what I have (more or less copy-n-paste from Roman's and boost's docs :)
struct PackageDepSpecFromPython
{
    PackageDepSpecFromPython()
    {
        bp::converter::registry::push_back(&convertible, &construct,
                boost::python::type_id<PackageDepSpec>());
    }

    static void *
    convertible(PyObject * obj_ptr)
    {
        return obj_ptr;
    }

    static void
    construct(PyObject * obj_ptr, 
bp::converter::rvalue_from_python_stage1_data * data)
    {
        typedef 
bp::converter::rvalue_from_python_storage<PythonPackageDepSpec> Storage;
        void * storage = reinterpret_cast<Storage *>(data)->storage.bytes;
        new (storage) PackageDepSpec("cat/123", pds_pm_permissive);  [1]
        data->convertible = storage;
    }
};

And a few questions:
1) First wrt [1], I would like to create the PackageDepSpec with properties 
taken from PythonPackageDepSpec equivalent - how can I get that C++ object 
from PyObject pointer?
2) Some of the functions taking PackageDepSpec want 
shared_ptr<PackageDepSpec> - how can I make a converter to that type?
3) Should I do any extra checks in convertible for such a custom type?

-- 
Best Regards,
Piotr Jaroszyński



More information about the Cplusplus-sig mailing list