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.



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

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



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

def cast
  @cast
end

#formatObject (readonly)

Returns the value of attribute format



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

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name



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

def name
  @name
end

#valueObject

Returns the value of attribute value



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

def value
  @value
end

Instance Method Details

#converted_valueObject

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

Returns:

  • constant value.



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

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

#ruby_nameString

get constant ruby name

Returns:

  • (String)


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

def ruby_name
  @ruby_name || @name
end

#to_rubyString

Get an evaluable string from constant.

Returns:

  • (String)


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

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