Thursday 17 July 2014

Wrapping Functions using MACRO INVOCATION

When you want to wrap functions (or member functions) that either :
  • have default arguments (or)
  • are overloaded with a common sequence of initial arguments
we can either do a Manual wrapping or we can go for MACRO INVOCATION introduced by Boost.Python which makes wrapping simpler and easier.

Manual Wrapping 


Overloaded Functions 

We have here our C++ example class :



Class hello has 3 overloaded functions. To expose these in Python, we have to wrap them manually as shown below, else Python wouldn't understand which function to call when.
This is achieved by introducing some member function pointer variables :


Default Arguments

To have an idea about how Boost.Python manual wrapping works with Default arguments, have a look at it here. Boost website provides a clear explanation for this. So, am not gonna explain it here.

Lets move onto see how MACROS makes things easier.

MACROS

The macros used for this are as below.

For global functions and static methods :



For Class Methods :



Using MACROS our above program, can be written as,


When built and run using Python interpreter, we get the following desired output.


Reference :






No comments:

Post a Comment