Class: FFI::Generators::Structures
- Inherits:
-
Object
- Object
- FFI::Generators::Structures
- Defined in:
- lib/ffi2/generators/structures.rb
Overview
Generates an FFI Struct layout.
Given the @@@ portion in:
module Zlib::ZStream < FFI::Struct
@@@
name "struct z_stream_s"
include "zlib.h"
field :next_in, :pointer
field :avail_in, :uint
field :total_in, :ulong
# ...
@@@
end
Structures will create the layout:
layout :next_in, :pointer, 0,
:avail_in, :uint, 4,
:total_in, :ulong, 8,
# ...
StructGenerator does its best to pad the layout it produces to preserve line numbers. Place the struct definition as close to the top of the file for best results.
Defined Under Namespace
Classes: Field
Instance Attribute Summary collapse
-
#fields ⇒ Object
readonly
Returns the value of attribute fields.
-
#size ⇒ Object
Returns the value of attribute size.
Instance Method Summary collapse
- #calculate ⇒ Object
- #field(name, type = nil) ⇒ Object
- #found? ⇒ Boolean
- #generate_layout ⇒ Object
- #get_field(name) ⇒ Object
- #include(i) ⇒ Object
- #include_dir(i) ⇒ Object
-
#initialize(name) ⇒ Structures
constructor
A new instance of Structures.
- #name(n) ⇒ Object
- #prepare(name, target) ⇒ Object
- #prepare_failed ⇒ Object
- #process(target) ⇒ Object
- #process_failed ⇒ Object
- #source(io) ⇒ Object
- #write_config(io) ⇒ Object
Constructor Details
#initialize(name) ⇒ Structures
Returns a new instance of Structures.
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ffi2/generators/structures.rb', line 68 def initialize(name) @name = name @struct_name = nil @includes = [] @include_dirs = [] @fields = [] @found = false @size = nil @platform = Platform.new if block_given? then yield self calculate end end |
Instance Attribute Details
#fields ⇒ Object (readonly)
Returns the value of attribute fields.
66 67 68 |
# File 'lib/ffi2/generators/structures.rb', line 66 def fields @fields end |
#size ⇒ Object
Returns the value of attribute size.
65 66 67 |
# File 'lib/ffi2/generators/structures.rb', line 65 def size @size end |
Instance Method Details
#calculate ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/ffi2/generators/structures.rb', line 122 def calculate raise "struct name not set" if @struct_name.nil? output = BodyGuard.new(self, @struct_name, @platform).perform.split "\n" sizeof = output.shift unless @size m = /\s*sizeof\([^)]+\) (\d+)/.match sizeof @size = m[1] end line_no = 0 output.each do |line| md = line.match(/.+ (\d+) (\d+)/) @fields[line_no].offset = md[1].to_i @fields[line_no].size = md[2].to_i line_no += 1 end @found = true end |
#field(name, type = nil) ⇒ Object
145 146 147 148 149 |
# File 'lib/ffi2/generators/structures.rb', line 145 def field(name, type=nil) field = Field.new(name, type) @fields << field return field end |
#found? ⇒ Boolean
151 152 153 |
# File 'lib/ffi2/generators/structures.rb', line 151 def found? @found end |
#generate_layout ⇒ Object
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ffi2/generators/structures.rb', line 161 def generate_layout buf = "" @fields.each_with_index do |field, i| if buf.empty? buf << "layout :#{field.name}, :#{field.type}, #{field.offset}" else buf << " :#{field.name}, :#{field.type}, #{field.offset}" end if i < @fields.length - 1 buf << ",\n" end end buf end |
#get_field(name) ⇒ Object
179 180 181 |
# File 'lib/ffi2/generators/structures.rb', line 179 def get_field(name) @fields.find { |f| name == f.name } end |
#include(i) ⇒ Object
183 184 185 |
# File 'lib/ffi2/generators/structures.rb', line 183 def include(i) @includes << i end |
#include_dir(i) ⇒ Object
187 188 189 |
# File 'lib/ffi2/generators/structures.rb', line 187 def include_dir(i) @include_dirs << i end |
#name(n) ⇒ Object
191 192 193 |
# File 'lib/ffi2/generators/structures.rb', line 191 def name(n) @struct_name = n end |
#prepare(name, target) ⇒ Object
106 107 108 |
# File 'lib/ffi2/generators/structures.rb', line 106 def prepare(name, target) @platform.compile(@include_dirs, name, target) end |
#prepare_failed ⇒ Object
110 111 112 |
# File 'lib/ffi2/generators/structures.rb', line 110 def prepare_failed "Compilation error generating struct #{@name} (#{@struct_name})" end |
#process(target) ⇒ Object
114 115 116 |
# File 'lib/ffi2/generators/structures.rb', line 114 def process(target) target end |
#process_failed ⇒ Object
118 119 120 |
# File 'lib/ffi2/generators/structures.rb', line 118 def process_failed "Error generating struct #{@name} (#{@struct_name})" end |
#source(io) ⇒ Object
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/ffi2/generators/structures.rb', line 84 def source(io) io.puts "#include <stdio.h>" @includes.each do |inc| io.puts "#include <#{inc}>" end io.puts "#include <stddef.h>\n\n" io.puts "int main(int argc, char **argv)\n{" io.puts " #{@struct_name} s;" io.puts %[ printf("sizeof(#{@struct_name}) %u\\n", (unsigned int) sizeof(#{@struct_name}));] @fields.each do |field| io.puts <<-EOF printf("#{field.name} %u %u\\n", (unsigned int) offsetof(#{@struct_name}, #{field.name}), (unsigned int) sizeof(s.#{field.name})); EOF end io.puts "\n return 0;\n}" end |
#write_config(io) ⇒ Object
155 156 157 158 159 |
# File 'lib/ffi2/generators/structures.rb', line 155 def write_config(io) io.puts "rbx.platform.#{@name}.sizeof = #{@size}" @fields.each { |field| io.puts field.to_config(@name) } end |