summaryrefslogtreecommitdiff
path: root/numpy/doc/swig/test/Farray.i
blob: 25f6cd025c918e7253314bbc07e958d64eca69d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// -*- c++ -*-

%module Farray

%{
#define SWIG_FILE_WITH_INIT
#include "Farray.h"
%}

// Get the NumPy typemaps
%include "../numpy.i"

 // Get the STL typemaps
%include "stl.i"

// Handle standard exceptions
%include "exception.i"
%exception
{
  try
  {
    $action
  }
  catch (const std::invalid_argument& e)
  {
    SWIG_exception(SWIG_ValueError, e.what());
  }
  catch (const std::out_of_range& e)
  {
    SWIG_exception(SWIG_IndexError, e.what());
  }
}
%init %{
  import_array();
%}

// Global ignores
%ignore *::operator=;
%ignore *::operator();

// Apply the 2D NumPy typemaps
%apply (int* DIM1 , int* DIM2 , long** ARGOUTVIEW_FARRAY2)
      {(int* nrows, int* ncols, long** data              )};

// Farray support
%include "Farray.h"
%extend Farray
{
  PyObject * __setitem__(PyObject* index, long v)
  {
    int i, j;
    if (!PyArg_ParseTuple(index, "ii:Farray___setitem__",&i,&j)) return NULL;
    self->operator()(i,j) = v;
    return Py_BuildValue("");
  }

  PyObject * __getitem__(PyObject * index)
  {
    int i, j;
    if (!PyArg_ParseTuple(index, "ii:Farray___getitem__",&i,&j)) return NULL;
    return SWIG_From_long(self->operator()(i,j));
  }

  int __len__()
  {
    return self->nrows() * self->ncols();
  }

  std::string __str__()
  {
    return self->asString();
  }
}