def erase_hash_allocator(cls_name): cls_name = defaults_eraser.replace_basic_string(cls_name) c_name, c_args = templates.split(cls_name) if len(c_args) < 3: return default_hash = None default_less = 'std::less' default_equal_to = 'std::equal_to' default_allocator = 'std::allocator' tmpl = None if 3 == len(c_args): default_hash = 'hash_compare' tmpl = "$container< $value_type, $hash<$value_type, $less<$value_type> >, $allocator<$value_type> >" elif 4 == len(c_args): default_hash = 'hash' tmpl = "$container< $value_type, $hash<$value_type >, $equal_to<$value_type >, $allocator<$value_type> >" else: return value_type = c_args[0] tmpl = string.Template(tmpl) for ns in std_namespaces: inst = tmpl.substitute(container=c_name, value_type=value_type, hash=ns + '::' + default_hash, less=default_less, equal_to=default_equal_to, allocator=default_allocator) if defaults_eraser.normalize( cls_name) == defaults_eraser.normalize(inst): return templates.join( c_name, [defaults_eraser.erase_recursive(value_type)])
def erase_hash_allocator( cls_name ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) if len( c_args ) < 3: return default_hash=None default_less='std::less' default_equal_to='std::equal_to' default_allocator='std::allocator' tmpl = None if 3 == len( c_args ): default_hash='hash_compare' tmpl = "$container< $value_type, $hash<$value_type, $less<$value_type> >, $allocator<$value_type> >" elif 4 == len( c_args ): default_hash='hash' tmpl = "$container< $value_type, $hash<$value_type >, $equal_to<$value_type >, $allocator<$value_type> >" else: return value_type = c_args[0] tmpl = string.Template( tmpl ) for ns in std_namespaces: inst = tmpl.substitute( container=c_name , value_type=value_type , hash= ns + '::' + default_hash , less=default_less , equal_to=default_equal_to , allocator=default_allocator ) if defaults_eraser.normalize( cls_name ) == defaults_eraser.normalize( inst ): return templates.join( c_name, [defaults_eraser.erase_recursive( value_type )] )
def erase_map_compare_allocator(cls_name, default_compare='std::less', default_allocator='std::allocator'): cls_name = defaults_eraser.replace_basic_string(cls_name) c_name, c_args = templates.split(cls_name) if 4 != len(c_args): return key_type = c_args[0] mapped_type = c_args[1] tmpls = [ string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< const $key_type, $mapped_type> > >" ), string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< $key_type const, $mapped_type> > >" ), string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< $key_type, $mapped_type> > >" ) ] for tmpl in tmpls: tmpl = tmpl.substitute(container=c_name, key_type=key_type, mapped_type=mapped_type, compare=default_compare, allocator=default_allocator) if defaults_eraser.normalize( cls_name) == defaults_eraser.normalize(tmpl): return templates.join(c_name, [ defaults_eraser.erase_recursive(key_type), defaults_eraser.erase_recursive(mapped_type) ])
def erase_allocator( cls_name, default_allocator='std::allocator' ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) if 2 != len( c_args ): return value_type = c_args[0] tmpl = string.Template( "$container< $value_type, $allocator<$value_type> >" ) tmpl = tmpl.substitute( container=c_name, value_type=value_type, allocator=default_allocator ) if defaults_eraser.normalize( cls_name ) == defaults_eraser.normalize( tmpl ): return templates.join( c_name, [defaults_eraser.erase_recursive( value_type )] )
def erase_container( cls_name, default_container_name='std::deque' ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) if 2 != len( c_args ): return value_type = c_args[0] dc_no_defaults = defaults_eraser.erase_recursive( c_args[1] ) if defaults_eraser.normalize( dc_no_defaults ) \ != defaults_eraser.normalize( templates.join( default_container_name, [value_type] ) ): return return templates.join( c_name, [defaults_eraser.erase_recursive( value_type )] )
def erase_hashmap_compare_allocator(cls_name): cls_name = defaults_eraser.replace_basic_string(cls_name) c_name, c_args = templates.split(cls_name) default_hash = None default_less = 'std::less' default_allocator = 'std::allocator' default_equal_to = 'std::equal_to' tmpl = None key_type = None mapped_type = None if 2 < len(c_args): key_type = c_args[0] mapped_type = c_args[1] else: return if 4 == len(c_args): default_hash = 'hash_compare' tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type, $less<$key_type> >, $allocator< std::pair< const $key_type, $mapped_type> > >" ) if key_type.startswith('const ') or key_type.endswith(' const'): tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type, $less<$key_type> >, $allocator< std::pair< $key_type, $mapped_type> > >" ) elif 5 == len(c_args): default_hash = 'hash' tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type >, $equal_to<$key_type>, $allocator< $mapped_type> >" ) if key_type.startswith('const ') or key_type.endswith(' const'): tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type >, $equal_to<$key_type>, $allocator< $mapped_type > >" ) else: return for ns in std_namespaces: inst = tmpl.substitute(container=c_name, key_type=key_type, mapped_type=mapped_type, hash=ns + '::' + default_hash, less=default_less, equal_to=default_equal_to, allocator=default_allocator) if defaults_eraser.normalize( cls_name) == defaults_eraser.normalize(inst): return templates.join(c_name, [ defaults_eraser.erase_recursive(key_type), defaults_eraser.erase_recursive(mapped_type) ])
def get_partial_name(name): import templates import container_traits #prevent cyclic dependencies ct = container_traits.find_container_traits(name) if ct: return ct.remove_defaults(name) elif templates.is_instantiation(name): tmpl_name, args = templates.split(name) for i, arg_name in enumerate(args): args[i] = get_partial_name(arg_name.strip()) return templates.join(tmpl_name, args) else: return name
def get_partial_name( name ): import templates import container_traits #prevent cyclic dependencies ct = container_traits.find_container_traits( name ) if ct: return ct.remove_defaults( name ) elif templates.is_instantiation( name ): tmpl_name, args = templates.split( name ) for i, arg_name in enumerate( args ): args[i] = get_partial_name( arg_name.strip() ) return templates.join( tmpl_name, args ) else: return name
def erase_container_compare( cls_name, default_container_name='std::vector', default_compare='std::less' ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) if 3 != len( c_args ): return dc_no_defaults = defaults_eraser.erase_recursive( c_args[1] ) if defaults_eraser.normalize( dc_no_defaults ) \ != defaults_eraser.normalize( templates.join( default_container_name, [c_args[0]] ) ): return dcomp_no_defaults = defaults_eraser.erase_recursive( c_args[2] ) if defaults_eraser.normalize( dcomp_no_defaults ) \ != defaults_eraser.normalize( templates.join( default_compare, [c_args[0]] ) ): return value_type = defaults_eraser.erase_recursive( c_args[0] ) return templates.join( c_name, [value_type] )
def erase_hashmap_compare_allocator( cls_name ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) default_hash=None default_less='std::less' default_allocator='std::allocator' default_equal_to = 'std::equal_to' tmpl = None key_type = None mapped_type = None if 2 < len( c_args ): key_type = c_args[0] mapped_type = c_args[1] else: return if 4 == len( c_args ): default_hash = 'hash_compare' tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type, $less<$key_type> >, $allocator< std::pair< const $key_type, $mapped_type> > >" ) if key_type.startswith( 'const ' ) or key_type.endswith( ' const' ): tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type, $less<$key_type> >, $allocator< std::pair< $key_type, $mapped_type> > >" ) elif 5 == len( c_args ): default_hash = 'hash' tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type >, $equal_to<$key_type>, $allocator< $mapped_type> >" ) if key_type.startswith( 'const ' ) or key_type.endswith( ' const' ): tmpl = string.Template( "$container< $key_type, $mapped_type, $hash<$key_type >, $equal_to<$key_type>, $allocator< $mapped_type > >" ) else: return for ns in std_namespaces: inst = tmpl.substitute( container=c_name , key_type=key_type , mapped_type=mapped_type , hash=ns + '::' + default_hash , less=default_less , equal_to = default_equal_to , allocator=default_allocator ) if defaults_eraser.normalize( cls_name ) == defaults_eraser.normalize( inst ): return templates.join( c_name , [ defaults_eraser.erase_recursive( key_type ) , defaults_eraser.erase_recursive( mapped_type )] )
def erase_map_compare_allocator( cls_name, default_compare='std::less', default_allocator='std::allocator' ): cls_name = defaults_eraser.replace_basic_string( cls_name ) c_name, c_args = templates.split( cls_name ) if 4 != len( c_args ): return key_type = c_args[0] mapped_type = c_args[1] tmpls = [ string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< const $key_type, $mapped_type> > >" ) , string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< $key_type const, $mapped_type> > >" ) , string.Template( "$container< $key_type, $mapped_type, $compare<$key_type>, $allocator< std::pair< $key_type, $mapped_type> > >" )] for tmpl in tmpls: tmpl = tmpl.substitute( container=c_name , key_type=key_type , mapped_type=mapped_type , compare=default_compare , allocator=default_allocator ) if defaults_eraser.normalize( cls_name ) == defaults_eraser.normalize( tmpl ): return templates.join( c_name , [ defaults_eraser.erase_recursive( key_type ) , defaults_eraser.erase_recursive( mapped_type )] )