D-Bus学习:类型系统
全文来之:http://dbus.freedesktop.org/doc/dbus-specification.html
非翻译,仅仅是本人看后的简单描述。
Type System(类型系统)
将其他任何格式的数据转换为wire format(数据传输格式,在线上传输/交换的数据格式,具体请看:http://en.wikipedia.org/wiki/Wire_format)格式数据的过程被称为marshaling(封送,http://en.wikipedia.org/wiki/Marshalling_%28computer_science%29),转换回来的过程被称为unmarshaling(解封)。
对于封送的数据,D-Bus需要一个统一的类型系统来对他们进行描述,即各个封送数据有一个对应的类型签名(type signature)。类型签名由零个或多个单一完整类型(single complete type)组成,而每一个单一完整类型由一个或多个类型代码(type code)组成。
类型代码由一个代表数值类型的ASCII字符,因此由类型代码作为基本组成的类型签名就总是一个有效的ASCII字符串,可以通过进行字符串比较判断两个类型签名是否一致。
单一完整类型由一系列类型代码组成,用于完整的描述一个数据类型:或基本类型(Basic type),或容器类型(Container type)。单一完整类型必须完整,像容器类型里的数组,必须给出数组元素类型,或结构体必须给出其字段类型。
基本类型(Basic type)
由一个字符表示的类型代码就是最简单的基本类型,基本类型由固定类型和string-like(类似字符串)类型组成。
比如类型代码i表示一个32位整型(INT32)。由一个INT32组成数据值,其类型签名就为”i”(固定类型),而由两个INT32组成数据值,其类型签名就为”ii”(string-like类型)。
下表是对固定类型的特性描述:
Conventional name | ASCII type-code | Encoding |
---|---|---|
BYTE |
y (121) |
Unsigned 8-bit integer |
BOOLEAN |
b (98) |
Boolean value: 0 is false, 1 is true, any other value allowed by the marshalling format is invalid |
INT16 |
n (110) |
Signed (two’s complement) 16-bit integer |
UINT16 |
q (113) |
Unsigned 16-bit integer |
INT32 |
i (105) |
Signed (two’s complement) 32-bit integer |
UINT32 |
u (117) |
Unsigned 32-bit integer |
INT64 |
x (120) |
Signed (two’s complement) 64-bit integer (mnemonic: x and t are the first characters in “sixty” not already used for something more common) |
UINT64 |
t (116) |
Unsigned 64-bit integer |
DOUBLE |
d (100) |
IEEE 754 double-precision floating point |
UNIX_FD |
h (104) |
Unsigned 32-bit integer representing an index into an out-of-band array of file descriptors, transferred via some platform-specific mechanism (mnemonic: h for handle) |
string-like类型是长度不定的固定类型构成。string-like类型的封送格式都是以一个零字节(NUL)作为结束,但该零字节并不被认为是数据的一部分(这应该和C语言里的字符串末尾零字节意义是一致的)。
下表是对string-like类型的特性描述:
Conventional name | ASCII type-code | Validity constraints |
---|---|---|
STRING |
s (115) |
No extra constraints |
OBJECT_PATH |
o (111) |
Must be a syntactically valid object path |
SIGNATURE |
g (103) |
Zero or more single complete types |
有效对象路径(Valid Object Path)
有效对象路径需要满足如下这些规则:
1,路径可以是任意长度。
2,路径必须以ASCII字符’/'作为开头,并且各个路径元素之间以斜线字符作为分隔。
3,每一个路径元素只能包含这些ASCII字符”[A-Z][a-z][0-9]_”。
4,路径元素不能为空。
5,’/'字符不能连续出现(这和第4点重复)。
6,不能以’/'字符作为路径结束,除非是由仅有一个字符’/'组成的根路径。
有效签名(Valid Signature)
有效签名需要满足如下这些规则:
1,有效签名由一系列单一完整类型组成,也就是数组必须包含有元素类型,结构体必须同时具有开闭圆括号。
2,有效签名里只允许出现类型代码、开闭圆括号、开闭大括号。STRUCT类型代码由圆括号替代,DICT_ENTRY由大括号替代,因此它们也就都不再允许出现在有效签名里。
3,容器类型的最大嵌套深度为32层数组类型和32层圆括号类型,也就是一共64层。
4,有效签名的最大长度为255。
封送格式里的有效签名同样是以一个零字节(NUL)作为结束,但该零字节也并不被认为是有效签名的一部分。
容器类型(Container type)
除了基本类型,还有另外四种容器类型:结构体(STRUCT),数组(ARRAY),变量(VARIANT)和字典记录(DICT_ENTRY)。
有一个标志结构体的ASCII字符r,但它并不会被有效签名使用到,换而使用的是’(‘和’)',因此,一个包含两个整型字段的结构体,其签名为:”(ii)”。
结构体可以嵌套,例如:”(i(ii))”,表示包含有一个整型字段和一个结构体字段。
空结构体是不允许的,即结构体至少得包含一个字段。
数组类型的ASCII代表字符为’a',其后必须跟随一个单一完整类型来表示数组元素类型。比如:”ai”就表示一个整型数组。数组元素可以是任何类型,比如:”a(ii)”表示一个结构体数组;”aai”表示一个数组的数组;
下表是对D-Bus中数据类型的总体概述:
Conventional Name | Code | Description |
---|---|---|
INVALID |
0 (ASCII NUL) | Not a valid type code, used to terminate signatures |
BYTE |
121 (ASCII ‘y’) | 8-bit unsigned integer |
BOOLEAN |
98 (ASCII ‘b’) | Boolean value, 0 is FALSE and 1 is TRUE . Everything else is invalid. |
INT16 |
110 (ASCII ‘n’) | 16-bit signed integer |
UINT16 |
113 (ASCII ‘q’) | 16-bit unsigned integer |
INT32 |
105 (ASCII ‘i’) | 32-bit signed integer |
UINT32 |
117 (ASCII ‘u’) | 32-bit unsigned integer |
INT64 |
120 (ASCII ‘x’) | 64-bit signed integer |
UINT64 |
116 (ASCII ‘t’) | 64-bit unsigned integer |
DOUBLE |
100 (ASCII ‘d’) | IEEE 754 double |
STRING |
115 (ASCII ‘s’) | UTF-8 string (must be valid UTF-8). Must be nul terminated and contain no other nul bytes. |
OBJECT_PATH |
111 (ASCII ‘o’) | Name of an object instance |
SIGNATURE |
103 (ASCII ‘g’) | A type signature |
ARRAY |
97 (ASCII ‘a’) | Array |
STRUCT |
114 (ASCII ‘r’), 40 (ASCII ‘(‘), 41 (ASCII ‘)’) | Struct; type code 114 ‘r’ is reserved for use in bindings and implementations to represent the general concept of a struct, and must not appear in signatures used on D-Bus. |
VARIANT |
118 (ASCII ‘v’) | Variant type (the type of the value is part of the value itself) |
DICT_ENTRY |
101 (ASCII ‘e’), 123 (ASCII ‘{‘), 125 (ASCII ‘}’) | Entry in a dict or map (array of key-value pairs). Type code 101 ‘e’ is reserved for use in bindings and implementations to represent the general concept of a dict or dict-entry, and must not appear in signatures used on D-Bus. |
UNIX_FD |
104 (ASCII ‘h’) | Unix file descriptor |
(reserved) | 109 (ASCII ‘m’) | Reserved for a ‘maybe’ type compatible with the one in GVariant, and must not appear in signatures used on D-Bus until specified here |
(reserved) | 42 (ASCII ‘*’) | Reserved for use in bindings/implementations to represent any single complete type, and must not appear in signatures used on D-Bus. |
(reserved) | 63 (ASCII ‘?’) | Reserved for use in bindings/implementations to represent any basic type, and must not appear in signatures used on D-Bus. |
(reserved) | 64 (ASCII ‘@’), 38 (ASCII ‘&’), 94 (ASCII ‘^’) |
Reserved for internal use by bindings/implementations, and must not appear in signatures used on D-Bus. GVariant uses these type-codes to encode calling conventions. |
转载请保留地址:http://lenky.info/archives/2013/06/18/2309 或 http://lenky.info/?p=2309
备注:如无特殊说明,文章内容均出自Lenky个人的真实理解而并非存心妄自揣测来故意愚人耳目。由于个人水平有限,虽力求内容正确无误,但仍然难免出错,请勿见怪,如果可以则请留言告之,并欢迎来讨论。另外值得说明的是,Lenky的部分文章以及部分内容参考借鉴了网络上各位网友的热心分享,特别是一些带有完全参考的文章,其后附带的链接内容也许更直接、更丰富,而我只是做了一下归纳&转述,在此也一并表示感谢。关于本站的所有技术文章,欢迎转载,但请遵从CC创作共享协议,而一些私人性质较强的心情随笔,建议不要转载。
法律:根据最新颁布的《信息网络传播权保护条例》,如果您认为本文章的任何内容侵犯了您的权利,请以或书面等方式告知,本站将及时删除相关内容或链接。