Class: FFI::DRY::DSLStructLayoutBuilder

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

Overview

This class provides the DSL for StructHelper.dsl_layout. You probably don’t want to use this directly but if you do, to use the DSL, you may either pass a structure definition into ‘instance_eval’ or call methods on the object. The methods __metadata and __layout_args return structure information back to the caller, which can use them to create a new structure.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pbind) {|_self| ... } ⇒ DSLStructLayoutBuilder

Initializes the a new builder class.

Yields:

  • (_self)

Yield Parameters:



223
224
225
226
227
228
# File 'lib/ffi/dry.rb', line 223

def initialize(pbind)
  @pbind = pbind
  @__layout_args = []
  @__metadata = []
  yield self if block_given?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, type, *extra) ⇒ Object

Experimental - Allows specifying structure fields by taking a missing method name as field name for the structure.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
# File 'lib/ffi/dry.rb', line 268

def method_missing(name, type, *extra)
  o={}
  if extra.size > 1
    raise(ArgumentError, 
      "Bad field definition. Use: name :type, {optional extra parameters}")
  elsif h=extra.first
    if h.kind_of? Hash
      o=h
    else
      raise(ArgumentError, "Options must be provided as a hash.")
    end
  end
  opts = o.merge(:name => name, :type => type)
  offset = opts[:offset]

  @__layout_args << name
  @__layout_args << type
  @__layout_args << offset if offset

  @__metadata << opts
  return opts
end

Instance Attribute Details

#__layout_argsObject (readonly)

Returns the value of attribute __layout_args.



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

def __layout_args
  @__layout_args
end

#__metadataObject (readonly)

Returns the value of attribute __metadata.



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

def 
  @__metadata
end

Instance Method Details

#field(name, type, o = {}) ⇒ Object Also known as: array, struct, union

Declaratively adds a field to the structure.

Syntax:

field field_name, ctype, { ... metadata ... }

:offset is a special key in metadata, specifies the offset of the field.



250
251
252
253
254
255
256
257
258
259
260
# File 'lib/ffi/dry.rb', line 250

def field(name, type, o={})
  opts = o.merge(:name => name, :type => type)
  offset = opts[:offset]

  @__layout_args << name
  @__layout_args << type
  @__layout_args << offset if offset

  @__metadata << opts
  return opts
end

#p_struct(name, klass, o = {}) ⇒ Object

A pointer to a structure. The structure does not allocate the entire space for the structure pointed to, just a pointer. When calling the accessor for a p_struct field, a new instance of the FFI::Struct type for the pointer will be returned.



234
235
236
237
238
239
240
241
# File 'lib/ffi/dry.rb', line 234

def p_struct(name, klass, o={})
  unless klass.kind_of?(Class)
    raise(::ArgumentError, "klass must be a Class")
  end
  opts = o.merge(:p_struct => klass)
  offset = opts[:offset]
  field(name, :pointer, opts)
end