
The SWIG documentation is being updated to reflect new SWIG features and enhancements. However, this update process is not quite finished--there is a lot of old SWIG-1.1 documentation and it is taking some time to update all of it. Please pardon our dust (or volunteer to help!).
This documentation has not been completely updated from SWIG-1.1, but most of the topics still apply to the current release. Make sure you read the SWIG Basics chapter before reading any of these chapters. Also, SWIG-1.3.10 features extensive changes to the implementation of typemaps. Make sure you read the Typemaps chapter above if you are using this feature.
Although SWIG was originally developed for scientific applications, it has since evolved into a general purpose tool that is used in a wide variety of applications--in fact almost anything where C/C++ programming is involved.
http://www.swig.org
This site contains the latest version of the software, users guide, and information regarding bugs, installation problems, and implementation tricks.
You can also subscribe to the SWIG mailing list by visiting the page
http://www.swig.org/mail.html
The mailing list often discusses some of the more technical aspects of SWIG along with information about beta releases and future work.
CVS access to the latest version of SWIG is also available. More information about this can be obtained at:
http://www.swig.org/cvs.html
This manual assumes that you know how to write C/C++ programs and that you have at least heard of scripting languages such as Tcl, Python, and Perl. A detailed knowledge of these scripting languages is not required although some familiarity won't hurt. No prior experience with building C extensions to these languages is required---after all, this is what SWIG does automatically. However, you should be reasonably familiar with the use of compilers, linkers, and makefiles since making scripting language extensions is somewhat more complicated than writing a normal C program.
Recent SWIG releases have become significantly more capable in their C++ handling--especially support for advanced features like namespaces, overloaded operators, and templates. Whenever possible, this manual tries to cover the technicalities of this interface. However, this isn't meant to be a tutorial on C++ programming. For many of the gory details, you will almost certainly want to consult a good C++ reference. If you don't program in C++, you may just want to skip those parts of the manual.
If you are a previous user of SWIG, don't expect recent versions of SWIG to provide backwards compatibility. In fact, backwards compatibility issues may arise even between successive 1.3.x releases. Although these incompatibilities are regrettable, SWIG-1.3 is an active development project. The primary goal of this effort is to make SWIG better---a process that would simply be impossible if the developers are constantly bogged down with backwards compatibility issues.
On a positive note, a few incompatibilities are a small price to pay for the large number of new features that have been added---namespaces, templates, smart pointers, overloaded methods, operators, and more.
If you need to work with different versions of SWIG and backwards compatibility is an issue, you can use the SWIG_VERSION preprocessor symbol which holds the version of SWIG being executed. SWIG_VERSION is a hexadecimal integer such as 0x010311 (corresponding to SWIG-1.3.11). This can be used in an interface file to define different typemaps, take advantage of different features etc:
#if SWIG_VERSION >= 0x010311 /* Use some fancy new feature */ #endif
Note: The version symbol is not defined in the generated SWIG wrapper file. The SWIG preprocessor has defined SWIG_VERSION since SWIG-1.3.11.
Historically, the following people contributed to early versions of SWIG. Peter Lomdahl, Brad Holian, Shujia Zhou, Niels Jensen, and Tim Germann at Los Alamos National Laboratory were the first users. Patrick Tullmann at the University of Utah suggested the idea of automatic documentation generation. John Schmidt and Kurtis Bleeker at the University of Utah tested out the early versions. Chris Johnson supported SWIG's developed at the University of Utah. John Buckman, Larry Virden, and Tom Schwaller provided valuable input on the first releases and improving the portability of SWIG. David Fletcher and Gary Holt have provided a great deal of input on improving SWIG's Perl5 implementation. Kevin Butler contributed the first Windows NT port.
SWIG was originally designed to make it extremely easy for scientists and engineers to build extensible scientific software without having to get a degree in software engineering. Because of this, the use of SWIG tends to be somewhat informal and ad-hoc (e.g., SWIG does not require users to provide formal interface specifications as you would find in a dedicated IDL compiler). Although this style of development isn't appropriate for every project, it is particularly well suited to software development in the small; especially the research and development work that is commonly found in scientific and engineering projects.
From the standpoint of C/C++, a lot of people use SWIG because they want to break out of the traditional monolithic C programming model which usually results in programs that resemble this:
SWIG tries to make the problem of C/C++ integration as painless as possible. This allows you to focus on the underlying C program and using the high-level language interface, but not the tedious and complex chore of making the two languages talk to each other. At the same time, SWIG recognizes that all applications are different. Therefore, it provides a wide variety of customization features that let you change almost every aspect of the language bindings. This is the main reason why SWIG has such a large user manual ;-).
/* File : example.c */
double My_variable = 3.0;
/* Compute factorial of n */
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
/* Compute n mod m */
int my_mod(int n, int m) {
return(n % m);
}
Suppose that you wanted to access these functions and the global variable My_variable from Tcl. You start by making a SWIG interface file as shown below (by convention, these files carry a .i suffix) :
/* File : example.i */
%module example
%{
/* Put headers and other declarations here */
%}
extern double My_variable;
extern int fact(int);
extern int my_mod(int n, int m);
The interface file contains ANSI C function prototypes and variable declarations. The %module directive defines the name of the module that will be created by SWIG. The %{,%} block provides a location for inserting additional code such as C header files or additional C declarations.
unix > swig -tcl example.i unix > gcc -c -fpic example.c example_wrap.c -I/usr/local/include unix > gcc -shared example.o example_wrap.o -o example.so unix > tclsh % load ./example.so % fact 4 24 % my_mod 23 7 2 % expr $My_variable + 4.5 7.5 %
The swig command produced a new file called example_wrap.c that should be compiled along with the example.c file. Most operating systems and scripting languages now support dynamic loading of modules. In our example, our Tcl module has been compiled into a shared library that can be loaded into Tcl. When loaded, Tcl can now access the functions and variables declared in the SWIG interface. A look at the file example_wrap.c reveals a hideous mess. However, you almost never need to worry about it.
unix > swig -perl5 example.i unix > gcc -c example.c example_wrap.c \ -I/usr/local/lib/perl5/sun4-solaris/5.003/CORE unix > ld -G example.o example_wrap.o -o example.so # This is for Solaris unix > perl5.003 use example; print example::fact(4), "\n"; print example::my_mod(23,7), "\n"; print $example::My_variable + 4.5, "\n"; <ctrl-d> 24 2 7.5 unix >
unix > swig -python example.i unix > gcc -c -fpic example.c example_wrap.c -I/usr/local/include/python2.0 unix > gcc -shared example.o example_wrap.o -o _example.so unix > python Python 2.0 (#6, Feb 21 2001, 13:29:45) [GCC egcs-2.91.66 19990314/Linux (egcs-1.1.2 release)] on linux2 Type "copyright", "credits" or "license" for more information. >>> import example >>> example.fact(4) 24 >>> example.my_mod(23,7) 2 >>> example.cvar.My_variable + 4.5 7.5
unix > swig -perl5 -module example example.h unix > gcc -c example.c example_wrap.c \ -I/usr/local/lib/perl5/sun4-solaris/5.003/CORE unix > ld -G example.o example_wrap.o -o example.so unix > perl5.003 use example; print example::fact(4), "\n"; print example::my_mod(23,7), "\n"; print $example::My_variable + 4.5, "\n"; <ctrl-d> 24 2 7.5
It is important to stress that SWIG is not a simplistic C++ lexing tool like several apparently similar wrapper generation tools. SWIG not only parses C++, it implements the full C++ type system and it is able to understand C++ semantics. SWIG generates its wrappers with full knowledge of this information. As a result, you will find SWIG to be just as capable of dealing with nasty corner cases as it is in wrapping simple C++ code. In fact, SWIG is able handle C++ code that stresses the very limits of many C++ compilers.
SWIG is a command line tool and as such can be incorporated into any build system that supports invoking external tools/compilers. SWIG is most commonly invoked from within a Makefile, but is also known to be invoked from from popular IDEs such as Microsoft Visual Studio.
If you are using the GNU Autotools ( Autoconf/ Automake / Libtool) to configure SWIG use in your project, the SWIG Autoconf macros can be used. The primary macro is ac_pkg_swig, see http://www.gnu.org/software/ac-archive/htmldoc/ac_pkg_swig.html. The ac_python_devel macro is also helpful for generating Python extensions. See the Autoconf Macro Archive for further information on this and other Autoconf macros.
There is growing support for SWIG in some build tools, for example CMake is a cross-platform, open-source build manager with built in support for SWIG. CMake can detect the SWIG executable and many of the target language libraries for linking against. CMake knows how to build shared libraries and loadable modules on many different operating systems. This allows easy cross platform SWIG development. It also can generate the custom commands necessary for driving SWIG from IDE's and makefiles. All of this can be done from a single cross platform input file. The following example is a CMake input file for creating a python wrapper for the SWIG interface file, example.i:
# This is a CMake example for Python
FIND_PACKAGE(SWIG REQUIRED)
INCLUDE(${SWIG_USE_FILE})
FIND_PACKAGE(PythonLibs)
INCLUDE_DIRECTORIES(${PYTHON_INCLUDE_PATH})
INCLUDE_DIRECTORIES(${CMAKE_CURRENT_SOURCE_DIR})
SET(CMAKE_SWIG_FLAGS "")
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES CPLUSPLUS ON)
SET_SOURCE_FILES_PROPERTIES(example.i PROPERTIES SWIG_FLAGS "-includeall")
SWIG_ADD_MODULE(example python example.i example.cxx)
SWIG_LINK_LIBRARIES(example ${PYTHON_LIBRARIES})
The above example will generate native build files such as
makefiles, nmake files and Visual Studio projects which will invoke
SWIG and compile the generated C++ files into _example.so (UNIX) or
_example.dll (Windows).
Ironically, the freedom that SWIG provides is countered by an extremely conservative approach to code generation. At it's core, SWIG tries to distill even the most advanced C++ code down to a small well-defined set of interface building techniques based on ANSI C programming. Because of this, you will find that SWIG interfaces can be easily compiled by virtually every C/C++ compiler and that they can be used on any platform. Again, this is an important part of staying out of the programmer's way----the last thing any developer wants to do is to spend their time debugging the output of a tool that relies on non-portable or unreliable programming features.
More information on each of the examples is available with the examples distributed with SWIG (Examples/index.html).
The list of required environment variables for each module language is also listed below. They are usually set from the Control Panel and System properties, but this depends on which flavour of Windows you are running. If you don't want to use environment variables then change all occurences of the environment variables in the .dsp files with hard coded values. If you are interested in how the project files are set up there is explanatory information in some of the language module's documentation.
Example using Python 2.1.1:
PYTHON_INCLUDE: d:\python21\include
PYTHON_LIB: d:\python21\libs\python21.lib
Example using ActiveTcl 8.3.3.3
TCL_INCLUDE: d:\tcl\include
TCL_LIB: d:\tcl\lib\tcl83.lib
Example using nsPerl 5.004_04:
PERL5_INCLUDE: D:\nsPerl5.004_04\lib\CORE
PERL5_LIB: D:\nsPerl5.004_04\lib\CORE\perl.lib
Example using JDK1.3:
JAVA_INCLUDE: d:\jdk1.3\include
JAVA_BIN: d:\jdk1.3\bin
Example using Ruby 1.6.4:
RUBY_INCLUDE: D:\ruby\lib\ruby\1.6\i586-mswin32
RUBY_LIB: D:\ruby\lib\mswin32-ruby16.lib
When a scripting language is used to control a C program, the resulting system tends to look as follows:

In this programming model, the scripting language interpreter is used for high level control whereas the underlying functionality of the C/C++ program is accessed through special scripting language "commands." If you have ever tried to write your own simple command interpreter, you might view the scripting language approach to be a highly advanced implementation of that. Likewise, If you have ever used a package such as MATLAB or IDL, it is a very similar model--the interpreter executes user commands and scripts. However, most of the underlying functionality is written in a low-level language like C or Fortran.
The two-language model of computing is extremely powerful because it exploits the strengths of each language. C/C++ can be used for maximal performance and complicated systems programming tasks. Scripting languages can be used for rapid prototyping, interactive debugging, scripting, and access to high-level data structures such associative arrays.
Typically, when you add a new command to a scripting interpreter you need to do two things; first you need to write a special "wrapper" function that serves as the glue between the interpreter and the underlying C function. Then you need to give the interpreter information about the wrapper by providing details about the name of the function, arguments, and so forth. The next few sections illustrate the process.
Suppose you have an ordinary C function like this :
int fact(int n) {
if (n <= 1) return 1;
else return n*fact(n-1);
}
In order to access this function from a scripting language, it is necessary to write a special "wrapper" function that serves as the glue between the scripting language and the underlying C function. A wrapper function must do three things :
As an example, the Tcl wrapper function for the fact() function above example might look like the following :
int wrap_fact(ClientData clientData, Tcl_Interp *interp,
int argc, char *argv[]) {
int result;
int arg0;
if (argc != 2) {
interp->result = "wrong # args";
return TCL_ERROR;
}
arg0 = atoi(argv[1]);
result = fact(arg0);
sprintf(interp->result,"%d", result);
return TCL_OK;
}
Once you have created a wrapper function, the final step is to tell the scripting language about the new function. This is usually done in an initialization function called by the language when the module is loaded. For example, adding the above function to the Tcl interpreter requires code like the following :
int Wrap_Init(Tcl_Interp *interp) {
Tcl_CreateCommand(interp, "fact", wrap_fact, (ClientData) NULL,
(Tcl_CmdDeleteProc *) NULL);
return TCL_OK;
}
When executed, Tcl will now have a new command called "fact " that you can use like any other Tcl command.
Although the process of adding a new function to Tcl has been illustrated, the procedure is almost identical for Perl and Python. Both require special wrappers to be written and both need additional initialization code. Only the specific details are different.
Variable linking refers to the problem of mapping a C/C++ global variable to a variable in the scripting language interpeter. For example, suppose you had the following variable:
double Foo = 3.5;
It might be nice to access it from a script as follows (shown for Perl):
$a = $Foo * 2.3; # Evaluation $Foo = $a + 2.0; # Assignment
To provide such access, variables are commonly manipulated using a pair of get/set functions. For example, whenever the value of a variable is read, a "get" function is invoked. Similarly, whenever the value of a variable is changed, a "set" function is called.
In many languages, calls to the get/set functions can be attached to evaluation and assignment operators. Therefore, evaluating a variable such as $Foo might implicitly call the get function. Similarly, typing $Foo = 4 would call the underlying set function to change the value.
To make constants available, their values can be stored in scripting language variables such as $RED, $BLUE, and $GREEN. Virtually all scripting languages provide C functions for creating variables so installing constants is usually a trivial exercise.#define RED 0xff0000 #define BLUE 0x0000ff #define GREEN 0x00ff00
The most straightforward technique for handling structures is to implement a collection of accessor functions that hide the underlying representation of a structure. For example,
struct Vector {
Vector();
~Vector();
double x,y,z;
};
can be transformed into the following set of functions :Now, from an interpreter these function might be used as follows:Vector *new_Vector(); void delete_Vector(Vector *v); double Vector_x_get(Vector *v); double Vector_y_get(Vector *v); double Vector_y_get(Vector *v); void Vector_x_set(Vector *v, double x); void Vector_y_set(Vector *v, double y); void Vector_z_set(Vector *v, double z);
% set v [new_Vector] % Vector_x_set $v 3.5 % Vector_y_get $v % delete_Vector $v % ...
Since accessor functions provide a mechanism for accessing the internals of an object, the interpreter does not need to know anything about the actual representation of a Vector.
In certain cases, it is possible to use the low-level accessor functions to create a proxy class, also known as a shadow class. A proxy class is a special kind of object that gets created in a scripting language to access a C/C++ class (or struct) in a way that looks like the original structure (that is, it proxies the real C++ class). For example, if you have the following C definition :
class Vector {
public:
Vector();
~Vector();
double x,y,z;
};
A proxy classing mechanism would allow you to access the structure in a more natural manner from the interpreter. For example, in Python, you might want to do this:
>>> v = Vector() >>> v.x = 3 >>> v.y = 4 >>> v.z = -13 >>> ... >>> del v
Similarly, in Perl5 you may want the interface to work like this:
$v = new Vector;
$v->{x} = 3;
$v->{y} = 4;
$v->{z} = -13;
Finally, in Tcl :Vector v v configure -x 3 -y 4 -z 13
When proxy classes are used, two objects are at really work--one in the scripting language, and an underlying C/C++ object. Operations affect both objects equally and for all practical purposes, it appears as if you are simply manipulating a C/C++ object.
The final step in using a scripting language with your C/C++ application is adding your extensions to the scripting language itself. There are two primary approaches for doing this. The preferred technique is to build a dynamically loadable extension in the form a shared library. Alternatively, you can recompile the scripting language interpreter with your extensions added to it.
To create a shared library or DLL, you often need to look at the manual pages for your compiler and linker. However, the procedure for a few common machines is shown below:
# Build a shared library for Solaris gcc -c example.c example_wrap.c -I/usr/local/include ld -G example.o example_wrap.o -o example.so # Build a shared library for Linux agcc -fpic -c example.c example_wrap.c -I/usr/local/include gcc -shared example.o example_wrap.o -o example.so # Build a shared library for Irix gcc -c example.c example_wrap.c -I/usr/local/include ld -shared example.o example_wrap.o -o example.so
To use your shared library, you simply use the corresponding command in the scripting language (load, import, use, etc...). This will import your module and allow you to start using it. For example:
% load ./example.so % fact 4 24 %
When working with C++ codes, the process of building shared libraries may be more complicated--primarily due to the fact that C++ modules may need additional code in order to operate correctly. On many machines, you can build a shared C++ module by following the above procedures, but changing the link line to the following :
c++ -shared example.o example_wrap.o -o example.so
When building extensions as shared libraries, it is not uncommon for your extension to rely upon other shared libraries on your machine. In order for the extension to work, it needs to be able to find all of these libraries at run-time. Otherwise, you may get an error such as the following :
>>> import graph
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "/home/sci/data1/beazley/graph/graph.py", line 2, in ?
import graphc
ImportError: 1101:/home/sci/data1/beazley/bin/python: rld: Fatal Error: cannot
successfully map soname 'libgraph.so' under any of the filenames /usr/lib/libgraph.so:/
lib/libgraph.so:/lib/cmplrs/cc/libgraph.so:/usr/lib/cmplrs/cc/libgraph.so:
>>>
What this error means is that the extension module created by SWIG depends upon a shared library called "libgraph.so" that the system was unable to locate. To fix this problem, there are a few approaches you can take.
With static linking, you rebuild the scripting language interpreter with extensions. The process usually involves compiling a short main program that adds your customized commands to the language and starts the interpreter. You then link your program with a library to produce a new scripting language executable.
Although static linking is supported on all platforms, this is not the preferred technique for building scripting language extensions. In fact, there are very few practical reasons for doing this--consider using shared libraries instead.
This is a subset of commandline options. Additional options are also defined for each target language. A full list can be obtained by typing swig -help or swig -lang -help .swig [ options ] filename -chicken Generate CHICKEN wrappers -csharp Generate C# wrappers -guile Generate Guile wrappers -java Generate Java wrappers -mzscheme Generate Mzscheme wrappers -ocaml Generate Ocaml wrappers -perl Generate Perl wrappers -php Generate PHP wrappers -pike Generate Pike wrappers -python Generate Python wrappers -ruby Generate Ruby wrappers -sexp Generate Lisp S-Expressions wrappers -tcl Generate Tcl wrappers -xml Generate XML wrappers -c++ Enable C++ parsing -Dsymbol Define a preprocessor symbol -Fstandard Display error/warning messages in commonly used format -Fmicrosoft Display error/warning messages in Microsoft format -help Display all options -Idir Add a directory to the file include path -lfile Include a SWIG library file. -module name Set the name of the SWIG module -o outfile Name of output file -outdir dir Set language specific files output directory -swiglib Show location of SWIG library -version Show SWIG version number
The most common format of a SWIG interface is as follows:
%module mymodule
%{
#include "myheader.h"
%}
// Now list ANSI C/C++ declarations
int foo;
int bar(int x);
...
The name of the module is supplied using the special
%module directive (or the -module command line option).
This directive must appear at the beginning of the file and is used to
name the resulting extension module (in addition, this name often
defines a namespace in the target language). If the module name is
supplied on the command line, it overrides the name specified with the
%module directive.
Everything in the %{ ... %} block is simply copied verbatim to the resulting wrapper file created by SWIG. This section is almost always used to include header files and other declarations that are required to make the generated wrapper code compile. It is important to emphasize that just because you include a declaration in a SWIG input file, that declaration does not automatically appear in the generated wrapper code---therefore you need to make sure you include the proper header files in the %{ ... %} section. It should be noted that the text enclosed in %{ ... %} is not parsed or interpreted by SWIG. The %{...%} syntax and semantics in SWIG is analogous to that of the declarations section used in input files to parser generation tools such as yacc or bison.
$ swig -c++ -python -o example_wrap.cpp example.i
The C/C++ output file created by SWIG often contains everything that is needed to construct a extension module for the target scripting language. SWIG is not a stub compiler nor is it usually necessary to edit the output file (and if you look at the output, you probably won't want to). To build the final extension module, the SWIG output file is compiled and linked with the rest of your C/C++ program to create a shared library.
Many target languages will also generate proxy class files in the target language. The default output directory for these language specific files is the same directory as the generated C/C++ file. This can can be modified using the -outdir option. For example:
If the directories cppfiles and pyfiles exist, the following will be generated:$ swig -c++ -python -outdir pyfiles -o cppfiles/example_wrap.cpp example.i
cppfiles/example_wrap.cpp pyfiles/example.py
It should also be noted that the SWIG preprocessor skips all text enclosed inside a %{...%} block. In addition, the preprocessor includes a number of macro handling enhancements that make it more powerful than the normal C preprocessor. These extensions are described in the "Preprocessor" chapter.
Since SWIG directives are not legal C syntax, it is generally not possible to include them in header files. However, SWIG directives can be included in C header files using conditional compilation like this:
SWIG is a special preprocessing symbol defined by SWIG when it is parsing an input file./* header.h --- Some header file */ /* SWIG directives -- only seen if SWIG is running */ #ifdef SWIG %module foo #endif
/* Non-conventional placement of storage specifier (extern) */ const int extern Number; /* Extra declarator grouping */ Matrix (foo); // A global variable /* Extra declarator grouping in parameters */ void bar(Spam (Grok)(Doh));
In practice, few (if any) C programmers actually write code like this since this style is never featured in programming books. However, if you're feeling particularly obfuscated, you can certainly break SWIG (although why would you want to?).
/* Not supported by SWIG */
int foo::bar(int) {
... whatever ...
}
In the event of a parsing error, conditional compilation can be used to skip offending code. For example:
Alternatively, you can just delete the offending code from the interface file.#ifndef SWIG ... some bad declarations ... #endif
One of the reasons why SWIG does not provide a full C++ parser implementation is that it has been designed to work with incomplete specifications and to be very permissive in its handling of C/C++ datatypes (e.g., SWIG can generate interfaces even when there are missing class declarations or opaque datatypes). Unfortunately, this approach makes it extremely difficult to implement certain parts of a C/C++ parser as most compilers use type information to assist in the parsing of more complex declarations (for the truly curious, the primary complication in the implementation is that the SWIG parser does not utilize a separate typedef-name terminal symbol as described on p. 234 of K&R).
In this file, there are two functions sin() and strcmp(), a global variable Foo, and two constants STATUS and VERSION. When SWIG creates an extension module, these declarations are accessible as scripting language functions, variables, and constants respectively. For example, in Tcl:%module example extern double sin(double x); extern int strcmp(const char *, const char *); extern int Foo; #define STATUS 50 #define VERSION "1.1"
Or in Python:% sin 3 5.2335956 % strcmp Dave Mike -1 % puts $Foo 42 % puts $STATUS 50 % puts $VERSION 1.1
>>> example.sin(3)
5.2335956
>>> example.strcmp('Dave','Mike')
-1
>>> print example.cvar.Foo
42
>>> print example.STATUS
50
>>> print example.VERSION
1.1
Whenever possible, SWIG creates an interface that closely
matches the underlying C/C++ code. However, due to subtle differences
between languages, run-time environments, and semantics, it is not
always possible to do so. The next few sections describes various
aspects of this mapping.
Most scripting languages provide a single integer type that is implemented using the int or long datatype in C. The following list shows all of the C datatypes that SWIG will convert to and from integers in the target language:
int short long unsigned signed unsigned short unsigned long unsigned char signed char bool
When an integral value is converted from C, a cast is used to convert it to the representation in the target language. Thus, a 16 bit short in C may be promoted to a 32 bit integer. When integers are converted in the other direction, the value is cast back into the original C type. If the value is too large to fit, it is silently truncated.
unsigned char and signed char are special cases that are handled as small 8-bit integers. Normally, the char datatype is mapped as a one-character ASCII string.
The bool datatype is cast to and from an integer value of 0 and 1 unless the target language provides a special boolean type.
Some care is required when working with large integer values. Most scripting languages use 32-bit integers so mapping a 64-bit long integer may lead to truncation errors. Similar problems may arise with 32 bit unsigned integers (which may appear as large negative numbers). As a rule of thumb, the int datatype and all variations of char and short datatypes are safe to use. For unsigned int and long datatypes, you will need to carefully check the correct operation of your program after it has been wrapped with SWIG.
Although the SWIG parser supports the long long datatype, not all language modules support it. This is because long long usually exceeds the integer precision available in the target language. In certain modules such as Tcl and Perl5, long long integers are encoded as strings. This allows the full range of these numbers to be represented. However, it does not allow long long values to be used in arithmetic expressions. It should also be noted that although long long is part of the ISO C99 standard, it is not universally supported by all C compilers. Make sure you are using a compiler that supports long long before trying to use this type with SWIG.
SWIG recognizes the following floating point types :
float double
Floating point numbers are mapped to and from the natural representation of floats in the target language. This is almost always a C double. The rarely used datatype of long double is not supported by SWIG.
The char datatype is mapped into a NULL terminated ASCII string with a single character. When used in a scripting language it shows up as a tiny string containing the character value. When converting the value back into C, SWIG takes a character string from the scripting language and strips off the first character as the char value. Thus if the value "foo" is assigned to a char datatype, it gets the value `f'.
The char * datatype is handled as a NULL-terminated ASCII string. SWIG maps this into a 8-bit character string in the target scripting language. SWIG converts character strings in the target language to NULL terminated strings before passing them into C/C++. The default handling of these strings does not allow them to have embedded NULL bytes. Therefore, the char * datatype is not generally suitable for passing binary data. However, it is possible to change this behavior by defining a SWIG typemap. See the chapter on Typemaps for details about this.
At this time, SWIG does not provide any special support for Unicode or wide-character strings (the C wchar_t type). This is a delicate topic that is poorly understood by many programmers and not implemented in a consistent manner across languages. For those scripting languages that provide Unicode support, Unicode strings are often available in an 8-bit representation such as UTF-8 that can be mapped to the char * type (in which case the SWIG interface will probably work). If the program you are wrapping uses Unicode, there is no guarantee that Unicode characters in the target language will use the same internal representation (e.g., UCS-2 vs. UCS-4). You may need to write some special conversion functions.
results in a scripting language variable like this:%module example double foo;
Whenever the scripting language variable is used, the underlying C global variable is accessed. Although SWIG makes every attempt to make global variables work like scripting language variables, it is not always possible to do so. For instance, in Python, all global variables must be accessed through a special variable object known as cvar (shown above). In Ruby, variables are accessed as attributes of the module. Other languages may convert variables to a pair of accessor functions. For example, the Java module generates a pair of functions double get_foo() and set_foo(double val) that are used to manipulate the value.# Tcl set foo [3.5] ;# Set foo to 3.5 puts $foo ;# Print the value of foo # Python cvar.foo = 3.5 # Set foo to 3.5 print cvar.foo # Print value of foo # Perl $foo = 3.5; # Set foo to 3.5 print $foo,"\n"; # Print value of foo # Ruby Module.foo = 3.5 # Set foo to 3.5 print Module.foo, "\n" # Print value of foo
Finally, if a global variable has been declared as const, it only supports read-only access. Note: this behavior is new to SWIG-1.3. Earlier versions of SWIG incorrectly handled const and created constants instead.
Constants can be created using #define, enumerations, or a special %constant directive. The following interface file shows a few valid constant declarations :
#define I_CONST 5 // An integer constant
#define PI 3.14159 // A Floating point constant
#define S_CONST "hello world" // A string constant
#define NEWLINE '\n' // Character constant
enum boolean {NO=0, YES=1};
enum months {JAN, FEB, MAR, APR, MAY, JUN, JUL, AUG,
SEP, OCT, NOV, DEC};
%constant double BLAH = 42.37;
#define F_CONST (double) 5 // A floating pointer constant with cast
#define PI_4 PI/4
#define FLAGS 0x04 | 0x08 | 0x40
In #define declarations, the type of a constant
is inferred by syntax. For example, a number with a decimal point is
assumed to be floating point. In addition, SWIG must be able to fully
resolve all of the symbols used in a #define in order for a
constant to actually be created. This restriction is necessary because
#define is also used to define preprocessor macros that are
definitely not meant to be part of the scripting language interface.
For example:In this case, you probably don't want to create a constant called EXTERN (what would the value be?). In general, SWIG will not create constants for macros unless the value can be completely determined by the preprocessor. For instance, in the above example, the declaration#define EXTERN extern EXTERN void foo();
defines a constant because PI was already defined as a constant and the value is known.#define PI_4 PI/4
The use of constant expressions is allowed, but SWIG does not evaluate them. Rather, it passes them through to the output file and lets the C compiler perform the final evaluation (SWIG does perform a limited form of type-checking however).
For enumerations, it is critical that the original enum definition be included somewhere in the interface file (either in a header file or in the %{,%} block). SWIG only translates the enumeration into code needed to add the constants to a scripting language. It needs the original enumeration declaration in order to get the correct enum values as assigned by the C compiler.
The %constant directive is used to more precisely create constants corresponding to different C datatypes. Although it is not usually not needed for simple values, it is more useful when working with pointers and other more complex datatypes. Typically, %constant is only used when you want to add constants to the scripting language interface that are not defined in the original header file.
Starting with SWIG-1.3, all variable declarations, regardless of any use of const, are wrapped as global variables. If a declaration happens to be declared as const, it is wrapped as a read-only variable. To tell if a variable is const or not, you need to look at the right-most occurrence of the const qualifier (that appears before the variable name). If the right-most const occurs after all other type modifiers (such as pointers), then the variable is const. Otherwise, it is not.
Here are some examples of const declarations.
Here is an example of a declaration that is not const :const char a; // A constant character char const b; // A constant character (the same) char *const c; // A constant pointer to a character const char *const d; // A constant pointer to a constant character
const char *e; // A pointer to a constant character. The pointer
// may be modified.
In this case, the pointer e can change---it's
only the value being pointed to that is read-only.
Compatibility Note: One reason for changing SWIG to handle const declarations as read-only variables is that there are many situations where the value of a const variable might change. For example, a library might export a symbol as const in its public API to discourage modification, but still allow the value to change through some other kind of internal mechanism. Furthermore, programmers often overlook the fact that with a constant declaration like char *const, the underlying data being pointed to can be modified--it's only the pointer itself that is constant. In an embedded system, a const declaration might refer to a read-only memory address such as the location of a memory-mapped I/O device port (where the value changes, but writing to the port is not supported by the hardware). Rather than trying to build a bunch of special cases into the const qualifier, the new interpretation of const as "read-only" is simple and exactly matches the actual semantics of const in C/C++. If you really want to create a constant as in older versions of SWIG, use the %constant directive instead. For example:
or%constant double PI = 3.14159;
#ifdef SWIG #define const %constant #endif const double foo = 3.4; const double bar = 23.4; const int spam = 42; #ifdef SWIG #undef const #endif ...
The primary source of problems are functions that might modify string data in place. A classic example would be a function like this:
Although SWIG will certainly generate a wrapper for this, its behavior will be undefined. In fact, it will probably cause your application to crash with a segmentation fault or other memory related problem. This is because s refers to some internal data in the target language---data that you shouldn't be touching.char *strcat(char *s, const char *t)
The bottom line: don't rely on char * for anything other than read-only input values. However, it must be noted that you could change the behavior of SWIG using typemaps.
Pointers to primitive C datatypes such as
int * double *** char **
are fully supported by SWIG. Rather than trying to convert the data being pointed to into a scripting representation, SWIG simply encodes the pointer itself into a representation that contains the actual value of the pointer and a type-tag. Thus, the SWIG representation of the above pointers (in Tcl), might look like this:
_10081012_p_int _1008e124_ppp_double _f8ac_pp_char
A NULL pointer is represented by the string "NULL" or the value 0 encoded with type information.
All pointers are treated as opaque objects by SWIG. Thus, a pointer may be returned by a function and passed around to other C functions as needed. For all practical purposes, the scripting language interface works in exactly the same way as you would use the pointer in a C program. The only difference is that there is no mechanism for dereferencing the pointer since this would require the target language to understand the memory layout of the underlying object.
The scripting language representation of a pointer value should never be manipulated directly. Even though the values shown look like hexadecimal addresses, the numbers used may differ from the actual machine address (e.g., on little-endian machines, the digits may appear in reverse order). Furthermore, SWIG does not normally map pointers into high-level objects such as associative arrays or lists (for example, converting an int * into an list of integers). There are several reasons why SWIG does not do this:
Like C, void * matches any kind of pointer. Furthermore, NULL pointers can be passed to any function that expects to receive a pointer. Although this has the potential to cause a crash, NULL pointers are also sometimes used as sentinel values or to denote a missing/empty value. Therefore, SWIG leaves NULL pointer checking up to the application.
For everything else (structs, classes, arrays, etc...) SWIG applies a very simple rule :
In other words, SWIG manipulates everything else by reference. This model makes sense because most C/C++ programs make heavy use of pointers and SWIG can use the type-checked pointer mechanism already present for handling pointers to basic datatypes.
Although this probably sounds complicated, it's really quite simple. Suppose you have an interface file like this :
%module fileio FILE *fopen(char *, char *); int fclose(FILE *); unsigned fread(void *ptr, unsigned size, unsigned nobj, FILE *); unsigned fwrite(void *ptr, unsigned size, unsigned nobj, FILE *); void *malloc(int nbytes); void free(void *);
In this file, SWIG doesn't know what a FILE is, but since it's used as a pointer, so it doesn't really matter what it is. If you wrapped this module into Python, you can use the functions just like you expect :
# Copy a file def filecopy(source,target): f1 = fopen(source,"r") f2 = fopen(target,"w") buffer = malloc(8192) nbytes = fread(buffer,8192,1,f1) while (nbytes > 0): fwrite(buffer,8192,1,f2) nbytes = fread(buffer,8192,1,f1) free(buffer)
In this case f1, f2, and buffer are all opaque objects containing C pointers. It doesn't matter what value they contain--our program works just fine without this knowledge.
When SWIG encounters an undeclared datatype, it automatically assumes that it is a structure or class. For example, suppose the following function appeared in a SWIG input file:
SWIG has no idea what a "Matrix" is. However, it is obviously a pointer to something so SWIG generates a wrapper using its generic pointer handling code.void matrix_multiply(Matrix *a, Matrix *b, Matrix *c);
Unlike C or C++, SWIG does not actually care whether Matrix has been previously defined in the interface file or not. This allows SWIG to generate interfaces from only partial or limited information. In some cases, you may not care what a Matrix really is as long as you can pass an opaque reference to one around in the scripting language interface.
An important detail to mention is that SWIG will gladly generate wrappers for an interface when there are unspecified type names. However, all unspecified types are internally handled as pointers to structures or classes! For example, consider the following declaration:
If size_t is undeclared, SWIG generates wrappers that expect to receive a type of size_t * (this mapping is described shortly). As a result, the scripting interface might behave strangely. For example:void foo(size_t num);
The only way to fix this problem is to make sure you properly declare type names using typedef.foo(40); TypeError: expected a _p_size_t.
Like C, typedef can be used to define new type names in SWIG. For example:
typedef definitions appearing in a SWIG interface are not propagated to the generated wrapper code. Therefore, they either need to be defined in an included header file or placed in the declarations section like this:typedef unsigned int size_t;
%{
/* Include in the generated wrapper file */
typedef unsigned int size_t;
%}
/* Tell SWIG about it */
typedef unsigned int size_t;
or
%inline %{
typedef unsigned int size_t;
%}
In certain cases, you might be able to include other
header files to collect type information. For example:In this case, you might run SWIG as follows:%module example %import "sys/types.h"
$ swig -I/usr/include -includeall example.i
It should be noted that your mileage will vary greatly here. System headers are notoriously complicated and may rely upon a variety of non-standard C coding extensions (e.g., such as special directives to GCC). Unless you exactly specify the right include directories and preprocessor symbols, this may not work correctly (you will have to experiment).
SWIG tracks typedef declarations and uses this information for run-time type checking. For instance, if you use the above typedef and had the following function declaration:The corresponding wrapper function will accept arguments of type unsigned int * or size_t *.void foo(unsigned int *ptr);
double dot_product(Vector a, Vector b);
To deal with this, SWIG transforms the function to use pointers by creating a wrapper equivalent to the following:
double wrap_dot_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
return dot_product(x,y);
}
In the target language, the dot_product() function now accepts pointers to Vectors instead of Vectors. For the most part, this transformation is transparent so you might not notice.
C functions that return structures or classes datatypes by value are more difficult to handle. Consider the following function:
Vector cross_product(Vector v1, Vector v2);
This function wants to return Vector, but SWIG only really supports pointers. As a result, SWIG creates a wrapper like this:
Vector *wrap_cross_product(Vector *v1, Vector *v2) {
Vector x = *v1;
Vector y = *v2;
Vector *result;
result = (Vector *) malloc(sizeof(Vector));
*(result) = cross(x,y);
return result;
}
or if SWIG was run with the -c++ option:
Vector *wrap_cross(Vector *v1, Vector *v2) {
Vector x = *v1;
Vector y = *v2;
Vector *result = new Vector(cross(x,y)); // Uses default copy constructor
return result;
}
In both cases, SWIG allocates a new object and returns a reference to it. It is up to the user to delete the returned object when it is no longer in use. Clearly, this will leak memory if you are unaware of the implicit memory allocation and don't take steps to free the result. That said, it should be noted that some language modules can now automatically track newly created objects and reclaim memory for you. Consult the documentation for each language module for more details.
It should also be noted that the handling of pass/return by value in C++ has some special cases. For example, the above code fragments don't work correctly if Vector doesn't define a default constructor. The section on SWIG and C++ has more information about this case.
When global variables or class members involving structures are encountered, SWIG handles them as pointers. For example, a global variable like this
Vector unit_i;
gets mapped to an underlying pair of set/get functions like this :
Vector *unit_i_get() {
return &unit_i;
}
void unit_i_set(Vector *value) {
unit_i = *value;
}
Again some caution is in order. A global variable created in this manner will show up as a pointer in the target scripting language. It would be an extremely bad idea to free or destroy such a pointer. Also, C++ classes must supply a properly defined copy constructor in order for assignment to work correctly.
SWIG generates the following code:char *foo;
/* C mode */
void foo_set(char *value) {
if (foo) free(foo);
foo = (char *) malloc(strlen(value)+1);
strcpy(foo,value);
}
/* C++ mode. When -c++ option is used */
void foo_set(char *value) {
if (foo) delete [] foo;
foo = new char[strlen(value)+1];
strcpy(foo,value);
}
If this is not the behavior that you want, consider making
the variable read-only using the %immutable directive.
Alternatively, you might write a short assist-function to set the value
exactly like you want. For example:
%inline %{
void set_foo(char *value) {
strncpy(foo,value, 50);
}
%}
Note: If you write an assist function like this, you will
have to call it as a function from the target scripting language (it
does not work like a variable). For example, in Python you will have to
write:
>>> set_foo("Hello World")
A common mistake with char * variables is to link
to a variable declared like this:In this case, the variable will be readable, but any attempt to change the value results in a segmentation or general protection fault. This is due to the fact that SWIG is trying to release the old value using free or delete when the string literal value currently assigned to the variable wasn't allocated using malloc() or new. To fix this behavior, you can either mark the variable as read-only, write a typemap (as described in Chapter 6), or write a special set function as shown. Another alternative is to declare the variable as an array:char *VERSION = "1.0";
When variables of type const char * are declared, SWIG still generates functions for setting and getting the value. However, the default behavior does not release the previous contents (resulting in a possible memory leak). In fact, you may get a warning message such as this when wrapping such a variable:char VERSION[64] = "1.0";
The reason for this behavior is that const char * variables are often used to point to string literals. For example:example.i:20. Typemap warning. Setting const char * variable may leak memory
Therefore, it's a really bad idea to call free() on such a pointer. On the other hand, it is legal to change the pointer to point to some other value. When setting a variable of this type, SWIG allocates a new string (using malloc or new) and changes the pointer to point to the new value. However, repeated modifications of the value will result in a memory leak since the old value is not released.const char *foo = "Hello World\n";
Arrays are fully supported by SWIG, but they are always handled as pointers instead of mapping them to a special array object or list in the target language. Thus, the following declarations :
int foobar(int a[40]); void grok(char *argv[]); void transpose(double a[20][20]);
are processed as if they were really declared like this:
Like C, SWIG does not perform array bounds checking. It is up to the user to make sure the pointer points a suitably allocated region of memory.int foobar(int *a); void grok(char **argv); void transpose(double (*a)[20]);
Multi-dimensional arrays are transformed into a pointer to an array of one less dimension. For example:
It is important to note that in the C type system, a multidimensional array a[][] is NOT equivalent to a single pointer *a or a double pointer such as **a. Instead, a pointer to an array is used (as shown above) where the actual value of the pointer is the starting memory location of the array. The reader is strongly advised to dust off their C book and re-read the section on arrays before using them with SWIG.int [10]; // Maps to int * int [10][20]; // Maps to int (*)[20] int [10][20][30]; // Maps to int (*)[20][30]
Array variables are supported, but are read-only by default. For example:
In this case, reading the variable 'a' returns a pointer of type int (*)[200] that points to the first element of the array &a[0][0]. Trying to modify 'a' results in an error. This is because SWIG does not know how to copy data from the target language into the array. To work around this limitation, you may want to write a few simple assist functions like this:int a[100][200];
%inline %{
void a_set(int i, int j, int val) {
a[i][j] = val;
}
int a_get(int i, int j) {
return a[i][j];
}
%}
To dynamically create arrays of various sizes and shapes,
it may be useful to write some helper functions in your interface. For
example:
// Some array helpers
%inline %{
/* Create any sort of [size] array */
int *int_array(int size) {
return (int *) malloc(size*sizeof(int));
}
/* Create a two-dimension array [size][10] */
int (*int_array_10(int size))[10] {
return (int (*)[10]) malloc(size*10*sizeof(int));
}
%}
Arrays of char are handled as a special case by
SWIG. In this case, strings in the target language can be stored in the
array. For example, if you have a declaration like this,SWIG generates functions for both getting and setting the value that are equivalent to the following code:char pathname[256];
char *pathname_get() {
return pathname;
}
void pathname_set(char *value) {
strncpy(pathname,value,256);
}
In the target language, the value can be set like a normal
variable.
A read-only variable can be created by using the %immutable directive as shown :
// File : interface.i int a; // Can read/write %immutable; int b,c,d // Read only variables %mutable; double x,y // read/write
The %immutable directive enables read-only mode until it is explicitly disabled using the %mutable directive. As an alternative to turning read-only mode off and on like this, individual declarations can also be tagged as immutable. For example:
Read-only variables are also created when declarations are declared as const. For example:%immutable x; // Make x read-only ... double x; // Read-only (from earlier %immutable directive) double y; // Read-write ...
const int foo; /* Read only variable */ char * const version="1.0"; /* Read only variable */
Compatibility note: Read-only access used to be controlled by a pair of directives %readonly and %readwrite. Although these directives still work, they generate a warning message. Simply change the directives to %immutable; and %mutable; to silence the warning. Don't forget the extra semicolon!
Normally, the name of a C declaration is used when that declaration is wrapped into the target language. However, this may generate a conflict with a keyword or already existing function in the scripting language. To resolve a name conflict, you can use the %rename directive as shown :
// interface.i %rename(my_print) print; extern void print(char *); %rename(foo) a_really_long_and_annoying_name; extern int a_really_long_and_annoying_name;
SWIG still calls the correct C function, but in this case the function print() will really be called "my_print()" in the target language.
The placement of the %rename directive is arbitrary as long as it appears before the declarations to be renamed. A common technique is to write code for wrapping a header file like this:
// interface.i %rename(my_print) print; %rename(foo) a_really_long_and_annoying_name; %include "header.h"
%rename applies a renaming operation to all future occurrences of a name. The renaming applies to functions, variables, class and structure names, member functions, and member data. For example, if you had two-dozen C++ classes, all with a member function named `print' (which is a keyword in Python), you could rename them all to `output' by specifying :
%rename(output) print; // Rename all `print' functions to `output'
SWIG does not normally perform any checks to see if the functions it wraps are already defined in the target scripting language. However, if you are careful about namespaces and your use of modules, you can usually avoid these problems.
Closely related to %rename is the %ignore directive. %ignore instructs SWIG to ignore declarations that match a given identifier. For example:
One use of %ignore is to selectively remove certain declarations from a header file without having to add conditional compilation to the header. However, it should be stressed that this only works for simple declarations. If you need to remove a whole section of problematic code, the SWIG preprocessor should be used instead.%ignore print; // Ignore all declarations named print %ignore _HAVE_FOO_H; // Ignore an include guard constant ... %include "foo.h" // Grab a header file ...
More powerful variants of %rename and %ignore directives can be used to help wrap C++ overloaded functions and methods or C++ methods which use default arguments. This is described in the Ambiguity resolution and renaming section in the C++ chapter.
Compatibility note: Older versions of SWIG provided a special %name directive for renaming declarations. For example:
This directive is still supported, but it is deprecated and should probably be avoided. The %rename directive is more powerful and better supports wrapping of raw header file information.%name(output) extern void print(char *);
SWIG supports default arguments in both C and C++ code. For example:
int plot(double x, double y, int color=WHITE);
In this case, SWIG generates wrapper code where the default arguments are optional in the target language. For example, this function could be used in Tcl as follows :
Although the ANSI C standard does not allow default arguments, default arguments specified in a SWIG interface work with both C and C++.% plot -3.4 7.5 # Use default value % plot -3.4 7.5 10 # set color to 10 instead
Note: There is a subtle semantic issue concerning the use of default arguments and the SWIG generated wrapper code. When default arguments are used in C code, the default values are emitted into the wrappers and the function is invoked with a full set of arguments. This is different to when wrapping C++ where an overloaded wrapper method is generated for each defaulted argument. Please refer to the section on default arguments in the C++ chapter for further details.
When you first wrap something like this into an extension module, you may find the function to be impossible to use. For instance, in Python:int binary_op(int a, int b, int (*op)(int,int));
The reason for this error is that SWIG doesn't know how to map a scripting language function into a C callback. However, existing C functions can be used as arguments provided you install them as constants. One way to do this is to use the %constant directive like this:>>> def add(x,y): ... return x+y ... >>> binary_op(3,4,add) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: Type error. Expected _p_f_int_int__int >>>
In this case, add, sub, and mul become function pointer constants in the target scripting language. This allows you to use them as follows:/* Function with a callback */ int binary_op(int a, int b, int (*op)(int,int)); /* Some callback functions */ %constant int add(int,int); %constant int sub(int,int); %constant int mul(int,int);
Unfortunately, by declaring the callback functions as constants, they are no longer accesible as functions. For example:>>> binary_op(3,4,add) 7 >>> binary_op(3,4,mul) 12 >>>
If you want to make a function available as both a callback function and a function, you can use the %callback and %nocallback directives like this:>>> add(3,4) Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: object is not callable: '_ff020efc_p_f_int_int__int' >>>
/* Function with a callback */
int binary_op(int a, int b, int (*op)(int,int));
/* Some callback functions */
%callback("%s_cb")
int add(int,int);
int sub(int,int);
int mul(int,int);
%nocallback
The argument to %callback is a printf-style
format string that specifies the naming convention for the callback
constants (%s gets replaced by the function name). The
callback mode remains in effect until it is explicitly disabled using
%nocallback. When you do this, the interface now works as follows:Notice that when the function is used as a callback, special names such as add_cb is used instead. To call the function normally, just use the original function name such as add().>>> binary_op(3,4,add_cb) 7 >>> binary_op(3,4,mul_cb) 12 >>> add(3,4) 7 >>> mul(3,4) 12
SWIG provides a number of extensions to standard C printf formatting that may be useful in this context. For instance, the following variation installs the callbacks as all upper-case constants such as ADD, SUB, and MUL:
/* Some callback functions */
%callback("%(upper)s")
int add(int,int);
int sub(int,int);
int mul(int,int);
%nocallback
A format string of "%(lower)s" converts all
characters to lower-case. A string of "%(title)s" capitalizes
the first character and converts the rest to lower case.
And now, a final note about function pointer support. Although SWIG does not normally allow callback functions to be written in the target language, this can be accomplished with the use of typemaps and other advanced SWIG features. This is described in a later chapter.
If SWIG encounters the definition of a structure or union, it creates a set of accessor functions. Although SWIG does not need structure definitions to build an interface, providing definitions make it possible to access structure members. The accessor functions generated by SWIG simply take a pointer to an object and allow access to an individual member. For example, the declaration :
struct Vector {
double x,y,z;
}
gets transformed into the following set of accessor functions :
double Vector_x_get(struct Vector *obj) {
return obj->x;
}
double Vector_y_get(struct Vector *obj) {
return obj->y;
}
double Vector_z_get(struct Vector *obj) {
return obj->z;
}
void Vector_x_set(struct Vector *obj, double value) {
obj->x = value;
}
void Vector_y_set(struct Vector *obj, double value) {
obj->y = value;
}
void Vector_z_set(struct Vector *obj, double value) {
obj->z = value;
}
In addition, SWIG creates default constructor and
destructor functions if none are defined in the interface. For example:
struct Vector *new_Vector() {
return (Vector *) calloc(1,sizeof(struct Vector));
}
void delete_Vector(struct Vector *obj) {
free(obj);
}
Using these low-level accessor functions, an object can be
minimally manipulated from the target language using code like this:However, most of SWIG's language modules also provide a high-level interface that is more convenient. Keep reading.v = new_Vector() Vector_x_set(v,2) Vector_y_set(v,10) Vector_z_set(v,-5) ... delete_Vector(v)
SWIG supports the following construct which is quite common in C programs :
typedef struct {
double x,y,z;
} Vector;
When encountered, SWIG assumes that the name of the object
is `Vector' and creates accessor functions like before. The only
difference is that the use of typedef allows SWIG to drop the
struct keyword on its generated code. For example:
double Vector_x_get(Vector *obj) {
return obj->x;
}
If two different names are used like this :
typedef struct vector_struct {
double x,y,z;
} Vector;
the name Vector is used instead of
vector_struct since this is more typical C programming style. If
declarations defined later in the interface use the type struct
vector_struct, SWIG knows that this is the same as Vector
and it generates the appropriate type-checking code.
Structures involving character strings require some care. SWIG assumes that all members of type char * have been dynamically allocated using malloc() and that they are NULL-terminated ASCII strings. When such a member is modified, the previously contents will be released, and the new contents allocated. For example :
%module mymodule
...
struct Foo {
char *name;
...
}
This results in the following accessor functions :
char *Foo_name_get(Foo *obj) {
return Foo->name;
}
char *Foo_name_set(Foo *obj, char *c) {
if (obj->name) free(obj->name);
obj->name = (char *) malloc(strlen(c)+1);
strcpy(obj->name,c);
return obj->name;
}
If this behavior differs from what you need in your applications, the SWIG "memberin" typemap can be used to change it. See the typemaps chapter for further details.
Note: If the -c++ option is used, new and delete are used to perform memory allocation.
Arrays may appear as the members of structures, but they will be read-only. SWIG will write an accessor function that returns the pointer to the first element of the array, but will not write a function to change the contents of the array itself. When this situation is detected, SWIG may generate a warning message such as the following :
To eliminate the warning message, typemaps can be used, but this is discussed in a later chapter. In many cases, the warning message is harmless.interface.i:116. Warning. Array member will be read-only
typedef struct Foo {
int x;
} Foo;
typedef struct Bar {
int y;
Foo f; /* struct member */
} Bar;
When a structure member is wrapped, it is always handled
as a pointer. For example:
Foo *Bar_f_get(Bar *b) {
return &b->f;
}
void Bar_f_set(Bar *b, Foo *value) {
b->f = *value;
}
The reasons for this are somewhat subtle but have to do
with the problem of modifying and accessing data inside the data
member. For example, suppose you wanted to modify the value of f.x
of a Bar object like this:Translating this assignment to function calls (as would be used inside the scripting language interface) results in the following code:Bar *b; b->f.x = 37;
In this code, if the Bar_f_get() function were to return a Foo instead of a Foo *, then the resulting modification would be applied to a copy of f and not the data member f itself. Clearly that's not what you want!Bar *b; Foo_x_set(Bar_f_get(b),37);
It should be noted that this transformation to pointers only occurs if SWIG knows that a data member is a structure or class. For instance, if you had a structure like this,
struct Foo {
WORD w;
};
and nothing was known about WORD, then SWIG will
generate more normal accessor functions like this:
WORD Foo_w_get(Foo *f) {
return f->w;
}
void Foo_w_set(FOO *f, WORD value) {
f->w = value;
}
Compatibility Note: SWIG-1.3.11 and earlier
releases transformed all non-primitive member datatypes to pointers.
Starting in SWIG-1.3.12, this transformation only occurs if a
datatype is known to be a structure, class, or union. This is unlikely
to break existing code. However, if you need to tell SWIG that an
undeclared datatype is really a struct, simply use a forward struct
declaration such as "struct Foo;".
If you don't want SWIG to generate constructors and destructors, you can use the %nodefault directive or the -no_default command line option. For example:
orswig -no_default example.i
If you need more precise control, %nodefault can selectively target individual structure definitions. For example:%module foo ... %nodefault; // Don't create default constructors/destructors ... declarations ... %makedefault; // Reenable default constructors/destructors
%nodefault Foo; // No default constructor/destructors for Foo
...
struct Foo { // No default generated.
};
struct Bar { // Default constructor/destructor generated.
};
Compatibility note: Prior to SWIG-1.3.7, SWIG did
not generate default constructors or destructors unless you explicitly
turned them on using -make_default. However, it appears that
most users want to have constructor and destructor functions so it has
now been enabled as the default behavior.
Most languages provide a mechanism for creating classes and supporting object oriented programming. From a C standpoint, object oriented programming really just boils down to the process of attaching functions to structures. These functions normally operate on an instance of the structure (or object). Although there is a natural mapping of C++ to such a scheme, there is no direct mechanism for utilizing it with C code. However, SWIG provides a special %extend directive that makes it possible to attach methods to C structures for purposes of building an object oriented interface. Suppose you have a C header file with the following declaration :
/* file : vector.h */
...
typedef struct {
double x,y,z;
} Vector;
You can make a Vector look alot like a class by
writing a SWIG interface like this:
// file : vector.i
%module mymodule
%{
#include "vector.h"
%}
%include vector.h // Just grab original C header file
%extend Vector { // Attach these functions to struct Vector
Vector(double x, double y, double z) {
Vector *v;
v = (Vector *) malloc(sizeof(Vector));
v->x = x;
v->y = y;
v->z = z;
return v;
}
~Vector() {
free(self);
}
double magnitude() {
return sqrt(self->x*self->x+self->y*self->y+self->z*self->z);
}
void print() {
printf("Vector [%g, %g, %g]\n", self->x,self->y,self->z);
}
};
Now, when used with proxy classes in Python, you can do things like this :
>>> v = Vector(3,4,0) # Create a new vector >>> print v.magnitude() # Print magnitude 5.0 >>> v.print() # Print it out [ 3, 4, 0 ] >>> del v # Destroy it
The %extend directive can also be used inside the definition of the Vector structure. For example:
// file : vector.i
%module mymodule
%{
#include "vector.h"
%}
typedef struct {
double x,y,z;
%extend {
Vector(double x, double y, double z) { ... }
~Vector() { ... }
...
}
} Vector;
Finally, %extend can be used to access externally written functions provided they follow the naming convention used in this example :
/* File : vector.c */
/* Vector methods */
#include "vector.h"
Vector *new_Vector(double x, double y, double z) {
Vector *v;
v = (Vector *) malloc(sizeof(Vector));
v->x = x;
v->y = y;
v->z = z;
return v;
}
void delete_Vector(Vector *v) {
free(v);
}
double Vector_magnitude(Vector *v) {
return sqrt(v->x*v->x+v->y*v->y+v->z*v->z);
}
// File : vector.i
// Interface file
%module mymodule
%{
#include "vector.h"
%}
typedef struct {
double x,y,z;
%extend {
Vector(int,int,int); // This calls new_Vector()
~Vector(); // This calls delete_Vector()
double magnitude(); // This will call Vector_magnitude()
...
}
} Vector;
A little known feature of the %extend directive
is that it can also be used to add synthesized attributes or to modify
the behavior of existing data attributes. For example, suppose you
wanted to make magnitude a read-only attribute of Vector
instead of a method. To do this, you might write some code like this:
// Add a new attribute to Vector
%extend Vector {
const double magnitude;
}
// Now supply the implementation of the Vector_magnitude_get function
%{
const double Vector_magnitude_get(Vector *v) {
return (const double) return sqrt(v->x*v->x+v->y*v->y+v->z*v->z);
}
%}
Now, for all practial purposes, magnitude will
appear like an attribute of the object.
A similar technique can also be used to work with problematic data members. For example, consider this interface:
struct Person {
char name[50];
...
}
By default, the name attribute is read-only
because SWIG does not normally know how to modify arrays. However, you
can rewrite the interface as follows to change this:
struct Person {
%extend {
char *name;
}
...
}
// Specific implementation of set/get functions
%{
char *Person_name_get(Person *p) {
return p->name;
}
void Person_name_set(Person *p, char *val) {
strncpy(p->name,val,50);
}
%}
Finally, it should be stressed that even though
%extend can be used to add new data members, these new members can
not require the allocation of additional storage in the object (e.g.,
their values must be entirely synthesized from existing attributes of
the structure).
Compatibility note: The %extend directive is a new name for the %addmethods directive. Since %addmethods could be used to extend a structure with more than just methods, a more suitable directive name has been chosen.
Occasionally, a C program will involve structures like this :
typedef struct Object {
int objtype;
union {
int ivalue;
double dvalue;
char *strvalue;
void *ptrvalue;
} intRep;
} Object;
When SWIG encounters this, it performs a structure splitting operation that transforms the declaration into the equivalent of the following:
typedef union {
int ivalue;
double dvalue;
char *strvalue;
void *ptrvalue;
} Object_intRep;
typedef struct Object {
int objType;
Object_intRep intRep;
} Object;
SWIG will then create an Object_intRep structure for use inside the interface file. Accessor functions will be created for both structures. In this case, functions like this would be created :
Object_intRep *Object_intRep_get(Object *o) {
return (Object_intRep *) &o->intRep;
}
int Object_intRep_ivalue_get(Object_intRep *o) {
return o->ivalue;
}
int Object_intRep_ivalue_set(Object_intRep *o, int value) {
return (o->ivalue = value);
}
double Object_intRep_dvalue_get(Object_intRep *o) {
return o->dvalue;
}
... etc ...
Although this process is a little hairy, it works like you
would expect in the target scripting language--especially when proxy
classes are used. For instance, in Perl:
# Perl5 script for accessing nested member
$o = CreateObject(); # Create an object somehow
$o->{intRep}->{ivalue} = 7 # Change value of o.intRep.ivalue
If you have a lot nested structure declarations, it is advisable to double-check them after running SWIG. Although, there is a good chance that they will work, you may have to modify the interface file in certain cases.
SWIG doesn't care if the declaration of a structure in a .i file exactly matches that used in the underlying C code (except in the case of nested structures). For this reason, there are no problems omitting problematic members or simply omitting the structure definition altogether. If you are happy passing pointers around, this can be done without ever giving SWIG a structure definition.
Starting with SWIG1.3, a number of improvements have been made to SWIG's code generator. Specifically, even though structure access has been described in terms of high-level accessor functions such as this,
double Vector_x_get(Vector *v) {
return v->x;
}
most of the generated code is actually inlined directly
into wrapper functions. Therefore, no function Vector_x_get()
actually exists in the generated wrapper file. For example, when
creating a Tcl module, the following function is generated instead:
static int
_wrap_Vector_x_get(ClientData clientData, Tcl_Interp *interp,
int objc, Tcl_Obj *CONST objv[]) {
struct Vector *arg1 ;
double result ;
if (SWIG_GetArgs(interp, objc, objv,"p:Vector_x_get self ",&arg0,
SWIGTYPE_p_Vector) == TCL_ERROR)
return TCL_ERROR;
result = (double ) (arg1->x);
Tcl_SetObjResult(interp,Tcl_NewDoubleObj((double) result));
return TCL_OK;
}
The only exception to this rule are methods defined with
%extend. In this case, the added code is contained in a separate
function.
Finally, it is important to note that most language modules may choose to build a more advanced interface. Although you may never use the low-level interface described here, most of SWIG's language modules use it in some way or another.
Sometimes it is necessary to insert special code into the resulting wrapper file generated by SWIG. For example, you may want to include additional C code to perform initialization or other operations. There are four common ways to insert code, but it's useful to know how the output of SWIG is structured first.
%runtime %{
... code in runtime section ...
%}
%header %{
... code in header section ...
%}
%wrapper %{
... code in wrapper section ...
%}
%init %{
... code in init section ...
%}
The bare %{ ... %} directive is a shortcut that
is the same as %header %{ ... %}.
Everything in a code insertion block is copied verbatim into the output file and is not parsed by SWIG. Most SWIG input files have at least one such block to include header files and support C code. Additional code blocks may be placed anywhere in a SWIG file as needed.
%module mymodule
%{
#include "my_header.h"
%}
... Declare functions here
%{
void some_extra_function() {
...
}
%}
A common use for code blocks is to write "helper" functions. These are functions that are used specifically for the purpose of building an interface, but which are generally not visible to the normal C program. For example :
%{
/* Create a new vector */
static Vector *new_Vector() {
return (Vector *) malloc(sizeof(Vector));
}
%}
// Now wrap it
Vector *new_Vector();
Since the process of writing helper functions is fairly common, there is a special inlined form of code block that is used as follows :
%inline %{
/* Create a new vector */
Vector *new_Vector() {
return (Vector *) malloc(sizeof(Vector));
}
%}
The %inline directive inserts all of the code that follows verbatim into the header portion of an interface file. The code is then parsed by both the SWIG preprocessor and parser. Thus, the above example creates a new command new_Vector using only one declaration. Since the code inside an %inline %{ ... %} block is given to both the C compiler and SWIG, it is illegal to include any SWIG directives inside a %{ ... %} block.
When code is included in the %init section, it is copied directly into the module initialization function. For example, if you needed to perform some extra initialization on module loading, you could write this:
%init %{
init_variables();
%}
This section describes the general approach for building interface with SWIG. The specifics related to a particular scripting language are found in later chapters.
SWIG doesn't require modifications to your C code, but if you feed it a collection of raw C header files or source code, the results might not be what you expect---in fact, they might be awful. Here's a series of steps you can follow to make an interface for a C program :
Although this may sound complicated, the process turns out to be fairly easy once you get the hang of it.
In the process of building an interface, SWIG may encounter syntax errors or other problems. The best way to deal with this is to simply copy the offending code into a separate interface file and edit it. However, the SWIG developers have worked very hard to improve the SWIG parser--you should report parsing errors to the swig-dev mailing list or to the SWIG bug tracker.
The preferred method of using SWIG is to generate separate interface file. Suppose you have the following C header file :
/* File : header.h */ #include <stdio.h> #include <math.h> extern int foo(double); extern double bar(int, int); extern void dump(FILE *f);
A typical SWIG interface file for this header file would look like the following :
/* File : interface.i */
%module mymodule
%{
#include "header.h"
%}
extern int foo(double);
extern double bar(int, int);
extern void dump(FILE *f);
Of course, in this case, our header file is pretty simple so we could have made an interface file like this as well:
/* File : interface.i */ %module mymodule %include header.h
Naturally, your mileage may vary.
Although SWIG can parse many header files, it is more common to write a special .i file defining the interface to a package. There are several reasons why you might want to do this:
%module graphics
%{
#include <GL/gl.h>
#include <GL/glu.h>
%}
// Put rest of declarations here
...
If your program defines a main() function, you may need to get rid of it or rename it in order to use a scripting language. Most scripting languages define their own main() procedure that is called instead. main() also makes no sense when working with dynamic loading. There are a few approaches to solving the main() conflict :
Getting rid of main() may cause potential initialization problems of a program. To handle this problem, you may consider writing a special function called program_init() that initializes your program upon startup. This function could then be called either from the scripting language as the first operation, or when the SWIG generated module is loaded.
As a general note, many C programs only use the main() function to parse command line options and to set parameters. However, by using a scripting language, you are probably trying to create a program that is more interactive. In many cases, the old main() program can be completely replaced by a Perl, Python, or Tcl script.
Note: If some cases, you might be inclined to create a scripting language wrapper for main(). If you do this, the compilation will probably work and your module might even load correctly. The only trouble is that when you call your main() wrapper, you will find that it actually invokes the main() of the scripting language interpreter itself! This behavior is a side effect of the symbol binding mechanism used in the dynamic linker. The bottom line: don't do this.
In part, the problem with C++ wrapping is that there is no semantically obvious (or automatic ) way to map many of its advanced features into other languages. As a simple example, consider the problem of wrapping C++ multiple inheritance to a target language with no such support. Similarly, the use of overloaded operators and overloaded functions can be problematic when no such capability exists in a target language.
A more subtle issue with C++ has to do with the way that some C++ programmers think about programming libraries. In the world of SWIG, you are really trying to create binary-level software components for use in other languages. In order for this to work, a "component" has to contain real executable instructions and there has to be some kind of binary linking mechanism for accessing its functionality. In contrast, C++ has increasingly relied upon generic programming and templates for much of its functionality. Although templates are a powerful feature, they are largely orthogonal to the whole notion of binary components and libraries. For example, an STL vector does not define any kind of binary object for which SWIG can just create a wrapper. To further complicate matters, these libraries often utilize a lot of behind the scenes magic in which the semantics of seemingly basic operations (e.g., pointer dereferencing, procedure call, etc.) can be changed in dramatic and sometimes non-obvious ways. Although this "magic" may present few problems in a C++-only universe, it greatly complicates the problem of crossing language boundaries and provides many opportunities to shoot yourself in the foot. You will just have to be careful.
It is important to emphasize that SWIG takes a deliberately conservative and non-intrusive approach to C++ wrapping. SWIG does not encapsulate C++ classes inside special C++ adaptor or proxy classes, it does not rely upon templates, nor does it use C++ inheritance when generating wrappers. The last thing that most C++ programs need is even more compiler magic. Therefore, SWIG tries to maintain a very strict and clean separation between the implementation of your C++ application and the resulting wrapper code. You might say that SWIG has been written to follow the principle of least surprise--it does not play sneaky tricks with the C++ type system, it doesn't mess with your class hierarchies, and it doesn't introduce new semantics. Although this approach might not provide the most seamless integration with C++, it is safe, simple, portable, and debuggable.
Most of this chapter focuses on the low-level procedural interface to C++ that is used as the foundation for all language modules. Keep in mind that most target languages also provide a high-level OO interface via proxy classes. A few general details about proxies can be found at the end of this chapter. However, more detailed coverage can be found in the documentation for each target language.
SWIG's currently supports the following C++ features :
The following C++ features are not currently supported :
SWIG's C++ support is an ongoing project so some of these limitations may be lifted in future releases. However, we make no promises. Also, submitting a bug report is a very good way to get problems fixed (wink).
When wrapping C++ code, it is critical that SWIG be called with the `-c++' option. This changes the way a number of critical features such as memory management are handled. It also enables the recognition of C++ keywords. Without the -c++ flag, SWIG will either issue a warning or a large number of syntax errors if it encounters C++ code in an interface file.
When compiling and linking the resulting wrapper file, it is normal to use the C++ compiler. For example:
$ swig -c++ -tcl example.i $ c++ -c example_wrap.cxx $ c++ example_wrap.o $(OBJS) -o example.so
Unfortunately, the process varies slightly on each machine. Make sure you refer to the documentation on each target language for further details. The SWIG Wiki also has further details.
The following code shows a SWIG interface file for a simple C++ class.
%module list
%{
#include "list.h"
%}
// Very simple C++ example for linked list
class List {
public:
List();
~List();
int search(char *value);
void insert(char *);
void remove(char *);
char *get(int n);
int length;
static void print(List *l);
};
To generate wrappers for this class, SWIG first reduces
the class to a collection of low-level C-style accessor functions. The
next few sections describe this process. Later parts of the chapter
decribe a higher level interface based on proxy classes.
C++ constructors and destructors are translated into accessor functions such as the following :
List * new_List(void) {
return new List;
}
void delete_List(List *l) {
delete l;
}
%nodefault; // Disable creation of constructor/destructor
class Foo {
...
};
%makedefault;
%nodefault can also take a class name. For
example:Compatibility Note: The generation of default constructors/destructors was made the default behavior in SWIG 1.3.7. This may break certain older modules, but the old behavior can be easily restored using %nodefault or the -nodefault command line option. Furthermore, in order for SWIG to properly generate (or not generate) default constructors, it must be able to gather information from both the private and protected sections (specifically, it needs to know if a private or protected constructor/destructor is defined). In older versions of SWIG, it was fairly common to simply remove or comment out the private and protected sections of a class due to parser limitations. However, this removal may now cause SWIG to erroneously generate constructors for classes that define a constructor in those sections. Consider restoring those sections in the interface or using %nodefault to fix the problem.%nodefault Foo; // Disable for class Foo only.
First, SWIG won't generate wrappers for protected or private constructors. For example:
class Foo {
protected:
Foo(); // Not wrapped.
public:
...
};
Next, SWIG won't generate wrappers for a class if it appears to be abstract--that is, it has undefined pure virtual methods. Here are some examples:
class Bar {
public:
Bar(); // Not wrappped. Bar is abstract.
virtual void spam(void) = 0;
};
class Grok : public Bar {
public:
Grok(); // Not wrapped. No implementation of abstract spam().
};
Some users are surprised (or confused) to find missing constructor wrappers in their interfaces. In almost all cases, this is caused when classes are determined to be abstract. To see if this is the case, run SWIG with all of its warnings turned on:
In this mode, SWIG will issue a warning for all abstract classes. It is possible to force a class to be non-abstract using this:% swig -Wall -python module.i
%feature("notabstract") Foo;
class Foo : public Bar {
public:
Foo(); // Generated no matter what---not abstract.
...
};
More information about %feature can be found in
the Customization features chapter.
class List {
public:
List();
List(const List &); // Copy constructor
...
};
then the copy constructor can be used as follows:If the target language does not support overloading, then the copy constructor is available through a special function like this:x = new_List() # Create a list y = new_List(x) # Copy list x
List *copy_List(List *f) {
return new List(*f);
}
Note: For a class X, SWIG only treats a constructor as a copy constructor if it can be applied to an object of type X or X *. If more than one copy constructor is defined, only the first definition that appears is used as the copy constructor--other definitions will result in a name-clash. Constructors such as X(const X &), X(X &), and X(X *) are handled as copy constructors in SWIG.
Note: SWIG does not generate a copy constructor wrapper unless one is explicitly declared in the class. This differs from the treatment of default constructors and destructors.
Compatibility note: Special support for copy constructors was not added until SWIG-1.3.12. In previous versions, copy constructors could be wrapped, but they had to be renamed. For example:
class Foo {
public:
Foo();
%name(CopyFoo) Foo(const Foo &);
...
};
For backwards compatibility, SWIG does not perform any
special copy-constructor handling if the constructor has been manually
renamed. For instance, in the above example, the name of the
constructor is set to new_CopyFoo(). This is the same as in
older versions.
All member functions are roughly translated into accessor functions like this :
int List_search(List *obj, char *value) {
return obj->search(value);
}
This translation is the same even if the member function
has been declared as virtual.
It should be noted that SWIG does not actually create a C accessor function in the code it generates. Instead, member access such as obj->search(value) is directly inlined into the generated wrapper functions. However, the name and calling convention of the wrappers match the accessor function prototype described above.
Usually, static members are accessed as functions with names in which the class name has been prepended with an underscore. For example, List_print.
Member data is handled in exactly the same manner as for C structures. A pair of accessor functions are created. For example :
int List_length_get(List *obj) {
return obj->length;
}
int List_length_set(List *obj, int value) {
obj->length = value;
return value;
}
A read-only member can be created using the %immutable and %mutable directives. For example, we probably wouldn't want the user to change the length of a list so we could do the following to make the value available, but read-only.
class List {
public:
...
%immutable;
int length;
%mutable;
...
};
Alternatively, you can specify an immutable member in
advance like this:
%immutable List::length;
...
class List {
...
int length; // Immutable by above directive
...
};
Similarly, all data attributes declared as const
are wrapped as read-only members.
There are some subtle issues when wrapping data members that are themselves classes. For instance, if you had another class like this,
class Foo {
public:
List items;
...
then access to the items member actually uses
pointers. For example:
List *Foo_items_get(Foo *self) {
return &self->items;
}
void Foo_items_set(Foo *self, List *value) {
self->items = *value;
}
More information about this can be found in the "Structure
data members" section of the SWIG Basics chapter.
Compatibility note: Read-only access used to be controlled by a pair of directives %readonly and %readwrite. Although these directives still work, they generate a warning message. Simply change the directives to %immutable; and %mutable; to silence the warning. Don't forget the extra semicolon!
Compatibility note: Prior to SWIG-1.3.12, all members of unknown type were wrapped into accessor functions using pointers. For example, if you had a structure like this
struct Foo {
size_t len;
};
and nothing was known about size_t, then
accessors would be written to work with size_t *. Starting in
SWIG-1.3.12, this behavior has been modified. Specifically, pointers
will only be used if SWIG knows that a datatype corresponds to
a structure or class. Therefore, the above code would be wrapped into
accessors involving size_t. This change is subtle, but it
smooths over a few problems related to structure wrapping and some of
SWIG's customization features.
class Foo {
public:
void bar(int x, int y = 3, int z = 4);
};
SWIG handles default arguments by generating an extra overloaded method for each defaulted argument. SWIG is effectively handling methods with default arguments as if it had wrapped the equivalent overloaded methods. Thus for the example above, it is as if we had instead given the following to SWIG:
class Foo {
public:
void bar(int x, int y, int z);
void bar(int x, int y);
void bar(int x);
};
The wrappers produced are exactly the same as if the above code was instead fed into SWIG. Details of this is covered later in the Wrapping Overloaded Functions and Methods section. This approach allows SWIG to wrap all possible default arguments, but can be verbose. For example if a method has ten default arguments, then eleven wrapper methods are generated.
Please see the Features and default arguments section for more information on using %feature with functions with default arguments. The Ambiguity resolution and renaming section also deals with using %rename and %ignore on methods with default arguments. If you are writing your own typemaps for types used in methods with default arguments, you may also need to write a typecheck typemap. See the Typemaps and overloading section for details or otherwise use the use the compactdefaultargs feature as mentioned below.
Compatibility note: Versions of SWIG prior to SWIG-1.3.23 wrapped default arguments slightly differently. Instead a single wrapper method was generated and the default values were copied into the C++ wrappers so that the method being wrapped was then called with all the arguments specified. If the size of the wrappers are a concern then this approach to wrapping methods with default arguments can be re-activated by using the compactdefaultargs feature.
%feature("compactdefaultargs") Foo::bar;
class Foo {
public:
void bar(int x, int y = 3, int z = 4);
};
This is great for reducing the size of the wrappers, but the caveat is it does not work for the strongly typed languages which don't have optional arguments in the language, such as C# and Java. Another restriction of this feature is that it cannot handle default arguments that are not public. The following example illustrates this:
class Foo {
private:
static const int spam;
public:
void bar(int x, int y = spam); // Won't work with %feature("compactdefaultargs") -
// private default value
};
This produces uncompileable wrapper code because default values in C++ are evaluated in the same scope as the member function whereas SWIG evaluates them in the scope of a wrapper function (meaning that the values have to be public).
This feature is automatically turned on when wrapping C code with default arguments and whenever keyword arguments (kwargs) are specified for either C or C++ code. Keyword arguments are a language feature of some scripting languages, for example Ruby and Python. SWIG is unable to support kwargs when wrapping overloaded methods, so the default approach cannot be used.
By default, members of a class definition are assumed to be private until you explicitly give a `public:' declaration (This is the same convention used by C++).
Enumerations and constants are handled differently by the different language modules and are described in detail in the appropriate language chapter. However, many languages map enums and constants in a class definition into constants with the classname as a prefix. For example :
class Swig {
public:
enum {ALE, LAGER, PORTER, STOUT};
};
Generates the following set of constants in the target scripting language :
Members declared as const are wrapped as read-only members and do not create constants.Swig_ALE = Swig::ALE Swig_LAGER = Swig::LAGER Swig_PORTER = Swig::PORTER Swig_STOUT = Swig::STOUT
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().
Although the low-level C-like interface is functional, most language modules also produce a higher level lates). mple, if you have this code:
class Foo {
public:
...
friend void blah(Foo *f);
...
};
then the friend declaration does not result in
any wrapper code. On the other hand, a declaration of the function
itself will work fine. For instance:
class Foo {
public:
...
friend void blah(Foo *f); // Ignored
...
};
void blah(Foo *f); // Generates wrappers
Unlike normal member functions or static member functions,
a friend declaration does not define a method that operates on an
instance of an object nor does it define a declaration in the scope of
the class. Therefore, it would make no sense for SWIG to create
wrappers as such.
C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :
class Foo {
public:
double bar(double &a);
}
is accessed using a function similar to this:
double Foo_bar(Foo *obj, double *a) {
obj->bar(*a);
}
As a special case, most language modules pass const
references to primitive datatypes (int, short,
float, etc.) by value instead of pointers. For example, if you have
a function like this,it is called from a script as follows:void foo(const int &x);
Functions that return a reference are remapped to return a pointer instead. For example:foo(3) # Notice pass by value
class Bar {
public:
Foo &spam();
};
Generates code like this:
Foo *Bar_spam(Bar *obj) {
Foo &result = obj->spam();
return &result;
}
However, functions that return const references
to primitive datatypes (int, short, etc.) normally
return the result as a value rather than a pointer. For example, a
function like this,will return integers such as 37 or 42 in the target scripting language rather than a pointer to an integer.const int &bar();
Don't return references to objects allocated as local variables on the stack. SWIG doesn't make a copy of the objects so this will probably cause your program to crash.
Note: The special treatment for references to primitive datatypes is necessary to provide more seamless integration with more advanced C++ wrapping applications---especially related to templates and the STL. This was first added in SWIG-1.3.12.
Occasionally, a C++ program will pass and return class objects by value. For example, a function like this might appear:
Vector cross_product(Vector a, Vector b);
If no information is supplied about Vector, SWIG creates a wrapper function similar to the following:
Vector *wrap_cross_product(Vector *a, Vector *b) {
Vector x = *a;
Vector y = *b;
Vector r = cross_product(x,y);
return new Vector(r);
}
In order for the wrapper code to compile, Vector
must define a copy constructor and a default constructor.
If Vector is defined as class in the interface, but it does not support a default constructor, SWIG changes the wrapper code by encapsulating the arguments inside a special C++ template wrapper class. This produces a wrapper that looks like this:
Vector cross_product(Vector *a, Vector *b) {
SwigValueWrapper<Vector> x = *a;
SwigValueWrapper<Vector> y = *b;
SwigValueWrapper<Vector> r = cross_product(x,y);
return new Vector(r);
}
This transformation is a little sneaky, but it provides
support for pass-by-value even when a class does not provide a default
constructor and it makes it possible to properly support a number of
SWIG's customization options. The definition of SwigValueWrapper
can be found by reading the SWIG wrapper code. This class is really
nothing more than a thin wrapper around a pointer.
Note: this transformation has no effect on typemaps or any other part of SWIG---it should be transparent except that you may see this code when reading the SWIG output file.
Note: This template transformation is new in SWIG-1.3.11 and may be refined in future SWIG releases. In practice, it is only necessary to do this for classes that don't define a default constructor.
Note: The use of this template only occurs when objects are passed or returned by value. It is not used for C++ pointers or references.
Note: The performance of pass-by-value is especially bad for large objects and should be avoided if possible (consider using references instead).
SWIG does not support private or protected inheritance (it is parsed, but it has no effect on the generated code). Note: private and protected inheritance do not define an "isa" relationship between classes so it would have no effect on type-checking anyways.
The following example shows how SWIG handles inheritance. For clarity, the full C++ code has been omitted.
// shapes.i
%module shapes
%{
#include "shapes.h"
%}
class Shape {
public:
double x,y;
virtual double area() = 0;
virtual double perimeter() = 0;
void set_location(double x, double y);
};
class Circle : public Shape {
public:
Circle(double radius);
~Circle();
double area();
double perimeter();
};
class Square : public Shape {
public:
Square(double size);
~Square();
double area();
double perimeter();
}
When wrapped into Python, we can now perform the following operations :
$ python >>> import shapes >>> circle = shapes.new_Circle(7) >>> square = shapes.new_Square(10) >>> print shapes.Circle_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(circle) 153.93804004599999757 >>> print shapes.Shape_area(square) 100.00000000000000000 >>> shapes.Shape_set_location(square,2,-3) >>> print shapes.Shape_perimeter(square) 40.00000000000000000 >>>
In this example, Circle and Square objects have been created. Member functions can be invoked on each object by making calls to Circle_area, Square_area, and so on. However, the same results can be accomplished by simply using the Shape_area function on either object.
One important point concerning inheritance is that the low-level accessor functions are only generated for classes in which they are actually declared. For instance, in the above example, the method set_location() is only accessible as Shape_set_location() and not as Circle_set_location() or Square_set_location() . Of course, the Shape_set_location() function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes x and y are generated as Shape_x_get(), Shape_x_set(), Shape_y_get(), and Shape_y_set(). Functions such as Circle_x_get() are not available--instead you should use Shape_x_get().