Class: DBus::Type

Inherits:
Object
  • Object
show all
Defined in:
lib/dbus/type.rb

Overview

Represents the D-Bus types.

Corresponds to SingleCompleteType. Instances are immutable/frozen once fully constructed.

See also Data::Signature which is “type on the wire”.

Defined Under Namespace

Classes: ArrayFactory, Factory, HashFactory, Parser, SignatureException, StructFactory

Constant Summary collapse

TYPE_MAPPING =

Mapping from type number to name and alignment.

{
  0 => ["INVALID", nil],
  "y" => ["BYTE", 1],
  "b" => ["BOOLEAN", 4],
  "n" => ["INT16", 2],
  "q" => ["UINT16", 2],
  "i" => ["INT32", 4],
  "u" => ["UINT32", 4],
  "x" => ["INT64", 8],
  "t" => ["UINT64", 8],
  "d" => ["DOUBLE", 8],
  "r" => ["STRUCT", 8],
  "a" => ["ARRAY", 4],
  "v" => ["VARIANT", 1],
  "o" => ["OBJECT_PATH", 4],
  "s" => ["STRING", 4],
  "g" => ["SIGNATURE", 1],
  "e" => ["DICT_ENTRY", 8],
  "h" => ["UNIX_FD", 4]
}.freeze
Type =

Formerly this was a Module and there was a DBus::Type::Type class but the class got too prominent to keep its double double name. This is for backward compatibility.

self
Array =

Examples:

t = Type::Array[Type::INT16]
ArrayFactory
Hash =

Examples:

t = Type::Hash[Type::INT16]
HashFactory
Struct =

Examples:

t = Type::Struct[Type::INT16, Type::STRING]
StructFactory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sigtype, abstract: false) ⇒ Type

Use DBus.type instead, because this allows constructing incomplete or invalid types, for backward compatibility.

Parameters:

  • abstract (Boolean) (defaults to: false)

    allow abstract types “r” and “e” (Enabled for internal usage by Parser.)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/dbus/type.rb', line 84

def initialize(sigtype, abstract: false)
  if !TYPE_MAPPING.keys.member?(sigtype)
    case sigtype
    when ")"
      raise SignatureException, "STRUCT unexpectedly closed: )"
    when "}"
      raise SignatureException, "DICT_ENTRY unexpectedly closed: }"
    else
      raise SignatureException, "Unknown type code #{sigtype.inspect}"
    end
  end

  unless abstract
    case sigtype
    when STRUCT
      raise SignatureException, "Abstract STRUCT, use \"(...)\" instead of \"#{STRUCT}\""
    when DICT_ENTRY
      raise SignatureException, "Abstract DICT_ENTRY, use \"{..}\" instead of \"#{DICT_ENTRY}\""
    end
  end

  @sigtype = sigtype.freeze
  @members = [] # not frozen yet, Parser#parse_one or Factory will do it
  freeze
end

Instance Attribute Details

#membersArray<Type> (readonly)

Returns contained member types.

Returns:



77
78
79
# File 'lib/dbus/type.rb', line 77

def members
  @members
end

#sigtypeString (readonly)

Returns the signature type character, eg “s” or “e”.

Returns:

  • (String)

    the signature type character, eg “s” or “e”.



75
76
77
# File 'lib/dbus/type.rb', line 75

def sigtype
  @sigtype
end

Instance Method Details

#<<(item) ⇒ Object

Add a new member type item.

Parameters:

Raises:

  • (ArgumentError)


155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/dbus/type.rb', line 155

def <<(item)
  raise ArgumentError unless item.is_a?(Type)

  if ![STRUCT, ARRAY, DICT_ENTRY].member?(@sigtype)
    raise SignatureException
  end
  raise SignatureException if @sigtype == ARRAY && !@members.empty?

  if @sigtype == DICT_ENTRY
    case @members.size
    when 2
      raise SignatureException, "DICT_ENTRY must have 2 subtypes, found 3 or more in #{@signature}"
    when 0
      if [STRUCT, ARRAY, DICT_ENTRY, VARIANT].member?(item.sigtype)
        raise SignatureException, "DICT_ENTRY key must be basic (non-container)"
      end
    end
  end
  @members << item
end

#==(other) ⇒ Object

A Type is equal to

  • another Type with the same string representation

  • a String (SingleCompleteType) describing the type



113
114
115
116
117
118
119
120
# File 'lib/dbus/type.rb', line 113

def ==(other)
  case other
  when ::String
    to_s == other
  else
    eql?(other)
  end
end

#alignmentObject

Return the required alignment for the type.



134
135
136
# File 'lib/dbus/type.rb', line 134

def alignment
  TYPE_MAPPING[@sigtype].last
end

#childObject

Return the first contained member type.



177
178
179
# File 'lib/dbus/type.rb', line 177

def child
  @members[0]
end

#eql?(other) ⇒ Boolean

A Type is eql? to

  • another Type with the same string representation

Hash key equality See ruby-doc.org/core-3.0.0/Object.html#method-i-eql-3F

Returns:

  • (Boolean)


127
128
129
130
131
# File 'lib/dbus/type.rb', line 127

def eql?(other)
  return false unless other.is_a?(Type)

  @sigtype == other.sigtype && @members == other.members
end

#inspectObject



181
182
183
184
185
186
187
# File 'lib/dbus/type.rb', line 181

def inspect
  s = TYPE_MAPPING[@sigtype].first
  if [STRUCT, ARRAY, DICT_ENTRY].member?(@sigtype)
    s += ": #{@members.inspect}"
  end
  s
end

#to_sObject

Return a string representation of the type according to the D-Bus specification.



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/dbus/type.rb', line 140

def to_s
  case @sigtype
  when STRUCT
    "(#{@members.collect(&:to_s).join})"
  when ARRAY
    "a#{child}"
  when DICT_ENTRY
    "{#{@members.collect(&:to_s).join}}"
  else
    @sigtype.chr
  end
end