Alf P. Steinbach /Usenet wrote:

#include <progrock/cppy/PyClass.h> // PyWeakPtr, PyPtr, PyModule, PyClass
using namespace progrock;

namespace {
    using namespace cppy;

    struct Noddy
    {
        PyPtr       first;
        PyPtr       last;
        int         number;

        Noddy( PyWeakPtr pySelf, PyPtr args, PyPtr kwArgs )
            : number( 0 )
        {
            devsupport::suppressUnusedWarning( pySelf );

            PyWeakPtr   pFirstName  = 0;
            PyWeakPtr   pLastName   = 0;

            static char*    kwlist[] = { "first", "last", "number", 0 };

            ::PyArg_ParseTupleAndKeywords(
                args.rawPtr(), kwArgs.rawPtr(), "|OOi", kwlist,
                pointerTo( pFirstName ), pointerTo( pLastName ), &number
                )
                >> Accept< IsNonZero >()
                || throwX( "Invalid args" );

            if( pFirstName != 0 )   { first = pFirstName; }
            if( pLastName != 0 )    { last = pLastName; }

Why not initiallize all member variables in the initializer list?


        }

        PyPtr name()
        {
            (first != 0)
                || throwX( ::PyExc_AttributeError, "first" );
            (last != 0)
                || throwX( ::PyExc_AttributeError, "last" );

Nice trick. I would still find this more readable :

if ( first != 0 )
{
  throwX( ::PyExc_AttributeError, "first" );
}


            return (PyString( first ) + L" " + PyString( last )).pyPtr();
        }
    };

    struct NoddyPyClass: PyClass< Noddy >
    {
NoddyPyClass( PyModule& m, PyString const& name, PyString const& doc )
            : PyClass< Noddy >( m, name, doc, Exposition()
                .method(
                    L"name",    CPPY_GLUE( name ),
                    L"Return the name, combining the first and last name"
                    )
                .attribute(
                    L"first",   CPPY_GLUE( first ),     L"first name"
                    )
                .attribute(
                    L"last",    CPPY_GLUE( last ),      L"last name"
                    )
                .attribute(
                    L"number",  CPPY_GLUE( number ),    L"noddy number"
                    )
                )
        {}
    };

    class NoddyModule: public PyModule
    {
    private:
        NoddyPyClass    noddyPyClass_;

    public:
        NoddyModule()
            : PyModule(
L"noddy2", L"Example module that creates an extension type." )
            , noddyPyClass_( *this,
                L"Noddy", L"A Noddy object has a name and a noddy number" )


hmmm what is L ?
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to