Class: FFI::ConstGenerator
- Inherits:
-
Object
- Object
- FFI::ConstGenerator
- Defined in:
- lib/ffi/tools/const_generator.rb
Overview
ConstGenerator turns C constants into ruby values.
Defined Under Namespace
Classes: Constant
Instance Attribute Summary collapse
-
#constants ⇒ Object
readonly
Returns the value of attribute constants.
Class Method Summary collapse
-
.options ⇒ Hash
Get class options.
-
.options=(options) ⇒ Hash
Set class options These options are merged with #initialize options when it is called with a block.
Instance Method Summary collapse
-
#[](name) ⇒ Object
Access a constant by name.
-
#calculate(options = {}) ⇒ nil
Calculate constants values.
-
#const(name, format = nil, cast = '', ruby_name = nil, converter = nil, &converter_proc) ⇒ Object
Request the value for C constant
name
. -
#dump_constants(io) ⇒ nil
Dump constants to
io
. -
#include(*i) ⇒ Array<String>
Add additional C include file(s) to calculate constants from.
-
#initialize(prefix = nil, options = {}) ⇒ Object
constructor
Creates a new constant generator that uses
prefix
as a name, and an options hash. -
#to_ruby ⇒ String
Outputs values for discovered constants.
Constructor Details
#initialize(prefix, options) ⇒ Object #initialize(prefix, options) {|gen| ... } ⇒ Object
Creates a new constant generator that uses prefix
as a name, and an options hash.
The only option is :required
, which if set to true
raises an error if a constant you have requested was not found.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/ffi/tools/const_generator.rb', line 39 def initialize(prefix = nil, = {}) @includes = ['stdio.h', 'stddef.h'] @constants = {} @prefix = prefix @required = [:required] @options = if block_given? then yield self calculate self.class..merge() end end |
Instance Attribute Details
#constants ⇒ Object (readonly)
Returns the value of attribute constants.
22 23 24 |
# File 'lib/ffi/tools/const_generator.rb', line 22 def constants @constants end |
Class Method Details
.options ⇒ Hash
Get class options.
61 62 63 |
# File 'lib/ffi/tools/const_generator.rb', line 61 def self. @options end |
.options=(options) ⇒ Hash
Set class options These options are merged with #initialize options when it is called with a block.
56 57 58 |
# File 'lib/ffi/tools/const_generator.rb', line 56 def self.() @options = end |
Instance Method Details
#[](name) ⇒ Object
Access a constant by name.
67 68 69 |
# File 'lib/ffi/tools/const_generator.rb', line 67 def [](name) @constants[name].converted_value end |
#calculate(options = {}) ⇒ nil
Calculate constants values.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ffi/tools/const_generator.rb', line 107 def calculate( = {}) binary_path = nil Tempfile.open("#{@prefix}.const_generator") do |f| binary_path = f.path + ".bin" @includes.each do |inc| f.puts "#include <#{inc}>" end f.puts "\nint main(int argc, char **argv)\n{" @constants.each_value do |const| f.puts <<-EOF #ifdef #{const.name} printf("#{const.name} #{const.format}\\n", #{const.cast}#{const.name}); #endif EOF end f.puts "\n\treturn 0;\n}" f.flush cc = ENV['CC'] || 'gcc' output = `#{cc} #{[:cppflags]} -D_DARWIN_USE_64_BIT_INODE -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -x c -Wall -Werror #{f.path} -o #{binary_path} 2>&1` unless $?.success? then output = output.split("\n").map { |l| "\t#{l}" }.join "\n" raise "Compilation error generating constants #{@prefix}:\n#{output}" end end output = `#{binary_path}` File.unlink(binary_path + (FFI::Platform.windows? ? ".exe" : "")) output.each_line do |line| line =~ /^(\S+)\s(.*)$/ const = @constants[$1] const.value = $2 end missing_constants = @constants.select do |name, constant| constant.value.nil? end.map { |name,| name } if @required and not missing_constants.empty? then raise "Missing required constants for #{@prefix}: #{missing_constants.join ', '}" end end |
#const(name, format = nil, cast = '', ruby_name = nil, converter = nil) ⇒ Object #const(name, format = nil, cast = '', ruby_name = nil) {|value| ... } ⇒ Object
Request the value for C constant name
.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/ffi/tools/const_generator.rb', line 86 def const(name, format = nil, cast = '', ruby_name = nil, converter = nil, &converter_proc) format ||= '%d' cast ||= '' if converter_proc and converter then raise ArgumentError, "Supply only converter or converter block" end converter = converter_proc if converter.nil? const = Constant.new name, format, cast, ruby_name, converter @constants[name.to_s] = const return const end |
#dump_constants(io) ⇒ nil
Dump constants to io
.
157 158 159 160 161 162 |
# File 'lib/ffi/tools/const_generator.rb', line 157 def dump_constants(io) @constants.each do |name, constant| name = [@prefix, name].join '.' if @prefix io.puts "#{name} = #{constant.converted_value}" end end |
#include(*i) ⇒ Array<String>
stdio.h
and stddef.h
automatically included
Add additional C include file(s) to calculate constants from.
181 182 183 |
# File 'lib/ffi/tools/const_generator.rb', line 181 def include(*i) @includes |= i.flatten end |
#to_ruby ⇒ String
Outputs values for discovered constants. If the constant’s value was not discovered it is not omitted.
167 168 169 170 171 172 173 174 175 |
# File 'lib/ffi/tools/const_generator.rb', line 167 def to_ruby @constants.sort_by { |name,| name }.map do |name, constant| if constant.value.nil? then "# #{name} not available" else constant.to_ruby end end.join "\n" end |