module HDLRuby::Serializer

Module adding serialization capability to HDLRuby classes

Public Instance Methods

to_basic(top = true, ref = false, types = {}, generated = [[]]) click to toggle source

Converts the object to a basic structure which can be dumped into an easy-to-write YAML string.

Other parameters:

+top+:: indicates if the object is the top of the
description or not. If it is the top, the namespace it comes
from is kept.
+types+:: contains the type objects which will have to be converted
separately.

def to_basic(top = true, types = {}) Converts the object to a basic structure which can be dumped into an easy-to-write YAML string.

Other parameters:

+top+:: indicates if the object is the top of the
description or not. If it is the top, the namespace it comes
from is kept.
+ref+:: indicates if the object is a reference or not.
        If it is a reference, its generation is to be skipped
        for later.
+types+:: contains the type objects which will have to be converted
separately.
+generated+:: is the stack of the generated named objects in the
current context.
# File lib/HDLRuby/hruby_serializer.rb, line 291
def to_basic(top = true, ref = false, types = {}, generated = [[]])
    # if !top and TO_BASICS_TYPES.include?(self.class) and
    # Ensure it is a initial ref: if the object has no name
    # it is the case.
    if ref then
        ref = self.respond_to?(:name) && !self.name.empty? 
    end
    # Process the object
    if !top and ref then
        # Refered object, but not the top, add it to the types list
        # without converting it if not already generated.
        unless generated.flatten.include?(self.name)
            # puts "Adding type with name=#{self.name}\n"
            types[self.name] = self
        end
        # puts "types=#{types}"
        # And return the name.
        return self.name.to_s
    end
    # puts "generating #{self.class} with name=#{self.name}\n" if self.respond_to?(:name)
    # Self is generated, remove it from the types to generate.
    generated[-1] << self.name if self.respond_to?(:name)
    # Add a level to the stack of generated named objects.
    generated << []
    # print "to_basic for class=#{self.class}\n"
    # Create the hash which will contains the content of the object.
    content = { }
    # Create the resulting hash with a single entry whose key
    # is the class name and whose value is the content of the
    # object.
    class_name = self.class.to_s
    # Keep only the class name
    class_name = HDLRuby.const_reduce(class_name)

    result = { class_name => content }
    # Fills the contents with the instance variables value.
    self.instance_variables.each do |var_sym|
        # Skip the fields that should not be serialized
        # next if var_sym == :@parent # Now added to FIELDS_TO_EXCLUDE
        next if (FIELDS_TO_EXCLUDE[self.class] and 
                 FIELDS_TO_EXCLUDE[self.class].include?(var_sym) )
        # print "for instance variable #{var_sym}...\n"
        # Skip the parent.
        # Get the value of the variable.
        var_val = self.instance_variable_get(var_sym)
        # Sets the content.
        # content[var_sym] = HDLRuby.value_to_basic(var_val,types)
        value = HDLRuby.value_to_basic(
            # FIELDS_OF_REF[self.class].include?(var_sym) &&
            # var_val.respond_to?(:name) && !var_val.name.empty?,
            FIELDS_OF_REF[self.class].include?(var_sym),
            var_val, types,generated)
        # Remove the @ from the symbol.
        var_sym = var_sym.to_s[1..-1]
        # EMPTY VALUES ARE NOT SKIPPED
        # # Empty values are skipped
        # unless value.respond_to?(:empty?) and value.empty? then
        #     content[var_sym] = value
        # end
        content[var_sym] = value
    end

    if top and !types.empty? then
        # It is a top and there where types.
        # The result is a sequence including each type and the
        # current object.
        result = [ result ]
        # Sort the type so that data types comes first.
        to_treat = types.each.partition {|name,type| !type.is_a?(Type) }
        to_treat.flatten!(1)
        while !to_treat.empty?
            others = {}
            to_treat.each do |name,type|
                # print "Dumping type with name=#{name}\n"
                # type_basic = type.to_basic(true)
                type_basic = type.to_basic(true,others)
                type_basic = type_basic.last if type_basic.is_a?(Array)
                result.unshift(type_basic)
            end
            to_treat = others
        end
            
    end

    # Restore the stack of generated named objets.
    generated.pop

    # Return the resulting hash.
    return result
end
to_yaml() click to toggle source

Converts the object to YAML string.

# File lib/HDLRuby/hruby_serializer.rb, line 383
def to_yaml
    # Convert the object to basic representations
    basics = to_basic
    # puts "basics=#{basics}"
    basics = [ basics ] unless basics.is_a?(Array)
    # Remove duplicate descriptions
    basics.uniq! { |elem| elem.first[1]["name"] }
    # Convert the basic representations to YAML
    return YAML.dump_stream(*basics)
end