How to extend a templated c++ class in python with SWIG to allow the [] operator

后端 未结 3 797
栀梦
栀梦 2020-12-19 10:17

I have a templated c++ array class which uses the standard vector class:

#include 
#include 

using namespace std;

template

        
3条回答
  •  时光说笑
    2020-12-19 10:20

    You can extend each type separately, like this:

    %extend doubleArray1D {
    

    Note that extension is virtual in that it just tells SWIG to generate code for extra functions that will be part of exported class but such function only has access to public interface of your c++ class.

    If you have a whole bunch of template instances, you could define and use a SWIG macro:

    %define ArrayExtend(name, T)
    %extend name {
        T& __getitem__(int i) {
        return (*self)[i];
        }
     }
    %enddef
    
    ArrayExtend(Array1D, double)
    ArrayExtend(Array1D, int)
    

提交回复
热议问题