diff options
Diffstat (limited to 'lib/asn1kit/types/real.rb')
-rw-r--r-- | lib/asn1kit/types/real.rb | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/lib/asn1kit/types/real.rb b/lib/asn1kit/types/real.rb new file mode 100644 index 0000000..3b2a108 --- /dev/null +++ b/lib/asn1kit/types/real.rb @@ -0,0 +1,46 @@ +# coding: ASCII-8BIT + +class ASN1Kit::Real < ASN1Kit::Type + asn1_tag :IMPLICIT, :UNIVERSAL, 9 + asn1_alias "REAL" + + PLUS_INFINITY = :plus_infinity + MINUS_INFINITY = :minus_infinity + NOT_A_NUMBER = :not_a_number + MINUS_ZERO = :minus_zero + + SPECIAL_VALUES = Set[ + PLUS_INFINITY, MINUS_INFINITY, NOT_A_NUMBER, MINUS_ZERO + ] + private_constant :SPECIAL_VALUES + + def initialize(value) + case value + when PLUS_INFINITY, MINUS_INFINITY, NOT_A_NUMBER, MINUS_ZERO + @value = value + when Integer + # TODO: Consider constraints + @value = { mantissa: value, base: 10, exponent: 0 } + when Float, Rational + raise NotImplementedError + else + # Assuming value is a { mantissa: M, base: B, exponent: E } value + @value = value + end + end + + def cast_to(type) + return type.new(@value) if type <= ASN1Kit::Real + super + end + + def plus_infinity?() @value == PLUS_INFINITY end + def minus_infinity?() @value == MINUS_INFINITY end + def not_a_number?() @value == NOT_A_NUMBER end + def minus_zero?() @value == MINUS_ZERO end + def raw_value() @value end + + def ==(other) + other.is_a?(ASN1Kit::Real) && @value == other.raw_value + end +end |