Class: FFI::ConstGenerator::Constant

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi/tools/const_generator.rb

Overview

This class hold constants for FFI::ConstGenerator

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format, cast, ruby_name = nil, converter = nil) ⇒ Constant

Returns a new instance of Constant.

Parameters:

  • name (#to_s)
  • format (String)

    a printf format string to print the value out

  • cast (String)

    a C cast for the value

  • ruby_name (defaults to: nil)

    alternate ruby name for #to_ruby

  • converter (#call) (defaults to: nil)

    convert the value from a string to the appropriate type for #to_ruby.


199
200
201
202
203
204
205
206
# File 'lib/ffi/tools/const_generator.rb', line 199

def initialize(name, format, cast, ruby_name = nil, converter=nil)
  @name = name
  @format = format
  @cast = cast
  @ruby_name = ruby_name
  @converter = converter
  @value = nil
end

Instance Attribute Details

#castObject (readonly)

Returns the value of attribute cast.


190
191
192
# File 'lib/ffi/tools/const_generator.rb', line 190

def cast
  @cast
end

#formatObject (readonly)

Returns the value of attribute format.


190
191
192
# File 'lib/ffi/tools/const_generator.rb', line 190

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.


190
191
192
# File 'lib/ffi/tools/const_generator.rb', line 190

def name
  @name
end

#valueObject

Returns the value of attribute value.


191
192
193
# File 'lib/ffi/tools/const_generator.rb', line 191

def value
  @value
end

Instance Method Details

#converted_valueObject

Return constant value (converted if a converter was defined).

Returns:

  • constant value.


210
211
212
213
214
215
216
# File 'lib/ffi/tools/const_generator.rb', line 210

def converted_value
  if @converter
    @converter.call(@value)
  else
    @value
  end
end

#ruby_nameString

get constant ruby name

Returns:

  • (String)

220
221
222
# File 'lib/ffi/tools/const_generator.rb', line 220

def ruby_name
  @ruby_name || @name
end

#to_rubyString

Get an evaluable string from constant.

Returns:

  • (String)

226
227
228
# File 'lib/ffi/tools/const_generator.rb', line 226

def to_ruby
  "#{ruby_name} = #{converted_value}"
end