Class: CShadow::Library

Inherits:
CGenerator::Library show all
Defined in:
lib/cgen/cshadow.rb

Instance Attribute Summary

Attributes inherited from CGenerator::Library

#include_dirs, #include_file, #init_library_function, #purge_source_dir, #show_times_flag, #source_file

Attributes inherited from CGenerator::Accumulator

#name, #parent

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CGenerator::Library

#add_file, #after_commit, #assert_uncommitted, #before_commit, #build_wrapper, #commit, #committed?, #declare, #declare_extern, #declare_extern_struct, #declare_module, #declare_struct, #declare_symbol, #define, #define_c_global_function, #define_c_method, #define_c_module_function, #define_c_singleton_method, #empty?, #extconf, #include, #library, #literal_symbol, #loadlib, #make, #make_program, #makedepend, #mkmf, #print_update_reason, #process_times, #setup, #show_times, #update_file, #use_work_dir, #write

Methods inherited from CGenerator::Template

accumulator

Methods inherited from CGenerator::Accumulator

#accept?, #add, #add_one, #add_one_really, #inspect, #inspect_one, #output, #output_one, #separator, #to_s

Constructor Details

#initialize(*args) ⇒ Library

:nodoc:



252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/cgen/cshadow.rb', line 252

def initialize(*args)  # :nodoc:
  super
  
  before_commit do
    @classes_to_commit = []
    ObjectSpace.each_object(CShadowClassMethods) do |cl|
      if cl.shadow_library == self
        @classes_to_commit << cl
      end
    end

    # This is done here, rather than in #inherited, to get around
    # the Ruby bug with names of nested classes. It's ugly...
    ## this may be fixed in 1.7
    # Better: register classes with libraries...
    
    classes = Library.sort_class_tree(@classes_to_commit)
    
    classes.each do |cl|
      cl.fill_in_defs
    end
  end

  after_commit do
    for cl in @classes_to_commit
      cl.protect_shadow_attrs
    end
  end
end

Class Method Details

.sort_class_tree(classes) ⇒ Object

Sort a list of classes. Sorting has the following properties:

Deterministic – you get the same output no matter what order the input is in, because we’re using #sort_by.

Compatible with original order on classes: superclass comes before subclass.

Unrelated classes are ordered alphabetically by name

Note that

classes.sort {|c,d| (d <=> c) || (c.name <=> d.name)}

is wrong.



298
299
300
# File 'lib/cgen/cshadow.rb', line 298

def self.sort_class_tree(classes)
  classes.sort_by {|c| c.ancestors.reverse!.map!{|d|d.to_s}}
end