Class: GLib::EnumDefinition

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

Instance Attribute Summary collapse

Class Method 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 = @EnumName.sub(/^[A-Z]/){|v| v.downcase}.gsub(/[A-Z]+/){|v| "_" + v.downcase}.sub(/(^_|_$)/, "")
  @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

Class Method Details

.parse(data, g_type_prefix, options = {}) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/glib-mkenums.rb', line 96

def self.parse(data, g_type_prefix, options={})
  options ||= {}
  enums = []
  data.scan(/^\s*typedef\s+enum\s*
            \{?\s*(.*?)
            \}\s*(\w+);/mx){|constants, name|
    enum_options = {}
    force_flags_patterns = [(options[:force_flags] || [])].flatten
    if force_flags_patterns.any? {|pattern| pattern === name}
      enum_options[:force_flags] = true
    end
    enum = new(name, constants, g_type_prefix, enum_options)
    enums << enum
  }
  enums
end

Instance Method Details

#create_cObject



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

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



89
90
91
92
93
# File 'lib/glib-mkenums.rb', line 89

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



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

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
48
49
# File 'lib/glib-mkenums.rb', line 31

def parse_const_lines(const_lines)
  ret = ""

  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