File: i_constant_info.rb

package info (click to toggle)
ruby-gir-ffi 0.16.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 924 kB
  • sloc: ruby: 6,849; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,097 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
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
40
41
42
43
44
45
46
47
48
49
50
51
# frozen_string_literal: true

module GObjectIntrospection
  # Wraps a GIConstantInfo struct; represents a constant.
  class IConstantInfo < IBaseInfo
    TYPE_TAG_TO_UNION_MEMBER = {
      gboolean: :v_boolean,
      gint8: :v_int8,
      gint16: :v_int16,
      gint32: :v_int32,
      gint64: :v_int64,
      guint8: :v_uint8,
      guint16: :v_uint16,
      guint32: :v_uint32,
      guint64: :v_uint64,
      gdouble: :v_double,
      utf8: :v_string
    }.freeze

    def value
      case type_tag
      when :utf8
        raw_value.force_encoding("utf-8")
      when :gboolean
        raw_value.nonzero? ? true : false
      else
        raw_value
      end
    end

    def constant_type
      @constant_type ||= ITypeInfo.wrap Lib.g_constant_info_get_type self
    end

    private

    def type_tag
      @type_tag ||= constant_type.tag
    end

    def raw_value
      value_union = Lib::GIArgument.new
      Lib.g_constant_info_get_value self, value_union
      value_union[union_member_key]
    end

    def union_member_key
      TYPE_TAG_TO_UNION_MEMBER[type_tag]
    end
  end
end