/trunk/Examples/python/functor/example.i

# · Swig · 29 lines · 19 code · 9 blank · 1 comment · 0 complexity · b66702553b0954370abef23b5d098791 MD5 · raw file

  1. /* File : example.i */
  2. %module example
  3. %inline %{
  4. // From B. Strousjoup, "The C++ Programming Language, Third Edition", p. 514
  5. template<class T> class Sum {
  6. T res;
  7. public:
  8. Sum(T i = 0) : res(i) { }
  9. void operator() (T x) { res += x; }
  10. T result() const { return res; }
  11. };
  12. %}
  13. // Rename the application operator to __call__ for python.
  14. // Note: this is normally automatic, but if you had to do it yourself
  15. // you would use this directive:
  16. //
  17. // %rename(__call__) *::operator();
  18. // Instantiate a few versions
  19. %template(intSum) Sum<int>;
  20. %template(doubleSum) Sum<double>;