Class | FFI::ConstGenerator
In: | lib/ffi/tools/const_generator.rb
Parent: | Object

ConstGenerator turns C constants into ruby values.

@example a simple example for stdio

     cg = FFI::ConstGenerator.new('stdio') do |gen|
       gen.const(:SEEK_SET)
       gen.const('SEEK_CUR')
       gen.const('seek_end')   # this constant does not exist
     end            # #calculate called automatically at the end of the block

     cg['SEEK_SET'] # => 0
     cg['SEEK_CUR'] # => 1
     cg['seek_end'] # => nil
     cg.to_ruby     # => "SEEK_SET = 0\nSEEK_CUR = 1\n# seek_end not available"

### Methods

[] calculate const dump_constants include new options options= to_ruby

### Classes and Modules

Class FFI::ConstGenerator::Constant

### Attributes

constants | [R] |

### Public Class methods

new(prefix = nil, options = {}) {|self| ...}

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.

@param [to_s] prefix @param [Hash] options @return @option options [Boolean] :required @overload initialize(prefix, options) @overload initialize(prefix, options) { |gen| … }

     @yieldparam [ConstGenerator] gen new generator is passed to the block
     When passed a block, {#calculate} is automatically called at the end of
     the block, otherwise you must call it yourself.

options()

Get class options. @return [Hash] class options

options=(options)

Set class options These options are merged with {initialize} options when it is called with a block. @param [Hash] options @return [Hash] class options

### Public Instance methods

[](name)

@param [String] name @return constant value (converted if a `converter` was defined). Access a constant by name.

calculate(options = {})

Calculate constants values. @param [Hash] options @option options [String] :cppflags flags for C compiler @return [nil] @raise if a constant is missing and +:required+ was set to `true` (see {initialize})

const(name, format = nil, cast = '', ruby_name = nil, converter = nil, &converter_proc)

Request the value for C constant `name`.

@param [to_s] name C constant name @param [String] format a printf format string to print the value out @param [String] cast a C cast for the value @param ruby_name alternate ruby name for {to_ruby}

@overload const(name, format=nil, cast=’’, ruby_name=nil, converter=nil)

     +converter+ is a Method or a Proc.
     @param [#call] converter convert the value from a string to the appropriate
      type for {#to_ruby}.

@overload const(name, format=nil, cast=’’, ruby_name=nil) { |value| … }

     Use a converter block. This block convert the value from a string to the
     appropriate type for {#to_ruby}.
     @yieldparam value constant value

dump_constants(io)

Dump constants to `io`. @param [puts] io @return [nil]

include(*i)

Add additional C include file(s) to calculate constants from. @note +stdio.h+ and +stddef.h+ automatically included @param [List<String>, Array<String>] i include file(s) @return [Array<String>] array of include files

to_ruby()

Outputs values for discovered constants. If the constant‘s value was not discovered it is not omitted. @return [String]

[Validate]