From: Barry S. <ba...@ba...> - 2010-04-09 20:27:42
|
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 |