[pygccxml-commit] SF.net SVN: pygccxml: [232] pygccxml_dev/unittests
Brought to you by:
mbaas,
roman_yakovenko
From: <rom...@us...> - 2006-06-19 13:24:47
|
Revision: 232 Author: roman_yakovenko Date: 2006-06-19 06:24:30 -0700 (Mon, 19 Jun 2006) ViewCVS: http://svn.sourceforge.net/pygccxml/?rev=232&view=rev Log Message: ----------- small refactoring to container traits to make it easy to add another container Modified Paths: -------------- pygccxml_dev/pygccxml/declarations/container_traits.py pygccxml_dev/pygccxml/declarations/type_traits.py pygccxml_dev/unittests/vector_traits_tester.py pyplusplus_dev/pyplusplus/module_creator/types_database.py Modified: pygccxml_dev/pygccxml/declarations/container_traits.py =================================================================== --- pygccxml_dev/pygccxml/declarations/container_traits.py 2006-06-19 12:30:09 UTC (rev 231) +++ pygccxml_dev/pygccxml/declarations/container_traits.py 2006-06-19 13:24:30 UTC (rev 232) @@ -7,81 +7,49 @@ defines few algorithms, that deals with different properties of std containers """ -#import typedef import calldef import cpptypes -#import variable -#import algorithm import namespace -#import templates -#import enumeration import class_declaration -#from sets import Set as set -#import types as build_in_types import type_traits -def is_defined_in_xxx( xxx, cls ): - if not cls.parent: - return False +class impl_details: + @staticmethod + def get_container_or_none( type, container_name ): + """returns reference to the class declaration or None""" + type = type_traits.remove_alias( type ) + type = type_traits.remove_cv( type ) + + cls = None + if isinstance( type, cpptypes.declarated_t ): + cls = type_traits.remove_alias( type.declaration ) + elif isinstance( type, class_declaration.class_t ): + cls = type + elif isinstance( type, class_declaration.class_declaration_t ): + cls = type + else: + return + + if not cls.name.startswith( container_name + '<' ): + return + + if not type_traits.impl_details.is_defined_in_xxx( 'std', cls ): + return + return cls - if not isinstance( cls.parent, namespace.namespace_t ): - return False +class vector_traits: + CONTAINER_NAME = 'vector' - if xxx != cls.parent.name: - return False - - xxx_ns = cls.parent - if not xxx_ns.parent: - return False - - if not isinstance( xxx_ns.parent, namespace.namespace_t ): - return False - - if '::' != xxx_ns.parent.name: - return False - - global_ns = xxx_ns.parent - return None is global_ns.parent - -class vector_traits: - @staticmethod - def declaration_or_none( type ): - global is_defined_in_std - """returns reference to the class declaration or None""" - type = type_traits.remove_alias( type ) - type = type_traits.remove_cv( type ) - - cls = None - if isinstance( type, cpptypes.declarated_t ): - cls = type_traits.remove_alias( type.declaration ) - elif isinstance( type, class_declaration.class_t ): - cls = type - elif isinstance( type, class_declaration.class_declaration_t ): - cls = type - else: - return - - if not cls.name.startswith( 'vector<' ): - return - - if not is_defined_in_xxx( 'std', cls ): - return - return cls - - @staticmethod def is_vector( type ): """ - Returns True if type represents instantiation of std class vector, - otherwise False. - """ - return not( None is vector_traits.declaration_or_none( type ) ) - + Returns True if type represents instantiation of std class vector, otherwise False.""" + return not( None is impl_details.get_container_or_none( type, vector_traits.CONTAINER_NAME ) ) @staticmethod def class_declaration( type ): """returns reference to the class declaration, """ - cls = vector_traits.declaration_or_none( type ) + cls = impl_details.get_container_or_none( type, vector_traits.CONTAINER_NAME ) if not cls: raise TypeError( 'Type "%s" is not instantiation of std::vector' % type.decl_string ) return cls @@ -93,17 +61,9 @@ if isinstance( cls, class_declaration.class_t ): return type_traits.remove_declarated( cls.typedef( "value_type", recursive=False ).type ) else: - value_type_str = templates.args( cls.name )[0] - if not value_type_str.startswith( '::' ): - value_type_str = '::' + value_type_str - found = cls.top_parent.decls( name=value_type_str - , function=lambda decl: not isinstance( decl, calldef.calldef_t ) - , allow_empty=True ) - if not found: - if cpptypes.FUNDAMENTAL_TYPES.has_key( value_type_str ): - return cpptypes.FUNDAMENTAL_TYPES[value_type_str] - if len( found ) == 1: - return found[0] - else: - raise RuntimeError( "Unable to find out vector value type. vector class is: %s" % cls.decl_string ) + value_type_str = templates.args( cls.name )[0] + ref = type_traits.impl_details.find_value_type( cls.top_parent, value_type_str ) + if None is ref: + raise RuntimeError( "Unable to find out vector value type. vector class is: %s" % cls.decl_string ) + return ref Modified: pygccxml_dev/pygccxml/declarations/type_traits.py =================================================================== --- pygccxml_dev/pygccxml/declarations/type_traits.py 2006-06-19 12:30:09 UTC (rev 231) +++ pygccxml_dev/pygccxml/declarations/type_traits.py 2006-06-19 13:24:30 UTC (rev 232) @@ -753,6 +753,46 @@ global_ns = xxx_ns.parent return None is global_ns.parent + +class impl_details: + @staticmethod + def is_defined_in_xxx( xxx, cls ): + if not cls.parent: + return False + + if not isinstance( cls.parent, namespace.namespace_t ): + return False + + if xxx != cls.parent.name: + return False + + xxx_ns = cls.parent + if not xxx_ns.parent: + return False + + if not isinstance( xxx_ns.parent, namespace.namespace_t ): + return False + + if '::' != xxx_ns.parent.name: + return False + + global_ns = xxx_ns.parent + return None is global_ns.parent + + @staticmethod + def find_value_type( global_ns, value_type_str ): + if not value_type_str.startswith( '::' ): + value_type_str = '::' + value_type_str + found = global_ns.decls( name=value_type_str + , function=lambda decl: not isinstance( decl, calldef.calldef_t ) + , allow_empty=True ) + if not found: + if cpptypes.FUNDAMENTAL_TYPES.has_key( value_type_str ): + return cpptypes.FUNDAMENTAL_TYPES[value_type_str] + if len( found ) == 1: + return found[0] + else: + return None class smart_pointer_traits: @staticmethod @@ -762,7 +802,7 @@ type = remove_declarated( type ) if not isinstance( type, ( class_declaration.class_declaration_t, class_declaration.class_t ) ): return False - if not is_defined_in_xxx( 'boost', type ): + if not impl_details.is_defined_in_xxx( 'boost', type ): return False return type.decl_string.startswith( '::boost::shared_ptr<' ) @@ -779,16 +819,11 @@ raise RuntimeError( "Unable to find out shared_ptr value type. shared_ptr class is: %s" % cls.decl_string ) else: value_type_str = templates.args( cls.name )[0] - found = cls.top_parent.classes( value_type_str, allow_empty=True ) - if not found: - if cpptypes.FUNDAMENTAL_TYPES.has_key( value_type_str ): - return cpptypes.FUNDAMENTAL_TYPES[value_type_str] - if len( found ) == 1: - return found[0] - else: + ref = impl_details.find_value_type( cls.top_parent, value_type_str ) + if None is ref: raise RuntimeError( "Unable to find out shared_ptr value type. shared_ptr class is: %s" % cls.decl_string ) - - + return ref + def is_std_string( type ): decl_strings = [ '::std::basic_string<char,std::char_traits<char>,std::allocator<char> >' Modified: pygccxml_dev/unittests/vector_traits_tester.py =================================================================== --- pygccxml_dev/unittests/vector_traits_tester.py 2006-06-19 12:30:09 UTC (rev 231) +++ pygccxml_dev/unittests/vector_traits_tester.py 2006-06-19 13:24:30 UTC (rev 232) @@ -27,7 +27,6 @@ def validate_yes( self, value_type, container ): traits = declarations.vector_traits - self.failUnless( traits.declaration_or_none( container ) ) self.failUnless( traits.is_vector( container ) ) self.failUnless( declarations.is_same( value_type, traits.value_type( container ) ) ) Modified: pyplusplus_dev/pyplusplus/module_creator/types_database.py =================================================================== --- pyplusplus_dev/pyplusplus/module_creator/types_database.py 2006-06-19 12:30:09 UTC (rev 231) +++ pyplusplus_dev/pyplusplus/module_creator/types_database.py 2006-06-19 13:24:30 UTC (rev 232) @@ -53,18 +53,18 @@ #will return True is type was treated type = declarations.remove_alias( type ) type = declarations.remove_pointer( type ) - type = declarations.remove_reference( type ) - may_be_vector = declarations.vector_traits.declaration_or_none( type ) - if not ( None is may_be_vector ): + type = declarations.remove_reference( type ) + if declarations.vector_traits.is_vector( type ): + vector = declarations.vector_traits.class_declaration( type ) try: - declarations.vector_traits.value_type( may_be_vector ) - self.__used_vectors.add( may_be_vector ) + declarations.vector_traits.value_type( vector ) + self.__used_vectors.add( vector ) return True except RuntimeError, error: msg = 'WARNING: pyplusplus found std::vector instantiation declaration, ' msg = msg + 'but can not find out value type!' msg = msg + os.linesep + 'This class will not be exported!' - msg = msg + os.linesep + 'std::vector instantiation is: ' + may_be_vector.decl_string + msg = msg + os.linesep + 'std::vector instantiation is: ' + vector.decl_string _logging_.logger.warn( msg ) return False This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |