Package pyplusplus :: Package decl_wrappers :: Module indexing_suite2 |

# Source Code for Module pyplusplus.decl_wrappers.indexing_suite2

      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  """defines interface for exposing STD containers, using next version of indexing suite"""
      7
      8  from pygccxml import declarations
      9  import call_policies
     10  """
     11  method_len
     12  method_iter
     13  method_getitem
     14  method_getitem_slice
     15  method_index
     16  method_contains
     17  method_count
     18  method_has_key
     19  method_setitem
     20  method_setitem_slice
     21  method_delitem
     22  method_delitem_slice
     23  method_reverse
     24  method_append
     25  method_insert
     26  method_extend
     27  method_sort
     28
     29  slice_methods = method_getitem_slice | method_setitem_slice | method_delitem_slice
     30  search_methods = method_index | method_contains | method_count | method_has_key
     31  reorder_methods = method_sort | method_reverse
     32  insert_methods = method_append | method_insert | method_extend
     33  """
     34
     35
     36  containers = {
     37        'vector' : "boost/python/suite/indexing/vector.hpp"
     38      , 'deque' : "boost/python/suite/indexing/deque.hpp"
     39      , 'list' : "boost/python/suite/indexing/list.hpp"
     40      , 'map' : "boost/python/suite/indexing/map.hpp"
     41      , 'multimap' : "boost/python/suite/indexing/multimap.hpp"
     42      , 'hash_map' : "boost/python/suite/indexing/map.hpp"
     43      , 'set' : "boost/python/suite/indexing/set.hpp"
     44      , 'hash_set' : "boost/python/suite/indexing/set.hpp"
     45      #TODO: queue, priority, stack, hash_multimap, multiset, hash_multiset
     46  }
     47
     48 -class indexing_suite2_t( object ):
     49      """
     50      This class helps user to export STD containers, using Boost.Python
     51      indexing suite V2.
     52      """
     53
     54      #List of method names. These method could be excluded from being exposed.
     55      METHODS = ( 'len', 'iter', 'getitem', 'getitem_slice', 'index', 'contains'
     56                  , 'count', 'has_key', 'setitem', 'setitem_slice', 'delitem'
     57                  , 'delitem_slice', 'reverse', 'append', 'insert', 'extend', 'sort' )
     58
     59      #Dictionary of method group names. These method groups could be excluded from
     60      #being exposed. Dictionary key is a method group name. Dictionary value is a
     61      #list of all methods, which belong to the group.
     62      METHOD_GROUPS = {
     63          'slice' : ( 'method_getitem_slice', 'method_setitem_slice', 'method_delitem_slice' )
     64          , 'search' : ( 'method_index', 'method_contains', 'method_count', 'method_has_key' )
     65          , 'reorder' : ( 'method_sort', 'method_reverse' )
     66          , 'insert' : ( 'method_append', 'method_insert', 'method_extend' )
     67      }
     68
     69 -    def __init__( self, container_class ):
     70          object.__init__( self )
     71          self.__call_policies = None
     72          self.__container_class = container_class
     73          self._disabled_methods = set()
     74          self._disabled_groups = set()
     75          self._default_applied = False
     76          self._use_container_suite = False
     77          self.__include_files = None
     78
     79 -    def get_use_container_suite( self ):
     80          return self._use_container_suite
     81 -    def set_use_container_suite( self, value ):
     82          self._use_container_suite = value
     83      use_container_suite = property( get_use_container_suite, set_use_container_suite )
     84
     85      @property
     86 -    def container_class( self ):
     87          """reference to the parent( STD container ) class"""
     88          return self.__container_class
     89
     90      @property
     91 -    def element_type(self):
     92          """reference to container value_type( mapped_type ) type"""
     93          return self.container_traits.element_type( self.container_class )
     94
     95      @property
     96 -    def container_traits( self ):
     97          "reference to container traits. See pygccxml documentation for more information."
     98          return self.container_class.container_traits
     99
    100 -    def _get_call_policies( self ):
    101          if self.__call_policies:
    102              return self.__call_policies
    103          if self.container_traits not in declarations.sequential_container_traits:
    104              #TODO: find out why map's don't like the policy
    105              return self.__call_policies
    106          element_type = None
    107          try:
    108              element_type = self.element_type
    109          except:
    110              return
    111          if declarations.is_const( element_type ):
    112              element_type = declarations.remove_const( element_type )
    113          if declarations.is_pointer( element_type ):
    114              self.__call_policies = call_policies.return_internal_reference()
    115          return self.__call_policies
    116
    117 -    def _set_call_policies( self, call_policies ):
    118          self.__call_policies = call_policies
    119      call_policies = property( _get_call_policies, _set_call_policies
    120                                , "Call policies, that should be used by Boost.Python container classes.")
    121
    122 -    def __apply_defaults_if_needed( self ):
    123          if self._default_applied:
    124              return
    125          self._default_applied = True
    126          #find out what operators are supported by element_type and
    127          #then configure the _disable_[methods|groups]
    128          pass
    129
    130 -    def disable_method( self, method_name ):
    131          """Disable method from being exposed"""
    132          assert method_name in self.METHODS
    133          self.__apply_defaults_if_needed()
    134          self._disabled_methods.add( method_name )
    135
    136 -    def enable_method( self, method_name ):
    137          """Enable method to be exposed"""
    138          assert method_name in self.METHODS
    139          self.__apply_defaults_if_needed()
    140          if method_name in self._disabled_methods:
    141              self._disabled_methods.remove( method_name )
    142
    143 -    def _get_disabled_methods( self ):
    144          self.__apply_defaults_if_needed()
    145          return self._disabled_methods
    146      disable_methods = property( _get_disabled_methods
    147                                  , doc="list of all disabled methods")
    148
    149 -    def disable_methods_group( self, group_name ):
    150          """Disable methods group from being exposed"""
    151          assert group_name in self.METHOD_GROUPS
    152          self.__apply_defaults_if_needed()
    153          self._disabled_groups.add( group_name )
    154
    155 -    def enable_methods_group( self, group_name ):
    156          """Enable methods group to be exposed"""
    157          assert group_name in self.METHOD_GROUPS
    158          self.__apply_defaults_if_needed()
    159          if group_name in self._disabled_groups:
    160              self._disabled_groups.remove( group_name )
    161
    162 -    def _get_disabled_methods_groups( self ):
    163          self.__apply_defaults_if_needed()
    164          return self._disabled_groups
    165      disabled_methods_groups = property( _get_disabled_methods_groups
    166                                          , doc="list of all disabled methods group")
    167
    168      @property
    169 -    def include_files( self ):
    170          """Return list of header files to be included in generated code"""
    171          if self.__include_files is None:
    172              name = self.container_class.name.split( '<' )[0]
    173              if name not in containers:
    174                  self.__include_files = [] #not supported
    175              else:
    176                  #impl details: the order of header files is IMPORTANT
    177                  self.__include_files = [ "boost/python/suite/indexing/container_suite.hpp"
    178                                           , containers[ name ] ]
    179          return self.__include_files
    180