pyplusplus.code_creators.smart_pointers

Package pyplusplus :: Package code_creators :: Module smart_pointers |

# Source Code for Module pyplusplus.code_creators.smart_pointers

      1  # Copyright 2004-2008 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  import os
      7  import algorithm
      8  import declaration_based
      9  import registration_based
     10  from pygccxml import declarations
     11
     12  templates = declarations.templates
     13
     14 -class held_type_t(object):
     15      """ Helper class that can hold name of smart_ptr type and create
     16          identifier for held type from that given a creator.
     17      """
     18 -    def __init__( self, smart_ptr ):
     19          """ smart_ptr: string of ptr type.  Ex: 'boost::shared_ptr'  """
     20          object.__init__( self )
     21          self._smart_ptr = smart_ptr
     22
     23 -    def _get_smart_ptr( self ):
     24          return self._smart_ptr
     25 -    def _set_smart_ptr( self, ptr ):
     26          self._smart_ptr = ptr
     27      smart_ptr = property( _get_smart_ptr, _set_smart_ptr )
     28
     29 -    def create( self, creator):
     30          """ Return string of type to use for held type.
     31              Ex: boost::shared_ptr<Class>
     32          """
     33          smart_ptr = algorithm.create_identifier( creator, self.smart_ptr )
     34          arg = algorithm.create_identifier( creator, creator.declaration.decl_string )
     35          return templates.join( smart_ptr, [ arg ] )
     36
     37 -class smart_pointer_registrator_t( registration_based.registration_based_t
     38                                     , declaration_based.declaration_based_t ):
     39      """ Convertor for boost::python::register_ptr_to_python<PTR>.
     40          Lets boost python know that it can use smart_ptr to hold a an object.
     41          See: http://www.boost.org/libs/python/doc/v2/register_ptr_to_python.html
     42      """
     43 -    def __init__( self, smart_ptr, class_creator ):
     44          """ smart_ptr: string of ptr type.  Ex: 'boost::shared_ptr'  """
     45          registration_based.registration_based_t.__init__( self )
     46          declaration_based.declaration_based_t.__init__( self, class_creator.declaration )
     47          self._smart_ptr = smart_ptr
     48          self._class_creator = class_creator
     49          self.works_on_instance = False
     50
     51 -    def _get_smart_ptr( self ):
     52          return self._smart_ptr
     53 -    def _set_smart_ptr( self, ptr ):
     54          self._smart_ptr = ptr
     55      smart_ptr = property( _get_smart_ptr, _set_smart_ptr )
     56
     57 -    def _get_class_creator( self ):
     58          return self._class_creator
     59 -    def _set_class_creator( self, cc ):
     60          self._class_creator = cc
     61      class_creator = property( _get_class_creator, _set_class_creator )
     62
     63 -    def _create_impl(self):
     64          if self.declaration.already_exposed:
     65              return ''
     66          if self.class_creator \
     67             and self.class_creator.held_type \
     68             and isinstance( self.class_creator.held_type, held_type_t ) \
     69             and self.class_creator.held_type.smart_ptr == self.smart_ptr \
     70             and self.target_configuration.boost_python_has_wrapper_held_type \
     71             and not self.class_creator.declaration.require_self_reference:
     72              return '' #boost.python does it automaticly
     73          rptp = algorithm.create_identifier( self, '::boost::python::register_ptr_to_python' )
     74          held_type = held_type_t(self.smart_ptr).create( self )
     75          return templates.join( rptp, [ held_type ] ) + '();'
     76
     77 -    def _get_system_headers_impl( self ):
     78          return []
     79
     80 -class smart_pointers_converter_t( registration_based.registration_based_t
     81                                    , declaration_based.declaration_based_t ):
     82      """ creator for boost::python::implicitly_convertible.
     83          This creates a statemnt that allows the usage of C++ implicit
     84          conversion from source to target.
     85          See: http://www.boost.org/libs/python/doc/v2/implicit.html
     86      """
     87 -    def __init__( self, smart_ptr, source, target ):
     88          registration_based.registration_based_t.__init__( self )
     89          declaration_based.declaration_based_t.__init__( self, source )
     90          self._target = target
     91          self._smart_ptr = smart_ptr
     92          self.works_on_instance = False
     93
     94 -    def _get_target(self):
     95          return self._target
     96      target = property( _get_target )
     97
     98 -    def _get_source(self):
     99          return self.declaration
    100      source = property( _get_source )
    101
    102 -    def _get_smart_ptr( self ):
    103          return self._smart_ptr
    104 -    def _set_smart_ptr( self, ptr ):
    105          self._smart_ptr = ptr
    106      smart_ptr = property( _get_smart_ptr, _set_smart_ptr )
    107
    108 -    def _instantiate_smart_ptr( self, decl ):
    109          identifier = algorithm.create_identifier( self, decl.partial_decl_string )
    110          return templates.join( self.smart_ptr, [identifier] )
    111
    112 -    def _create_impl(self):
    113          implicitly_convertible = algorithm.create_identifier( self, '::boost::python::implicitly_convertible' )
    114          from_arg = self._instantiate_smart_ptr( self.source )
    115          to_arg = self._instantiate_smart_ptr( self.target )
    116          return templates.join(implicitly_convertible, [ from_arg, to_arg ] ) + '();'
    117
    118 -    def _get_system_headers_impl( self ):
    119          return []
    120