Ada Reference Manual (Ada 2022) — Legal Information

# A.18.18 The Generic Package Containers.Indefinite_Holders

1/3
The language-defined generic package Containers.Indefinite_Holders provides a private type Holder and a set of operations for that type. A holder container holds a single element of an indefinite type.
2/3
A holder container allows the declaration of an object that can be used like an uninitialized variable or component of an indefinite type.
3/3
A holder container may be empty. An empty holder does not contain an element.

#### Static Semantics

4/3
The generic library package Containers.Indefinite_Holders has the following declaration:
5/5
generic
type Element_Type (<>) is private;
with function "=" (Left, Right : Element_Type) return Boolean is <>;
package Ada.Containers.Indefinite_Holders
with Preelaborate, Remote_Types,
Nonblocking, Global => in out synchronized is
6/5
type Holder is tagged private
with Stable_Properties => (Is_Empty,
Tampering_With_The_Element_Prohibited),
Default_Initial_Condition => Is_Empty (Holder),
Preelaborable_Initialization;
7/3
Empty_Holder : constant Holder;
7.1/5
function Equal_Element (Left, Right : Element_Type) return Boolean
renames "=";
8/3
function "=" (Left, Right : Holder) return Boolean;
8.1/5
function Tampering_With_The_Element_Prohibited
(Container : Holder) return Boolean
with Nonblocking, Global => null, Use_Formal => null;
8.2/5
function Empty return Holder
is (Empty_Holder)
with Post =>
not Tampering_With_The_Element_Prohibited (Empty'Result)
and then Is_Empty (Empty'Result);
9/5
function To_Holder (New_Item : Element_Type) return Holder
with Post => not Is_Empty (To_Holder'Result);
10/5
function Is_Empty (Container : Holder) return Boolean
with Global => null, Use_Formal => null;
11/5
procedure Clear (Container : in out Holder)
with Pre => not Tampering_With_The_Element_Prohibited (Container)
or else raise Program_Error,
Post => Is_Empty (Container);
12/5
function Element (Container : Holder) return Element_Type
with Pre => not Is_Empty (Container) or else raise Constraint_Error,
Global => null, Use_Formal => Element_Type;
13/5
procedure Replace_Element (Container : in out Holder;
New_Item : in Element_Type)
with Pre => not Tampering_With_The_Element_Prohibited (Container)
or else raise Program_Error,
Post => not Is_Empty (Container);
14/5
procedure Query_Element
(Container : in Holder;
Process : not null access procedure (Element : in Element_Type))
with Pre => not Is_Empty (Container) or else raise Constraint_Error;
15/5
procedure Update_Element
(Container : in out Holder;
Process : not null access procedure (Element : in out Element_Type))
with Pre => not Is_Empty (Container) or else raise Constraint_Error;
16/5
type Constant_Reference_Type
(Element : not null access constant Element_Type) is private
with Implicit_Dereference => Element,
Nonblocking, Global => in out synchronized,
Default_Initial_Condition => (raise Program_Error);
17/5
type Reference_Type
(Element : not null access Element_Type) is private
with Implicit_Dereference => Element,
Nonblocking, Global => in out synchronized,
Default_Initial_Condition => (raise Program_Error);
18/5
function Constant_Reference (Container : aliased in Holder)
return Constant_Reference_Type
with Pre => not Is_Empty (Container)
or else raise Constraint_Error,
Post => Tampering_With_The_Element_Prohibited (Container),
Nonblocking, Global => null, Use_Formal => null;
19/5
function Reference (Container : aliased in out Holder)
return Reference_Type
with Pre => not Is_Empty (Container)
or else raise Constraint_Error,
Post => Tampering_With_The_Element_Prohibited (Container),
Nonblocking, Global => null, Use_Formal => null;
20/5
procedure Assign (Target : in out Holder; Source : in Holder)
with Post => (Is_Empty (Source) = Is_Empty (Target));
21/5
function Copy (Source : Holder) return Holder
with Post => (Is_Empty (Source) = Is_Empty (Copy'Result));
22/5
procedure Move (Target : in out Holder; Source : in out Holder)
with Pre => (not Tampering_With_The_Element_Prohibited (Target)
or else raise Program_Error) and then
(not Tampering_With_The_Element_Prohibited (Source)
or else raise Program_Error),
Post => (if not Target'Has_Same_Storage (Source) then
Is_Empty (Source) and then (not Is_Empty (Target)));
22.1/5
procedure Swap (Left, Right : in out Holder)
with Pre => (not Tampering_With_The_Element_Prohibited (Left)
or else raise Program_Error) and then
(not Tampering_With_The_Element_Prohibited (Right)
or else raise Program_Error),
Post => Is_Empty (Left) = Is_Empty (Right)'Old and then
Is_Empty (Right) = Is_Empty (Left)'Old;
23/3
private
24/3
... -- not specified by the language
25/3
end Ada.Containers.Indefinite_Holders;
26/3
The actual function for the generic formal function "=" on Element_Type values is expected to define a reflexive and symmetric relationship and return the same result value each time it is called with a particular pair of values. If it behaves in some other manner, the function "=" on holder values returns an unspecified value. The exact arguments and number of calls of this generic formal function by the function "=" on holder values are unspecified.
27/3
The type Holder is used to represent holder containers. The type Holder needs finalization (see 7.6).
28/3
Empty_Holder represents an empty holder object. If an object of type Holder is not otherwise initialized, it is initialized to the same value as Empty_Holder.
29/5
Some operations check for “tampering with the element” of a container because they depend on the element of the container not being replaced. When tampering with the element is prohibited for a particular holder object H, Program_Error is propagated by the finalization of H, as well as by a call that passes H to certain of the operations of this package, as indicated by the precondition of such an operation.
Paragraphs 30 through 35 are removed as preconditions now describe these rules.
36/3
function "=" (Left, Right : Holder) return Boolean;
37/3
If Left and Right denote the same holder object, then the function returns True. Otherwise, it compares the element contained in Left to the element contained in Right using the generic formal equality operator, returning the result of that operation. Any exception raised during the evaluation of element equality is propagated.
37.1/5
function Tampering_With_The_Element_Prohibited
(Container : Holder) return Boolean
with Nonblocking, Global => null, Use_Formal => null;
37.2/5
Returns True if tampering with the element is currently prohibited for Container, and returns False otherwise.
38/5
function To_Holder (New_Item : Element_Type) return Holder
with Post => not Is_Empty (To_Holder'Result);
39/4
Returns a nonempty holder containing an element initialized to New_Item. To_Holder performs indefinite insertion (see A.18).
40/5
function Is_Empty (Container : Holder) return Boolean
with Global => null, Use_Formal => null;
41/3
Returns True if Container is empty, and False if it contains an element.
42/5
procedure Clear (Container : in out Holder)
with Pre => not Tampering_With_The_Element_Prohibited (Container)
or else raise Program_Error,
Post => Is_Empty (Container);
43/5
Removes the element from Container.
44/5
function Element (Container : Holder) return Element_Type
with Pre => not Is_Empty (Container) or else raise Constraint_Error,
Global => null, Use_Formal => Element_Type;
45/5
Returns the element stored in Container.
46/5
procedure Replace_Element (Container : in out Holder;
New_Item : in Element_Type)
with Pre => not Tampering_With_The_Element_Prohibited (Container)
or else raise Program_Error,
Post => not Is_Empty (Container);
47/5
Replace_Element assigns the value New_Item into Container, replacing any preexisting content of Container; Replace_Element performs indefinite insertion (see A.18).
48/5
procedure Query_Element
(Container : in Holder;
Process : not null access procedure (Element : in Element_Type))
with Pre => not Is_Empty (Container) or else raise Constraint_Error,
Global => null, Use_Formal => null;
49/5
Query_Element calls Process.all with the contained element as the argument. Tampering with the element of Container is prohibited during the execution of the call on Process.all. Any exception raised by Process.all is propagated.
50/5
procedure Update_Element
(Container : in out Holder;
Process : not null access procedure (Element : in out Element_Type))
with Pre => not Is_Empty (Container) or else raise Constraint_Error;
51/5
Update_Element calls Process.all with the contained element as the argument. Tampering with the element of Container is prohibited during the execution of the call on Process.all. Any exception raised by Process.all is propagated.
52/5
type Constant_Reference_Type
(Element : not null access constant Element_Type) is private
with Implicit_Dereference => Element,
Nonblocking, Global => in out synchronized,
Default_Initial_Condition => (raise Program_Error);
53/5
type Reference_Type (Element : not null access Element_Type) is private
with Implicit_Dereference => Element,
Nonblocking, Global => in out synchronized,
Default_Initial_Condition => (raise Program_Error);
54/3
The types Constant_Reference_Type and Reference_Type need finalization.
55/5
This paragraph was deleted.
56/5
function Constant_Reference (Container : aliased in Holder)
return Constant_Reference_Type
with Pre => not Is_Empty (Container) or else raise Constraint_Error,
Post => Tampering_With_The_Element_Prohibited (Container),
Nonblocking, Global => null, Use_Formal => null;
57/3
This function (combined with the Implicit_Dereference aspect) provides a convenient way to gain read access to the contained element of a holder container.
58/5
Constant_Reference returns an object whose discriminant is an access value that designates the contained element. Tampering with the element of Container is prohibited while the object returned by Constant_Reference exists and has not been finalized.
59/5
function Reference (Container : aliased in out Holder)
return Reference_Type
with Pre => not Is_Empty (Container) or else raise Constraint_Error,
Post => Tampering_With_The_Element_Prohibited (Container),
Nonblocking, Global => null, Use_Formal => null;
60/3
This function (combined with the Implicit_Dereference aspects) provides a convenient way to gain read and write access to the contained element of a holder container.
61/5
Reference returns an object whose discriminant is an access value that designates the contained element. Tampering with the element of Container is prohibited while the object returned by Reference exists and has not been finalized.
62/5
procedure Assign (Target : in out Holder; Source : in Holder)
with Post => (Is_Empty (Source) = Is_Empty (Target));
63/3
If Target denotes the same object as Source, the operation has no effect. If Source is empty, Clear (Target) is called. Otherwise, Replace_Element (Target, Element (Source)) is called.
64/5
function Copy (Source : Holder) return Holder
with Post => (Is_Empty (Source) = Is_Empty (Copy'Result));
65/3
If Source is empty, returns an empty holder container; otherwise, returns To_Holder (Element (Source)).
66/5
procedure Move (Target : in out Holder; Source : in out Holder)
with Pre => (not Tampering_With_The_Element_Prohibited (Target)
or else raise Program_Error) and then
(not Tampering_With_The_Element_Prohibited (Source)
or else raise Program_Error),
Post => (if not Target'Has_Same_Storage (Source) then
Is_Empty (Source) and then (not Is_Empty (Target)));
67/5
If Target denotes the same object as Source, then the operation has no effect. Otherwise, the element contained by Source (if any) is removed from Source and inserted into Target, replacing any preexisting content.
67.1/5
procedure Swap (Left, Right : in out Holder)
with Pre => (not Tampering_With_The_Element_Prohibited (Left)
or else raise Program_Error) and then
(not Tampering_With_The_Element_Prohibited (Right)
or else raise Program_Error),
Post => Is_Empty (Left) = Is_Empty (Right)'Old and then
Is_Empty (Right) = Is_Empty (Left)'Old;
67.2/5
If Left denotes the same object as Right, then the operation has no effect. Otherwise, operation exchanges the elements (if any) contained by Left and Right.

#### Bounded (Run-Time) Errors

68/3
It is a bounded error for the actual function associated with a generic formal subprogram, when called as part of an operation of this package, to tamper with the element of any Holder parameter of the operation. Either Program_Error is raised, or the operation works as defined on the value of the Holder either prior to, or subsequent to, some or all of the modifications to the Holder.
69/3
It is a bounded error to call any subprogram declared in the visible part of Containers.Indefinite_Holders when the associated container has been finalized. If the operation takes Container as an in out parameter, then it raises Constraint_Error or Program_Error. Otherwise, the operation either proceeds as it would for an empty container, or it raises Constraint_Error or Program_Error.

#### Erroneous Execution

70/3
Execution is erroneous if the holder container associated with the result of a call to Reference or Constant_Reference is finalized before the result object returned by the call to Reference or Constant_Reference is finalized.

#### Implementation Requirements

71/3
No storage associated with a holder object shall be lost upon assignment or scope exit.
72/3
The execution of an assignment_statement for a holder container shall have the effect of copying the element (if any) from the source holder object to the target holder object.

#### Implementation Advice

73/5
Move and Swap should not copy any elements, and should minimize copying of internal data structures.
74/3
If an exception is propagated from a holder operation, no storage should be lost, nor should the element be removed from a holder container unless specified by the operation.

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe