Package pyplusplus :: Package module_creator :: Module opaque_types_manager

# Source Code for Module pyplusplus.module_creator.opaque_types_manager

     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  from pygccxml import declarations
     7  from pyplusplus import code_creators
     8  from pyplusplus import decl_wrappers
     9
    10 -def find_out_opaque_decl( type_, ensure_opaque_decl ):
    11      naked_type = declarations.remove_cv( type_ )
    12      if not declarations.is_pointer( naked_type ):
    13          return None
    14      naked_type = declarations.remove_pointer( declarations.remove_cv( type_ ) )
    15      if decl_wrappers.python_traits.is_immutable( naked_type ):
    16          return None#immutable types could not be opaque
    17      decl = None
    18      if declarations.is_class( naked_type ):
    19          decl = declarations.class_traits.get_declaration( naked_type )
    20      elif declarations.is_class_declaration( naked_type ):#class declaration:
    21          decl = declarations.class_declaration_traits.get_declaration( naked_type )
    22      else:
    23          return None
    24      if ensure_opaque_decl:
    25          if decl.opaque:
    26              return decl
    27          else:
    28              return None
    29      else:
    30          return decl
    31
    32
    33 -class manager_t( object ):
    34 -    def __init__( self, extmodule ):
    35          object.__init__( self )
    36          self.__extmodule = extmodule
    37          self.__exposed_opaque_decls = {} #decl: creator
    38
    39 -    def __find_out_opaque_decls( self, decl ):
    40          opaque_types = []
    41          is_opaque_policy = decl_wrappers.is_return_opaque_pointer_policy
    42          if isinstance( decl, declarations.variable_t ):
    43              opaque_decl = find_out_opaque_decl( decl.type, ensure_opaque_decl=True )
    44              if opaque_decl:
    45                  opaque_types.append( opaque_decl )
    46              elif is_opaque_policy( decl.getter_call_policies ) or is_opaque_policy( decl.setter_call_policies ):
    47                  opaque_decl = find_out_opaque_decl( decl.type, ensure_opaque_decl=False )
    48                  if opaque_decl:
    49                      opaque_types.append( opaque_decl )
    50              else:
    51                  pass
    52          elif isinstance( decl, declarations.calldef_t ):
    53              if is_opaque_policy( decl.call_policies ):
    54                  opaque_decl = find_out_opaque_decl( decl.return_type, ensure_opaque_decl=False )
    55                  if opaque_decl:
    56                      opaque_types.append( opaque_decl )
    57              all_types = decl.argument_types[:]
    58              if decl.return_type:
    59                  all_types.append( decl.return_type )
    60              for type_ in all_types:
    61                  opaque_decl = find_out_opaque_decl( type_, ensure_opaque_decl=True )
    62                  if opaque_decl:
    63                      opaque_types.append( opaque_decl )
    64          else:
    65              pass
    66          return opaque_types
    67
    68
    69 -    def register_opaque( self, creator, decl_or_decls ):
    70          opaque_decls = []
    71          for decl in declarations.make_flatten( decl_or_decls ):
    72              opaque_decls.extend( self.__find_out_opaque_decls( decl ) )
    73
    74          for decl in opaque_decls:
    75              opaque_type_registrator = None
    76              if id(decl) not in self.__exposed_opaque_decls.keys():
    77                  opaque_type_registrator = code_creators.opaque_type_registrator_t( decl )
    78                  self.__exposed_opaque_decls[ id(decl) ] = opaque_type_registrator
    79                  self.__extmodule.adopt_declaration_creator( opaque_type_registrator )
    80              else:
    81                  opaque_type_registrator = self.__exposed_opaque_decls[ id(decl) ]
    82              creator.associated_decl_creators.append(opaque_type_registrator)
    83