Class: RLTK::CG::GenericValue

Inherits:
Object
  • Object
show all
Includes:
BindingClass
Defined in:
lib/rltk/cg/generic_value.rb

Overview

GenericValue objects are used to pass parameters into ExecutionEngines as well as retreive an evaluated function’s result. They may contain values of several different types:

* Integer
* Float
* Boolean

Instance Attribute Summary collapse

Attributes included from BindingClass

#ptr

Instance Method Summary collapse

Methods included from BindingClass

#==

Constructor Details

#initialize(ruby_val, type = nil, signed = true) ⇒ GenericValue

Creates a new GenericValue from a given Ruby value.

Parameters:

  • ruby_val (FFI::Pointer, Integer, ::Float, Boolean)
  • type (Type) (defaults to: nil)

    Type of Integer or Float to create.

  • signed (Boolean) (defaults to: true)

    Signed or unsigned Integer.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rltk/cg/generic_value.rb', line 38

def initialize(ruby_val, type = nil, signed = true)
	@ptr, @type =
	case ruby_val
	when FFI::Pointer
		[ruby_val, nil]
		
	when ::Integer
		type = if type then check_cg_type(type, IntType) else NativeIntType.instance end
		
		[Bindings.create_generic_value_of_int(type, ruby_val, signed.to_i), type]
		
	when ::Float
		type = if type then check_cg_type(type, RealType) else FloatType.instance end
		
		[Bindings.create_generic_value_of_float(type, ruby_val), type]
		
	when TrueClass
		[Bindings.create_generic_value_of_int(Int1Type, 1, 0), Int1Type]
		
	when FalseClass
		[Bindings.create_generic_value_of_int(Int1Type, 0, 0), Int1Type]
	end
end

Instance Attribute Details

#typeType (readonly)

Returns LLVM type of this GenericValue.

Returns:

  • (Type)

    LLVM type of this GenericValue.



31
32
33
# File 'lib/rltk/cg/generic_value.rb', line 31

def type
  @type
end

Instance Method Details

#disposevoid

This method returns an undefined value.

Frees the resources used by LLVM for this value.



65
66
67
68
69
70
# File 'lib/rltk/cg/generic_value.rb', line 65

def dispose
	if @ptr
		Bindings.dispose_generic_value(@ptr)
		@ptr = nil
	end
end

#to_bBoolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/rltk/cg/generic_value.rb', line 89

def to_b
	self.to_i(false).to_bool
end

#to_f(type = RLTK::CG::FloatType) ⇒ Float

Parameters:

  • type (FloatType) (defaults to: RLTK::CG::FloatType)

    Type of the real value stored in this GenericValue.

Returns:



84
85
86
# File 'lib/rltk/cg/generic_value.rb', line 84

def to_f(type = RLTK::CG::FloatType)
	Bindings.generic_value_to_float(@type || check_cg_type(type, RLTK::CG::NumberType), @ptr)
end

#to_i(signed = true) ⇒ Integer

Parameters:

  • signed (Boolean) (defaults to: true)

    Treat the GenericValue as a signed integer.

Returns:



75
76
77
78
79
# File 'lib/rltk/cg/generic_value.rb', line 75

def to_i(signed = true)
	val = Bindings.generic_value_to_int(@ptr, signed.to_i)
	
	if signed and val >= 2**63 then val - 2**64 else val end
end

#to_ptr_valueFFI::Pointer

Returns GenericValue as a pointer.

Returns:

  • (FFI::Pointer)

    GenericValue as a pointer.



94
95
96
# File 'lib/rltk/cg/generic_value.rb', line 94

def to_ptr_value
	Bindings.generic_value_to_pointer(@ptr)
end