StructType 编辑

StructType represents C structures.

Syntax

Returns a CType object describing a new structure data type. This data type provides the ability to define and manipulate values of the C struct type.

Note: At this time, there isn't a way to denote a packed structure (that is, one created in C using #pragma pack).
CType StructType(
  name[,
  fields]
);
Parameters
name
The name of the structure. Must be a string.
fields Optional
An array of field descriptors, describing all the entries in the structure. If this isn't specified, the structure is considered opaque and you can't access its contents directly. You can call the resulting type's define() method to assign it a non-opaque type later. See Opaque structures for further explanation and an example.
Return value

A CType object describing the newly created struct data type.

Exceptions thrown
TypeError
The name is not a string, or one or more of the fields does not have a defined size.
RangeError
The size of the structure, in bytes, cannot be represented both as a size_t and as a JavaScript Number.
Describing fields

The fields array is comprised of field descriptors, one for each field in the structure. Each field descriptor contains the field's name and its type, such as {'serialNumber': ctypes.int}. A complete field descriptor list might look like this:

[ {'serialNumber': ctypes.int}, {'userName': ctypes.char.ptr} ]

Properties

PropertyTypeDescription
fieldsCType[]A sealed array of field descriptors. Read only.

Properties inherited from CType

These properties are available on all CType objects.

PropertyTypeDescription
nameString

The type's name. Read only.

For primitive types, this is just the name of the corresponding C type. For structure and opaque pointer types, this is simply the string that was passed to the constructor. For other function, pointer, and array types, this should be a valid C type expression.

ptrCTypeReturns a CType representing the data type "pointer to this type". This is the result of calling ctypes.PointerType(the_type). Read only.
sizeNumber

The size of the type, in bytes. This is the same value as the C sizeof. Read only.

Note: ctypes.void_t.size is undefined.

Method overview

define(fields)

Methods inherited from CType

CType array([n])
String toSource()
String toString()

Methods

define()

Defines a previously declared opaque type's fields. This lets you convert an opaque structure type into a defined structure type.

define(
  fields
);
Parameters
fields
A field specification, as described above.

StructType CData Syntax

CData structtype();
structtype is StructType CType.
Return value

A CData representing the newly allocated struct.

Exceptions thrown
Error
Fields are not yet defined.

StructType CData Properties

Every struct object has a getter and a setter for each member field of the structure.

Properties inherited from CData

PropertyTypeDescription
constructorCType

The data type of the CData object, as a CType. Read only.

Note: This is never ctypes.void_t or an array type with an unspecified length.
valueobjectThe JavaScript equivalent of the CData object's value. This will throw a TypeError exception if the value can't be converted.

StructType CData Method_overview

CData addressOfField(name)

Methods inherited from CData

CData address()
String toSource()
String toString()

StructType CData Methods

addressOfField()

Returns a new CData object of the appropriate pointer type, whose value points to the specified field of the structure on which the method was called. See Working with strings for more information on how to convert strings.

CData addressOfField(
  name
);
Parameters
name
The name of the field whose address is to be returned.
Return value

A new CData object of the appropriate pointer type, whose value points to the contents of the specified field.

Exceptions thrown
TypeError
name is not a JavaScript string, or doesn't name a member field of the structure.

See Also

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:118 次

字数:10362

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文