Class: Origen::Value
- Defined in:
- lib/origen/value.rb,
lib/origen/value/bin_str_val.rb,
lib/origen/value/hex_str_val.rb
Overview
This class wraps various different class which handle number representation in various formats.
The user should never instantiate those directly and should always instantiate an Origen::Value instance, thereby ensuring a common API regardless of the internal representation and handling of the value
Defined Under Namespace
Classes: BinStrVal, HexStrVal, X, Z
Instance Method Summary collapse
- #[](index) ⇒ Object
- #bin_str_val? ⇒ Boolean (also: #bin_str_value?)
- #hex_str_val? ⇒ Boolean (also: #hex_str_value?)
-
#initialize(val, options = {}) ⇒ Value
constructor
A new instance of Value.
-
#numeric? ⇒ Boolean
Returns true if all bits have a numeric value - i.e.
-
#size ⇒ Object
(also: #bits, #number_of_bits)
Returns the size of the value in bits.
-
#to_i ⇒ Object
Converts to an integer, returns nil if the value contains non-numeric bits.
-
#to_s ⇒ Object
Converts to a string, the format of it depends on the underlying value type.
- #value? ⇒ Boolean
Constructor Details
#initialize(val, options = {}) ⇒ Value
Returns a new instance of Value.
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/origen/value.rb', line 48 def initialize(val, = {}) if val.is_a?(Integer) @val = val else val = val.to_s case val[0].downcase when 'b' @val = BinStrVal.new(val, ) when 'h' @val = HexStrVal.new(val, ) when 'd' @val = val.to_s[1..-1].to_i else if val =~ /^[0-9]+$/ @val = val.to_i else fail 'Unsupported value syntax' end end end end |
Instance Method Details
#[](index) ⇒ Object
106 107 108 109 110 111 112 |
# File 'lib/origen/value.rb', line 106 def [](index) if index.is_a?(Range) fail 'Currently, only single bit extraction from a Value object is supported' end val[index] end |
#bin_str_val? ⇒ Boolean Also known as: bin_str_value?
101 102 103 |
# File 'lib/origen/value.rb', line 101 def bin_str_val? val.is_a?(BinStrVal) end |
#hex_str_val? ⇒ Boolean Also known as: hex_str_value?
96 97 98 |
# File 'lib/origen/value.rb', line 96 def hex_str_val? val.is_a?(HexStrVal) end |
#numeric? ⇒ Boolean
Returns true if all bits have a numeric value - i.e. no X or Z
71 72 73 |
# File 'lib/origen/value.rb', line 71 def numeric? val.numeric? end |
#size ⇒ Object Also known as: bits, number_of_bits
Returns the size of the value in bits
90 91 92 |
# File 'lib/origen/value.rb', line 90 def size val.size end |
#to_i ⇒ Object
Converts to an integer, returns nil if the value contains non-numeric bits
80 81 82 |
# File 'lib/origen/value.rb', line 80 def to_i val.to_i end |
#to_s ⇒ Object
Converts to a string, the format of it depends on the underlying value type
85 86 87 |
# File 'lib/origen/value.rb', line 85 def to_s val.to_s end |
#value? ⇒ Boolean
75 76 77 |
# File 'lib/origen/value.rb', line 75 def value? true end |