On 17 Jul, 15:02, "Alf P. Steinbach /Usenet" <alf.p.steinbach +use...@gmail.com> 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; } > } > > PyPtr name() > { > (first != 0) > || throwX( ::PyExc_AttributeError, "first" ); > (last != 0) > || throwX( ::PyExc_AttributeError, "last" ); > 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" ) > {} > }; > > } // namespace <anon> > > PyMODINIT_FUNC > PyInit_noddy2() > { > return cppy::safeInit< NoddyModule >();} > I wonder if this is readable / self-documenting, or not? Are you serious? It's C++, Heaven forbid, and you wonder if it's readable or not? -- http://mail.python.org/mailman/listinfo/python-list