1 // -*- c++ -*- 2 3 %module Farray 4 5 %{ 6 #define SWIG_FILE_WITH_INIT 7 #include "Farray.h" 8 %} 9 10 // Get the NumPy typemaps 11 %include "../numpy.i" 12 13 // Get the STL typemaps 14 %include "stl.i" 15 16 // Handle standard exceptions 17 %include "exception.i" 18 %exception 19 { 20 try 21 { 22 $action 23 } catch(const std::invalid_argument & e)24 catch (const std::invalid_argument& e) 25 { 26 SWIG_exception(SWIG_ValueError, e.what()); 27 } catch(const std::out_of_range & e)28 catch (const std::out_of_range& e) 29 { 30 SWIG_exception(SWIG_IndexError, e.what()); 31 } 32 } 33 %init %{ 34 import_array(); 35 %} 36 37 // Global ignores 38 %ignore *::operator=; 39 %ignore *::operator(); 40 41 // Apply the 2D NumPy typemaps 42 %apply (int* DIM1 , int* DIM2 , long** ARGOUTVIEW_FARRAY2) 43 {(int* nrows, int* ncols, long** data )}; 44 45 // Farray support 46 %include "Farray.h" 47 %extend Farray 48 { __setitem__(PyObject * index,long v)49 PyObject * __setitem__(PyObject* index, long v) 50 { 51 int i, j; 52 if (!PyArg_ParseTuple(index, "ii:Farray___setitem__",&i,&j)) return NULL; 53 self->operator()(i,j) = v; 54 return Py_BuildValue(""); 55 } 56 __getitem__(PyObject * index)57 PyObject * __getitem__(PyObject * index) 58 { 59 int i, j; 60 if (!PyArg_ParseTuple(index, "ii:Farray___getitem__",&i,&j)) return NULL; 61 return SWIG_From_long(self->operator()(i,j)); 62 } 63 __len__()64 int __len__() 65 { 66 return self->nrows() * self->ncols(); 67 } 68 __str__()69 std::string __str__() 70 { 71 return self->asString(); 72 } 73 } 74