Class: FFI::DRY::DSLStructLayoutBuilder
- Inherits:
-
Object
- Object
- FFI::DRY::DSLStructLayoutBuilder
- 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
-
#__layout_args ⇒ Object
readonly
Returns the value of attribute __layout_args.
-
#__metadata ⇒ Object
readonly
Returns the value of attribute __metadata.
Instance Method Summary collapse
-
#field(name, type, o = {}) ⇒ Object
(also: #array, #struct, #union)
Declaratively adds a field to the structure.
-
#initialize(pbind) {|_self| ... } ⇒ DSLStructLayoutBuilder
constructor
Initializes the a new builder class.
-
#method_missing(name, type, *extra) ⇒ Object
Experimental - Allows specifying structure fields by taking a missing method name as field name for the structure.
-
#p_struct(name, klass, o = {}) ⇒ Object
A pointer to a structure.
Constructor Details
#initialize(pbind) {|_self| ... } ⇒ DSLStructLayoutBuilder
Initializes the a new builder class.
241 242 243 244 245 246 |
# File 'lib/ffi/dry.rb', line 241 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.
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/ffi/dry.rb', line 286 def method_missing(name, type, *extra) o={} if extra.size > 1 raise(ArgumentError, "Bad field syntax. Use: 'name :type, {optional extra parameters}'") elsif h=extra.first if h.kind_of? Hash o=h else raise(TypeError, "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_args ⇒ Object (readonly)
Returns the value of attribute __layout_args.
238 239 240 |
# File 'lib/ffi/dry.rb', line 238 def __layout_args @__layout_args end |
#__metadata ⇒ Object (readonly)
Returns the value of attribute __metadata.
238 239 240 |
# File 'lib/ffi/dry.rb', line 238 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.
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/ffi/dry.rb', line 268 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.
252 253 254 255 256 257 258 259 |
# File 'lib/ffi/dry.rb', line 252 def p_struct(name, klass, o={}) unless klass.kind_of?(Class) raise(TypeError, "klass must be a Class") end opts = o.merge(:p_struct => klass) offset = opts[:offset] field(name, :pointer, opts) end |