Skip to content

Commit ee51703

Browse files
committed
privacy: limited private, indef private, opaque pointers
1 parent 8e9329b commit ee51703

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
================
2+
Advanced Private
3+
================
4+
5+
---------------
6+
Limited Private
7+
---------------
8+
9+
.. code:: Ada
10+
11+
type T is limited private;
12+
13+
* Same interface as private
14+
15+
- Removes :ada:`=` and :ada:`/=`
16+
- Removes assignments
17+
- Removes copies
18+
19+
.. note::
20+
21+
Private type is a **view**
22+
23+
- Completion should provide **at least** the same set of features
24+
- Completion can be a :ada:`limited record`
25+
- ... but doesn't **have** to
26+
27+
---------------
28+
Limited Private
29+
---------------
30+
31+
* No assignment: user cannot duplicate a key
32+
* No equality: user cannot check two keys are the same
33+
* Private type: user cannot access or change the issued date
34+
35+
.. code:: Ada
36+
37+
package Key_Stuff is
38+
type Key is limited private;
39+
function Make_Key ( ... ) return Key;
40+
...
41+
42+
package body Key_Stuff is
43+
function Make_Key ( ... ) return Key is
44+
begin
45+
return New_Key: Key do
46+
New_Key.Issued := Today;
47+
...
48+
end return;
49+
end Make_Key;
50+
51+
.. warning::
52+
53+
* Definite type
54+
* User **doesn't** have to call :ada:`Make_Key`
55+
56+
------------------
57+
Indefinite Private
58+
------------------
59+
60+
* Indefinite: user **must** use the constructors
61+
* Delegated :ada:`constant` objects are static constructors
62+
63+
.. code:: Ada
64+
65+
package Binary_Trees is
66+
type Tree_T (<>) is private;
67+
68+
Empty_Tree : constant Tree_T;
69+
70+
type Nodes_T is ...
71+
type Edges_T is ...
72+
procedure Make (N : Nodes_T; E : Edges_T);
73+
...
74+
private
75+
type Tree_T is record
76+
...
77+
78+
Empty_Tree : constant Tree_T := ...;
79+
80+
end Binary_Trees;
81+
82+
.. tip::
83+
84+
Type completion **can** be definite
85+
86+
---------------
87+
Opaque Pointers
88+
---------------
89+
90+
* User can instatiate
91+
* Completion is an :ada:`access`
92+
* Concrete type being pointed to is **incomplete**
93+
* Implementation is done entirely within the body
94+
95+
.. code:: Ada
96+
97+
package Black_Boxes is
98+
type Box_T is private;
99+
procedure Foo (B : Box_T);
100+
private
101+
type Internal_Box_T; -- incomplete
102+
type Box_T is access all Internal_Box_T;
103+
end Black_Boxes;
104+
105+
------------------------------
106+
Example: A String Holder (1/2)
107+
------------------------------
108+
109+
* Implementation not discussed here
110+
111+
.. code:: Ada
112+
113+
package String_Holders is
114+
type Info is limited private;
115+
116+
function Contains (I : Info; S : String) return Boolean
117+
with Ghost;
118+
function Equals (A, B : Info) return Boolean
119+
with Ghost;
120+
121+
.. tip::
122+
123+
These are only used for contracts, hence the :ada:`Ghost` aspect
124+
125+
.. code:: Ada
126+
127+
function To_Info (S : String) return Info
128+
with Post => Contains (To_Info'Result, S);
129+
130+
function To_String (Obj : Info)
131+
return String
132+
with Post => Contains (Obj, To_String'Result);
133+
134+
procedure Copy (To : in out Info;
135+
From : Info)
136+
with Post => Equals (To, From);
137+
138+
procedure Append (Obj : in out Info;
139+
S : String)
140+
with Post => Contains (Obj, To_String (Obj)'Old & S);
141+
142+
procedure Destroy (Obj : in out Info);
143+
144+
------------------------------
145+
Example: A String Holder (2/2)
146+
------------------------------
147+
148+
.. code:: Ada
149+
150+
private
151+
type Info is access String;
152+
153+
function To_String_Internal (I : Info) return String
154+
is (if I = null then "" else I.all);
155+
.. tip::
156+
157+
This can be used by contracts implementation below, and child packages
158+
159+
.. code:: Ada
160+
161+
function Contains (I : Info; S : String) return Boolean
162+
is (I /= null and then I.all = S);
163+
function Equals (A, B : Info) return Boolean
164+
is (To_String_Internal (A) = To_String_Internal (B));
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
**************************
2+
Expert Resource Management
3+
**************************
4+
5+
.. container:: PRELUDE BEGIN
6+
7+
.. container:: PRELUDE ROLES
8+
9+
.. role:: ada(code)
10+
:language: Ada
11+
12+
.. role:: C(code)
13+
:language: C
14+
15+
.. role:: cpp(code)
16+
:language: C++
17+
18+
.. role:: rust(code)
19+
:language: Rust
20+
21+
.. container:: PRELUDE SYMBOLS
22+
23+
.. |rightarrow| replace:: :math:`\rightarrow`
24+
.. |forall| replace:: :math:`\forall`
25+
.. |exists| replace:: :math:`\exists`
26+
.. |equivalent| replace:: :math:`\iff`
27+
.. |le| replace:: :math:`\le`
28+
.. |ge| replace:: :math:`\ge`
29+
.. |lt| replace:: :math:`<`
30+
.. |gt| replace:: :math:`>`
31+
.. |checkmark| replace:: :math:`\checkmark`
32+
33+
.. container:: PRELUDE REQUIRES
34+
35+
.. container:: PRELUDE PROVIDES
36+
37+
.. container:: PRELUDE END
38+
39+
.. include:: 110_private_types/07-limited-private.rst

0 commit comments

Comments
 (0)