Class: FFI::Inline::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/inline/builders.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(code = '') ⇒ Builder

Returns a new instance of Builder.



49
50
51
52
# File 'lib/ffi/inline/builders.rb', line 49

def initialize (code = '')
	@code  = code
	@evals = []
end

Instance Attribute Details

#codeObject (readonly)

Returns the value of attribute code.



47
48
49
# File 'lib/ffi/inline/builders.rb', line 47

def code
  @code
end

#compilerObject (readonly)

Returns the value of attribute compiler.



47
48
49
# File 'lib/ffi/inline/builders.rb', line 47

def compiler
  @compiler
end

Class Method Details

.[](name) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/ffi/inline/builders.rb', line 18

def self.[] (name)
	return name if name.is_a?(Builder)

	@builders.find {|builder|
		builder.name.downcase == name.downcase ||
		builder.aliases.any? { |ali| ali.downcase == name.downcase }
	}
end

.define(name, *aliases, &block) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ffi/inline/builders.rb', line 27

def self.define (name, *aliases, &block)
	inherit_from = self

	if name.is_a?(Builder)
		name = name.class
	end

	if name.is_a?(Class)
		inherit_from = name
		name         = aliases.shift
	end

	@builders << Class.new(inherit_from, &block).tap {|k|
		k.instance_eval {
			define_singleton_method :name do name end
			define_singleton_method :aliases do aliases end
		}
	}
end

Instance Method Details

#buildObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/ffi/inline/builders.rb', line 82

def build
	builder = self
	blocks  = @evals

	mod = Module.new
	mod.instance_eval {
		extend FFI::Library

		ffi_lib builder.shared_object

		blocks.each { |block| instance_eval &block }

		builder.signatures.each {|s|
			attach_function s.name, s.arguments.compact.map {|a|
				builder.to_ffi_type(a, self)
			}, builder.to_ffi_type(s.return, self), :blocking => s.blocking
		}
	}

	mod
end

#eval(&block) ⇒ Object



62
63
64
# File 'lib/ffi/inline/builders.rb', line 62

def eval (&block)
	@evals << block
end

#raw(code) ⇒ Object



58
59
60
# File 'lib/ffi/inline/builders.rb', line 58

def raw (code)
	@code << code
end

#shared_objectObject



70
71
72
# File 'lib/ffi/inline/builders.rb', line 70

def shared_object
	raise 'the Builder has not been specialized'
end

#signaturesObject



74
75
76
# File 'lib/ffi/inline/builders.rb', line 74

def signatures
	raise 'the Builder has not been specialized'
end

#symbolsObject



78
79
80
# File 'lib/ffi/inline/builders.rb', line 78

def symbols
	signatures.map { |s| s.name.to_sym }
end

#to_ffi_type(type) ⇒ Object



66
67
68
# File 'lib/ffi/inline/builders.rb', line 66

def to_ffi_type (type)
	raise 'the Builder has not been specialized'
end

#use_compiler(compiler) ⇒ Object



54
55
56
# File 'lib/ffi/inline/builders.rb', line 54

def use_compiler (compiler)
	@compiler = Compiler[compiler]
end