How to get Ada private types in C
chris at mimsy.UUCP
chris at mimsy.UUCP
Thu Mar 12 18:22:06 AEST 1987
While I do not know the precise details of Ada private types, I
can say that in general, private types are in fact implemented
through the use of a `generic pointer' type. That is, outside of
a specific module, the only handle anything has on an object of a
private type is a pointer to that object. In 4BSD, the generic
pointer type is `caddr_t' (Core ADDRess Type); in ANSI draft C it
is `void *' (a name that I find utterly lacking in taste, though
it does avoid a new keyword).
For example, a private data structure that implments longer integers
might look like this:
struct longer {
long low;
long high;
};
but the routines that deal with one of these take and return only
`caddr_t' types:
caddr_t
new_longer()
{
struct longer *l;
l = (struct longer *) malloc(sizeof (struct longer));
if (l != NULL)
l->low = l->high = 0;
return ((caddr_t) l);
}
void
incr(obj)
caddr_t obj;
{
struct longer *l = (struct longer *) obj;
if (++l->low == 0) /* overflow */
l->high+++;
}
This is often employed in an `object oriented' manner by providing
a structure containing pointers to the object-specific routines,
as well as an instance of the object itself:
struct representation {
caddr_t r_obj; /* the object */
void (*r_incr)(); /* a function that counts it up */
void (*r_print)(); /* a function that prints it */
};
In this case, only the routine that creates a specific instance
of an object need know the names of the functions that implement
that object. Everywhere else in the code, only the operations
provided by the `representation' are available:
add_1_and_print(r)
register struct representation *r;
{
(*r->incr)(r->obj);
(*r->print)(r->obj);
}
Of course, doing this is considerably simpler if the compiler helps
out, which is why we have languages like C++ in the first place.
Otherwise we could all write the C code that the C++ compiler
produces.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7690)
UUCP: seismo!mimsy!chris ARPA/CSNet: chris at mimsy.umd.edu
More information about the Comp.lang.c
mailing list