From: chris@mimsy.UUCP
Subject: Re: How to get Ada private types in C
Date: Thu, 12-Mar-87 03:22:06 EST [thread overview]
Date: Thu Mar 12 03:22:06 1987
Message-ID: <5767@mimsy.UUCP> (raw)
In-Reply-To: 513@ci-dandelion.UUCP
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@mimsy.umd.edu
next prev parent reply other threads:[~1987-03-12 8:22 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1987-03-09 17:23 How to get Ada private types in C MHx7002
1987-03-11 18:28 ` congdon
1987-03-12 8:22 ` chris [this message]
1987-03-18 1:25 ` Bob Dietrich
-- strict thread matches above, loose matches on Subject: below --
1987-03-11 7:50 bzs
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox