pyplusplus.function_transformers.templates

Package pyplusplus :: Package function_transformers :: Module templates |

# Source Code for Module pyplusplus.function_transformers.templates

     1  # Copyright 2006 Roman Yakovenko.
     2  # Distributed under the Boost Software License, Version 1.0. (See
     3  # accompanying file LICENSE_1_0.txt or copy at
     4  # http://www.boost.org/LICENSE_1_0.txt)
     5
     6  # Matthias Baas is an initial author of the templates.
     7
     8  """defines few templates, which will be used to create a function-wrapper
     9  definition."""
    10
    11  import os
    12  from string import Template
    13
    14
    15 -class sealed_fun:
    16      body = Template( os.linesep.join([
    17            'static $return_type $unique_function_name( $arg_declarations ){'
    18          , '    $declare_variables'
    19          , '    $pre_call'
    20          , '    $save_result$function_name($arg_expressions);'
    21          , '    $post_call'
    22          , '    $return'
    23          , '}'
    24      ]))
    25
    26 -class virtual_mem_fun:
    27      override = Template( os.linesep.join([
    28            'virtual $return_type $function_name( $arg_declarations )$constness $throw{'
    29          , '    namespace bpl = boost::python;'
    30          , '    if( bpl::override $py_function_var = this->get_override( "$function_alias" ) ){'
    31          , '        $declare_py_variables'
    32          , '        $py_pre_call'
    33          , '        ${save_py_result}bpl::call<bpl::object>( $py_function_var.ptr()$py_arg_expressions );'
    34          , '        $py_post_call'
    35          , '        $py_return'
    36          , '    }'
    37          , '    else{'
    38          , '        $cpp_return$wrapped_class::$function_name( $cpp_arg_expressions );'
    39          , '    }'
    40          , '}'
    41      ]))
    42
    43      default = Template( os.linesep.join([
    44            'static $return_type $unique_function_name( $arg_declarations ){'
    45          , '    $declare_variables'
    46          , '    $pre_call'
    47          , '    if( dynamic_cast< $wrapper_class $wrapped_inst_constness* >( boost::addressof( $wrapped_inst ) ) ){'
    48          , '        $save_result$wrapped_inst.$wrapped_class::$function_name($arg_expressions);'
    49          , '    }'
    50          , '    else{'
    51          , '        $save_result$wrapped_inst.$function_name($arg_expressions);'
    52          , '    }'
    53          , '    $post_call'
    54          , '    $return'
    55          , '}'
    56      ]))
    57
    58  #TODO: FT for constructor
    59  #~ class constructor:
    60      #~ #User cannot apply transformation on constructor of abstract class
    61      #~ #It is not possible to create an instance of such class
    62      #~ body = Template( os.linesep.join([
    63            #~ 'std::auto_ptr<$exposed_class> $unique_function_name( $arg_declarations ){'
    64          #~ , '    $declare_variables'
    65          #~ , '    $pre_call'
    66          #~ , '    std::auto_ptr<$exposed_class> $result( new $exposed_class($arg_expressions) );'
    67          #~ , '    $post_call'
    68          #~ , '    return $result;'
    69          #~ , '}'
    70      #~ ]))
    71
    72
    73 -def substitute( text, **keywd ):
    74      return Template( text ).substitute( **keywd )
    75