pyplusplus.code_creators.indexing_suites

Package pyplusplus :: Package code_creators :: Module indexing_suites |

# Source Code for Module pyplusplus.code_creators.indexing_suites

      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 types
      8  import algorithm
      9  import code_creator
     10  import declaration_based
     11  import registration_based
     12  from pygccxml import declarations
     13
     14 -class indexing_suite1_t( registration_based.registration_based_t
     15                           , declaration_based.declaration_based_t ):
     16 -    def __init__(self, container ):
     17          registration_based.registration_based_t.__init__( self )
     18          declaration_based.declaration_based_t.__init__( self, declaration=container )
     19
     20      @property
     21 -    def configuration( self ):
     22          return self.declaration.indexing_suite
     23
     24      @property
     25 -    def container( self ):
     26          return self.declaration
     27
     28 -    def guess_suite_name( self ):
     29          if self.container.name.startswith( 'vector' ):
     30              return 'boost::python::vector_indexing_suite'
     31          else:
     32              return 'boost::python::map_indexing_suite'
     33
     34 -    def _create_suite_declaration( self ):
     35          suite_identifier = algorithm.create_identifier( self, self.guess_suite_name() )
     36          args = [ self.container.partial_decl_string ]
     37          try:
     38              no_proxy = str( self.configuration.no_proxy ).lower()
     39          except:
     40              no_proxy = 'false'
     41          if self.configuration.derived_policies:
     42              args.append( no_proxy )
     43              args.append( self.configuration.derived_policies )
     44          else:
     45              if 'true' == no_proxy:
     46                  args.append( no_proxy)
     47          return declarations.templates.join( suite_identifier, args )
     48
     49 -    def _create_impl(self):
     50          return "def( %s() )" %  self._create_suite_declaration()
     51
     52 -    def _get_system_headers_impl( self ):
     53          return self.configuration.include_files
     54
     55 -class indexing_suite2_t( registration_based.registration_based_t
     56                           , declaration_based.declaration_based_t ):
     57 -    def __init__(self, container ):
     58          registration_based.registration_based_t.__init__( self )
     59          declaration_based.declaration_based_t.__init__( self, declaration=container )
     60          self.__method_mask_var_name = "methods_mask"
     61          self.works_on_instance = not self.does_user_disable_methods()
     62
     63 -    def does_user_disable_methods( self ):
     64          return bool( self.declaration.indexing_suite.disabled_methods_groups ) \
     65                 or bool( self.declaration.indexing_suite.disable_methods )
     66
     67 -    def generate_algorithm_mask( self ):
     68          disable = []
     69          for group in self.declaration.indexing_suite.disabled_methods_groups:
     70              group_id = algorithm.create_identifier(self, "::boost::python::indexing::%s_methods" % group )
     71              disable.append( group_id )
     72          for method in self.declaration.indexing_suite.disable_methods:
     73              method_id = algorithm.create_identifier(self, "::boost::python::indexing::method_" + method )
     74              disable.append( method_id )
     75          answer = [ 'unsigned long const %s = ' % self.__method_mask_var_name ]
     76          answer.append( algorithm.create_identifier(self, "::boost::python::indexing::all_methods" ) )
     77          answer.append( ' & ~' )
     78          if 1 == len ( disable ):
     79              answer.append( disable[0] )
     80          else:
     81              answer.append( '( ' )
     82              answer.append( ' |  '.join( disable ) )
     83              answer.append( ' ) ' )
     84          answer.append( ';' )
     85          return ''.join( answer )
     86
     87 -    def _create_impl( self ):
     88          if self.declaration.already_exposed:
     89              return ''
     90
     91          answer = []
     92          if self.does_user_disable_methods():
     93              answer.append( self.generate_algorithm_mask() )
     94              answer.append( os.linesep )
     95          if not self.works_on_instance:
     96              answer.append( '%s.def( ' % self.parent.class_var_name)
     97          else:
     98              answer.append( 'def( ' )
     99          bpi = algorithm.create_identifier(self, "::boost::python::indexing" )
    100          if self.declaration.indexing_suite.use_container_suite:
    101              answer.append( bpi + '::container_suite' )
    102          else:
    103              answer.append( bpi + '::' + self.declaration.name.split( '<' )[0] + '_suite' )
    104          answer.append( '< ' )
    105          answer.append( self.decl_identifier )
    106          if self.does_user_disable_methods():
    107              answer.append( self.PARAM_SEPARATOR )
    108              answer.append( self.__method_mask_var_name )
    109          answer.append( ' >' )
    110          if self.declaration.indexing_suite.call_policies \
    111             and not self.declaration.indexing_suite.call_policies.is_default():
    112              answer.append( '::with_policies(%s)'
    113                             % self.declaration.indexing_suite.call_policies.create( self )  )
    114          else:
    115              answer.append( '()' )
    116          answer.append( ' )' )
    117          if not self.works_on_instance:
    118              answer.append( ';' )
    119          return ''.join( answer )
    120
    121 -    def _get_system_headers_impl( self ):
    122          return self.declaration.indexing_suite.include_files
    123
    124 -class value_traits_t( code_creator.code_creator_t
    125                        , declaration_based.declaration_based_t ):
    126 -    def __init__( self, value_class ):
    127          code_creator.code_creator_t.__init__( self )
    128          declaration_based.declaration_based_t.__init__( self, declaration=value_class )
    129
    130 -    def generate_value_traits( self ):
    131          tmpl = os.linesep.join([
    132                "namespace boost { namespace python { namespace indexing {"
    133              , ""
    134              , "template<>"
    135              , "struct value_traits< %(value_class)s >{"
    136              , ""
    137              , self.indent( "static bool const equality_comparable = %(has_equal)s;" )
    138              , self.indent( "%(equal_to)s" )
    139              , ""
    140              , self.indent( "static bool const less_than_comparable = %(has_lessthan)s;" )
    141              , self.indent( "%(less)s" )
    142              , ""
    143              , self.indent( "template<typename PythonClass, typename Policy>" )
    144              , self.indent( "static void visit_container_class(PythonClass &, Policy const &){" )
    145              , self.indent( "%(visitor_helper_body)s", 2 )
    146              , self.indent( "}" )
    147              , ""
    148              , "};"
    149              , ""
    150              , "}/*indexing*/ } /*python*/ } /*boost*/"
    151          ])
    152
    153          less = ''
    154          if self.declaration.less_than_comparable:
    155              less = "typedef std::less< %s > less;" % self.decl_identifier
    156
    157          equal_to = ''
    158          if self.declaration.equality_comparable:
    159              equal_to = "typedef std::equal_to< %s > equal_to;" % self.decl_identifier
    160
    161          return tmpl % { 'value_class' : self.decl_identifier
    162                          , 'has_equal' : str( bool( self.declaration.equality_comparable ) ) .lower()
    163                          , 'equal_to' : equal_to
    164                          , 'has_lessthan' : str( bool( self.declaration.less_than_comparable ) ).lower()
    165                          , 'less' : less
    166                          , 'visitor_helper_body' : '' }
    167
    168 -    def generate_value_class_fwd_declaration( self ):
    169          pass # for inner class this code will generate error :-((((
    170
    171 -    def _create_impl( self ):
    172          #if self.declaration.already_exposed:
    173          #    return ''
    174          #This is the error to skip generation in case the class is already exposed,
    175          #because we still expose container, so it needs to know how to work with
    176          #the value_type
    177          return self.generate_value_traits()
    178
    179 -    def _get_system_headers_impl( self ):
    180          return ['boost/python/suite/indexing/value_traits.hpp']
    181