StructType

Draft
This page is not complete.

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

Property Type Description
fields CType[] A sealed array of field descriptors. Read only.

Properties inherited from CType

These properties are available on all CType objects.

Property Type Description
name String

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.

ptr CType Returns a CType representing the data type "pointer to this type". This is the result of calling ctypes.PointerType(the_type). Read only.
size Number

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

Property Type Description
constructor CType

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.
value object The 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

Document Tags and Contributors

 Last updated by: rolfedh,