1   
  2   
  3   
  4   
  5   
  6  from pygccxml import declarations 
  7  from pyplusplus import decl_wrappers 
  8   
 13   
 16          object.__init__( self ) 
 17   
 18          self.__classes = filter( lambda x: isinstance( x, declarations.class_t ) 
 19                                   , decls ) 
 20          self.__classes.sort( lambda cls1, cls2: cmp( cls1.decl_string, cls2.decl_string ) ) 
 21          self.__dependencies_graph = self._build_graph() 
 22          self.__time = 0 
 23          self.__colors = dict( zip( self.__dependencies_graph.keys() 
 24                                , [ COLOR.WHITE ] * len( self.__dependencies_graph ) ) ) 
 25          self.__class_discovered = dict( zip( self.__dependencies_graph.keys() 
 26                                          , [ 0 ] * len( self.__dependencies_graph ) ) ) 
 27          self.__class_treated = dict( zip( self.__dependencies_graph.keys() 
 28                                       , [ 0 ] * len( self.__dependencies_graph ) ) ) 
 29   
 30          self.__desired_order = [] 
 31   
 32          self._topological_sort() 
  33   
 42   
 76   
 83   
 86   
 91   
 93          self.__colors[base] = COLOR.GRAY 
 94          self.__time += 1 
 95          self.__class_discovered[base] = self.__time 
 96          for derived in self.__dependencies_graph[base]: 
 97              if self.__colors.has_key( derived ) and self.__colors[derived] == COLOR.WHITE: 
 98                  self._dfs_visit( derived ) 
 99              else: 
100                  pass 
101                   
102                   
103                   
104   
105          self.__colors[base] = COLOR.BLACK 
106          self.__time += 1 
107          self.__class_treated = self.__time 
108          self.__desired_order.append(base) 
 109   
134          groups = { None: [] } 
135          for d in decls: 
136              if not isinstance( d, declarations.calldef_t ) or 1 != len( d.required_args ): 
137                  groups[ None ].append( d ) 
138              else: 
139                  if not groups.has_key( d.name ): 
140                      groups[ d.name ] = [] 
141                  groups[ d.name ].append( d ) 
142          return groups 
 143   
146   
147 -    def __cmp( self, f1, f2 ): 
 148          result = self.__cmp_types( f1.arguments[0].type, f2.arguments[0].type )     
149          if None is result: 
150              result = self.__cmp_unrelated( f1, f2 ) 
151          return result 
 152   
154          for group in groups.keys(): 
155              if None is group: 
156                  continue 
157              groups[ group ].sort( self.__cmp ) 
 158       
160          decls = [] 
161          sorted_keys = groups.keys() 
162          sorted_keys.sort() 
163          for group in sorted_keys: 
164              decls.extend( groups[group] ) 
165          return decls 
 166   
167 -    def sort( self, decls ): 
 168          groups = self.__build_groups( decls ) 
169          self.__sort_groups(groups) 
170          result = self.__join_groups(groups) 
171          return result     
 172   
176   
179   
180  USE_CALLDEF_ORGANIZER = False  
181   
182   
183   
185      classes = filter( lambda x: isinstance( x, declarations.class_t ), decls ) 
186      ordered = sort_classes( classes ) 
187   
188      ids = set( [ id( inst ) for inst in ordered ] ) 
189      for decl in decls: 
190          if id( decl ) not in ids: 
191              ids.add( id(decl) ) 
192              ordered.append( decl ) 
193       
194      variables = [] 
195      enums = [] 
196      others = [] 
197      classes = [] 
198      constructors = [] 
199      for inst in ordered: 
200          if isinstance( inst, declarations.variable_t ): 
201              variables.append( inst ) 
202          elif isinstance( inst, declarations.enumeration_t ): 
203              enums.append( inst ) 
204          elif isinstance( inst, ( declarations.class_t, declarations.class_declaration_t ) ): 
205              classes.append( inst ) 
206          elif isinstance( inst, declarations.constructor_t ): 
207              constructors.append( inst ) 
208          else: 
209              others.append( inst ) 
210       
211      cmp_by_name = lambda d1, d2: cmp( d1.name, d2.name ) 
212      cmp_by_line = lambda d1, d2: cmp( d1.location.line, d2.location.line ) 
213   
214      enums.sort( cmp=cmp_by_name ) 
215      variables.sort( cmp=cmp_by_name ) 
216      if USE_CALLDEF_ORGANIZER: 
217          others = sort_calldefs(others) 
218          constructors = sort_calldefs(constructors) 
219      else: 
220          others.sort( cmp=cmp_by_name ) 
221          constructors.sort( cmp=cmp_by_line ) 
222       
223      new_ordered = [] 
224      new_ordered.extend( enums ) 
225      new_ordered.extend( classes ) 
226      new_ordered.extend( constructors ) 
227      new_ordered.extend( others ) 
228      new_ordered.extend( variables ) 
229      return new_ordered  
 230