next up previous
Next: 3.2.3 Recursive type specifications Up: 3.2.2 Meta Operator TypeSpecs Previous: 3.2.2.2 Example 2: prototyped

3.2.2.3 Example 3: pointer type specifications

 

As a third example, let us consider how the concept of pointers can be realized by type specifications. For this, we use a meta operator (typically, we would use the operator MP_CopProtoPointer provided in the prototype dictionary4) which has 0 in its number-of-argument field and a prototype annotation attached to the meta operator specifying the type of the data ``pointed to''. The corresponding IMP_Uint32 which is transmitted at data communication and which precedes the data ``pointed to'', may then have the value zero (pointer is NULL) or one (pointer is not NULL).

Consider an array of two struct1 structures as defined below:

struct struct2 {
  MP_String   a;
  MP_Uint32   b;
}

struct struct1 {
  MP_Sint32         x;
  MP_Real32         y;
  struct struct2 *  struct2ptr;  // a ptr to struct2
}

and the following data:
{ {456, 90.12, NULL }, - First structure
{71 , 2.1, &{"Blue", 2}} - Second structure
}
where the structure that is preceeded by the & sign indicates a ``pointer to'' that structure.


  
Figure 7: Array of structures containing pointers
\begin{figure}% latex2html id marker 529
\setlength{\tabcolsep}{1.0mm} \centerin...
...ar93 2 \\
IMP\_Uint32 && 2 && (12)\\ \hline\end{tabular}\end{small}\end{figure}

Figure 7 shows what this would look like in MP. The first prototype on line 1 indicates that each element of the array is a 3-field structure. Line 3 indicates that the third field is a pointer to an object. Lines 4 - 7 give the prototype describing the object pointed to on line 3. The structuring prototype on line 5 says that the object pointed to on line 3 is a 2-field structure. The prototyped data tree follows the prototype tree beginning on line 8. The third field of the first element of the array appears on line 9. This is the field corresponding to the structure pointer from line 3. The value here is 0, indicating a NULL pointer, so the receiver skips the nested prototype (lines 4 - 7) describing the structure pointed to. However, the value for the pointer field for the second array element on line 10 is 1 (non-NULL), so the receiver uses the prototype from lines 4 - 7 to read the object pointed to: Line 6 tells the receiver to read a String (found on line 11), and line 7 says to read a Uint32 (found on line 12). There were only two elements to the array, so the end of the MP Tree has been reached.


next up previous
Next: 3.2.3 Recursive type specifications Up: 3.2.2 Meta Operator TypeSpecs Previous: 3.2.2.2 Example 2: prototyped
| ZCA Home | Reports |