[pygccxml-commit] SF.net SVN: pygccxml:[1408] pygccxml_dev
Brought to you by:
mbaas,
roman_yakovenko
|
From: <rom...@us...> - 2008-08-26 11:23:47
|
Revision: 1408
http://pygccxml.svn.sourceforge.net/pygccxml/?rev=1408&view=rev
Author: roman_yakovenko
Date: 2008-08-26 11:23:56 +0000 (Tue, 26 Aug 2008)
Log Message:
-----------
add key_type functionality for container_traits
add element_type and key_type caching
Modified Paths:
--------------
pygccxml_dev/pygccxml/declarations/algorithms_cache.py
pygccxml_dev/pygccxml/declarations/container_traits.py
pygccxml_dev/unittests/find_container_traits_tester.py
pygccxml_dev/unittests/vector_traits_tester.py
Modified: pygccxml_dev/pygccxml/declarations/algorithms_cache.py
===================================================================
--- pygccxml_dev/pygccxml/declarations/algorithms_cache.py 2008-08-26 08:55:58 UTC (rev 1407)
+++ pygccxml_dev/pygccxml/declarations/algorithms_cache.py 2008-08-26 11:23:56 UTC (rev 1408)
@@ -17,7 +17,9 @@
self._access_type = None
self._demangled_name = None
self._declaration_path = None
- self._partial_declaration_path = None
+ self._partial_declaration_path = None
+ self._container_key_type = None
+ self._container_element_type = None
def disable( self ):
self._enabled = False
@@ -31,27 +33,22 @@
def _get_full_name( self ):
return self._full_name
-
def _set_full_name( self, fname ):
if not self.enabled:
fname = None
- self._full_name = fname
-
+ self._full_name = fname
full_name = property( _get_full_name, _set_full_name )
def _get_full_partial_name( self ):
return self._full_partial_name
-
def _set_full_partial_name( self, fname ):
if not self.enabled:
fname = None
- self._full_partial_name = fname
-
+ self._full_partial_name = fname
full_partial_name = property( _get_full_partial_name, _set_full_partial_name )
def _get_access_type( self ):
return self._access_type
-
def _set_access_type( self, access_type ):
if not self.enabled:
access_type = None
@@ -60,35 +57,45 @@
def _get_demangled_name( self ):
return self._demangled_name
-
def _set_demangled_name( self, demangled_name ):
if not self.enabled:
demangled_name = None
- self._demangled_name = demangled_name
-
+ self._demangled_name = demangled_name
demangled_name = property( _get_demangled_name, _set_demangled_name )
def _get_declaration_path( self ):
return self._declaration_path
-
def _set_declaration_path( self, declaration_path ):
if not self.enabled:
declaration_path = None
self._declaration_path = declaration_path
-
declaration_path = property( _get_declaration_path, _set_declaration_path )
def _get_partial_declaration_path( self ):
return self._partial_declaration_path
-
def _set_partial_declaration_path( self, partial_declaration_path ):
if not self.enabled:
partial_declaration_path = None
self._partial_declaration_path = partial_declaration_path
-
partial_declaration_path = property( _get_partial_declaration_path
, _set_partial_declaration_path )
+ def _get_container_element_type( self ):
+ return self._container_element_type
+ def _set_container_element_type( self, etype ):
+ if not self.enabled:
+ etype = None
+ self._container_element_type = etype
+ container_element_type = property( _get_container_element_type, _set_container_element_type )
+
+ def _get_container_key_type( self ):
+ return self._container_key_type
+ def _set_container_key_type( self, ktype ):
+ if not self.enabled:
+ ktype = None
+ self._container_key_type = ktype
+ container_key_type = property( _get_container_key_type, _set_container_key_type )
+
def reset( self ):
self.full_name = None
self.full_partial_name = None
@@ -96,6 +103,8 @@
self.demangled_name = None
self.declaration_path = None
self.partial_declaration_path = None
+ self.container_key_type = None
+ self.container_element_type = None
def reset_name_based( self ):
self.full_name = None
@@ -103,6 +112,8 @@
self.demangled_name = None
self.declaration_path = None
self.partial_declaration_path = None
+ self.container_key_type = None
+ self.container_element_type = None
def reset_access_type( self ):
self.access_type = None
@@ -124,7 +135,6 @@
def _get_remove_alias( self ):
return self._remove_alias
-
def _set_remove_alias( self, remove_alias ):
if not type_algs_cache_t.enabled:
remove_alias = None
@@ -134,4 +144,4 @@
def reset(self):
self.remove_alias = None
-
\ No newline at end of file
+
Modified: pygccxml_dev/pygccxml/declarations/container_traits.py
===================================================================
--- pygccxml_dev/pygccxml/declarations/container_traits.py 2008-08-26 08:55:58 UTC (rev 1407)
+++ pygccxml_dev/pygccxml/declarations/container_traits.py 2008-08-26 11:23:56 UTC (rev 1408)
@@ -312,21 +312,54 @@
raise TypeError( 'Type "%s" is not instantiation of std::%s' % ( type.decl_string, self.name() ) )
return cls
+ def is_sequence( self, type ):
+ #raise exception if type is not container
+ unused = self.class_declaration( type )
+ return self.key_type_index is None
+
+ def is_mapping( self, type ):
+ return not self.is_sequence( type )
+
+ def __find_xxx_type( self, type, xxx_index, xxx_typedef, cache_property_name ):
+ cls = self.class_declaration( type )
+ result = getattr( cls.cache, cache_property_name )
+ if not result:
+ if isinstance( cls, class_declaration.class_t ):
+ xxx_type = cls.typedef( xxx_typedef, recursive=False ).type
+ result = type_traits.remove_declarated( xxx_type )
+ else:
+ xxx_type_str = templates.args( cls.name )[xxx_index]
+ result = type_traits.impl_details.find_value_type( cls.top_parent, xxx_type_str )
+ if None is result:
+ raise RuntimeError( "Unable to find out %s '%s' key\\value type."
+ % ( self.name(), cls.decl_string ) )
+ setattr( cls.cache, cache_property_name, result )
+ return result
+
def element_type( self, type ):
"""returns reference to the class value\\mapped type declaration"""
- cls = self.class_declaration( type )
- if isinstance( cls, class_declaration.class_t ):
- value_type = cls.typedef( self.element_type_typedef, recursive=False ).type
- return type_traits.remove_declarated( value_type )
- else:
- value_type_str = templates.args( cls.name )[self.element_type_index]
- ref = type_traits.impl_details.find_value_type( cls.top_parent, value_type_str )
- if None is ref:
- raise RuntimeError( "Unable to find out %s '%s' value type."
- % ( self.name(), cls.decl_string ) )
- return ref
+ return self.__find_xxx_type( type
+ , self.element_type_index
+ , self.element_type_typedef
+ , 'container_element_type')
+ def key_type( self, type ):
+ """returns reference to the class key type declaration"""
+ if not self.is_mapping( type ):
+ raise TypeError( 'Type "%s" is not "mapping" container' % str( type ) )
+ return self.__find_xxx_type( type
+ , self.key_type_index
+ , self.key_type_typedef
+ , 'container_key_type' )
+
def remove_defaults( self, type_or_string ):
+ """remove template defaults from a template class instantiation
+
+ For example:
+ std::vector< int, std::allocator< int > >
+ will become
+ std::vector< int >
+ """
name = type_or_string
if not isinstance( type_or_string, types.StringTypes ):
name = self.class_declaration( type_or_string ).name
@@ -338,30 +371,87 @@
else:
return no_defaults
-list_traits = container_traits_impl_t( 'list', 0, 'value_type', defaults_eraser.erase_allocator )
+create_traits = container_traits_impl_t
+list_traits = create_traits( 'list'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_allocator )
-deque_traits = container_traits_impl_t( 'deque', 0, 'value_type', defaults_eraser.erase_allocator )
+deque_traits = create_traits( 'deque'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_allocator )
-queue_traits = container_traits_impl_t( 'queue', 0, 'value_type', defaults_eraser.erase_container )
+queue_traits = create_traits( 'queue'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_container )
-priority_queue_traits = container_traits_impl_t( 'priority_queue', 0, 'value_type', defaults_eraser.erase_container_compare )
+priority_queue_traits = create_traits( 'priority_queue'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_container_compare )
-vector_traits = container_traits_impl_t( 'vector', 0, 'value_type', defaults_eraser.erase_allocator )
+vector_traits = create_traits( 'vector'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_allocator )
-stack_traits = container_traits_impl_t( 'stack', 0, 'value_type', defaults_eraser.erase_container )
+stack_traits = create_traits( 'stack'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_container )
-map_traits = container_traits_impl_t( 'map', 1, 'mapped_type', defaults_eraser.erase_map_compare_allocator )
-multimap_traits = container_traits_impl_t( 'multimap', 1, 'mapped_type', defaults_eraser.erase_map_compare_allocator )
+map_traits = create_traits( 'map'
+ , 1
+ , 'mapped_type'
+ , defaults_eraser.erase_map_compare_allocator
+ , key_type_index=0
+ , key_type_typedef='key_type')
+
+multimap_traits = create_traits( 'multimap'
+ , 1
+ , 'mapped_type'
+ , defaults_eraser.erase_map_compare_allocator
+ , key_type_index=0
+ , key_type_typedef='key_type')
-hash_map_traits = container_traits_impl_t( 'hash_map', 1, 'mapped_type', defaults_eraser.erase_hashmap_compare_allocator )
-hash_multimap_traits = container_traits_impl_t( 'hash_multimap', 1, 'mapped_type', defaults_eraser.erase_hashmap_compare_allocator )
-set_traits = container_traits_impl_t( 'set', 0, 'value_type', defaults_eraser.erase_compare_allocator)
-multiset_traits = container_traits_impl_t( 'multiset', 0, 'value_type', defaults_eraser.erase_compare_allocator )
+hash_map_traits = create_traits( 'hash_map'
+ , 1
+ , 'mapped_type'
+ , defaults_eraser.erase_hashmap_compare_allocator
+ , key_type_index=0
+ , key_type_typedef='key_type')
+
+
+hash_multimap_traits = create_traits( 'hash_multimap'
+ , 1
+ , 'mapped_type'
+ , defaults_eraser.erase_hashmap_compare_allocator
+ , key_type_index=0
+ , key_type_typedef='key_type')
-hash_set_traits = container_traits_impl_t( 'hash_set', 0, 'value_type', defaults_eraser.erase_hash_allocator )
-hash_multiset_traits = container_traits_impl_t( 'hash_multiset', 0, 'value_type', defaults_eraser.erase_hash_allocator )
+set_traits = create_traits( 'set'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_compare_allocator)
+
+multiset_traits = create_traits( 'multiset'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_compare_allocator )
+hash_set_traits = create_traits( 'hash_set'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_hash_allocator )
+
+hash_multiset_traits = create_traits( 'hash_multiset'
+ , 0
+ , 'value_type'
+ , defaults_eraser.erase_hash_allocator )
+
container_traits = (
list_traits
, deque_traits
Modified: pygccxml_dev/unittests/find_container_traits_tester.py
===================================================================
--- pygccxml_dev/unittests/find_container_traits_tester.py 2008-08-26 08:55:58 UTC (rev 1407)
+++ pygccxml_dev/unittests/find_container_traits_tester.py 2008-08-26 11:23:56 UTC (rev 1408)
@@ -23,7 +23,7 @@
tester_t.global_ns = declarations.get_global_namespace( decls )
tester_t.global_ns.init_optimizer()
- def __cmp_traits( self, typedef, expected, partial_name):
+ def __cmp_traits( self, typedef, expected, partial_name, key_type=None):
if isinstance( typedef, str ):
typedef = self.global_ns.typedef( typedef )
traits = declarations.find_container_traits( typedef )
@@ -34,7 +34,21 @@
cls = declarations.remove_declarated( typedef )
self.failUnless( cls.container_traits is expected )
self.failUnless( cls.partial_name == partial_name )
-
+ cls = traits.class_declaration( cls )
+
+ self.failUnless( traits.element_type( typedef ) )
+ self.failUnless( cls.cache.container_element_type, "For some reason cache was not updated" )
+
+ if key_type:
+ self.failUnless( traits.is_mapping( typedef ) )
+ real_key_type = traits.key_type( typedef )
+ self.failUnless( real_key_type.decl_string == key_type
+ , 'Error extracting key type. Expected type "%s", got "%s"'
+ % ( key_type, real_key_type.decl_string ) )
+ self.failUnless( cls.cache.container_key_type, "For some reason cache was not updated" )
+ else:
+ self.failUnless( traits.is_sequence( typedef ) )
+
def test_find_traits( self ):
self.__cmp_traits( 'v_int', declarations.vector_traits, "vector< int >" )
self.__cmp_traits( 'l_int', declarations.list_traits, "list< int >" )
@@ -43,12 +57,12 @@
self.__cmp_traits( 'pq_int', declarations.priority_queue_traits, "priority_queue< int >")
self.__cmp_traits( 's_v_int', declarations.set_traits, "set< std::vector< int > >")
self.__cmp_traits( 'ms_v_int', declarations.multiset_traits, "multiset< std::vector< int > >")
- self.__cmp_traits( 'm_i2d', declarations.map_traits, "map< int, double >" )
- self.__cmp_traits( 'mm_i2d', declarations.multimap_traits, "multimap< int, double >" )
+ self.__cmp_traits( 'm_i2d', declarations.map_traits, "map< int, double >", 'int' )
+ self.__cmp_traits( 'mm_i2d', declarations.multimap_traits, "multimap< int, double >", 'int' )
self.__cmp_traits( 'hs_v_int', declarations.hash_set_traits, "hash_set< std::vector< int > >" )
self.__cmp_traits( 'mhs_v_int', declarations.hash_multiset_traits, "hash_multiset< std::vector< int > >" )
- self.__cmp_traits( 'hm_i2d', declarations.hash_map_traits, "hash_map< int, double >" )
- self.__cmp_traits( 'hmm_i2d', declarations.hash_multimap_traits, "hash_multimap< int, double >" )
+ self.__cmp_traits( 'hm_i2d', declarations.hash_map_traits, "hash_map< int, double >", 'int' )
+ self.__cmp_traits( 'hmm_i2d', declarations.hash_multimap_traits, "hash_multimap< int, double >", 'int' )
def test_multimap( self ):
mm = self.global_ns.classes( lambda decl: decl.name.startswith( 'multimap' ) )
Modified: pygccxml_dev/unittests/vector_traits_tester.py
===================================================================
--- pygccxml_dev/unittests/vector_traits_tester.py 2008-08-26 08:55:58 UTC (rev 1407)
+++ pygccxml_dev/unittests/vector_traits_tester.py 2008-08-26 11:23:56 UTC (rev 1408)
@@ -29,6 +29,7 @@
traits = declarations.vector_traits
self.failUnless( traits.is_my_case( container ) )
self.failUnless( declarations.is_same( value_type, traits.element_type( container ) ) )
+ self.failUnless( traits.is_sequence( container ) )
def test_global_ns( self ):
value_type = self.global_ns.class_( '_0_' )
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|