Prev Next

@(@\newcommand{\B}[1]{ {\bf #1} } \newcommand{\R}[1]{ {\rm #1} }@)@
Example Using Swig with C++

C++ Includes
This include file defines the interface to the C++ object library.
 
%{
# include "swig_xam.hpp"
%}
see swig_xam.hpp .

py_swig_xam
This is the name of the Python Swig interface to the C++ library.
 
# ifdef SWIGPYTHON
%module py_swig_xam
# endif
see check_swig_xam.py .

m_swig_xam
This is the name of the Octave Swig interface to the C++ library.
 
# ifdef SWIGOCTAVE
%module m_swig_xam
# endif
see check_swig_xam.m .

pl_swig_xam
This is the name of the Perl Swig interface to the C++ library.
 
# ifdef SWIGPERL
%module pl_swig_xam
# endif
see check_swig_xam.pl .

exception
The following standard exceptions can be thrown by the C++ code and caught by the target language code:
     std::exception::runtime_error(const std::string& 
what)
 
%include exception.i
%exception {
   try {
        $action
    } catch (std::runtime_error& e) {
        SWIG_exception(SWIG_RuntimeError, const_cast<char*>( e.what() ) );
    }
}

int_class
This is a Swig interface to a an integer value.
 
%include "cpointer.i"
%pointer_class(int, int_class);

int_array_ptr
This is a Swig interface to a pointer to an array of integer values.
 
%include "carrays.i"
%array_functions(int, int_array_ptr);

int_array_class
This is a Swig interface to a pointer to an integer array class
 
%array_class(int, int_array_class);

vector_double
This is a Swig interface to a std::vector<Type> where Type is double.
 
%include "std_vector.i"
%template(vector_double) std::vector<double>;

swig_xam.hpp
This include file defines the normal functions and normal classes interfaces that are part of this Swig example.
 
%include "swig_xam.hpp"

double_class
This is a Swig interface to a template_class<Type> where Type is double. Note that it uses template_class which is defined as part of swig_xam.hpp.
 
%template(double_class)  template_class<double>;

Contents
swig_xam.hppExample C++ Library That is Called by Swig
check_swig_xam.pySwig Example / Test: Python Script
check_swig_xam.mSwig Example / Test: Octave Script
check_swig_xam.plSwig Example / Test: Perl Script

Input File: swig_xam/swig_xam.i