-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathobject-handle.ads
175 lines (172 loc) · 6.03 KB
/
object-handle.ads
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
-- --
-- package Object.Handle Copyright (c) Dmitry A. Kazakov --
-- Interface Luebeck --
-- Winter, 2002 --
-- --
-- Last revision : 20:41 21 Jul 2017 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
--
-- The generic package Object.Handle provides the the type Handle used
-- to reference decendants of the type Entity (declared in the package
-- Object). An object is not destroyed as long as at least one handle of
-- a object exists. The object is destoyed upon finalization of the last
-- handle to it. Handles can be copied.
--
-- Object_Type - A type derived from Object.Entity
-- Object_Type_Ptr - A class-wide access type for Object_Type'Class
--
generic
type Object_Type (<>) is abstract new Entity with private;
type Object_Type_Ptr is access Object_Type'Class;
package Object.Handle is
type Handle is new Ada.Finalization.Controlled with private;
Null_Handle : constant Handle;
--
-- Adjust -- Assignment
--
-- Reference - The handle
--
-- This procedure increases the reference count the object.
--
procedure Adjust (Reference : in out Handle);
--
-- Finalize -- Destructor
--
-- Reference - The handle
--
-- When the last handle to the object is finalized, the object is
-- destroyed.
--
procedure Finalize (Reference : in out Handle);
--
-- Invalidate -- Detach handle from the object
--
-- Reference - The handle
--
-- This procedure makes handle pointing to nothing. If it was the last
-- reference to the object, the later is destroyed.
--
procedure Invalidate (Reference : in out Handle);
--
-- Is_Valid -- Check if the handle is associated with an object
--
-- Reference - The handle
--
-- Returns :
--
-- True if the handle can de dereferenced
--
function Is_Valid (Reference : Handle) return Boolean;
--
-- Ptr -- Get the pointer to the object by the handle
--
-- Reference - The handle
--
-- Returns :
--
-- The referenced object or null if Reference is invalid
--
function Ptr (Reference : Handle) return Object_Type_Ptr;
--
-- Ref -- Get handle to an object
--
-- Thing - A pointer to the object
--
-- Returns :
--
-- Handle to the object
--
function Ref (Thing : Object_Type_Ptr) return Handle;
--
-- Set -- Handle to an object
--
-- Reference - A handle to set
-- Thing - A pointer to the object
--
procedure Set (Reference : in out Handle; Thing : Object_Type_Ptr);
--
-- <, <=, =, >=, > -- Comparisons
--
-- Left - The first argument
-- Right - The second argument
--
-- Only valid handles are comparable. However it is exception-safe to
-- compare Null_Handle for equality. In all other cases Constraint_Error
-- is propagated.
--
-- Returns :
--
-- The result of comparison of the objects
--
-- Exceptions :
--
-- Constraint_Error - One of arguments is Null_Handle
--
function "<" (Left, Right : Handle) return Boolean;
function "<=" (Left, Right : Handle) return Boolean;
function "=" (Left, Right : Handle) return Boolean;
function ">=" (Left, Right : Handle) return Boolean;
function ">" (Left, Right : Handle) return Boolean;
--
-- = -- Equality (handle and object)
--
-- Left - The first argument
-- Right - The second argument
--
-- Returns :
--
-- The result of comparison
--
function "=" (Left : Handle; Right : access Object_Type'Class)
return Boolean;
function "=" (Left : access Object_Type'Class; Right : Handle)
return Boolean;
private
pragma Inline (Adjust);
pragma Inline (Finalize);
pragma Inline (Invalidate);
pragma Inline (Is_Valid);
pragma Inline (Ptr);
pragma Inline (Ref);
pragma Inline (Set);
pragma Inline ("<=", "<", "=", ">", ">=");
pragma Inline ("=");
type Handle is new Ada.Finalization.Controlled with record
Ptr : Object_Type_Ptr := null;
end record;
--
-- Release -- Decrement object's use count
--
-- Ptr - To the object
--
-- The object pointed by Ptr is deleted if its use count in 1. Otherwise
-- the use count is decremented. Nothing happens if Ptr is null.
--
-- Exceptions :
--
-- Program_Error - Use count is already zero
--
procedure Release (Ptr : in out Object_Type_Ptr);
pragma Inline (Release);
Null_Handle : constant Handle :=
(Ada.Finalization.Controlled with null);
end Object.Handle;