[C++-sig] Re: playing with pygame

Brett Calcott brett.calcott at paradise.net.nz
Fri Jun 6 09:18:56 CEST 2003


>
> lvalue_from_pytype is a simple template; take a look.  You could write
> an extract function which does the same thing, but which uses your
> non-constant PyTypeObject* expression in place of the template
> parameter, and register that just the way the lvalue_from_pytype
> constructor does.
>

Uh yeah. That wasn't so hard.

A bit of messing around with your template got me this:

template <class Extractor>
struct lvalue_from_nonconst_pytype
{
    lvalue_from_nonconst_pytype(PyTypeObject *type)
    {
        // assume this is called only once
        m_type = type;
        converter::registry::insert(
            &extract, detail::extractor_type_id(&Extractor::execute));
    }
 private:
    static PyTypeObject *m_type;

    static void* extract(PyObject* op)
    {
        return PyObject_TypeCheck(op, m_type)
            ? const_cast<void*>(
                static_cast<void const volatile*>(

detail::normalize<Extractor>(&Extractor::execute).execute(op)))
            : 0
            ;
    }
};

template <class Extractor> PyTypeObject *
    lvalue_from_nonconst_pytype<Extractor>::m_type = 0;


BOOST_PYTHON_MODULE(test)
{
    import_pygame_rect();
    lvalue_from_nonconst_pytype<extract_identity<PyRectObject> >
        need_this_or_msvc_barfs(&PyRect_Type);

    ...etc...

}


Finding out what the msvc problem was took more time than changing the
template...

I am having great fun :)
Thanks again,

Brett







More information about the Cplusplus-sig mailing list