simpler name for unique wrapped class from a class template

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

simpler name for unique wrapped class from a class template

Pierre-Henri Wuillemin
Hello,

Let's say I have a class template in C++ :

template<typename T> class Picture ...

In C++, I can use Picture<int>, Picture<char>, etc .. But in the python
wrapper, I just want to use Picture<double>. Swig allows me to specify :

%template (Picture_double) Picture<double>;

Since there will be no other type of Picture in the python wrapper, I
would like to get rid of the uninformative '_double' and to have a
simple python class "Picture".

1- as far as I know, swig does not allow
%template (Picture) Picture<double>;

2- python allows me to create alias
Picture = Picture_double

but this is not satisfying : help(Picture) does not work as wanted,
Picture_double is still in the namespace, error messages use
Picture_double and not Picture, etc.


3- I just noticed that if I carefully replace the name of the class in
the swig-generated python file (with sed for instance), I have the
complete, correct and awaited result. Especially no clash of names, etc.


So, could it be possible to distinguish an internal name
(Picture_double) and a name for the target language (Picture) ? The
internal name might even be automatically generated. And the unambiguous
syntax

%template (Picture) Picture<double>;

could be accepted.

In our real application, we have ~25 classes in this situation... Almost
all our C++ API has a template parameter which is fixed to 'double' in
the python wrapper.


Thanks,
/PH



------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simpler name for unique wrapped class from a class template

William S Fulton


On 27 May 2017 at 15:48, Pierre-Henri Wuillemin <[hidden email]> wrote:
Hello,

Let's say I have a class template in C++ :

template<typename T> class Picture ...

In C++, I can use Picture<int>, Picture<char>, etc .. But in the python wrapper, I just want to use Picture<double>. Swig allows me to specify :

%template (Picture_double) Picture<double>;

Since there will be no other type of Picture in the python wrapper, I would like to get rid of the uninformative '_double' and to have a simple python class "Picture".

1- as far as I know, swig does not allow
%template (Picture) Picture<double>;

2- python allows me to create alias
Picture = Picture_double

but this is not satisfying : help(Picture) does not work as wanted, Picture_double is still in the namespace, error messages use Picture_double and not Picture, etc.


3- I just noticed that if I carefully replace the name of the class in the swig-generated python file (with sed for instance), I have the complete, correct and awaited result. Especially no clash of names, etc.


So, could it be possible to distinguish an internal name (Picture_double) and a name for the target language (Picture) ? The internal name might even be automatically generated. And the unambiguous syntax

%template (Picture) Picture<double>;

could be accepted.

In our real application, we have ~25 classes in this situation... Almost all our C++ API has a template parameter which is fixed to 'double' in the python wrapper.



It's bit of a hack, but try this approach:

#define Picture PictureT
%{
#define PictureT Picture
template<typename T> class Picture {
};
%}

template<typename T> class Picture {
};
#undef Picture

%template (Picture) PictureT<double>;

There will be a few error messages in the generated C++ layer referring to PictureT though, take a look at the generated code. You could change the error messages with some custom typemaps though.

William

------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: simpler name for unique wrapped class from a class template

Pierre-Henri Wuillemin
Thank you for your answer.

Indeed it is a bit of a hack ... quite difficult to implement for a large set of
headers ... Tihe question/request was rather about the possibility of removing
the constraint on the name of the class wrapped from a template class in
swig...

Thanks again,
/PH


Le samedi 1 juillet 2017, 16:56:03 CEST William S Fulton a écrit :

> On 27 May 2017 at 15:48, Pierre-Henri Wuillemin <
>
> [hidden email]> wrote:
> > Hello,
> >
> > Let's say I have a class template in C++ :
> >
> > template<typename T> class Picture ...
> >
> > In C++, I can use Picture<int>, Picture<char>, etc .. But in the python
> > wrapper, I just want to use Picture<double>. Swig allows me to specify :
> >
> > %template (Picture_double) Picture<double>;
> >
> > Since there will be no other type of Picture in the python wrapper, I
> > would like to get rid of the uninformative '_double' and to have a simple
> > python class "Picture".
> >
> > 1- as far as I know, swig does not allow
> > %template (Picture) Picture<double>;
> >
> > 2- python allows me to create alias
> > Picture = Picture_double
> >
> > but this is not satisfying : help(Picture) does not work as wanted,
> > Picture_double is still in the namespace, error messages use
> > Picture_double
> > and not Picture, etc.
> >
> >
> > 3- I just noticed that if I carefully replace the name of the class in the
> > swig-generated python file (with sed for instance), I have the complete,
> > correct and awaited result. Especially no clash of names, etc.
> >
> >
> > So, could it be possible to distinguish an internal name (Picture_double)
> > and a name for the target language (Picture) ? The internal name might
> > even
> > be automatically generated. And the unambiguous syntax
> >
> > %template (Picture) Picture<double>;
> >
> > could be accepted.
> >
> > In our real application, we have ~25 classes in this situation... Almost
> > all our C++ API has a template parameter which is fixed to 'double' in the
> > python wrapper.
>
> It's bit of a hack, but try this approach:
>
> #define Picture PictureT
> %{
> #define PictureT Picture
> template<typename T> class Picture {
> };
> %}
>
> template<typename T> class Picture {
> };
> #undef Picture
>
> %template (Picture) PictureT<double>;
>
> There will be a few error messages in the generated C++ layer referring to
> PictureT though, take a look at the generated code. You could change the
> error messages with some custom typemaps though.
>
> William

--
Associate Professor - Maître de conférences
Office 403/26-00  | BC 169 | 4, place Jussieu | 75252 Paris Cedex 05
gpg   : http://webia.lip6.fr/~phw/phw.asc
phone : +33 1 44 27 71 48          | fax : +33 1 44 27 88 89
------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user

signature.asc (201 bytes) Download Attachment
Loading...