class DBus::Type

Represents the D-Bus types.

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

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

Constants

Array

Syntactic helper for constructing an array Type. You may be looking for {Data::Array} instead. @example

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

Syntactic helper for constructing a hash Type. You may be looking for {Data::Array} and {Data::DictEntry} instead. @example

t = Type::Hash[Type::STRING, Type::VARIANT]
Struct

Syntactic helper for constructing a struct Type. You may be looking for {Data::Struct} instead. @example

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

Mapping from type number to name and alignment.

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.

Attributes

members[R]

@return [Array<Type>] contained member types.

sigtype[R]

@return [String] the signature type character, eg “s” or “e”.

Public Class Methods

new(sigtype, abstract: false) click to toggle source

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

@param abstract [Boolean] allow abstract types “r” and “e”

(Enabled for internal usage by {Parser}.)
# 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

Public Instance Methods

<<(item) click to toggle source

Add a new member type item. @param item [Type]

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

A Type is equal to

  • another Type with the same string representation

  • a String ({SingleCompleteType}) describing the type

# File lib/dbus/type.rb, line 113
def ==(other)
  case other
  when ::String
    to_s == other
  else
    eql?(other)
  end
end
alignment() click to toggle source

Return the required alignment for the type.

# File lib/dbus/type.rb, line 134
def alignment
  TYPE_MAPPING[@sigtype].last
end
child() click to toggle source

Return the first contained member type.

# File lib/dbus/type.rb, line 177
def child
  @members[0]
end
eql?(other) click to toggle source

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

# File lib/dbus/type.rb, line 127
def eql?(other)
  return false unless other.is_a?(Type)

  @sigtype == other.sigtype && @members == other.members
end
inspect() click to toggle source
# 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_s() click to toggle source

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

# 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