class GLib::EnumDefinition - glib2-3.1.8 Documentation

# class GLib::EnumDefinition

### Attributes

ENUM_NAME[RW]
ENUM_SHORT[RW]
EnumName[RW]
Type[RW]
constants[R]
enum_name[RW]
g_type_prefix[RW]
prefix[RW]
type[RW]

### Public Class Methods

new(name, const_lines, g_type_prefix, options={}) click to toggle source

    # 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

### Private Class Methods

parse(data, g_type_prefix, options={}) click to toggle source

    # File lib/glib-mkenums.rb, line 109
    def self.parse(data, g_type_prefix, options={})
      options ||= {}
      enums = []
      data.force_encoding("utf-8") if data.respond_to?(:force_encoding)
      data.scan(/^\s*typedef\s+enum\s*(\/\*<\s*flags\s*>\*\/)?\s*
                \{?\s*(.*?)
                \}\s*(\w+);/mx) do |force_flags, constants, name|
        enum_options = {}
        enum_options[:force_flags] = !force_flags.nil?
        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
      end
      enums
    end

### Public Instance Methods

create_c() click to toggle source

    # 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_h() click to toggle source

    # 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) click to toggle source

    # 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) click to toggle source

    # 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

### Private Instance Methods

to_snail_case(name) click to toggle source

    # File lib/glib-mkenums.rb, line 94
    def to_snail_case(name)
      prefix_processed_name = name.sub(/^[A-Z]/) do |prefix|
        prefix.downcase
      end
      snail_cased_name = prefix_processed_name.gsub(/[A-Z]+/) do |upper_case|
        down_case = upper_case.downcase
        if down_case.size == 1
          "_#{down_case}"
        else
          "_#{down_case[0..-2]}_#{down_case[-1..-1]}"
        end
      end
      snail_cased_name.sub(/(^_|_$)/, "")
    end