next up previous contents
Next: discriminated unions Up: Type declarations in the Previous: pointer array

typedef

Typedefs are very useful in powerRPC. A generated XDR function for type T is always of the following prototype,

                int xdr_T(XDR*, T* pT);
i.e., it takes only two arguments, the first is a pointer to an XDR object, and the second is the pointer to the object(of type T) to be Marshalled. To encapsulate the size information of an array in an XDR function, we need to use typedef.

For example, we can define an array of 512 chars,

 typedef char c_arr512[512];

To define a C string (char*) with a maximum length of 1023,

 typedef char [size = strlen(*this)+1, 1024] str1024;
here we used the powerRPC keyword this, which can be used in a type declaration (typedef, struct or union) to signify the pointer pT passed to the XDR function of the typedef.

The corresponding C declaration for str1024 is simply char *, however, the elaborate size specification above actually defines str1024 as a C string. In powerRPC, instead of being provided a fixed number of predefined types such as string8 (meaning C string), you can use its expressive IDL to defined your own types. Suppose you need to declare an array of long integers ending with a zero, you can do it yourself as follows,

        typedef long [size =strlen32(*this)+1, 1024] string32;
provided that you define the strlen32() function somewhere like this:
        int strlen32(long*la) {
             int i;
             for(i=0; la[i]; i++);
             return i;
         }



Copyright (C) Netbula LLC, 1996