From: Nitro <ni...@dr...> - 2009-03-21 23:07:38
|
Am 21.03.2009, 19:13 Uhr, schrieb Josh Cherry <jc...@nc...>: > Or use something like $descriptor(T *). > > However, it seems to me that calling SWIG_ConvertPtr is the wrong thing > to > do. The object that you get from the list (o) is not a SWIG > representation of an int*. Rather, it is a Python integer. You would > use > something like PyInt_AsLong to get the integer, and PyInt_Check to check > it. You would need to store those integers somewhere and create an array > of pointers to them. You may need to call Py_DECREF on o to avoid a > memory leak. Even simpler, you can use the traits fragment stuff (which is part of the UTL I think) and just use the swig::from or swig::from_ptr. Because of partial template specialization it will automatically resolve to the fitting code ( be it PyInt_AsLong if T is int or if T is an std::vector<T2> it will automatically place the code to covert from a python sequence to an std::vector<T2> ). Actually this is a mechanism I'd love to see manifested and used a bit more in SWIG, because it's very useful and you don't have to whip up custom and redundant typemap code. The swig::from, swig::from_ptr, swig::as and swig::as_ptr templates are single entry points to do conversion between C++ and target language (e.g. Python-to-C++ or C++-to-Python). One place where I use this is to write generic OUTPUT& typemaps (e.g. wrapping a function with a prototype like "void generateVector( std::vector<int>& vec )" ). > It would be easier to add a function to the interface that takes an int* > or, as I suggested earlier, a const vector<int>&. Yes, that's usually the easiest way. If you sequences are very large, I recommend to take a look at numpy and the numpy.i interface file which deals with lots of this stuff in a performant, somewhat standard manner. -Matthias |