function template uses templated header - swig interface file

View: New views
3 Messages — Rating Filter:   Alert me  

function template uses templated header - swig interface file

by jared rubin :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

function template uses templated header - swig interface file

How does one create the swig interface file for a function (in a separate file) that uses a templated header file?
My Bar.C uses a template instance of Foo.h. I am not seeing the code being wrapped correctly
Thanks


# A function template that uses a class template
Foo.h
==============
template <class T> class Foo {
  private:
    T* data;
  public Foo(T *d)
  {
    data = t;
  }
}

Bar.h
=================
#include "Foo.h"
void bar(Foo<int> f);

Bar.C
=================
#include "Bar.h"
void bar(Foo<int> f) {
  // do something;
  return;
}

FooBar.i
=========
%module FooBar
%include "Foo.h"
%template(FooI) Foo<int>;
%include "Bar.h"


------------------------------------------------------------------------------
Crystal Reports - New Free Runtime and 30 Day Trial
Check out the new simplified licensing option that enables unlimited
royalty-free distribution of the report engine for externally facing
server and web deployment.
http://p.sf.net/sfu/businessobjects
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Re: function template uses templated header - swig interface file

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Rubin, Jared wrote:

> How does one create the swig interface file for a function (in a
> separate file) that uses a templated header file?
> My Bar.C uses a template instance of Foo.h. I am not seeing the code
> being wrapped correctly
> Thanks
>
>
> # A function template that uses a class template
> Foo.h
> ==============
> template <class T> class Foo {
>   private:
>     T* data;
>   public Foo(T *d)
>   {
>     data = t;
>   }
> }
>
> Bar.h
> =================
> #include "Foo.h"
> void bar(Foo<int> f);
>
> Bar.C
> =================
> #include "Bar.h"
> void bar(Foo<int> f) {
>   // do something;
>   return;
> }
>
> FooBar.i
> =========
> %module FooBar
> %include "Foo.h"
> %template(FooI) Foo<int>;
> %include "Bar.h"
>
How is it not being wrapped correctly? Assuming you fix the C++ code so
that it compiles, it should generate wrappers, although you'd have a
SWIGTYPE_p_int type wrapper class which isn't great. See the docs for
improving the wrapping pointers to primitive types.

William

------------------------------------------------------------------------------
Are you an open source citizen? Join us for the Open Source Bridge conference!
Portland, OR, June 17-19. Two days of sessions, one day of unconference: $250.
Need another reason to go? 24-hour hacker lounge. Register today!
http://ad.doubleclick.net/clk;215844324;13503038;v?http://opensourcebridge.org
_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user

Parent Message unknown Re: function template uses templated header - swig interface file

by wsfulton :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

You have a working example as I sent you, use that approach. Your error is shown below.

2009/6/29 Rubin, Jared <JARED.RUBIN@...>

William,
  The following doesn't work. Maybe you can see an obvious problem. I get the below compiler error

Bar_wrap.cxx: In function `PyObject* _wrap_BarInt(PyObject*, PyObject*)':
Bar_wrap.cxx:2866: error: `FooInt' undeclared (first use this function)





Foo.h
=====
template<class T> class Foo {
  public:
    T data;
  public:
  // null constructor
  Foo() {}
  Foo(T d)
  {
    data = d;

  }
};

Bar.h
=====
#include "Foo.h"
template<class T>
void Bar(T& f);


Bar.C
======
#include "Bar.h"
template<class T>
void Bar(T& f) {
  // do something;
  f.data = f.data + 1;
  return;
}

Bar.i
=====
%module Bar

%{
#include "Bar.h"
%}
%include "Foo.h"
%include "Bar.h"
%template(FooInt) Foo<int>;
%template(BarInt) Bar<FooInt>;

FooInt is not a C++ type, so you can't use it as a type in the C++ template. You probably intended:
%template(BarInt) Bar< Foo<int> >;

and then reorder the %include and %template as per my example so that the %template(BarInt) is declared before it is used.

William
 




Thanks
Jared















Jared



-----Original Message-----
From: william@... on behalf of William S Fulton
Sent: Thu 6/25/2009 10:58 AM
To: Rubin, Jared
Subject: Re: [Swig-user] function template uses templated header - swig interface file

Rubin, that post shows what to do quite clearly. Not sure what else I
can do. Try make a small standalone interface file of what you want to wrap.

William

Rubin, Jared wrote:
> William,
>   Thanks for the response. I have been able to instantiate a templated
> C++ class. However I have not
> been able to get a separate templated function (that is outside the
> scope of the above C++ class) that requires
> an instance of the templated function to work. I even followed the below
> thread with no luck
>
> http://www.nabble.com/template-function-not-wrapped-td17401882.html#a17402122
> Jared
>
>
>
>
> -----Original Message-----
> From: william@... on behalf of William S Fulton
> Sent: Fri 6/19/2009 5:49 PM
> To: Rubin, Jared
> Cc: swig-user@...
> Subject: Re: [Swig-user] function template uses templated header - swig
> interface file
>
> Rubin, Jared wrote:
>  > How does one create the swig interface file for a function (in a
>  > separate file) that uses a templated header file?
>  > My Bar.C uses a template instance of Foo.h. I am not seeing the code
>  > being wrapped correctly
>  > Thanks
>  >
>  >
>  > # A function template that uses a class template
>  > Foo.h
>  > ==============
>  > template <class T> class Foo {
>  >   private:
>  >     T* data;
>  >   public Foo(T *d)
>  >   {
>  >     data = t;
>  >   }
>  > }
>  >
>  > Bar.h
>  > =================
>  > #include "Foo.h"
>  > void bar(Foo<int> f);
>  >
>  > Bar.C
>  > =================
>  > #include "Bar.h"
>  > void bar(Foo<int> f) {
>  >   // do something;
>  >   return;
>  > }
>  >
>  > FooBar.i
>  > =========
>  > %module FooBar
>  > %include "Foo.h"
>  > %template(FooI) Foo<int>;
>  > %include "Bar.h"
>  >
> How is it not being wrapped correctly? Assuming you fix the C++ code so
> that it compiles, it should generate wrappers, although you'd have a
> SWIGTYPE_p_int type wrapper class which isn't great. See the docs for
> improving the wrapping pointers to primitive types.
>
> William
>




------------------------------------------------------------------------------

_______________________________________________
Swig-user mailing list
Swig-user@...
https://lists.sourceforge.net/lists/listinfo/swig-user