From: Fernando L. <fer...@gm...> - 2010-04-09 21:05:55
|
Excellent explanation ! I promise to start a deeper study of the code this weekend in order to minimize bothering you (but not stop, ha ha). Fernando 2010/4/9 Barry Scott <ba...@ba...> > > On 9 Apr 2010, at 15:27, Fernando Libonati wrote: > > Two questions ... > a) How can I pass a pointer to a new type object? > > > Can you get a pointer from a Py::Object? Yes > > b) Can I access methods from this pointer? > > > yes. But it depends on the method. If its > a C++ method then its trivia. If its a python method > on the object that may have been overridden in > a derived class then you need to use a PyCXX helper. > > The code below depends on PyCXX trunk that I will release > as 6.2.0 soon. > > > > Like in > > class Block : Public PythonClass<Block> { > Block *parent; > Block(Py::PythonClassInstance *self, Py::Tuple &args, Py::Dict &kwds): > : Py::PythonClass< Block >::PythonClass( self, args, kwds ) > { > args.verify_length(1); > // Is this possible ??? or something like this > parent = (Block*) args[0]; > > > This does not work on lots of levels. > a) Py::Object just holds a pointer to the PyObject. > b) You cannot cast a PyObject as its not part of the C++ class. > It contains a pointer to the C++ class. > > Use this to make sure you really have a Block: > > Py::PythonClassObject<Block> py_block( args[0] ); > > Now you can get the C++ object pointer: > > Block *p_block = py_block.getCxxObject(); > > To call getFullName you have a couple of choices: > > Py::String s( p_block->getFullName() ); > > or > > Py::TupleN args; > Py::String py_block.callMemberFunction( "getFullName", args ); > > > } > ... > ... > Py::Object getFullName( void ) > { > std::string r = ""; > if( ! parent->isNone() ) > r += Py::String( parent->getFullName()).as_std_string( "utf-8" > ) + "."; > > /* I want to access parent's methods directly to avoid the overload of > calling > callMemberFunction("getFullName") and many others that I need. > Is it possible? > Is callMemberFunction restricted to the exposed ones? > */ > r += name.as_std_string( "utf-8" ); > return Py::String(r); > } > ... > ... > }; > > > Barry > > > > ------------------------------------------------------------------------------ > Download Intel® Parallel Studio Eval > Try the new software tools for yourself. Speed compiling, find bugs > proactively, and fine-tune applications for parallel performance. > See why Intel Parallel Studio got high marks during beta. > http://p.sf.net/sfu/intel-sw-dev > _______________________________________________ > CXX-Users mailing list > CXX...@li... > https://lists.sourceforge.net/lists/listinfo/cxx-users > > -- Fernando Libonati fernando.libonati at gmail.com |