Package pygccxml :: Package declarations :: Module mdecl_wrapper

Source Code for Module pygccxml.declarations.mdecl_wrapper

 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  """ 
 7  defines class L{mdecl_wrapper_t} that allows to work on set of declarations, 
 8  as it was one declaration. 
 9   
10  The L{class<mdecl_wrapper_t>} allows user to not write "for" loops within the code. 
11  """ 
12   
13  import os 
14   
15 -class call_redirector_t( object ):
16 """Internal class used to call some function of objects"""
17 - def __init__( self, name, decls ):
18 """creates call_redirector_t instance. 19 20 @param name: name of method, to be called on every object in C{decls} list 21 @param decls: list of objects 22 """ 23 object.__init__( self ) 24 self.name = name 25 self.decls = decls
26
27 - def __call__( self, *arguments, **keywords ):
28 """calls method C{self.name} on every object within C{self.decls} list""" 29 for d in self.decls: 30 callable_ = getattr(d, self.name) 31 callable_( *arguments, **keywords )
32
33 -class mdecl_wrapper_t( object ):
34 """Multiple declarations wrapper. 35 36 The main purpose of this class is to allow an user to work on many 37 declarations, as they were only one single declaration. 38 39 Example: 40 mb = module_builder_t( ... ) 41 #lets say we want to exclude all member functions, that returns reference to int: 42 mb.member_functions( return_type='int &' ).exclude() 43 44 "exclude" function will be called on every function that match the criteria. 45 """ 46
47 - def __init__( self, decls ):
48 """@param decls: list of declarations to operate on. 49 @type decls: list of L{declaration wrappers<decl_wrapper_t>} 50 """ 51 object.__init__( self ) 52 self.__dict__['declarations'] = decls
53
54 - def __nonzero__( self ):
55 return bool( self.declarations )
56
57 - def __len__( self ):
58 """returns the number of declarations""" 59 return len( self.declarations )
60
61 - def __getitem__( self, index ):
62 """provides access to declaration""" 63 return self.declarations[index]
64
65 - def __iter__( self ):
66 return iter(self.declarations)
67
68 - def __ensure_attribute( self, name ):
69 invalid_decls = filter( lambda d: not hasattr( d, name ), self.declarations ) 70 sep = os.linesep + ' ' 71 if invalid_decls: 72 raise RuntimeError( "Next declarations don't have '%s' attribute: %s" 73 % ( name, sep.join( map( str, invalid_decls ) ) ) )
74
75 - def __setattr__( self, name, value ):
76 """Updates the value of attribute on all declarations. 77 @param name: name of attribute 78 @param value: new value of attribute 79 """ 80 self.__ensure_attribute( name ) 81 for d in self.declarations: 82 setattr( d, name, value )
83
84 - def __getattr__( self, name ):
85 """@param name: name of method 86 """ 87 return call_redirector_t( name, self.declarations )
88
89 - def __contains__( self, item ):
90 return item in self.declarations
91
92 - def to_list(self):
93 l = [] 94 for d in self.declarations: 95 l.append( d ) 96 return l
97