Class: FFIGen::Enum

Inherits:
Type
  • 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, constants, description) ⇒ Enum

Returns a new instance of Enum.



81
82
83
84
85
86
# File 'lib/ffi_gen.rb', line 81

def initialize(generator, name, constants, description)
  @generator = generator
  @name = name
  @constants = constants
  @description = description
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



79
80
81
# File 'lib/ffi_gen.rb', line 79

def name
  @name
end

Instance Method Details

#java_descriptionObject



102
103
104
# File 'lib/ffi_gen/java_output.rb', line 102

def java_description
  "Symbol from _enum_#{java_name}_"
end

#java_jna_typeObject



98
99
100
# File 'lib/ffi_gen/java_output.rb', line 98

def java_jna_type
  java_name
end

#java_nameObject



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

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

#ruby_descriptionObject



77
78
79
# File 'lib/ffi_gen/ruby_output.rb', line 77

def ruby_description
  "Symbol from _enum_#{ruby_name}_"
end

#ruby_ffi_typeObject



73
74
75
# File 'lib/ffi_gen/ruby_output.rb', line 73

def ruby_ffi_type
  ":#{ruby_name}"
end

#ruby_nameObject



69
70
71
# File 'lib/ffi_gen/ruby_output.rb', line 69

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

#shorten_namesObject



88
89
90
91
92
93
# File 'lib/ffi_gen.rb', line 88

def shorten_names
  return if @constants.size < 2
  names = @constants.map { |constant| constant[:name].parts }
  names.each(&:shift) while names.map(&:first).uniq.size == 1 and @name.parts.map(&:downcase).include? names.first.first.downcase
  names.each(&:pop) while names.map(&:last).uniq.size == 1 and @name.parts.map(&:downcase).include? names.first.last.downcase
end

#write_java(writer) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/ffi_gen/java_output.rb', line 67

def write_java(writer)
  return if @name.nil?
  shorten_names
  
  writer.comment do
    writer.write_description @description
    writer.puts "", "<em>This entry is only for documentation and no real method. The FFI::Enum can be accessed via #enum_type(:#{java_name}).</em>"
    writer.puts "", "=== Options:"
    @constants.each do |constant|
      writer.puts "#{constant[:name].to_java_constant} ::"
      writer.write_description constant[:comment], false, "  ", "  "
    end
    writer.puts "", "@method _enum_#{java_name}_", "@return [Symbol]", "@scope class"
  end
  
  writer.puts "public enum #{java_name} implements NativeEnum {"
  writer.indent do
    writer.write_array @constants, "," do |constant|
      "#{constant[:name].to_java_constant}(#{constant[:value]})"
    end
    writer.puts ";"
    
    writer.puts "", "private int nativeInt;", "", "private #{java_name}(int nativeInt) {", "    this.nativeInt = nativeInt;", "}", "", "@Override", "public int toNativeInt() {", "    return nativeInt;", "}"
  end
  writer.puts "}", ""
end

#write_ruby(writer) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ffi_gen/ruby_output.rb', line 41

def write_ruby(writer)
  return if @name.nil?
  shorten_names

  @constants.each do |constant|
    constant[:symbol] = ":#{constant[:name].to_ruby_downcase}"
  end

  writer.comment do
    writer.write_description @description
    writer.puts "", "<em>This entry is only for documentation and no real method. The FFI::Enum can be accessed via #enum_type(:#{ruby_name}).</em>"
    writer.puts "", "=== Options:"
    @constants.each do |constant|
      writer.puts "#{constant[:symbol]} ::"
      writer.write_description constant[:comment], false, "  ", "  "
    end
    writer.puts "", "@method _enum_#{ruby_name}_", "@return [Symbol]", "@scope class"
  end

  writer.puts "enum :#{ruby_name}, ["
  writer.indent do
    writer.write_array @constants, "," do |constant|
      "#{constant[:symbol]}, #{constant[:value]}"
    end
  end
  writer.puts "]", ""
end