Personal tools
You are here: Home Forum How to use Salome? Wrapping with SWIG + Templates

Wrapping with SWIG + Templates

Up to How to use Salome?
  • Wrapping with SWIG + Templates

    Posted by Dan Popov at September 11. 2007
    Dear salome-team,

    firstly thanks for your great software which I frequently use with pleasure =)

    Considering that salome seems to be wrapped with python+SWIG
    I was wondering how you manage to wrap template classes.
    I myself want to wrap a software which is programmed in generic programming
    style which takes extentive use of templating. Unfortunately, we are bound to
    use boost.python instead of (Cable-)SWIG.
    Anyhow I wanted to have a look into your code to see how you handle
    the problem.

    Do you hardcode the template-specializations you need, or is there place
    for any python-templating mechanism which is able to wrap C++-templates?
    Maybe anyone of you could give me a hint in which  source-file I could find
    a simple exmaple. I tried to understand the interface file
    ./salome_3.2.6/SWIG-1.3.24/share/swig/1.3.24/python/std_pair.i
    but due to my poor knowledge of SWIG it is not clear to me at all.
     

    Many thanks in advance any suggestion would be great help...!
    Dan
    • Re: Wrapping with SWIG + Templates

      Posted by Edward AGAPOV at September 11. 2007
      Dear Dan

      We use capabilities provided by SWIG. You can see an example of wrapping
      template class FIELD;
      in MED_SRC/src/MEDMEM_SWIG/libMEDMEM_Swig.i

      As python does not support templates, SWIG generates a separete python class for each instantiation of a template.
      Here is what SWIG manual says about wrapping templates:

      C++ templates don't present a huge problem for SWIG. However, in order to create wrappers, you have to tell SWIG to create wrappers for a particular template instantiation. To do this, you use the %template directive. For example:

      %module example
      %{
      #include "pair.h"
      %}

      template
      struct pair {
      typedef T1 first_type;
      typedef T2 second_type;
      T1 first;
      T2 second;
      pair();
      pair(const T1&, const T2&;);
      ~pair();
      };

      %template(pairii) pair;
      As a result class pairii is available via python
      • Re: Wrapping with SWIG + Templates

        Posted by Dan Popov at September 11. 2007
        Dear Edward,

        thank you very much for your detailed answer. The file libMEDMEM_Swig.i is exactly what I was looking for.
        So, it seems creating a wrapper for each specialization of a template class is sufficient for salome.

        Best regards,
        Dan

        P.S.:
        In addition a remark for those who are interested in wrapping C++ code with python: There
        exists a software called cable-swig which seems to be able to wrap some C++-structures
        which are not covered by swig. It was originally built to wrap itk. I didn't explore it, though, here
        a link: http://www.itk.org/HTML/CableSwig.html
        • Re: Wrapping with SWIG + Templates

          Posted by Dan Popov at September 11. 2007
          >So, it seems creating a wrapper for each specialization of a template class is sufficient for salome.<br >

          Indeed, it is probably the one and only method to wrap templates at this time =)
        • Re: Wrapping with SWIG + Templates

          Posted by Dan Popov at September 11. 2007
          >So, it seems creating a wrapper for each specialization of a template class is sufficient for salome.<br >

          Moreover, it is probably the one and only method to wrap templates at this time =)
Powered by Ploneboard
Document Actions