Contents Index Search Related Documents Previous Next
3.9.4 Interface Types
1/2
An interface type is an abstract tagged type
that provides a restricted form of multiple inheritance. A tagged type,
task type, or protected type may have one or more interface types as
ancestors.
Syntax
2/2
interface_type_definition
::=
[
limited |
task |
protected |
synchronized]
interface [
and interface_list]
3/2
interface_list
::= interface_subtype_mark {
and interface_subtype_mark}
Static Semantics
4/2
An interface type (also called an
interface)
is
a specific abstract tagged
type that is defined by an
interface_type_definition.
5/2
An interface with the reserved word
limited,
task,
protected, or
synchronized in its definition
is termed, respectively, a
limited interface, a
task interface,
a
protected interface, or a
synchronized interface. In
addition,
all
task and protected interfaces are synchronized interfaces, and all synchronized
interfaces are limited interfaces. A view of an object that is of a task
interface type (or of a corresponding class-wide type) is a task object.
Similarly, a view of an object that is of a protected interface type
(or of a corresponding class-wide type) is a protected object.
6/2
A
task or protected type derived from an interface is a tagged type. Such
a tagged type is called a
synchronized tagged type, as are synchronized
interfaces and private extensions derived from synchronized interfaces.
7/2
An interface type has no components.
8/2
An
interface_subtype_mark
in an
interface_list names a
progenitor
subtype; its type is the
progenitor type. An interface type
inherits user-defined primitive subprograms from each progenitor type
in the same way that a derived type inherits user-defined primitive subprograms
from its progenitor types (see
3.4).
Legality Rules
9/2
All user-defined primitive subprograms of an
interface type shall be abstract subprograms or null procedures.
10/2
The type of a subtype named in an interface_list
shall be an interface type.
11/2
A descendant of a nonlimited interface shall
be nonlimited. A descendant of a task interface shall be a task type
or a task interface. A descendant of a protected interface shall be a
protected type or a protected interface. A descendant of a synchronized
interface shall be a task type, a protected type, or a synchronized interface.
12/2
In addition to the places where Legality Rules
normally apply (see
12.3), these rules apply
also in the private part of an instance of a generic unit.
Dynamic Semantics
13/2
The elaboration of an interface_type_definition
has no effect.
14/2
80 Nonlimited interface
types have predefined nonabstract equality operators. These may be overridden
with user-defined abstract equality operators. Such operators will then
require an explicit overriding for any nonabstract descendant of the
interface.
Examples
15/2
Example of
a limited interface and a synchronized interface extending it:
16/2
type Queue is limited interface;
procedure Append(Q : in out Queue; Person : in Person_Name) is abstract;
procedure Remove_First(Q : in out Queue; Person : out Person_Name) is abstract;
function Cur_Count(Q : in Queue) return Natural is abstract;
function Max_Count(Q : in Queue) return Natural is abstract;
-- See 3.10.1 for Person_Name.
17/2
Queue_Error : exception;
-- Append raises Queue_Error if Count(Q) = Max_Count(Q)
-- Remove_First raises Queue_Error if Count(Q) = 0
18/2
type Synchronized_Queue is synchronized interface and Queue; -- see 9.11
procedure Append_Wait(Q : in out Synchronized_Queue; Person : in Person_Name) is abstract;
procedure Remove_First_Wait(Q : in out Synchronized_Queue; Person : out Person_Name) is abstract;
19/2
...
20/2
procedure Transfer(From : in out Queue'Class;
To : in out Queue'Class;
Number : in Natural := 1) is
Person : Person_Name;
begin
for I in 1..Number loop
Remove_First(From, Person);
Append(To, Person);
end loop;
end Transfer;
21/2
This defines a Queue interface defining a queue
of people. (A similar design could be created to define any kind of queue
simply by replacing Person_Name by an appropriate type.) The Queue interface
has four dispatching operations, Append, Remove_First, Cur_Count, and
Max_Count. The body of a class-wide operation, Transfer is also shown.
Every non-abstract extension of Queue must provide implementations for
at least its four dispatching operations, as they are abstract. Any object
of a type derived from Queue may be passed to Transfer as either the
From or the To operand. The two operands need not be of the same type
in any given call.
22/2
The Synchronized_Queue interface inherits the
four dispatching operations from Queue and adds two additional dispatching
operations, which wait if necessary rather than raising the Queue_Error
exception. This synchronized interface may only be implemented by a task
or protected type, and as such ensures safe concurrent access.
23/2
Example use
of the interface:
24/2
type Fast_Food_Queue is new Queue with record ...;
procedure Append(Q : in out Fast_Food_Queue; Person : in Person_Name);
procedure Remove_First(Q : in out Fast_Food_Queue; Person : in Person_Name);
function Cur_Count(Q : in Fast_Food_Queue) return Natural;
function Max_Count(Q : in Fast_Food_Queue) return Natural;
25/2
...
26/2
Cashier, Counter : Fast_Food_Queue;
27/2
...
-- Add George (see 3.10.1) to the cashier's queue:
Append (Cashier, George);
-- After payment, move George to the sandwich counter queue:
Transfer (Cashier, Counter);
...
28/2
An interface such as Queue can be used directly
as the parent of a new type (as shown here), or can be used as a progenitor
when a type is derived. In either case, the primitive operations of the
interface are inherited. For Queue, the implementation of the four inherited
routines must be provided. Inside the call of Transfer, dispatching calls
to the implementations of Append and Remove_First for type Fast_Food_Queue
will be made.
29/2
Example of
a task interface:
30/2
type Serial_Device is task interface; -- see 9.1
procedure Read (Dev : in Serial_Device; C : out Character) is abstract;
procedure Write(Dev : in Serial_Device; C : in Character) is abstract;
31/2
The Serial_Device interface has two dispatching
operations which are intended to be implemented by task entries (see
9.1).
Contents Index Search Related Documents Previous Next Legal