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.



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

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.



188
189
190
# File 'lib/ffi/tools/const_generator.rb', line 188

def cast
  @cast
end

#formatObject (readonly)

Returns the value of attribute format.



188
189
190
# File 'lib/ffi/tools/const_generator.rb', line 188

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



188
189
190
# File 'lib/ffi/tools/const_generator.rb', line 188

def name
  @name
end

#valueObject

Returns the value of attribute value.



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

def value
  @value
end

Instance Method Details

#converted_valueObject

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

Returns:

  • constant value.



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

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

#ruby_nameString

get constant ruby name

Returns:

  • (String)


218
219
220
# File 'lib/ffi/tools/const_generator.rb', line 218

def ruby_name
  @ruby_name || @name
end

#to_rubyString

Get an evaluable string from constant.

Returns:

  • (String)


224
225
226
# File 'lib/ffi/tools/const_generator.rb', line 224

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