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

David Abrahams dave at boost-consulting.com
Fri Jun 6 13:22:34 CEST 2003


"Brett Calcott" <brett.calcott at paradise.net.nz> writes:

>>
>> 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...
>
> }

Wow, for some reason I couldn't see a generic solution and didn't
think of doing it that way.  That's totally cool!  Maybe I should
change the implementation in the library to allow non-const
PyTypeObjects.  It's more flexible, after all.


> I am having great fun :)

I'm very pleased :)

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list