Package pyplusplus :: Package code_creators :: Module enum

# Source Code for Module pyplusplus.code_creators.enum

     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
    11 -class enum_t( registration_based.registration_based_t
    12                , declaration_based.declaration_based_t ):
    13      """
    14      Creates boost.python code that expose C++ enum
    15      """
    16 -    def __init__(self, enum ):
    17          registration_based.registration_based_t.__init__( self )
    18          declaration_based.declaration_based_t.__init__( self, declaration=enum)
    19          self.works_on_instance = False
    20
    21 -    def _get_value_aliases(self):
    22          return self.declaration.value_aliases
    23 -    def _set_value_aliases(self, value_aliases):
    24          self.declaration.value_aliases = value_aliases
    25      value_aliases = property( _get_value_aliases, _set_value_aliases )
    26
    27 -    def _get_export_values(self):
    28          return self.declaration.export_values
    29 -    def _set_export_values(self, export_values):
    30          self.declaration.export_values = export_values
    31      export_values = property( _get_export_values, _set_export_values )
    32
    33 -    def _generate_value_code(self, value_name):
    34          #in C++ you can't write namespace::enum::value, you should write namespace::value
    35          full_name = self.declaration.parent.decl_string
    36          return '.value("%(alias)s", %(name)s)' \
    37                 % { 'alias' : self.value_aliases.get( value_name, value_name )
    38                      , 'name' : algorithm.create_identifier( self, full_name + '::' + value_name ) }
    39
    40 -    def _create_impl(self):
    41          if self.declaration.already_exposed:
    42              return ''
    43
    44          bpl_enum = '%(bpl::enum_)s< %(name)s>("%(alias)s")' \
    45                     % { 'bpl::enum_' : algorithm.create_identifier( self, '::boost::python::enum_' )
    46                         , 'name' : algorithm.create_identifier( self, self.declaration.decl_string )
    47                         , 'alias' : self.alias }
    48
    49          values = []
    50          # Add the values that should be exported
    51          for value_name in self.declaration.export_values:
    52              values.append( self._generate_value_code( value_name ) )
    53
    54          # Export the values
    55          if len(self.declaration.export_values)>0:
    56              values.append( '.export_values()' )
    57
    58          # Add the values that should not be exported
    59          for name in self.declaration.no_export_values:
    60              values.append( self._generate_value_code( name ) )
    61
    62          values.append( ';' )
    63
    64          values = self.indent( os.linesep.join( values ) )
    65          return bpl_enum + os.linesep + values
    66
    67 -    def _get_system_headers_impl( self ):
    68          return []
    69