modify_type transformer

Definition

“modify_type” transformer changes type of the function argument.

“modify_type” transformer takes two arguments:

  1. name or index of the original function argument
  2. a callable, which takes as argument reference to type and returns new type

New in version greater than 0.8.5.

Pay attention!

If implicit conversion between new type and the old one does not exist “reinterpret_cast” will be used.

Example

#include <string>

inline void hello_world( std::string& hw ){
    hw = "hello world!";
}

Lets say that you need to expose hello_world function. As you know std::string is mapped to Python string, which is immutable type, so you have to create small wrapper for the function. The following Py++ code does it for you:

from pygccxml import declarations
from pyplusplus import module_builder
from pyplusplus import function_transformers as FT

mb = module_builder.module_builder_t( ... )
hw = mb.mem_fun( 'hello_world' )
hw.add_transformation( FT.modify_type(0, declarations.remove_reference) )

What you see below is the relevant pieces of generated code:

namespace bp = boost::python;

static void hello_world_a3478182294a057b61508c30b1361318( ::std::string hw ){
    ::hello_world(hw);
}

BOOST_PYTHON_MODULE(...){
    ...
    bp::def( "hello_world", &hello_world_a3478182294a057b61508c30b1361318 );
}