3

I am new to SWIG

And less time to do things. I am trying to bind c++ classes to python. I have set up SWIG in windows and tried running it. It was successful

My example.i file is like

/* File: example.i */
%module example

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

But it seems it has to include or declare the header files class, constructor, templates etc...

Can anyone suggest how to create a SWIG interface file.

Follwing are the header file (Item.h) that i need to create interface file.

#ifndef __ITEM_H__
#define __ITEM_H__

#include <complex>
#include <functional>
#include <string>

template<typename T>
class Item
{
  std::string name_;
  T val_;

public:
  Item(std::string name, T val) : name_(name), val_(val) {}
  Item(Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<T> &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  T operator()() const { return val_; }
  double norm() const { return sqrt(val_ * val_); }
};

template<>
class Item<std::complex<double> >
{
  std::string name_;
  std::complex<double> val_;

public:
  Item(std::string name, std::complex<double> val) : name_(name), val_(val) {}
  Item(Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  Item(const Item<std::complex<double> > &rhs) : name_(rhs.name_), val_(rhs.val_) {}
  ~Item() {}

  std::string name() const { return name_; }
  std::complex<double> operator()() const { return val_; }
  double norm() const { return sqrt(val_.real() * val_.real() + val_.imag() * val_.imag()); }
};

template<typename T>
struct ItemComparator : public std::binary_function<Item<T>, Item<T>, bool>
{
  inline bool operator()(Item<T> lhs, Item<T> rhs)
  {
    return lhs.norm() < rhs.norm();
  }
};

#endif
svs
  • 251
  • 1
  • 4
  • 12
  • How do you expect a C++ template class to be exported to Python? AFAIK one can only export specializations. Also, don't use names beginning with double underscores http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier. – Benjamin Bannier Nov 13 '12 at 13:14
  • You might also consider [boost.python](http://www.boost.org/doc/libs/release/libs/python/) as an alternative to SWIG. I've found that writing high quality wrappers is easier than it is with SWIG. – Geoff Reedy Nov 14 '12 at 03:45

1 Answers1

10

Here's an interface file for your example. SWIG has built-in support for std::string and std::complex. You must declare the concrete classes of the templates you want to use via %template:

%module Item

%{
#include "Item.h"
%}

%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%template(Int) Item<int>;
%template(Complex) Item<std::complex<double> >;

Use it like:

>>> import Item
>>> a=Item.Int('Int1',5)
>>> b=Item.Complex('Cplx1',2+3j)
>>> a.name()
'Int1'
>>> b.name()
'Cplx1'
>>> a.norm()
5.0
>>> b.norm()
3.605551275463989
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251