When using structured data types, it is important to pay attention to the alignment and size of structure elements. The table below shows the size of all conventional data types that can be used within function declarations in Silk Performer scripts.
Data Type | Size |
---|---|
Char, unsigned char | 1 |
Short, unsigned short | 2 |
Long | 4 |
Double | 8 |
Float | 4 |
Char[x] | x |
The example below clarifies structure composition by showing a data structure that contains all the simple data types.
// declare a demo data structure #pragma pack(1) // alignment to 1-byte boundaries typedef struct { char myChar; // size: 1 byte pos: 1 short myShort; // size: 2 bytes pos: 2 long myLong; // size: 4 bytes pos: 4 double myDouble; // size: 8 bytes pos: 8 float myFloat; // size: 4 bytes pos: 16 unsigned short myUShort; // size: 2 bytes pos: 20 char myString[10]; // size: 10 bytes pos: 22 unsigned char myUChar; // size: 1 byte pos: 32 } TESTSTRUCT; #pragma pack() char* T_STRUCT(TESTSTRUCT * s) { char sBuf[10000]; // manipulate values of data structure and generate a // return value for demonstration purpose ... return sBuf; }
To handle structured data types, BDL uses strings of appropriate length and number of type-dependent Get and Set functions. For each structured data type passed to a function, an inout string parameter must be defined. These strings must be exactly as long as the data structures defined in the DLLs.
The first position in a BDL string that can hold a structured data type is one (1). When variables are aligned to one-byte boundaries, the starting position of the next element is derived by adding the current position to the size of the current element. For the data structure declared in the example above, a 32-byte string is required.
"T_STRUCT" function t_struct(inout string(32)): string(1000);
In the following example, arbitrary values are assigned to all the elements of the data structure using the corresponding Set functions. Next, the external function t_struct is called to manipulate the data structure.
dcltrans transaction TMain var n1, n2, n3, n4, n5 : number; n6, n7, n8, n9, n10 : number; f1, f2, f3, f4 : float; sStruct : string(100); sRet : string(1000); begin /* one-byte alignment */ SetChar (sStruct, 1, 127); SetShort (sStruct, 2, 32000); SetLong (sStruct, 4, 2000000); SetDouble(sStruct, 8, 12345.12345); SetFloat (sStruct, 16, 12.99); SetUShort(sStruct, 20, 64000); SetString(sStruct, 22, "123456789", 10); SetUChar (sStruct, 32, 255); sRet := t_struct(sStruct); end TMain;