Class: GLib::EnumDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/glib-mkenums.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, const_lines, g_type_prefix, options = {}) ⇒ EnumDefinition

Returns a new instance of EnumDefinition.



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/glib-mkenums.rb', line 19

def initialize(name, const_lines, g_type_prefix, options={})
  @options = options || {}
  @EnumName = name
  @g_type_prefix = g_type_prefix
  @constants = []
  @enum_name = to_snail_case(@EnumName)
  @ENUM_NAME = @enum_name.upcase
  @ENUM_SHORT = @ENUM_NAME.sub(/^#{@g_type_prefix.sub(/_TYPE.*$/, "")}/, "").sub(/^_/, "")

  parse_const_lines(const_lines)
end

Instance Attribute Details

#constantsObject (readonly)

Returns the value of attribute constants.



17
18
19
# File 'lib/glib-mkenums.rb', line 17

def constants
  @constants
end

#enum_nameObject

Returns the value of attribute enum_name.



13
14
15
# File 'lib/glib-mkenums.rb', line 13

def enum_name
  @enum_name
end

#ENUM_NAMEObject

Returns the value of attribute ENUM_NAME.



13
14
15
# File 'lib/glib-mkenums.rb', line 13

def ENUM_NAME
  @ENUM_NAME
end

#ENUM_SHORTObject

Returns the value of attribute ENUM_SHORT.



13
14
15
# File 'lib/glib-mkenums.rb', line 13

def ENUM_SHORT
  @ENUM_SHORT
end

#EnumNameObject

Returns the value of attribute EnumName.



13
14
15
# File 'lib/glib-mkenums.rb', line 13

def EnumName
  @EnumName
end

#g_type_prefixObject

Returns the value of attribute g_type_prefix.



15
16
17
# File 'lib/glib-mkenums.rb', line 15

def g_type_prefix
  @g_type_prefix
end

#prefixObject

Returns the value of attribute prefix.



15
16
17
# File 'lib/glib-mkenums.rb', line 15

def prefix
  @prefix
end

#typeObject

Returns the value of attribute type.



14
15
16
# File 'lib/glib-mkenums.rb', line 14

def type
  @type
end

#TypeObject

Returns the value of attribute Type.



14
15
16
# File 'lib/glib-mkenums.rb', line 14

def Type
  @Type
end

Instance Method Details

#create_cObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/glib-mkenums.rb', line 64

def create_c
  constants = "\n" + @constants.collect{|name, nick|
    %Q[      { #{name}, "#{name}", "#{nick}" },\n] 
  }.join +
    %Q[      { 0, NULL, NULL }]

  ret = <<-CREATE_C

GType
#{@enum_name}_get_type (void)
{
  static GType etype = 0;
  if (etype == 0) {
static const G#{@Type}Value values[] = {#{constants}
};
etype = g_#{@type}_register_static ("#{@EnumName}", values);
  }
  return etype;
}
  CREATE_C
  ret
end

#create_hObject



87
88
89
90
91
# File 'lib/glib-mkenums.rb', line 87

def create_h
  %Q[
GType #{@enum_name}_get_type (void);
#define #{@g_type_prefix}#{@ENUM_SHORT} (#{@enum_name}_get_type())]
end

#extract_prefix(ary) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/glib-mkenums.rb', line 49

def extract_prefix(ary)
  return [] if ary == nil
  a = ary[0].split(//)
  if ary.size == 1
    @ENUM_NAME + "_"
  else
    ary[1..-1].each do |b|
      b = b.split(//)
      l = [a.length, b.length].min
      a = a[0, (0...l).find{|i| a[i] != b[i] } || l]
    end 
    a.join('')
  end
end

#parse_const_lines(const_lines) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/glib-mkenums.rb', line 31

def parse_const_lines(const_lines)
  if @options[:force_flags] or /<</ =~ const_lines
    @type = "flags"
    @Type = "Flags"
  else
    @type = "enum"
    @Type = "Enum"
  end
  constants = []
  const_lines.scan(/^\s*([^\s,]*).*\n/) do |name|
    constants << name[0] unless name[0] =~ /(^[\/\*]|^#|^$)/
  end
  @prefix = extract_prefix(constants)
  constants.each do |name|
    @constants << [name, name.sub(/#{@prefix}/, "").gsub(/_/, "-").downcase]
  end
end