Class: FFIGen::StructOrUnion

Inherits:
Object
  • Object
show all
Defined in:
lib/ffi_gen.rb,
lib/ffi_gen/java_output.rb,
lib/ffi_gen/ruby_output.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(generator, name, is_union) ⇒ StructOrUnion

Returns a new instance of StructOrUnion.



98
99
100
101
102
103
104
105
106
# File 'lib/ffi_gen.rb', line 98

def initialize(generator, name, is_union)
  @generator = generator
  @name = name
  @is_union = is_union
  @comment = ""
  @fields = []
  @oo_functions = []
  @written = false
end

Instance Attribute Details

#commentObject

Returns the value of attribute comment.



95
96
97
# File 'lib/ffi_gen.rb', line 95

def comment
  @comment
end

#fieldsObject (readonly)

Returns the value of attribute fields.



96
97
98
# File 'lib/ffi_gen.rb', line 96

def fields
  @fields
end

#nameObject

Returns the value of attribute name.



95
96
97
# File 'lib/ffi_gen.rb', line 95

def name
  @name
end

#oo_functionsObject (readonly)

Returns the value of attribute oo_functions.



96
97
98
# File 'lib/ffi_gen.rb', line 96

def oo_functions
  @oo_functions
end

#writtenObject (readonly)

Returns the value of attribute written.



96
97
98
# File 'lib/ffi_gen.rb', line 96

def written
  @written
end

Instance Method Details

#java_nameObject



180
181
182
# File 'lib/ffi_gen/java_output.rb', line 180

def java_name
  @java_name ||= @name.to_java_classname
end

#ruby_nameObject



190
191
192
# File 'lib/ffi_gen/ruby_output.rb', line 190

def ruby_name
  @ruby_name ||= @name.to_ruby_classname
end

#write_java(writer) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/ffi_gen/java_output.rb', line 151

def write_java(writer)
  @fields.each do |field|
    field[:symbol] = field[:name].to_java_downcase
    field[:type_data] = @generator.to_java_type field[:type]
  end
  
  writer.comment do
    writer.write_description @comment
    unless @fields.empty?
      writer.puts "", "= Fields:"
      @fields.each do |field|
        writer.puts "#{field[:symbol]} ::"
        writer.write_description field[:comment], false, "  (#{field[:type_data][:description]}) ", "  "
      end
    end
  end
  
  writer.puts "public static class #{java_name} extends #{@is_union ? 'Union' : (@fields.empty? ? 'PointerType' : 'Structure')} {"
  writer.indent do
    @fields.each do |field|
      writer.puts "public #{field[:type_data][:jna_type]} #{field[:symbol]};"
    end
    writer.puts "// hidden structure" if @fields.empty?
  end
  writer.puts "}", ""
  
  @written = true
end

#write_ruby(writer) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/ffi_gen/ruby_output.rb', line 142

def write_ruby(writer)
  @fields.each do |field|
    field[:symbol] = ":#{field[:name].to_ruby_downcase}"
    field[:type_data] = @generator.to_ruby_type field[:type]
  end
  
  writer.comment do
    writer.write_description @comment
    unless @fields.empty?
      writer.puts "", "= Fields:"
      @fields.each do |field|
        writer.puts "#{field[:symbol]} ::"
        writer.write_description field[:comment], false, "  (#{field[:type_data][:description]}) ", "  "
      end
    end
  end
  
  @fields << { symbol: ":dummy", type_data: { ffi_type: ":char" } } if @fields.empty?

  unless @oo_functions.empty?
    writer.puts "module #{ruby_name}Wrappers"
    writer.indent do
      @oo_functions.each_with_index do |(name, function, return_type_declaration), index|
        parameter_names = function.parameters[1..-1].map { |parameter| !parameter[:name].empty? ? parameter[:name].to_ruby_downcase : "arg#{function.parameters.index(parameter)}" }
        writer.puts "" unless index == 0
        writer.puts "def #{name.to_ruby_downcase}(#{parameter_names.join(', ')})"
        writer.indent do
          cast = return_type_declaration ? "#{return_type_declaration.ruby_name}.new " : ""
          writer.puts "#{cast}#{@generator.module_name}.#{function.ruby_name}(#{(["self"] + parameter_names).join(', ')})"
        end
        writer.puts "end"
      end
    end
    writer.puts "end", ""
  end
        
  writer.puts "class #{ruby_name} < #{@is_union ? 'FFI::Union' : 'FFI::Struct'}"
  writer.indent do
    writer.puts "include #{ruby_name}Wrappers" unless @oo_functions.empty?
    writer.write_array @fields, ",", "layout ", "       " do |field|
      "#{field[:symbol]}, #{field[:type_data][:ffi_type]}"
    end
  end
  writer.puts "end", ""
  
  @written = true
end