Class: HDLRuby::High::Namespace

Inherits:
Object
  • Object
show all
Includes:
SingletonExtend
Defined in:
lib/HDLRuby/hruby_high.rb

Overview

Describes a namespace. Used for managing the access points to internals of hardware constructs.

Constant Summary collapse

RESERVED =

The reserved names

[ :user, :initialize, :add_method, :concat_namespace,
:to_namespace, :user?, :user_deep? ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SingletonExtend

#eigen_extend

Constructor Details

#initialize(user) ⇒ Namespace

Creates a new namespace attached to +user+.



63
64
65
66
67
68
# File 'lib/HDLRuby/hruby_high.rb', line 63

def initialize(user)
    # Sets the user.
    @user = user
    # Initialize the concat namespaces.
    @concats = []
end

Instance Attribute Details

#userObject (readonly)

The construct using the namespace.



60
61
62
# File 'lib/HDLRuby/hruby_high.rb', line 60

def user
  @user
end

Instance Method Details

#add_method(name, &ruby_block) ⇒ Object

Adds method +name+ provided the name is not empty and the method is not already defined in the current namespace.



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/HDLRuby/hruby_high.rb', line 83

def add_method(name,&ruby_block)
    # puts "add_method with name=#{name} and parameters=#{ruby_block.parameters}"
    unless name.empty? then
        if RESERVED.include?(name.to_sym) then
            raise AnyError, 
                  "Resevered name #{name} cannot be overridden."
        end
        # Deactivated: overriding is now accepted.
        # if self.respond_to?(name) then
        #     raise AnyError,
        #           "Symbol #{name} is already defined."
        # end
        define_singleton_method(name,&ruby_block) 
    end
end

#cloneObject

Clones (safely) the namespace.



71
72
73
74
75
76
77
78
79
# File 'lib/HDLRuby/hruby_high.rb', line 71

def clone
    # Create the new namespace.
    res = Namespace.new(@user)
    # Adds the concats.
    @concats.each do |concat|
        res.concat_namespace(concat)
    end
    return res
end

#concat_namespace(namespace) ⇒ Object

Concats another +namespace+ to current one.



100
101
102
103
104
105
106
# File 'lib/HDLRuby/hruby_high.rb', line 100

def concat_namespace(namespace)
    # Ensure namespace is really a namespace and concat it.
    namespace = namespace.to_namespace
    self.eigen_extend(namespace)
    # Adds the concat the the list.
    @concats << namespace
end

#to_namespaceObject

Ensure it is a namespace



109
110
111
# File 'lib/HDLRuby/hruby_high.rb', line 109

def to_namespace
    return self
end

#user?(object) ⇒ Boolean

Tell if an +object+ is the user of the namespace.

Returns:

  • (Boolean)


114
115
116
# File 'lib/HDLRuby/hruby_high.rb', line 114

def user?(object)
    return @user.equal?(object)
end

#user_deep?(object) ⇒ Boolean

Tell if an +object+ is the user of the namespace or of one of its concats.

Returns:

  • (Boolean)


120
121
122
123
124
125
126
127
128
# File 'lib/HDLRuby/hruby_high.rb', line 120

def user_deep?(object)
    # puts "@user=#{@user}, @concats=#{@concats.size}, object=#{object}"
    # Convert the object to a user if appliable (for SystemT)
    object = object.to_user if object.respond_to?(:to_user)
    # Maybe object is the user of this namespace.
    return true if user?(object)
    # No, try in the concat namespaces.
    @concats.any? { |concat| concat.user_deep?(object) }
end