Python Class Assignment Operator C++

6 SWIG and C++

This chapter describes SWIG's support for wrapping C++. As a prerequisite, you should first read the chapter SWIG Basics to see how SWIG wraps ANSI C. Support for C++ builds upon ANSI C wrapping and that material will be useful in understanding this chapter.

6.1 Comments on C++ Wrapping

Because of its complexity and the fact that C++ can be difficult to integrate with itself let alone other languages, SWIG only provides support for a subset of C++ features. Fortunately, this is now a rather large subset.

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 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.

6.2 Approach

To wrap C++, SWIG uses a layered approach to code generation. At the lowest level, SWIG generates a collection of procedural ANSI-C style wrappers. These wrappers take care of basic type conversion, type checking, error handling, and other low-level details of the C++ binding. These wrappers are also sufficient to bind C++ into any target language that supports built-in procedures. In some sense, you might view this layer of wrapping as providing a C library interface to C++. On top of the low-level procedural (flattened) interface, SWIG generates proxy classes that provide a natural object-oriented (OO) interface to the underlying code. The proxy classes are typically written in the target language itself. For instance, in Python, a real Python class is used to provide a wrapper around the underlying C++ object.

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 a special C++ adaptor, it does not rely upon templates, nor does it add in additional 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.

Some 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 the target languages also provide the high-level OO interface via proxy classes. More detailed coverage can be found in the documentation for each target language.

6.3 Supported C++ features

SWIG currently supports most C++ features including the following:

  • Classes
  • Constructors and destructors
  • Virtual functions
  • Public inheritance (including multiple inheritance)
  • Static functions
  • Function and method overloading
  • Operator overloading for many standard operators
  • References
  • Templates (including specialization and member templates)
  • Pointers to members
  • Namespaces
  • Default parameters
  • Smart pointers

The following C++ features are not currently supported:

  • Overloaded versions of certain operators (new, delete, etc.)

As a rule of thumb, SWIG should not be used on raw C++ source files, use header files only.

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).

6.4 Command line options and compilation

When wrapping C++ code, it is critical that SWIG be called with the `' 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 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++ -fPIC -c example_wrap.cxx $ c++ example_wrap.o $(OBJS) -o example.so

Unfortunately, the process varies slightly on each platform. Make sure you refer to the documentation on each target language for further details. The SWIG Wiki also has further details.

Compatibility Note: Early versions of SWIG generated just a flattened low-level C style API to C++ classes by default. The commandline option is recognised by many target languages and will generate just this interface as in earlier versions.

6.5 Proxy classes

In order to provide a natural mapping from C++ classes to the target language classes, SWIG's target languages mostly wrap C++ classes with special proxy classes. These proxy classes are typically implemented in the target language itself. For example, if you're building a Python module, each C++ class is wrapped by a Python proxy class. Or if you're building a Java module, each C++ class is wrapped by a Java proxy class.

6.5.1 Construction of proxy classes

Proxy classes are always constructed as an extra layer of wrapping that uses low-level accessor functions. To illustrate, suppose you had a C++ class like this:

class Foo { public: Foo(); ~Foo(); int bar(int x); int x; };

Using C++ as pseudocode, a proxy class looks something like this:

class FooProxy { private: Foo *self; public: FooProxy() { self = new_Foo(); } ~FooProxy() { delete_Foo(self); } int bar(int x) { return Foo_bar(self, x); } int x_get() { return Foo_x_get(self); } void x_set(int x) { Foo_x_set(self, x); } };

Of course, always keep in mind that the real proxy class is written in the target language. For example, in Python, the proxy might look roughly like this:

class Foo: def __init__(self): self.this = new_Foo() def __del__(self): delete_Foo(self.this) def bar(self, x): return Foo_bar(self.this, x) def __getattr__(self, name): if name == 'x': return Foo_x_get(self.this) ... def __setattr__(self, name, value): if name == 'x': Foo_x_set(self.this, value) ...

Again, it's important to emphasize that the low-level accessor functions are always used by the proxy classes. Whenever possible, proxies try to take advantage of language features that are similar to C++. This might include operator overloading, exception handling, and other features.

6.5.2 Resource management in proxies

A major issue with proxies concerns the memory management of wrapped objects. Consider the following C++ code:

class Foo { public: Foo(); ~Foo(); int bar(int x); int x; }; class Spam { public: Foo *value; ... };

Consider some script code that uses these classes:

f = Foo() # Creates a new Foo s = Spam() # Creates a new Spam s.value = f # Stores a reference to f inside s g = s.value # Returns stored reference g = 4 # Reassign g to some other value del f # Destroy f

Now, ponder the resulting memory management issues. When objects are created in the script, the objects are wrapped by newly created proxy classes. That is, there is both a new proxy class instance and a new instance of the underlying C++ class. In this example, both and are created in this way. However, the statement is rather curious---when executed, a pointer to is stored inside another object. This means that the scripting proxy class AND another C++ class share a reference to the same object. To make matters even more interesting, consider the statement . When executed, this creates a new proxy class that provides a wrapper around the C++ object stored in . In general, there is no way to know where this object came from---it could have been created by the script, but it could also have been generated internally. In this particular example, the assignment of results in a second proxy class for . In other words, a reference to is now shared by two proxy classes and a C++ class.

Finally, consider what happens when objects are destroyed. In the statement, , the variable is reassigned. In many languages, this makes the old value of available for garbage collection. Therefore, this causes one of the proxy classes to be destroyed. Later on, the statement destroys the other proxy class. Of course, there is still a reference to the original object stored inside another C++ object. What happens to it? Is the object still valid?

To deal with memory management problems, proxy classes provide an API for controlling ownership. In C++ pseudocode, ownership control might look roughly like this:

class FooProxy { public: Foo *self; int thisown; FooProxy() { self = new_Foo(); thisown = 1; // Newly created object } ~FooProxy() { if (thisown) delete_Foo(self); } ... // Ownership control API void disown() { thisown = 0; } void acquire() { thisown = 1; } }; class FooPtrProxy: public FooProxy { public: FooPtrProxy(Foo *s) { self = s; thisown = 0; } }; class SpamProxy { ... FooProxy *value_get() { return FooPtrProxy(Spam_value_get(self)); } void value_set(FooProxy *v) { Spam_value_set(self, v->self); v->disown(); } ... };

Looking at this code, there are a few central features:

  • Each proxy class keeps an extra flag to indicate ownership. C++ objects are only destroyed if the ownership flag is set.
  • When new objects are created in the target language, the ownership flag is set.
  • When a reference to an internal C++ object is returned, it is wrapped by a proxy class, but the proxy class does not have ownership.
  • In certain cases, ownership is adjusted. For instance, when a value is assigned to the member of a class, ownership is lost.
  • Manual ownership control is provided by special and methods.

Given the tricky nature of C++ memory management, it is impossible for proxy classes to automatically handle every possible memory management problem. However, proxies do provide a mechanism for manual control that can be used (if necessary) to address some of the more tricky memory management problems.

6.5.3 Language specific details

Language specific details on proxy classes are contained in the chapters describing each target language. This chapter has merely introduced the topic in a very general way.

6.6 Simple C++ wrapping

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 which are then used by the proxy classes.

6.6.1 Constructors and destructors

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; }

6.6.2 Default constructors, copy constructors and implicit destructors

Following the C++ rules for implicit constructor and destructors, SWIG will automatically assume there is one even when they are not explicitly declared in the class interface.

In general then:

  • If a C++ class does not declare any explicit constructor, SWIG will automatically generate a wrapper for one.
  • If a C++ class does not declare an explicit copy constructor, SWIG will automatically generate a wrapper for one if the is used.
  • If a C++ class does not declare an explicit destructor, SWIG will automatically generate a wrapper for one.

And as in C++, a few rules that alters the previous behavior:

  • A default constructor is not created if a class already defines a constructor with arguments.
  • Default constructors are not generated for classes with pure virtual methods or for classes that inherit from an abstract class, but don't provide definitions for all of the pure methods.
  • A default constructor is not created unless all base classes support a default constructor.
  • Default constructors and implicit destructors are not created if a class defines them in a or section.
  • Default constructors and implicit destructors are not created if any base class defines a non-public default constructor or destructor.

SWIG should never generate a default constructor, copy constructor or default destructor wrapper for a class in which it is illegal to do so. In some cases, however, it could be necessary (if the complete class declaration is not visible from SWIG, and one of the above rules is violated) or desired (to reduce the size of the final interface) by manually disabling the implicit constructor/destructor generation.

To manually disable these, the and feature flag directives can be used. Note that these directives only affects the implicit generation, and they have no effect if the default/copy constructors or destructor are explicitly declared in the class interface.

For example:

%nodefaultctor Foo; // Disable the default constructor for class Foo. class Foo { // No default constructor is generated, unless one is declared ... }; class Bar { // A default constructor is generated, if possible ... };

The directive can also be applied "globally", as in:

%nodefaultctor; // Disable creation of default constructors class Foo { // No default constructor is generated, unless one is declared ... }; class Bar { public: Bar(); // The default constructor is generated, since one is declared }; %clearnodefaultctor; // Enable the creation of default constructors again

The corresponding directive can be used to disable the generation of the default or implicit destructor, if needed. Be aware, however, that this could lead to memory leaks in the target language. Hence, it is recommended to use this directive only in well known cases. For example:

%nodefaultdtor Foo; // Disable the implicit/default destructor for class Foo. class Foo { // No destructor is generated, unless one is declared ... };

Compatibility Note: The generation of default constructors/implicit 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 or the 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 and 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 to fix the problem.

Note: The directive/ options described above, which disable both the default constructor and the implicit destructors, could lead to memory leaks, and so it is strongly recommended to not use them.

6.6.3 When constructor wrappers aren't created

If a class defines a constructor, SWIG normally tries to generate a wrapper for it. However, SWIG will not generate a constructor wrapper if it thinks that it will result in illegal wrapper code. There are really two cases where this might show up.

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 wrapped. 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:

% swig -Wall -python module.i

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:

%feature("notabstract") Foo; class Foo : public Bar { public: Foo(); // Generated no matter what---not abstract. ... };

More information about can be found in the Customization features chapter.

6.6.4 Copy constructors

If a class defines more than one constructor, its behavior depends on the capabilities of the target language. If overloading is supported, the copy constructor is accessible using the normal constructor function. For example, if you have this:

class List { public: List(); List(const List &); // Copy constructor ... };

then the copy constructor can be used as follows:

x = List() # Create a list y = List(x) # Copy list x

If the target language does not support overloading, then the copy constructor is available through a special function like this:

List *copy_List(List *f) { return new List(*f); }

Note: For a class , SWIG only treats a constructor as a copy constructor if it can be applied to an object of type or . 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 , , and 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. However, copy constructor wrappers can be generated if using the feature flag. For example:

%copyctor List; class List { public: List(); };

Will generate a copy constructor wrapper for .

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 . This is the same as in older versions.

6.6.5 Member functions

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 .

It should be noted that SWIG does not actually create a C accessor function in the code it generates. Instead, member access such as is directly inlined into the generated wrapper functions. However, the name and calling convention of the low-level procedural wrappers match the accessor function prototype described above.

6.6.6 Static members

Static member functions are called directly without making any special transformations. For example, the static member function directly invokes in the generated wrapper code.

6.6.7 Member data

Member data is handled in exactly the same manner as for C structures. A pair of accessor functions are effectively 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 and feature flag directive. 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 are wrapped as read-only members.

By default, SWIG uses the const reference typemaps for members that are primitive types. There are some subtle issues when wrapping data members that are not primitive types, such as classes. For instance, if you had another class like this,

class Foo { public: List items; ...

then the low-level accessor to the 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 SWIG Basics chapter, Structure data members section.

The wrapper code to generate the accessors for classes comes from the pointer typemaps. This can be somewhat unnatural for some types. For example, a user would expect the STL std::string class member variables to be wrapped as a string in the target language, rather than a pointer to this class. The const reference typemaps offer this type of marshalling, so there is a feature to tell SWIG to use the const reference typemaps rather than the pointer typemaps. It is the naturalvar feature and can be used to effectively change the way accessors are generated to the following:

const List &Foo_items_get(Foo *self) { return self->items; } void Foo_items_set(Foo *self, const List &value) { self->items = value; }

The directive is a macro for, and hence equivalent to, . It can be used as follows:

// All List variables will use const List& typemaps %naturalvar List; // Only Foo::myList will use const List& typemaps %naturalvar Foo::myList; struct Foo { List myList; }; // All non-primitive types will use const reference typemaps %naturalvar;

The observant reader will notice that works like any other feature flag directive but with some extra flexibility. The first of the example usages above shows attaching to the 's variable type, that is the class. The second usage shows attaching to the variable name. Hence the naturalvar feature can be used on either the variable's name or type. Note that using the naturalvar feature on a variable's name overrides any naturalvar feature attached to the variable's type.

It is generally a good idea to use this feature globally as the reference typemaps have extra NULL checking compared to the pointer typemaps. A pointer can be NULL, whereas a reference cannot, so the extra checking ensures that the target language user does not pass in a value that translates to a NULL pointer and thereby preventing any potential NULL pointer dereferences. The feature will apply to global variables in addition to member variables in some language modules, eg C# and Java.

The naturalvar behavior can also be turned on as a global setting via the commandline option or the module mode option, . However, any use of will override the global setting.

Compatibility note: The feature was introduced in SWIG-1.3.28, prior to which it was necessary to manually apply the const reference typemaps, eg , but this example would also apply the typemaps to methods taking a pointer.

Compatibility note: Read-only access used to be controlled by a pair of directives and . Although these directives still work, they generate a warning message. Simply change the directives to and 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 , then accessors would be written to work with . 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 . This change is subtle, but it smooths over a few problems related to structure wrapping and some of SWIG's customization features.

6.7 Default arguments

SWIG will wrap all types of functions that have default arguments. For example member functions:

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 was wrapping 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 are 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 with functions with default arguments. The Ambiguity resolution and renaming section also deals with using and 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 typemap. See the Typemaps and overloading section for details or otherwise use the feature flag 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 feature flag.

%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 statically typed languages, such as C# and Java, which don't have optional arguments in the language, 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 uncompilable 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).

The feature is automatically turned on when wrapping C code with default arguments. Some target languages will also automatically turn on this feature if the keyword arguments feature (kwargs) is specified for either C or C++ functions, and the target language supports kwargs, the feature is also automatically turned on. 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.

6.8 Protection

SWIG wraps class members that are public following the C++ conventions, i.e., by explicit public declaration or by the use of the directive. In general, anything specified in a private or protected section will be ignored, although the internal code generator sometimes looks at the contents of the private and protected sections so that it can properly generate code for default constructors and destructors. Directors could also modify the way non-public virtual protected members are treated.

By default, members of a class definition are assumed to be private until you explicitly give a `' declaration (This is the same convention used by C++).

6.9 Enums and constants

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 :

Swig_ALE = Swig::ALE Swig_LAGER = Swig::LAGER Swig_PORTER = Swig::PORTER Swig_STOUT = Swig::STOUT

Members declared as are wrapped as read-only members and do not create constants.

6.10 Friends

Friend declarations are recognised by SWIG. For example, if you have this code:

class Foo { public: ... friend void blah(Foo *f); ... };

then the declaration does result in a wrapper code equivalent to one generated for the following declaration

class Foo { public: ... }; void blah(Foo *f);

A friend declaration, as in C++, is understood to be in the same scope where the class is declared, hence, you can have

%ignore bar::blah(Foo *f); namespace bar { class Foo { public: ... friend void blah(Foo *f); ... }; }

and a wrapper for the method 'blah' will not be generated.

6.11 References and pointers

C++ references are supported, but SWIG transforms them back into pointers. For example, a declaration like this :

class Foo { public: double bar(double &a); }

has a low-level accessor

double Foo_bar(Foo *obj, double *a) { obj->bar(*a); }

As a special case, most language modules pass references to primitive datatypes (, , , etc.) by value instead of pointers. For example, if you have a function like this,

void foo(const int &x);

it is called from a script as follows:

foo(3) # Notice pass by value

Functions that return a reference are remapped to return a pointer instead. For example:

class Bar { public: Foo &spam(); };

Generates an accessor like this:

Foo *Bar_spam(Bar *obj) { Foo &result = obj->spam(); return &result; }

However, functions that return references to primitive datatypes (, , 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.

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.

6.12 Pass and return by value

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 , 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, must define a copy constructor and a default constructor.

If is defined as a 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, through a process called the "Fulton Transform". 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 can be found by reading the SWIG wrapper code. This class is really nothing more than a thin wrapper around a pointer.

Although SWIG usually detects the classes to which the Fulton Transform should be applied, in some situations it's necessary to override it. That's done with to ensure it is used and to ensure it is not used:

%feature("novaluewrapper") A; class A; %feature("valuewrapper") B; struct B { B(); // .... };

It is well worth considering turning this feature on for classes that do have a default constructor. It will remove a redundant constructor call at the point of the variable declaration in the wrapper, so will generate notably better performance for large objects or for classes with expensive construction. Alternatively consider returning a reference or 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 absolutely 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.

6.13 Inheritance

SWIG supports C++ inheritance of classes and allows both single and multiple inheritance, as limited or allowed by the target language. The SWIG type-checker knows about the relationship between base and derived classes and allows pointers to any object of a derived class to be used in functions of a base class. The type-checker properly casts pointer values and is safe to use with multiple inheritance.

SWIG treats private or protected inheritance as close to the C++ spirit, and target language capabilities, as possible. In most cases, this means that SWIG will parse the non-public inheritance declarations, but that will have no effect in the generated code, besides the implicit policies derived for constructors and destructors.

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 perform the following operations (shown using the low level Python accessors):

$ 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 , , and so on. However, the same results can be accomplished by simply using the 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 is only accessible as and not as or . Of course, the function will accept any kind of object derived from Shape. Similarly, accessor functions for the attributes and are generated as , , , and . Functions such as are not available--instead you should use .

Note that there is a one to one correlation between the low-level accessor functions and the proxy methods and therefore there is also a one to one correlation between the C++ class methods and the generated proxy class methods.

Note: For the best results, SWIG requires all base classes to be defined in an interface. Otherwise, you may get a warning message like this:

example.i:18: Warning 401: Nothing known about base class 'Foo'. Ignored.

If any base class is undefined, SWIG still generates correct type relationships. For instance, a function accepting a will accept any object derived from regardless of whether or not SWIG actually wrapped the class. If you really don't want to generate wrappers for the base class, but you want to silence the warning, you might consider using the directive to include the file that defines . simply gathers type information, but doesn't generate wrappers. Alternatively, you could just define as an empty class in the SWIG interface or use warning suppression.

Note:-names can be used as base classes. For example:

class Foo { ... }; typedef Foo FooObj; class Bar : public FooObj { // Ok. Base class is Foo ... };

Similarly, allows unnamed structures to be used as base classes. For example:

typedef struct { ... } Foo; class Bar : public Foo { // Ok. ... };

Compatibility Note: Starting in version 1.3.7, SWIG only generates low-level accessor wrappers for the declarations that are actually defined in each class. This differs from SWIG1.1 which used to inherit all of the declarations defined in base classes and regenerate specialized accessor functions such as , , , and . This behavior resulted in huge amounts of replicated code for large class hierarchies and made it awkward to build applications spread across multiple modules (since accessor functions are duplicated in every single module). It is also unnecessary to have such wrappers when advanced features like proxy classes are used. Note: Further optimizations are enabled when using the option, which avoids the regenerating of wrapper functions for virtual members that are already defined in a base class.

6.14 A brief discussion of multiple inheritance, pointers, and type checking

When a target scripting language refers to a C++ object, it normally uses a tagged pointer object that contains both the value of the pointer and a type string. For example, in Tcl, a C++ pointer might be encoded as a string like this:

A somewhat common question is whether or not the type-tag could be safely removed from the pointer. For instance, to get better performance, could you strip all type tags and just use simple integers instead?

In general, the answer to this question is no. In the wrappers, all pointers are converted into a common data representation in the target language. Typically this is the equivalent of casting a pointer to . This means that any C++ type information associated with the pointer is lost in the conversion.

The problem with losing type information is that it is needed to properly support many advanced C++ features--especially multiple inheritance. For example, suppose you had code like this:

class A { public: int x; }; class B { public: int y; }; class C : public A, public B { }; int A_function(A *a) { return a->x; } int B_function(B *b) { return b->y; }

Now, consider the following code that uses .

C *c = new C(); void *p = (void *) c; ... int x = A_function((A *) p); int y = B_function((B *) p);

In this code, both and may legally accept an object of type (via inheritance). However, one of the functions will always return the wrong result when used as shown. The reason for this is that even though points to an object of type , the casting operation doesn't work like you would expect. Internally, this has to do with the data representation of . With multiple inheritance, the data from each base class is stacked together. For example:

------------ <--- (C *), (A *) | A | |------------| <--- (B *) | B | ------------

Because of this stacking, a pointer of type may change value when it is converted to a or . However, this adjustment does not occur if you are converting from a .

The use of type tags marks all pointers with the real type of the underlying object. This extra information is then used by SWIG generated wrappers to correctly cast pointer values under inheritance (avoiding the above problem).

Some of the language modules are able to solve the problem by storing multiple instances of the pointer, for example, , in the A proxy class as well as in the C proxy class. The correct cast can then be made by choosing the correct pointer to use and is guaranteed to work as the cast to a void pointer and back to the same type does not lose any type information:

C *c = new C(); void *p = (void *) c; void *pA = (void *) c; void *pB = (void *) c; ... int x = A_function((A *) pA); int y = B_function((B *) pB);

In practice, the pointer is held as an integral number in the target language proxy class.

6.15 Wrapping Overloaded Functions and Methods

In many language modules, SWIG provides partial support for overloaded functions, methods, and constructors. For example, if you supply SWIG with overloaded functions like this:

void foo(int x) { printf("x is %d\n", x); } void foo(char *x) { printf("x is '%s'\n", x); }

The function is used in a completely natural way. For example:

>>> foo(3) x is 3 >>> foo("hello") x is 'hello' >>>

Overloading works in a similar manner for methods and constructors. For example if you have this code,

class Foo { public: Foo(); Foo(const Foo &); // Copy constructor void bar(int x); void bar(char *s, int y); };

it might be used like this

>>> f = Foo() # Create a Foo >>> f.bar(3) >>> g = Foo(f) # Copy Foo >>> f.bar("hello", 2)

6.15.1 Dispatch function generation

The implementation of overloaded functions and methods is somewhat complicated due to the dynamic nature of scripting languages. Unlike C++, which binds overloaded methods at compile time, SWIG must determine the proper function as a runtime check for scripting language targets. This check is further complicated by the typeless nature of certain scripting languages. For instance, in Tcl, all types are simply strings. Therefore, if you have two overloaded functions like this,

void foo(char *x); void foo(int x);

the order in which the arguments are checked plays a rather critical role.

For statically typed languages, SWIG uses the language's method overloading mechanism. To implement overloading for the scripting languages, SWIG generates a dispatch function that checks the number of passed arguments and their types. To create this function, SWIG first examines all of the overloaded methods and ranks them according to the following rules:

  1. Number of required arguments. Methods are sorted by increasing number of required arguments.
  2. Argument type precedence. All C++ datatypes are assigned a numeric type precedence value (which is determined by the language module).

    Type Precedence ---------------- ---------- TYPE * 0 (High) void * 20 Integers 40 Floating point 60 char 80 Strings 100 (Low)

    Using these precedence values, overloaded methods with the same number of required arguments are sorted in increased order of precedence values.

This may sound very confusing, but an example will help. Consider the following collection of overloaded methods:

void foo(double); void foo(int); void foo(Bar *); void foo(); void foo(int x, int y, int z, int w); void foo(int x, int y, int z = 3); void foo(double x, double y); void foo(double x, Bar *z);

The first rule simply ranks the functions by required argument count. This would produce the following list:

rank ----- [0] foo() [1] foo(double); [2] foo(int); [3] foo(Bar *); [4] foo(int x, int y, int z = 3); [5] foo(double x, double y) [6] foo(double x, Bar *z) [7] foo(int x, int y, int z, int w);

The second rule, simply refines the ranking by looking at argument type precedence values.

rank ----- [0] foo() [1] foo(Bar *); [2] foo(int); [3] foo(double); [4] foo(int x, int y, int z = 3); [5] foo(double x, Bar *z) [6] foo(double x, double y) [7] foo(int x, int y, int z, int w);

Finally, to generate the dispatch function, the arguments passed to an overloaded method are simply checked in the same order as they appear in this ranking.

If you're still confused, don't worry about it---SWIG is probably doing the right thing.

6.15.2 Ambiguity in Overloading

Regrettably, SWIG is not able to support every possible use of valid C++ overloading. Consider the following example:

void foo(int x); void foo(long x);

In C++, this is perfectly legal. However, in a scripting language, there is generally only one kind of integer object. Therefore, which one of these functions do you pick? Clearly, there is no way to truly make a distinction just by looking at the value of the integer itself ( and may even be the same precision). Therefore, when SWIG encounters this situation, it may generate a warning message like this for scripting languages:

example.i:4: Warning 509: Overloaded method foo(long) effectively ignored, example.i:3: Warning 509: as it is shadowed by foo(int).

or for statically typed languages like Java:

example.i:4: Warning 516: Overloaded method foo(long) ignored, example.i:3: Warning 516: using foo(int) instead. at example.i:3 used.

This means that the second overloaded function will be inaccessible from a scripting interface or the method won't be wrapped at all. This is done as SWIG does not know how to disambiguate it from an earlier method.

Ambiguity problems are known to arise in the following situations:

  • Integer conversions. Datatypes such as , , and cannot be disambiguated in some languages. Shown above.
  • Floating point conversion. and can not be disambiguated in some languages.
  • Pointers and references. For example, and .
  • Pointers and arrays. For example, and .
  • Pointers and instances. For example, and . Note: SWIG converts all instances to pointers.
  • Qualifiers. For example, and .
  • Default vs. non default arguments. For example, and .

When an ambiguity arises, methods are checked in the same order as they appear in the interface file. Therefore, earlier methods will shadow methods that appear later.

When wrapping an overloaded function, there is a chance that you will get a warning message like this:

example.i:3: Warning 467: Overloaded foo(int) not supported (incomplete type checking rule - no precedence level in typecheck typemap for 'int').

This error means that the target language module supports overloading, but for some reason there is no type-checking rule that can be used to generate a working dispatch function. The resulting behavior is then undefined. You should report this as a bug to the SWIG bug tracking database if this is due to one of the typemaps supplied with SWIG.

If you get an error message such as the following,

foo.i:6. Overloaded declaration ignored. Spam::foo(double ) foo.i:5. Previous declaration is Spam::foo(int ) foo.i:7. Overloaded declaration ignored. Spam::foo(Bar *, Spam *, int ) foo.i:5. Previous declaration is Spam::foo(int )

it means that the target language module has not yet implemented support for overloaded functions and methods. The only way to fix the problem is to read the next section.

6.15.3 Ambiguity resolution and renaming

If an ambiguity in overload resolution occurs or if a module doesn't allow overloading, there are a few strategies for dealing with the problem. First, you can tell SWIG to ignore one of the methods. This is easy---simply use the directive. For example:

%ignore foo(long); void foo(int); void foo(long); // Ignored. Oh well.

The other alternative is to rename one of the methods. This can be done using . For example:

%rename("foo_short") foo(short); %rename(foo_long) foo(long); void foo(int); void foo(short); // Accessed as foo_short() void foo(long); // Accessed as foo_long()

Note that the quotes around the new name are optional, however, should the new name be a C/C++ keyword they would be essential in order to avoid a parsing error. The and directives are both rather powerful in their ability to match declarations. When used in their simple form, they apply to both global functions and methods. For example:

/* Forward renaming declarations */ %rename(foo_i) foo(int); %rename(foo_d) foo(double); ... void foo(int); // Becomes 'foo_i' void foo(char *c); // Stays 'foo' (not renamed) class Spam { public: void foo(int); // Becomes 'foo_i' void foo(double); // Becomes 'foo_d' ... };

If you only want the renaming to apply to a certain scope, the C++ scope resolution operator (::) can be used. For example:

%rename(foo_i) ::foo(int); // Only rename foo(int) in the global scope. // (will not rename class members) %rename(foo_i) Spam::foo(int); // Only rename foo(int) in class Spam

When a renaming operator is applied to a class as in , it is applied to that class and all derived classes. This can be used to apply a consistent renaming across an entire class hierarchy with only a few declarations. For example:

%rename(foo_i) Spam::foo(int); %rename(foo_d) Spam::foo(double); class Spam { public: virtual void foo(int); // Renamed to foo_i virtual void foo(double); // Renamed to foo_d ... }; class Bar : public Spam { public: virtual void foo(int); // Renamed to foo_i virtual void foo(double); // Renamed to foo_d ... }; class Grok : public Bar { public: virtual void foo(int); // Renamed to foo_i virtual void foo(double); // Renamed to foo_d ... };

It is also possible to include specifications in the class definition itself. For example:

class Spam { %rename(foo_i) foo(int); %rename(foo_d) foo(double); public: virtual void foo(int); // Renamed to foo_i virtual void foo(double); // Renamed to foo_d ... }; class Bar : public Spam { public: virtual void foo(int); // Renamed to foo_i virtual void foo(double); // Renamed to foo_d ... };

In this case, the directives still get applied across the entire inheritance hierarchy, but it's no longer necessary to explicitly specify the class prefix .

A special form of can be used to apply a renaming just to class members (of all classes):

%rename(foo_i) *::foo(int); // Only rename foo(int) if it appears in a class.

Note: the syntax is non-standard C++, but the '*' is meant to be a wildcard that matches any class name (we couldn't think of a better alternative so if you have a better idea, send email to the swig-devel mailing list.

Although this discussion has primarily focused on all of the same rules also apply to . For example:

%ignore foo(double); // Ignore all foo(double) %ignore Spam::foo; // Ignore foo in class Spam %ignore Spam::foo(double); // Ignore foo(double) in class Spam %ignore *::foo(double); // Ignore foo(double) in all classes

When applied to a base class, forces all definitions in derived classes to disappear. For example, will eliminate in and all classes derived from .

Notes on %rename and %ignore:

  • Since, the declaration is used to declare a renaming in advance, it can be placed at the start of an interface file. This makes it possible to apply a consistent name resolution without having to modify header files. For example:

    %module foo /* Rename these overloaded functions */ %rename(foo_i) foo(int); %rename(foo_d) foo(double); %include "header.h"
  • The scope qualifier (::) can also be used on simple names. For example:

    %rename(bar) ::foo; // Rename foo to bar in global scope only %rename(bar) Spam::foo; // Rename foo to bar in class Spam only %rename(bar) *::foo; // Rename foo in classes only
  • Name matching tries to find the most specific match that is defined. A qualified name such as always has higher precedence than an unqualified name . has higher precedence than and has higher precedence than . A parameterized name has higher precedence than an unparameterized name within the same scope level. However, an unparameterized name with a scope qualifier has higher precedence than a parameterized name in global scope (e.g., a renaming of takes precedence over a renaming of ).

  • The order in which directives are defined does not matter as long as they appear before the declarations to be renamed. Thus, there is no difference between saying:

    %rename(bar) foo; %rename(foo_i) Spam::foo(int); %rename(Foo) Spam::foo;

    and this

    %rename(Foo) Spam::foo; %rename(bar) foo; %rename(foo_i) Spam::foo(int);

    (the declarations are not stored in a linked list and order has no importance). Of course, a repeated directive will change the setting for a previous directive if exactly the same name, scope, and parameters are supplied.

  • For multiple inheritance where renaming rules are defined for multiple base classes, the first renaming rule found on a depth-first traversal of the class hierarchy is used.
  • The name matching rules strictly follow member qualification rules. For example, if you have a class like this:

    class Spam { public: ... void bar() const; ... };

    the declaration

    %rename(name) Spam::bar();

    will not apply as there is no unqualified member . The following will apply as the qualifier matches correctly:

    %rename(name) Spam::bar() const;

    An often overlooked C++ feature is that classes can define two different overloaded members that differ only in their qualifiers, like this:

    class Spam { public: ... void bar(); // Unqualified member void bar() const; // Qualified member ... };

    %rename can then be used to target each of the overloaded methods individually. For example we can give them separate names in the target language:

    %rename(name1) Spam::bar(); %rename(name2) Spam::bar() const;

    Similarly, if you merely wanted to ignore one of the declarations, use with the full qualification. For example, the following directive would tell SWIG to ignore the version of above:

    %ignore Spam::bar() const; // Ignore bar() const, but leave other bar() alone
  • Currently no resolution is performed in order to match function parameters. This means function parameter types must match exactly. For example, namespace qualifiers and typedefs will not work. The following usage of typedefs demonstrates this:

    typedef int Integer; %rename(foo_i) foo(int); class Spam { public: void foo(Integer); // Stays 'foo' (not renamed) }; class Ham { public: void foo(int); // Renamed to foo_i };
  • The name matching rules also use default arguments for finer control when wrapping methods that have default arguments. Recall that methods with default arguments are wrapped as if the equivalent overloaded methods had been parsed (Default arguments section). Let's consider the following example class:

    class Spam { public: ... void bar(int i=-1, double d=0.0); ... };

    The following will match exactly and apply to all the target language overloaded methods because the declaration with the default arguments exactly matches the wrapped method:

    %rename(newbar) Spam::bar(int i=-1, double d=0.0);

    The C++ method can then be called from the target language with the new name no matter how many arguments are specified, for example: , or . However, if the does not contain the default arguments, it will only apply to the single equivalent target language overloaded method. So if instead we have:

    %rename(newbar) Spam::bar(int i, double d);

    The C++ method must then be called from the target language with the new name when both arguments are supplied or with the original name as (one argument) or (no arguments). In fact it is possible to use on the equivalent overloaded methods, to rename all the equivalent overloaded methods:

    %rename(bar_2args) Spam::bar(int i, double d); %rename(bar_1arg) Spam::bar(int i); %rename(bar_default) Spam::bar();

    Similarly, the extra overloaded methods can be selectively ignored using .

    Compatibility note: The directive introduced the default argument matching rules in SWIG-1.3.23 at the same time as the changes to wrapping methods with default arguments was introduced.

6.15.4 Comments on overloading

Support for overloaded methods was first added in SWIG-1.3.14. The implementation is somewhat unusual when compared to similar tools. For instance, the order in which declarations appear is largely irrelevant in SWIG. Furthermore, SWIG does not rely upon trial execution or exception handling to figure out which method to invoke.

Internally, the overloading mechanism is completely configurable by the target language module. Therefore, the degree of overloading support may vary from language to language. As a general rule, statically typed languages like Java are able to provide more support than dynamically typed languages like Perl, Python, Ruby, and Tcl.

6.16 Wrapping overloaded operators

C++ overloaded operator declarations can be wrapped. For example, consider a class like this:

class Complex { private: double rpart, ipart; public: Complex(double r = 0, double i = 0) : rpart(r), ipart(i) { } Complex(const Complex &c) : rpart(c.rpart), ipart(c.ipart) { } Complex &operator=(const Complex &c) { rpart = c.rpart; ipart = c.ipart; return *this; } Complex operator+(const Complex &c) const { return Complex(rpart+c.rpart, ipart+c.ipart); } Complex operator-(const Complex &c) const { return Complex(rpart-c.rpart, ipart-c.ipart); } Complex operator*(const Complex &c) const { return Complex(rpart*c.rpart - ipart*c.ipart, rpart*c.ipart + c.rpart*ipart); } Complex operator-() const { return Complex(-rpart, -ipart); } double re() const { return rpart; } double im() const { return ipart; } };

When operator declarations appear, they are handled in exactly the same manner as regular methods. However, the names of these methods are set to strings like "" or "". The problem with these names is that they are illegal identifiers in most scripting languages. For instance, you can't just create a method called "" in Python--there won't be any way to call it.

Some language modules already know how to automatically handle certain operators (mapping them into operators in the target language). However, the underlying implementation of this is really managed in a very general way using the directive. For example, in Python a declaration similar to this is used:

%rename(__add__) Complex::operator+;

This binds the + operator to a method called (which is conveniently the same name used to implement the Python + operator). Internally, the generated wrapper code for a wrapped operator will look something like this pseudocode:

_wrap_Complex___add__(args) { ... get args ... obj->operator+(args); ... }

When used in the target language, it may now be possible to use the overloaded operator normally. For example:

>>> a = Complex(3, 4) >>> b = Complex(5, 2) >>> c = a + b # Invokes __add__ method

It is important to realize that there is nothing magical happening here. The directive really only picks a valid method name. If you wrote this:

The resulting scripting interface might work like this:

a = Complex(3, 4) b = Complex(5, 2) c = a.add(b) # Call a.operator+(b)

All of the techniques described to deal with overloaded functions also apply to operators. For example:

%ignore Complex::operator=; // Ignore = in class Complex %ignore *::operator=; // Ignore = in all classes %ignore operator=; // Ignore = everywhere. %rename(__sub__) Complex::operator-; %rename(__neg__) Complex::operator-(); // Unary -

The last part of this example illustrates how multiple definitions of the method might be handled.

Handling operators in this manner is mostly straightforward. However, there are a few subtle issues to keep in mind:

  • In C++, it is fairly common to define different versions of the operators to account for different types. For example, a class might also include a friend function like this:

    class Complex { public: friend Complex operator+(Complex &, double); }; Complex operator+(Complex &, double);

    SWIG simply ignores all declarations. Furthermore, it doesn't know how to associate the associated with the class (because it's not a member of the class).

    It's still possible to make a wrapper for this operator, but you'll have to handle it like a normal function. For example:

    %rename(add_complex_double) operator+(Complex &, double);
  • Certain operators are ignored by default. For instance, and operators are ignored as well as conversion and index operators. A warning such as the one below is shown:

    example.i:12: Warning 503: Can't wrap 'operator []' unless renamed to a valid identifier.
  • The index operator, , is particularly difficult to overload due to differences in C++ implementations. Specifically, the get and set operators in other languages typically are separated into two methods such that additional logic can be packed into the operations; C# uses , Python uses and , etc. In C++ if the return type of is a reference and the method is const, it is often indicative of the setter, and and the getter is usually a const function return an object by value. In the absence of any hard and fast rules and the fact that there may be multiple index operators, it is up to the user to choose the getter and setter to use by using %rename as shown earlier.

  • The semantics of certain C++ operators may not match those in the target language.

6.17 Class extension

New methods can be added to a class using the directive. This directive is primarily used in conjunction with proxy classes to add additional functionality to an existing class. For example :

%module vector %{ #include "vector.h" %} class Vector { public: double x, y, z; Vector(); ~Vector(); ... bunch of C++ methods ... %extend { char *__str__() { static char temp[256]; sprintf(temp, "[ %g, %g, %g ]", $self->x, $self->y, $self->z); return &temp[0]; } } };

This code adds a method to our class for producing a string representation of the object. In Python, such a method would allow us to print the value of an object using the command.

>>> >>> v = Vector(); >>> v.x = 3 >>> v.y = 4 >>> v.z = 0 >>> print(v) [ 3.0, 4.0, 0.0 ] >>>

The C++ 'this' pointer is often needed to access member variables, methods etc. The special variable should be used wherever you could use 'this'. The example above demonstrates this for accessing member variables. Note that the members dereferenced by must be public members as the code is ultimately generated into a global function and so will not have any access to non-public members. The implicit 'this' pointer that is present in C++ methods is not present in methods. In order to access anything in the extended class or its base class, an explicit 'this' is required. The following example shows how one could access base class members:

struct Base { virtual void method(int v) { ... } int value; }; struct Derived : Base { }; %extend Derived { virtual void method(int v) { $self->Base::method(v); // akin to this->Base::method(v); $self->value = v; // akin to this->value = v; ... } }

The following special variables are expanded if used within a %extend block: $name, $symname, $overname, $decl, $fulldecl, $parentclassname and $parentclasssymname. The Special variables section provides more information each of these special variables.

The directive follows all of the same conventions as its use with C structures. Please refer to the Adding member functions to C structures section for further details.

Compatibility note: The directive is a new name for the directive in SWIG1.1. Since could be used to extend a structure with more than just methods, a more suitable directive name has been chosen.

6.18 Templates

Template type names may appear anywhere a type is expected in an interface file. For example:

void foo(vector<int> *a, int n); void bar(list<int, 100> *x);

There are some restrictions on the use of non-type arguments. Simple literals are supported, and so are some constant expressions. However, use of '<' and '>' within a constant expressions currently is not supported by SWIG ('<=' and '>=' are though). For example:

void bar(list<int, 100> *x); // OK void bar(list<int, 2*50> *x); // OK void bar(list<int, (2>1 ? 100 : 50)> *x) // Not supported

The type system is smart enough to figure out clever games you might try to play with . For instance, consider this code:

typedef int Integer; void foo(vector<int> *x, vector<Integer> *y);

In this case, is exactly the same type as . The wrapper for will accept either variant.

Starting with SWIG-1.3.7, simple C++ template declarations can also be wrapped. SWIG-1.3.12 greatly expands upon the earlier implementation. Before discussing this any further, there are a few things you need to know about template wrapping. First, a bare C++ template does not define any sort of runnable object-code for which SWIG can normally create a wrapper. Therefore, in order to wrap a template, you need to give SWIG information about a particular template instantiation (e.g., , , etc.). Second, an instantiation name such as is generally not a valid identifier name in most target languages. Thus, you will need to give the template instantiation a more suitable name such as when creating a wrapper.

To illustrate, consider the following template definition:

template<class T> class List { private: T *data; int nitems; int maxitems; public: List(int max) { data = new T [max]; nitems = 0; maxitems = max; } ~List() { delete [] data; }; void append(T obj) { if (nitems < maxitems) { data[nitems++] = obj; } } int length() { return nitems; } T get(int n) { return data[n]; } };

By itself, this template declaration is useless--SWIG simply ignores it because it doesn't know how to generate any code until unless a definition of is provided.

One way to create wrappers for a specific template instantiation is to simply provide an expanded version of the class directly like this:

%rename(intList) List<int>; // Rename to a suitable identifier class List<int> { private: int *data; int nitems; int maxitems; public: List(int max); ~List(); void append(int obj); int length(); int get(int n); };

The directive is needed to give the template class an appropriate identifier name in the target language (most languages would not recognize C++ template syntax as a valid class name). The rest of the code is the same as what would appear in a normal class definition.

Since manual expansion of templates gets old in a hurry, the directive can be used to create instantiations of a template class. Semantically, is simply a shortcut---it expands template code in exactly the same way as shown above. Here are some examples:

/* Instantiate a few different versions of the template */ %template(intList) List<int>; %template(doubleList) List<double>;

The argument to is the name of the instantiation in the target language. The name you choose should not conflict with any other declarations in the interface file with one exception---it is okay for the template name to match that of a typedef declaration. For example:

%template(intList) List<int>; ... typedef List<int> intList; // OK

SWIG can also generate wrappers for function templates using a similar technique. For example:

// Function template template<class T> T max(T a, T b) { return a > b ? a : b; } // Make some different versions of this function %template(maxint) max<int>; %template(maxdouble) max<double>;

In this case, and become unique names for specific instantiations of the function.

The number of arguments supplied to should match that in the original template definition. Template default arguments are supported. For example:

template vector<typename T, int max=100> class vector { ... }; %template(intvec) vector<int>; // OK %template(vec1000) vector<int, 1000>; // OK

The directive should not be used to wrap the same template instantiation more than once in the same scope. This will generate an error. For example:

%template(intList) List<int>; %template(Listint) List<int>; // Error. Template already wrapped.

This error is caused because the template expansion results in two identical classes with the same name. This generates a symbol table conflict. Besides, it probably more efficient to only wrap a specific instantiation only once in order to reduce the potential for code bloat.

Since the type system knows how to handle , it is generally not necessary to instantiate different versions of a template for typenames that are equivalent. For instance, consider this code:

%template(intList) vector<int>; typedef int Integer; ... void foo(vector<Integer> *x);

In this case, is exactly the same type as . Any use of is mapped back to the instantiation of created earlier. Therefore, it is not necessary to instantiate a new class for the type (doing so is redundant and will simply result in code bloat).

When a template is instantiated using , information about that class is saved by SWIG and used elsewhere in the program. For example, if you wrote code like this,

... %template(intList) List<int>; ... class UltraList : public List<int> { ... };

then SWIG knows that was already wrapped as a class called and arranges to handle the inheritance correctly. If, on the other hand, nothing is known about , you will get a warning message similar to this:

example.h:42: Warning 401. Nothing known about class 'List<int >'. Ignored. example.h:42: Warning 401. Maybe you forgot to instantiate 'List<int >' using %template.

If a template class inherits from another template class, you need to make sure that base classes are instantiated before derived classes. For example:

template<class T> class Foo { ... }; template<class T> class Bar : public Foo<T> { ... }; // Instantiate base classes first %template(intFoo) Foo<int>; %template(doubleFoo) Foo<double>; // Now instantiate derived classes %template(intBar) Bar<int>; %template(doubleBar) Bar<double>;

The order is important since SWIG uses the instantiation names to properly set up the inheritance hierarchy in the resulting wrapper code (and base classes need to be wrapped before derived classes). Don't worry--if you get the order wrong, SWIG should generate a warning message.

Occasionally, you may need to tell SWIG about base classes that are defined by templates, but which aren't supposed to be wrapped. Since SWIG is not able to automatically instantiate templates for this purpose, you must do it manually. To do this, simply use the empty template instantiation, that is, with no name. For example:

// Instantiate traits<double, double>, but don't wrap it. %template() traits<double, double>;

If you have to instantiate a lot of different classes for many different types, you might consider writing a SWIG macro. For example:

%define TEMPLATE_WRAP(prefix, T...) %template(prefix ## Foo) Foo<T >; %template(prefix ## Bar) Bar<T >; ... %enddef TEMPLATE_WRAP(int, int) TEMPLATE_WRAP(double, double) TEMPLATE_WRAP(String, char *) TEMPLATE_WRAP(PairStringInt, std::pair<string, int>) ...

Note the use of a vararg macro for the type T. If this wasn't used, the comma in the templated type in the last example would not be possible.

The SWIG template mechanism does support specialization. For instance, if you define a class like this,

template<> class List<int> { private: int *data; int nitems; int maxitems; public: List(int max); ~List(); void append(int obj); int length(); int get(int n); };

then SWIG will use this code whenever the user expands . In practice, this may have very little effect on the underlying wrapper code since specialization is often used to provide slightly modified method bodies (which are ignored by SWIG). However, special SWIG directives such as , , and so forth can be attached to a specialization to provide customization for specific types.

Partial template specialization is partially supported by SWIG. For example, this code defines a template that is applied when the template argument is a pointer.

template<class T> class List<T*> { private: T *data; int nitems; int maxitems; public: List(int max); ~List(); void append(int obj); int length(); T get(int n); };

SWIG supports both template explicit specialization and partial specialization. Consider:

template<class T1, class T2> class Foo { }; // (1) primary template template<> class Foo<double *, int *> { }; // (2) explicit specialization template<class T1, class T2> class Foo<T1, T2 *> { }; // (3) partial specialization

SWIG is able to properly match explicit instantiations:

// explicit specialization matching (2)

SWIG implements template argument deduction so that the following partial specialization examples work just like they would with a C++ compiler:

// partial specialization matching (3) // partial specialization matching (3) // partial specialization matching (3)

Member function templates are supported. The underlying principle is the same as for normal templates--SWIG can't create a wrapper unless you provide more information about types. For example, a class with a member template might look like this:

class Foo { public: template<class T> void bar(T x, T y) { ... }; ... };

To expand the template, simply use inside the class.

class Foo { public: template<class T> void bar(T x, T y) { ... }; ... %template(barint) bar<int>; %template(bardouble) bar<double>; };

Or, if you want to leave the original class definition alone, just do this:

class Foo { public: template<class T> void bar(T x, T y) { ... }; ... }; ... %extend Foo { %template(barint) bar<int>; %template(bardouble) bar<double>; };

or simply

class Foo { public: template<class T> void bar(T x, T y) { ... }; ... }; ... %template(bari) Foo::bar<int>; %template(bard) Foo::bar<double>;

In this case, the directive is not needed, and does exactly the same job, i.e., it adds two new methods to the Foo class.

Note: because of the way that templates are handled, the directive must always appear after the definition of the template to be expanded.

Now, if your target language supports overloading, you can even try

%template(bar) Foo::bar<int>; %template(bar) Foo::bar<double>;

and since the two new wrapped methods have the same name 'bar', they will be overloaded, and when called, the correct method will be dispatched depending on the argument type.

When used with members, the directive may be placed in another template class. Here is a slightly perverse example:

// A template template<class T> class Foo { public: // A member template template<class S> T bar(S x, S y) { ... }; ... }; // Expand a few member templates %extend Foo { %template(bari) bar<int>; %template(bard) bar<double>; } // Create some wrappers for the template %template(Fooi) Foo<int>; %template(Food) Foo<double>;

Miraculously, you will find that each expansion of has member functions and added.

A common use of member templates is to define constructors for copies and conversions. For example:

template<class T1, class T2> struct pair { T1 first; T2 second; pair() : first(T1()), second(T2()) { } pair(const T1 &x, const T2 &y) : first(x), second(y) { } template<class U1, class U2> pair(const pair<U1, U2> &x) : first(x.first), second(x.second) { } };

This declaration is perfectly acceptable to SWIG, but the constructor template will be ignored unless you explicitly expand it. To do that, you could expand a few versions of the constructor in the template class itself. For example:

%extend pair { %template(pair) pair<T1, T2>; // Generate default copy constructor };

When using in this manner, notice how you can still use the template parameters in the original template definition.

Alternatively, you could expand the constructor template in selected instantiations. For example:

// Instantiate a few versions %template(pairii) pair<int, int>; %template(pairdd) pair<double, double>; // Create a default constructor only %extend pair<int, int> { %template(paird) pair<int, int>; // Default constructor }; // Create default and conversion constructors %extend pair<double, double> { %template(paird) pair<double, dobule>; // Default constructor %template(pairc) pair<int, int>; // Conversion constructor };

And if your target language supports overloading, then you can try instead:

// Create default and conversion constructors %extend pair<double, double> { %template(pair) pair<double, dobule>; // Default constructor %template(pair) pair<int, int>; // Conversion constructor };

In this case, the default and conversion constructors have the same name. Hence, SWIG will overload them and define an unique visible constructor, that will dispatch the proper call depending on the argument type.

If all of this isn't quite enough and you really want to make someone's head explode, SWIG directives such as , , and can be included directly in template definitions. For example:

// File : list.h template<class T> class List { ... public: %rename(__getitem__) get(int); List(int max); ~List(); ... T get(int index); %extend { char *__str__() { /* Make a string representation */ ... } } };

In this example, the extra SWIG directives are propagated to every template instantiation.

It is also possible to separate these declarations from the template class. For example:

%rename(__getitem__) List::get; %extend List { char *__str__() { /* Make a string representation */ ... } /* Make a copy */ T *__copy__() { return new List<T>(*$self); } }; ... template<class T> class List { ... public: List() { } T get(int index); ... };

When is decoupled from the class definition, it is legal to use the same template parameters as provided in the class definition. These are replaced when the template is expanded. In addition, the directive can be used to add additional methods to a specific instantiation. For example:

%template(intList) List<int>; %extend List<int> { void blah() { printf("Hey, I'm an List<int>!\n"); } };

SWIG even supports overloaded templated functions. As usual the directive is used to wrap templated functions. For example:

template<class T> void foo(T x) { }; template<class T> void foo(T x, T y) { }; %template(foo) foo<int>;

This will generate two overloaded wrapper methods, the first will take a single integer as an argument and the second will take two integer arguments.

It is even possible to extend a class via with template methods, for example:

%include <std_string.i> %inline %{ class ExtendMe { public: template <typename T> T do_stuff_impl(int a, T b, double d) { return b; } }; %} %extend ExtendMe { template<typename T> T do_overloaded_stuff(T b) { return $self->do_stuff_impl(0, b, 4.0); } } %template(do_overloaded_stuff) ExtendMe::do_overloaded_stuff<std::string>; %template(do_overloaded_stuff) ExtendMe::do_overloaded_stuff<double>;

The wrapped class will then have two (overloaded) methods called .

What are operators in python?

Operators are special symbols in Python that carry out arithmetic or logical computation. The value that the operator operates on is called the operand.

For example:

Here, is the operator that performs addition. and are the operands and is the output of the operation.


Arithmetic operators

Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication etc.

OperatorMeaningExample
+Add two operands or unary plusx + y
+2
-Subtract right operand from the left or unary minusx - y
-2
*Multiply two operandsx * y
/Divide left operand by the right one (always results into float)x / y
%Modulus - remainder of the division of left operand by the rightx % y (remainder of x/y)
//Floor division - division that results into whole number adjusted to the left in the number linex // y
**Exponent - left operand raised to the power of rightx**y (x to the power y)

Example #1: Arithmetic operators in Python

x = 15 y = 4 # Output: x + y = 19 print('x + y =',x+y) # Output: x - y = 11 print('x - y =',x-y) # Output: x * y = 60 print('x * y =',x*y) # Output: x / y = 3.75 print('x / y =',x/y) # Output: x // y = 3 print('x // y =',x//y) # Output: x ** y = 50625 print('x ** y =',x**y)

When you run the program, the output will be:


Comparison operators

Comparison operators are used to compare values. It either returns or according to the condition.

OperatorMeaningExample
>Greater that - True if left operand is greater than the rightx > y
<Less that - True if left operand is less than the rightx < y
==Equal to - True if both operands are equalx == y
!=Not equal to - True if operands are not equalx != y
>=Greater than or equal to - True if left operand is greater than or equal to the rightx >= y
<=Less than or equal to - True if left operand is less than or equal to the rightx <= y

Example #2: Comparison operators in Python

x = 10 y = 12 # Output: x > y is False print('x > y is',x>y) # Output: x < y is True print('x < y is',x<y) # Output: x == y is False print('x == y is',x==y) # Output: x != y is True print('x != y is',x!=y) # Output: x >= y is False print('x >= y is',x>=y) # Output: x <= y is True print('x <= y is',x<=y)

Logical operators

Logical operators are the , , operators.

OperatorMeaningExample
andTrue if both the operands are truex and y
orTrue if either of the operands is truex or y
notTrue if operand is false (complements the operand)not x

Example #3: Logical Operators in Python

x = True y = False # Output: x and y is False print('x and y is',x and y) # Output: x or y is True print('x or y is',x or y) # Output: not x is False print('not x is',not x)

Here is the truth table for these operators.


Bitwise operators

Bitwise operators act on operands as if they were string of binary digits. It operates bit by bit, hence the name.

For example, 2 is in binary and 7 is .

In the table below: Let = 10 ( in binary) and = 4 ( in binary)

OperatorMeaningExample
&Bitwise ANDx& y = 0 ()
|Bitwise ORx | y = 14 ()
~Bitwise NOT~x = -11 ()
^Bitwise XORx ^ y = 14 ()
>>Bitwise right shiftx>> 2 = 2 ()
<<Bitwise left shiftx<< 2 = 40 ()

Assignment operators

Assignment operators are used in Python to assign values to variables.

is a simple assignment operator that assigns the value 5 on the right to the variable on the left.

There are various compound operators in Python like that adds to the variable and later assigns the same. It is equivalent to .

OperatorExampleEquivatent to
=x = 5x = 5
+=x += 5x = x + 5
-=x -= 5x = x - 5
*=x *= 5x = x * 5
/=x /= 5x = x / 5
%=x %= 5x = x % 5
//=x //= 5x = x // 5
**=x **= 5x = x ** 5
&=x &= 5x = x & 5
|=x |= 5x = x | 5
^=x ^= 5x = x ^ 5
>>=x >>= 5x = x >> 5
<<=x <<= 5x = x << 5

]Special operators

Python language offers some special type of operators like the identity operator or the membership operator. They are described below with examples.

Identity operators

and are the identity operators in Python. They are used to check if two values (or variables) are located on the same part of the memory. Two variables that are equal does not imply that they are identical.

OperatorMeaningExample
isTrue if the operands are identical (refer to the same object)x is True
is notTrue if the operands are not identical (do not refer to the same object)x is not True

Example #4: Identity operators in Python

x1 = 5 y1 = 5 x2 = 'Hello' y2 = 'Hello' x3 = [1,2,3] y3 = [1,2,3] # Output: False print(x1 is not y1) # Output: True print(x2 is y2) # Output: False print(x3 is y3)

Here, we see that and are integers of same values, so they are equal as well as identical. Same is the case with and (strings).

But and are list. They are equal but not identical. Since list are mutable (can be changed), interpreter locates them separately in memory although they are equal.


Membership operators

and are the membership operators in Python. They are used to test whether a value or variable is found in a sequence (string, list, tuple, set and dictionary).

In a dictionary we can only test for presence of key, not the value.

OperatorMeaningExample
inTrue if value/variable is found in the sequence5 in x
not inTrue if value/variable is not found in the sequence5 not in x

Example #5: Membership operators in Python

x = 'Hello world' y = {1:'a',2:'b'} # Output: True print('H' in x) # Output: True print('hello' not in x) # Output: True print(1 in y) # Output: False print('a' in y)

Here, is in but is not present in (remember, Python is case sensitive). Similary, is key and is the value in dictionary . Hence, returns .

0 thoughts on “Python Class Assignment Operator C++”

    -->

Leave a Comment

Your email address will not be published. Required fields are marked *