Created Templates and SWIG

I have the following problem that I do not know how to solve. I want to create a Java wrapper using SWIG for these two classes that are in the same file:

utilities.h:

template<class T>
class EncoderInterface
{
 public:
  virtual ~EncoderInterface()
  {
  }
  virtual const cdap_rib::SerializedObject* encode(const T &object) = 0;
  virtual T* decode(
      const cdap_rib::SerializedObject &serialized_object) const = 0;
};

class IntEncoder : public rib::EncoderInterface<int>
{
 public:
  const cdap_rib::SerializedObject* encode(const int &object);
  int* decode(const cdap_rib::SerializedObject &serialized_object) const;
};

Then I do the usual swig stuff in .i:

%{
#include "utilities.h"
%}

%include "utilities.h"

And he says:

Warning 401: Nothing known about base class 'EncoderInterface< int >'. Ignored.
Warning 401: Maybe you forgot to instantiate 'EncoderInterface< int >' using %template.

If I try to use the% template thing like this:

%template(IntEncoder) EncoderInterface<int>;

Warning 302: Identifier 'IntEncoder' redefined (ignored) (Renamed from 'EncoderInterface< int >'),
utilities.h:302: Warning 302: previous definition of 'IntEncoder'.

IntEncoderhas a code in utilities.cc, and I want the user of utilities to create new instances of the templates or use this if he wants. I do not want to change the name IntEncoder, so any library user (from C ++ or Java) will use the same names.

I read something about file sharing (saving a EncoderInterfacetemplate in one file and creating an instance in another) - is this the only solution to this problem? I do not want to create new files if I can avoid it.

+4
1
Warning 401: Nothing known about base class 'EncoderInterface< int >'. Ignored.
Warning 401: Maybe you forgot to instantiate 'EncoderInterface< int >' using %template.

EncoderInterface<int> IntEncoder. SWIG IntEncoder Java, , .

SWIG , , Java IntEncoder EncoderInterface<int>, .

% template, :

%template(IntEncoder) EncoderInterface<int>;

Warning 302: Identifier 'IntEncoder' redefined (ignored) (Renamed from 'EncoderInterface< int >'),
utilities.h:302: Warning 302: previous definition of 'IntEncoder'.

, SWIG EncoderInterface<int> , , IntEncoder.

SWIG, EncoderInterface<int> - , . IntEncoderInterface:

%template(IntEncoderInterface) EncoderInterface<int>;

, , , Java API.

++ SWIG, , , Java.

EncoderInterface<T> T, %template , SWIG Java.

Java EncoderInterface, Java generics, , , . , Java API ++ API, API ( , - , , API , , - , ).

- ( EncoderInterface ), ? , .

, , , , , SWIG, . , .

, ( , IntEncoder EncoderInterface<int> SWIG , EncoderInterface):

#ifndef SWIG
template<class T>
class EncoderInterface
{
 public:
  virtual ~EncoderInterface()
  {
  }
  virtual const cdap_rib::SerializedObject* encode(const T &object) = 0;
  virtual T* decode(
      const cdap_rib::SerializedObject &serialized_object) const = 0;
};
#endif

class IntEncoder
#ifndef SWIG
   : public EncoderInterface<int>
#endif
{
 public:
  const cdap_rib::SerializedObject* encode(const int &object);
  int* decode(const cdap_rib::SerializedObject &serialized_object) const;
};

, , int* decode SWIGTYPE_p_int, , . , decode (int) , int Java.

+2

All Articles