Class: RubyBindgen::Namer

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-bindgen/namer.rb

Instance Method Summary collapse

Constructor Details

#initialize(rename_types = NameMapper.new, rename_methods = NameMapper.new, conversion_mappings = NameMapper.new) ⇒ Namer

Returns a new instance of Namer.



3
4
5
6
7
8
# File 'lib/ruby-bindgen/namer.rb', line 3

def initialize(rename_types = NameMapper.new, rename_methods = NameMapper.new,
               conversion_mappings = NameMapper.new)
  @rename_types = rename_types
  @rename_methods = rename_methods
  @conversion_mappings = conversion_mappings
end

Instance Method Details

#apply_rename_types(*names) ⇒ Object

Apply rename_types to a generated Ruby class name. Accepts one or more candidate names to try (e.g., raw C++ name, camelized name). Returns the first match, or the last candidate as fallback.



68
69
70
# File 'lib/ruby-bindgen/namer.rb', line 68

def apply_rename_types(*names)
  @rename_types.lookup(*names) || names.last
end

#cruby(cursor) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ruby-bindgen/namer.rb', line 41

def cruby(cursor)
  case cursor.kind
    when :cursor_class_decl
      "rb_c#{cursor.type.spelling.sub("(anonymous namespace)", "Anonymous").camelize}"
    when :cursor_struct
      "rb_c#{cursor.type.spelling.camelize}"
    when :cursor_enum_decl
      "rb_c#{cursor.type.spelling.sub("(anonymous namespace)", "Anonymous").camelize}"
    when :cursor_namespace
      if cursor.anonymous?
        # qualified_name is nil for translation units
        "rb_m#{cursor.semantic_parent.qualified_name&.camelize}Anonymous"
      else
        "rb_m#{cursor.qualified_name.camelize}"
      end
    when :cursor_typedef_decl
      "rb_c#{cursor.spelling.sub("(anonymous namespace)", "Anonymous").camelize}"
    when :cursor_translation_unit
      "Class(rb_cObject)"
    else
      cursor.spelling.underscore
  end
end

#ruby(cursor) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ruby-bindgen/namer.rb', line 10

def ruby(cursor)
  if cursor.anonymous? && cursor.kind == :cursor_namespace
    "Anonymous"
  elsif cursor.anonymous?
    definer = cursor.anonymous_definer
    definer ? definer.spelling.camelize : nil
  else
    case cursor.kind
      when :cursor_translation_unit
        basename = File.basename(cursor.spelling, File.extname(cursor.spelling))
        basename.camelize
      when :cursor_conversion_function
        ruby_conversion_function(cursor)
      when :cursor_function, :cursor_cxx_method
        ruby_operator_or_method(cursor)
      when :cursor_struct, :cursor_union, :cursor_enum_decl, :cursor_class_decl, :cursor_namespace
        @rename_types.lookup(cursor.spelling) || cursor.spelling.camelize
      when :cursor_field_decl
        cursor.spelling.underscore
      when :cursor_typedef_decl
        cursor.underlying_type.declaration.invalid? ?
          cursor.spelling.underscore :
          ruby(cursor.underlying_type.declaration)
      when :cursor_variable
        cursor.spelling.camelize
      else
        cursor.spelling.underscore
    end
  end
end