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. @examplet = Type::Array[Type::INT16]
- Hash
Syntactic helper for constructing a hash
Type
. You may be looking for {Data::Array} and {Data::DictEntry} instead. @examplet = Type::Hash[Type::STRING, Type::VARIANT]
- Struct
Syntactic helper for constructing a struct
Type
. You may be looking for {Data::Struct} instead. @examplet = 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 aDBus::Type::Type
class but the class got too prominent to keep its double double name. This is for backward compatibility.
Attributes
@return [Array<Type>] contained member types.
@return [String] the signature type character, eg “s” or “e”.
Public Class Methods
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
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
Return the required alignment for the type.
# File lib/dbus/type.rb, line 134 def alignment TYPE_MAPPING[@sigtype].last end
Return the first contained member type.
# File lib/dbus/type.rb, line 177 def child @members[0] end
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
# 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
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