Skip to content

Commit 3121390

Browse files
committed
privacy: limited private, indef private, opaque pointers
1 parent c347c03 commit 3121390

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
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+
* Reminder: private type is a **view**
20+
21+
- Completion should provide **at least** the same set of features
22+
- Completion can be a :ada:`limited record`
23+
- ... but doesn't **have** to
24+
25+
---------------
26+
Limited Private
27+
---------------
28+
29+
* No assignment: user cannot duplicate a key
30+
* No equality: user cannot check two keys are the same
31+
* Private type: user cannot access or change the issued date
32+
* Definite: user **doesn't** have to call :ada:`Make_Key`
33+
34+
.. code:: Ada
35+
36+
package Key_Stuff is
37+
type Key is limited private;
38+
function Make_Key( ... ) return Key;
39+
private
40+
type Key is limited record
41+
Issued: Date;
42+
Code: Integer;
43+
end record;
44+
end Key_Stuff;
45+
46+
package body Key_Stuff is
47+
function Make_Key ( ... ) return Key is
48+
begin
49+
return New_Key: Key do
50+
New_Key.Issued := Today;
51+
New_Key.Code := ... ;
52+
end return;
53+
end Make_Key;
54+
...
55+
end Key_Stuff;
56+
57+
------------------
58+
Indefinite Private
59+
------------------
60+
61+
* Indefinite: user **must** use the constructors
62+
* Delegated :ada:`constant` objects are static constructors
63+
* Type completion **can** be definite
64+
65+
.. code:: Ada
66+
67+
package Binary_Trees is
68+
type Tree_T (<>) is private;
69+
70+
Empty_Tree : constant Tree_T;
71+
72+
type Nodes_T is ...
73+
type Edges_T is ...
74+
procedure Make (N : Nodes_T; E : Edges_T);
75+
...
76+
private
77+
type Tree_T is record
78+
...
79+
80+
Empty_Tree : constant Tree_T := ...;
81+
82+
end Binary_Trees;
83+
84+
---------------
85+
Opaque Pointers
86+
---------------
87+
88+
* User can instatiate
89+
* Completion is an :ada:`access`
90+
* Concrete type being pointed to is **incomplete**
91+
* Implementation is done entirely within the body
92+
93+
.. code:: Ada
94+
95+
package Black_Boxes is
96+
type Box_T is private;
97+
procedure Foo (B : Box_T);
98+
private
99+
type Internal_Box_T; -- incomplete
100+
type Box_T is access all Internal_Box_T;
101+
end Black_Boxes;
102+
103+
------------------------
104+
Example: A String Holder
105+
------------------------
106+
107+
.. code:: Ada
108+
109+
package String_Holders is
110+
type Info is limited private;
111+
112+
function Contains (I : Info; S : String) return Boolean
113+
with Ghost;
114+
function Equals (A, B : Info) return Boolean
115+
with Ghost;
116+
117+
function To_Info (S : String) return Info
118+
with Post => Contains (To_Info'Result, S);
119+
function To_String (Obj : Info)
120+
return String
121+
with Post => Contains (Obj, To_String'Result);
122+
123+
function Copy (Obj : Info) return Info
124+
with Post => Equals (Copy'Result, Obj);
125+
procedure Copy (To : in out Info;
126+
From : Info)
127+
with Post => Equals (To, From);
128+
129+
procedure Append (Obj : in out Info;
130+
S : String)
131+
with Post => Contains (Obj, To_String (Obj)'Old & S);
132+
133+
procedure Reset (Obj : in out Info);
134+
procedure Destroy (Obj : in out Info);
135+
136+
private
137+
type Info is access String;
138+
function To_String_Internal (I : Info) return String
139+
is (if I = null then "" else I.all);
140+
141+
function Contains (I : Info; S : String) return Boolean
142+
is (I /= null and then I.all = S);
143+
function Equals (A, B : Info) return Boolean
144+
is (To_String_Internal (A) = To_String_Internal (B));
145+
end String_Holders;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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+
.. container:: PRELUDE SYMBOLS
19+
20+
.. |rightarrow| replace:: :math:`\rightarrow`
21+
.. |forall| replace:: :math:`\forall`
22+
.. |exists| replace:: :math:`\exists`
23+
.. |equivalent| replace:: :math:`\iff`
24+
.. |le| replace:: :math:`\le`
25+
.. |ge| replace:: :math:`\ge`
26+
.. |lt| replace:: :math:`<`
27+
.. |gt| replace:: :math:`>`
28+
.. |checkmark| replace:: :math:`\checkmark`
29+
30+
.. container:: PRELUDE REQUIRES
31+
32+
.. container:: PRELUDE PROVIDES
33+
34+
.. container:: PRELUDE END
35+
36+
.. include:: 110_private_types/07-limited-private.rst

0 commit comments

Comments
 (0)