formatted comments

This commit is contained in:
Sami Samhuri 2010-01-19 23:18:16 -08:00
parent aea6e5c070
commit c452fc1c28

View file

@ -1,7 +1,9 @@
# Struct does some trickery with custom allocators so we can't subclass it without writing C. # Struct does some trickery with custom allocators so we can't
# Instead we define a CStruct class that does something similar enough for our purpose. It is # subclass it without writing C. Instead we define a CStruct class
# subclassed just like any other class. A nice side-effect of this syntax is that it is always # that does something similar enough for our purpose. It is
# clear that a CStruct is just a class and instances of the struct are objects. # subclassed just like any other class. A nice side-effect of this
# syntax is that it is always clear that a CStruct is just a class and
# instances of the struct are objects.
# #
# Some light metaprogramming is used to make the following syntax possible: # Some light metaprogramming is used to make the following syntax possible:
# #
@ -27,9 +29,26 @@
# uint32 # uint32
# end # end
# #
# Nothing tricky or confusing there. Members of a CStruct class are declared in the # Nothing tricky or confusing there. Members of a CStruct class are
# class definition. A different definition using a more static approach probably wouldn't # declared in the class definition. A different definition using a
# be very hard... if performance is critical ... but then why are you using Ruby? ;-) # more static approach probably wouldn't be very hard... if
# performance is critical ... but then why are you using Ruby? ;-)
#
#
# TODO support bit fields
#
# Bit fields should be supported by passing the number of bits a field
# should occupy. Perhaps we could use the size 'pack' for the rest of
# the field.
#
# class RelocationInfo < CStruct
# int32 :address
# uint32 :symbolnum, 24
# pack :pcrel, 1
# pack :length, 2
# pack :extern, 1
# pack :type, 4
# end
class CStruct class CStruct
@ -85,24 +104,28 @@ class CStruct
# Class Instance Methods # # Class Instance Methods #
########################## ##########################
# Note: const_get and const_set are used so the constants are bound at runtime, to the # Note: const_get and const_set are used so the constants are bound
# real class that has subclassed CStruct. I figured Ruby would do this but I haven't # at runtime, to the real class that has subclassed CStruct.
# looked at the implementation of constants so it might be tricky. # I figured Ruby would do this but I haven't looked at the
# implementation of constants so it might be tricky.
# #
# All of this could probably be avoided with Ruby 1.9 and private class variables. # All of this could probably be avoided with Ruby 1.9 and
# That is definitely something to experiment with. # private class variables. That is definitely something to
# experiment with.
class <<self class <<self
def inherited(subclass) def inherited(subclass)
subclass.instance_eval do subclass.instance_eval do
# These "constants" are only constant references. Structs can be modified. # These "constants" are only constant references. Structs can
# After the struct is defined it is still open, but good practice would be not # be modified. After the struct is defined it is still open,
# to change a struct after it has been defined. # but good practice would be not to change a struct after it
# has been defined.
# #
# To support inheritance properly we try to get these constants from the enclosing # To support inheritance properly we try to get these
# scope (and clone them before modifying them!), and default to empty, er, defaults. # constants from the enclosing scope (and clone them before
# modifying them!), and default to empty, er, defaults.
members = const_get(:Members).clone rescue [] members = const_get(:Members).clone rescue []
member_index = const_get(:MemberIndex).clone rescue {} member_index = const_get(:MemberIndex).clone rescue {}