# 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
     84 def initialize(sigtype, abstract: false)
     85   if !TYPE_MAPPING.keys.member?(sigtype)
     86     case sigtype
     87     when ")"
     88       raise SignatureException, "STRUCT unexpectedly closed: )"
     89     when "}"
     90       raise SignatureException, "DICT_ENTRY unexpectedly closed: }"
     91     else
     92       raise SignatureException, "Unknown type code #{sigtype.inspect}"
     93     end
     94   end
     95
     96   unless abstract
     97     case sigtype
     98     when STRUCT
     99       raise SignatureException, "Abstract STRUCT, use \"(...)\" instead of \"#{STRUCT}\""
    100     when DICT_ENTRY
    101       raise SignatureException, "Abstract DICT_ENTRY, use \"{..}\" instead of \"#{DICT_ENTRY}\""
    102     end
    103   end
    104
    105   @sigtype = sigtype.freeze
    106   @members = [] # not frozen yet, Parser#parse_one or Factory will do it
    107   freeze
    108 end

### Public Instance Methods

<<(item) click to toggle source

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

        # File lib/dbus/type.rb
    155 def <<(item)
    156   raise ArgumentError unless item.is_a?(Type)
    157
    158   if ![STRUCT, ARRAY, DICT_ENTRY].member?(@sigtype)
    159     raise SignatureException
    160   end
    161   raise SignatureException if @sigtype == ARRAY && !@members.empty?
    162
    163   if @sigtype == DICT_ENTRY
    164     case @members.size
    165     when 2
    166       raise SignatureException,
    167             "DICT_ENTRY must have 2 subtypes, found 3 or more: #{@members.inspect} << #{item.inspect}"
    168     when 0
    169       if [STRUCT, ARRAY, DICT_ENTRY, VARIANT].member?(item.sigtype)
    170         raise SignatureException, "DICT_ENTRY key must be basic (non-container)"
    171       end
    172     end
    173   end
    174   @members << item
    175 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
    113 def ==(other)
    114   case other
    115   when ::String
    116     to_s == other
    117   else
    118     eql?(other)
    119   end
    120 end

alignment() click to toggle source

Return the required alignment for the type.

        # File lib/dbus/type.rb
    134 def alignment
    135   TYPE_MAPPING[@sigtype].last
    136 end

child() click to toggle source

Return the first contained member type.

        # File lib/dbus/type.rb
    178 def child
    179   @members[0]
    180 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
    127 def eql?(other)
    128   return false unless other.is_a?(Type)
    129
    130   @sigtype == other.sigtype && @members == other.members
    131 end

inspect() click to toggle source

        # File lib/dbus/type.rb
    182 def inspect
    183   s = TYPE_MAPPING[@sigtype].first
    184   if [STRUCT, ARRAY, DICT_ENTRY].member?(@sigtype)
    185     s += ": #{@members.inspect}"
    186   end
    187   s
    188 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
    140 def to_s
    141   case @sigtype
    142   when STRUCT
    143     "(#{@members.collect(&:to_s).join})"
    144   when ARRAY
    145     "a#{child}"
    146   when DICT_ENTRY
    147     "{#{@members.collect(&:to_s).join}}"
    148   else
    149     @sigtype.chr
    150   end
    151 end