[Numpy-discussion] Numpi array and typemap

Michel Dupront michel.dupront at hotmail.fr
Mon Mar 15 04:45:40 EDT 2010


 
Hello,


I am trying to do what was suppose to be a very easy 
exercice. It consists in converting a C++ class Container into a 
numpy array as show below using typename
My problem is that if I make the method getContainer run from 
python I will get a numpy array as expected by with wrong values.
I guess I have a memory problem but I do not understand exactly 
wwhat is going on. 
I was hoping that somebody could help me with that and maybe 
tell me how to use the PyArray_SimpleNewFromData macro.


Thanks




%module Foo
%{
#define SWIG_FILE_WITH_INIT
#include "container.h"

template <class T> Container<T> getContainer()
{ 
... C Container construction that will have a pointer to (1,2,3,4,5)...
return C;
}
%}


%typedef unsigned int size_t;
%include "numpy.i"
%init %{
import_array();
%}


template <class T> class Container
{
public:
Container(size_t length, T* pfirst_value);
size_t lentgh() { ... return the lentgh ...}
T* ptr() { ... return pointer to the first value ... } 
};


%template(cN) Container<size_t>;


%typemap(python,out) Container<size_t>
{
npy_intp dims[] = { 5 };
PyObject* obj = PyArray_SimpleNewFromData (1,dims,PyArray_UINT,$1.ptr());
$result = obj;
}


template <class T> navlib::array<T> getContainer();
%template(getContainerN ) getContainer<size_t>;



 		 	   		  
_________________________________________________________________
Découvrez comment SURFER DISCRETEMENT sur un site de rencontres !
http://clk.atdmt.com/FRM/go/206608211/direct/01/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20100315/a237454d/attachment.html>


More information about the NumPy-Discussion mailing list