From: William S F. <ws...@fu...> - 2015-08-10 18:28:27
|
On 6 August 2015 at 19:35, Sven Wegener <sve...@gm...> wrote: > Hello, > I need your help with wrapping the following short, self-contained example > to Java. > > The code below declares two template classes, Mixin1 and Mixin2. Then > another class, "Product", is declared, which again is templated to depend > on either Mixin1 or Mixin2. > > The problem is, that I have to give some generic name ("AnyMixin" below) to > the Product template argument, and that swig uses this generic name in > the wrapper > rather than the actual name (Mixin1 or Mixin2) which instantiates the > template. > > Swig produces the following code: > --- Mixins_wrap.cxx --- > [...] > SWIGEXPORT void JNICALL Java_testJNI_intMixin1Product_1m_1set(JNIEnv *jenv, > jclass jcls, jlong jarg1, jobject jarg1_, jlong jarg2) { > Product< int,Mixin1 > *arg1 = (Product< int,Mixin1 > *) 0 ; > AnyMixin< int > arg2 ; > AnyMixin< int > *argp2 ; > [...] > ------------------------ > > and the compiler doesn't know about AnyMixin, of course: > --- compiler output: --- > Mixins_wrap.cxx:226:3: error: ‘AnyMixin’ was not declared in this scope > AnyMixin< int > arg2 ; > [...] > ------------------------ > > Do you have any suggestion, how to wrap the example below without messing > around with the code itself? The full code has 25 Mixins and 12 Product > classes, so that I *need* the template mechanism, if I don't want to write > 300 classes by hand. > > To reproduce the problem, simply save the code below as "Mixins.H" and > execute: > > export CPPFLAGS= -I$(JAVA_HOME)/include -I$(JAVA_HOME)/include/linux > swig3.0 -java -c++ Mixins.H > g++ -c $(CPPFLAGS) Mixins_wrap.cxx > > --- Mixins.H --- > #ifdef SWIG > %module test > %{ > #include "Mixins.H" > template <class T> Mixin1<T>::Mixin1(T x){ this->x=x; } > template <class T> Mixin2<T>::Mixin2(T x){ this->x=x; } > template <class T,template<class> class AnyMixin> > Product<T,AnyMixin>::Product(AnyMixin<T> m){ this->m=m; } > %} > > #endif > > template <class T> class Mixin1{ > public: > T x; > Mixin1(T x=0); > }; > template <class T> class Mixin2{ > public: > T x; > Mixin2(T x=0); > }; > > template <class T, template<class> class AnyMixin> class Product{ > public: > AnyMixin<T> m; > Product(AnyMixin<T>); > }; > > #ifdef SWIG > %template(intMixin1Product) Product<int,Mixin1 >; > %template(intMixin2Product) Product<int,Mixin2 >; > #endif > ----------------- I'm afraid that template template parameters are poorly supported and development in SWIG is required to make this work. Sorry, someone needs to do this. Regards William |