I have a simple test.h file with my own array class (which uses the standard vector class):
#include <vector>
#include <string>
using namespace std;
class Array1D{
private:
vector<double> data_;
int xsize_;
public:
Array1D(): xsize_(0) {};
// creates vector of size nx and sets each element to t
Array1D(const int& nx, const double& t): xsize_(nx) {
data_.resize(xsize_, t);
}
double& operator()(int i) {return data_[i];}
const double& operator[](int i) const {return data_[i];}
};
I want to be able to use the [] operator in python using swig. My current SWIG interface file looks like
%module test
%{
#define SWIG_FILE_WITH_INIT
#include "test.h"
%}
%include "std_vector.i"
namespace std{
%template(DoubleVector) vector<double>;
}
%include "test.h"
When I make the module, everything runs fine, but when I instantiate an object of Array1D, a = test.Array1D(10,2), which creates a length 10 vector with 2 in each element, and type a[1] I get
TypeError: 'Array1D' object does not support indexing.
How should my SWIG interface file look in order to extend the operator method so I can output a[1] properly within python? I would also like to be able to do something like a[1] = 3.0;