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 | pragma License (Modified_GPL); |
---|
27 | pragma Ada_2022; |
---|
28 | |
---|
29 | with Ada.Containers; |
---|
30 | with Ada.Strings.Text_Buffers; |
---|
31 | with AdaCL.Pointer.Element; |
---|
32 | with AdaCL.Pointer.Holder; |
---|
33 | |
---|
34 | private with Ada.Containers.Bounded_Doubly_Linked_Lists; |
---|
35 | private with Ada.Containers.Synchronized_Queue_Interfaces; |
---|
36 | private with AdaCL.Pointer.Unique; |
---|
37 | |
---|
38 | |
---|
39 | |
---|
40 | |
---|
41 | |
---|
42 | |
---|
43 | |
---|
44 | |
---|
45 | |
---|
46 | |
---|
47 | |
---|
48 | |
---|
49 | |
---|
50 | |
---|
51 | |
---|
52 | generic |
---|
53 | type Element_Type is private; |
---|
54 | Null_Element : in Element_Type; |
---|
55 | Default_Maximum_Size : in Ada.Containers.Count_Type := 32; |
---|
56 | with function "=" (Left, Right : Element_Type) return Boolean is <>; |
---|
57 | package AdaCL.Queue is |
---|
58 | use type Ada.Containers.Count_Type; |
---|
59 | |
---|
60 | |
---|
61 | |
---|
62 | package Interfaces is |
---|
63 | |
---|
64 | type Object is limited interface; |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | |
---|
70 | procedure Finish (This : in out Object) is abstract; |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | function Is_Finishing (This : in Object) return Boolean is abstract; |
---|
78 | |
---|
79 | |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | function Current_Use (This : Object) return Ada.Containers.Count_Type is abstract with |
---|
85 | Inline; |
---|
86 | |
---|
87 | |
---|
88 | |
---|
89 | |
---|
90 | |
---|
91 | |
---|
92 | function Is_Empty (This : Object) return Boolean is abstract with |
---|
93 | Post'Class => Is_Empty'Result = (This.Current_Use = 0); |
---|
94 | |
---|
95 | |
---|
96 | |
---|
97 | |
---|
98 | |
---|
99 | |
---|
100 | function Has_Finished (This : in Object) return Boolean is abstract with |
---|
101 | Post'Class => Has_Finished'Result = (This.Is_Finishing and then This.Is_Empty); |
---|
102 | |
---|
103 | |
---|
104 | |
---|
105 | |
---|
106 | |
---|
107 | procedure Wait_Finished (This : in out Object) is abstract with |
---|
108 | Post'Class => (This.Is_Finishing and then This.Is_Empty); |
---|
109 | |
---|
110 | |
---|
111 | |
---|
112 | |
---|
113 | |
---|
114 | |
---|
115 | function Available (This : Object) return Ada.Containers.Count_Type is abstract; |
---|
116 | |
---|
117 | |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | |
---|
122 | procedure Enqueue (This : in out Object; New_Item : in Element_Type) is abstract; |
---|
123 | |
---|
124 | |
---|
125 | |
---|
126 | |
---|
127 | |
---|
128 | |
---|
129 | |
---|
130 | |
---|
131 | |
---|
132 | procedure Dequeue |
---|
133 | (This : in out Object; |
---|
134 | Element : out Element_Type; |
---|
135 | EmptyAndFinished : out Boolean) is abstract with |
---|
136 | Post'Class => (if EmptyAndFinished then Element = Null_Element); |
---|
137 | |
---|
138 | |
---|
139 | |
---|
140 | |
---|
141 | |
---|
142 | procedure Abort_Queue (This : in out Object) is abstract with |
---|
143 | Post'Class => (This.Is_Finishing and then This.Is_Empty); |
---|
144 | |
---|
145 | end Interfaces; |
---|
146 | |
---|
147 | |
---|
148 | |
---|
149 | package Protect is |
---|
150 | Illeagal_State_Error : exception; |
---|
151 | |
---|
152 | |
---|
153 | |
---|
154 | |
---|
155 | |
---|
156 | type Object (Maximum_Size : Ada.Containers.Count_Type := Default_Maximum_Size) is |
---|
157 | new AdaCL.Pointer.Element.Object and Interfaces.Object with private; |
---|
158 | |
---|
159 | |
---|
160 | |
---|
161 | |
---|
162 | |
---|
163 | overriding procedure Finish (This : in out Object); |
---|
164 | |
---|
165 | |
---|
166 | |
---|
167 | |
---|
168 | |
---|
169 | |
---|
170 | overriding function Is_Finishing (This : in Object) return Boolean with |
---|
171 | Inline; |
---|
172 | |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | |
---|
177 | |
---|
178 | overriding function Current_Use (This : Object) return Ada.Containers.Count_Type; |
---|
179 | |
---|
180 | |
---|
181 | |
---|
182 | |
---|
183 | |
---|
184 | |
---|
185 | overriding function Is_Empty (This : Object) return Boolean with |
---|
186 | Post'Class => Is_Empty'Result = (This.Current_Use = 0); |
---|
187 | |
---|
188 | |
---|
189 | |
---|
190 | |
---|
191 | |
---|
192 | |
---|
193 | overriding function Has_Finished (This : in Object) return Boolean with |
---|
194 | Post'Class => Has_Finished'Result = (This.Is_Finishing and then This.Is_Empty); |
---|
195 | |
---|
196 | |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | overriding procedure Wait_Finished (This : in out Object) with |
---|
201 | Post'Class => (This.Is_Finishing and then This.Is_Empty); |
---|
202 | |
---|
203 | |
---|
204 | |
---|
205 | |
---|
206 | |
---|
207 | |
---|
208 | overriding function Available (This : Object) return Ada.Containers.Count_Type with |
---|
209 | Post'Class => Available'Result = (if This.Is_Finishing then 0 else This.Maximum_Size - This.Current_Use); |
---|
210 | |
---|
211 | |
---|
212 | |
---|
213 | |
---|
214 | |
---|
215 | |
---|
216 | overriding procedure Enqueue (This : in out Object; New_Item : in Element_Type); |
---|
217 | |
---|
218 | |
---|
219 | |
---|
220 | |
---|
221 | |
---|
222 | |
---|
223 | |
---|
224 | |
---|
225 | |
---|
226 | overriding procedure Dequeue |
---|
227 | (This : in out Object; |
---|
228 | Element : out Element_Type; |
---|
229 | EmptyAndFinished : out Boolean) with |
---|
230 | Post'Class => (if EmptyAndFinished then Element = Null_Element); |
---|
231 | |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | |
---|
236 | overriding procedure Abort_Queue (This : in out Object) with |
---|
237 | Post'Class => (This.Is_Finishing and then This.Is_Empty); |
---|
238 | |
---|
239 | private |
---|
240 | package Inherited renames AdaCL.Pointer.Element; |
---|
241 | package Core is new Ada.Containers.Synchronized_Queue_Interfaces (Element_Type); |
---|
242 | package Lists is new Ada.Containers.Bounded_Doubly_Linked_Lists (Element_Type); |
---|
243 | subtype List is Lists.List; |
---|
244 | |
---|
245 | protected type Sychronized_Object |
---|
246 | (Maximum_Size : Ada.Containers.Count_Type) |
---|
247 | is new Core.Queue and Interfaces.Object with |
---|
248 | overriding procedure Finish; |
---|
249 | |
---|
250 | overriding function Available return Ada.Containers.Count_Type with |
---|
251 | Inline; |
---|
252 | |
---|
253 | overriding function Is_Finishing return Boolean with |
---|
254 | Inline; |
---|
255 | |
---|
256 | overriding function Current_Use return Ada.Containers.Count_Type with |
---|
257 | Inline; |
---|
258 | |
---|
259 | overriding function Peak_Use return Ada.Containers.Count_Type with |
---|
260 | Inline; |
---|
261 | |
---|
262 | overriding function Is_Empty return Boolean with |
---|
263 | Inline; |
---|
264 | |
---|
265 | overriding function Has_Finished return Boolean with |
---|
266 | Inline; |
---|
267 | |
---|
268 | overriding entry Wait_Finished; |
---|
269 | |
---|
270 | overriding entry Enqueue (New_Item : in Element_Type); |
---|
271 | |
---|
272 | overriding entry Dequeue (Element : out Element_Type); |
---|
273 | |
---|
274 | overriding entry Dequeue (Element : out Element_Type; EmptyAndFinished : out Boolean); |
---|
275 | |
---|
276 | overriding procedure Abort_Queue; |
---|
277 | |
---|
278 | private |
---|
279 | |
---|
280 | |
---|
281 | |
---|
282 | Data : List (Maximum_Size); |
---|
283 | |
---|
284 | |
---|
285 | |
---|
286 | |
---|
287 | Finish_Flag : Boolean := False; |
---|
288 | end Sychronized_Object; |
---|
289 | |
---|
290 | package Sychronized_Holder is new AdaCL.Pointer.Unique (Sychronized_Object); |
---|
291 | |
---|
292 | |
---|
293 | |
---|
294 | |
---|
295 | |
---|
296 | type Object (Maximum_Size : Ada.Containers.Count_Type := Default_Maximum_Size) is |
---|
297 | new AdaCL.Pointer.Element.Object and Interfaces.Object with record |
---|
298 | Delegate : Sychronized_Holder.Object := Sychronized_Holder.Create (new Sychronized_Object (Maximum_Size)); |
---|
299 | end record with |
---|
300 | Put_Image => Object_Image; |
---|
301 | |
---|
302 | procedure Object_Image (Output : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; This : Object); |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | |
---|
307 | |
---|
308 | overriding function Is_Finishing (This : in Object) return Boolean is (This.Delegate.Get.Is_Finishing); |
---|
309 | |
---|
310 | overriding function Current_Use (This : in Object) return Ada.Containers.Count_Type is |
---|
311 | (This.Delegate.Get.Current_Use); |
---|
312 | |
---|
313 | overriding function Is_Empty (This : Object) return Boolean is (This.Delegate.Get.Is_Empty); |
---|
314 | |
---|
315 | overriding function Has_Finished (This : in Object) return Boolean is (This.Delegate.Get.Has_Finished); |
---|
316 | |
---|
317 | overriding function Available (This : Object) return Ada.Containers.Count_Type is (This.Delegate.Get.Available); |
---|
318 | |
---|
319 | end Protect; |
---|
320 | |
---|
321 | package Protected_Holder is new AdaCL.Pointer.Holder (Protect.Object); |
---|
322 | subtype Protected_Object is Protected_Holder.Object; |
---|
323 | |
---|
324 | |
---|
325 | |
---|
326 | |
---|
327 | |
---|
328 | function Create_Protect |
---|
329 | (Maximum_Size : in Ada.Containers.Count_Type := Default_Maximum_Size) return Protected_Object is |
---|
330 | (Protected_Holder.Create (new Protect.Object (Maximum_Size))); |
---|
331 | |
---|
332 | end AdaCL.Queue; |
---|
333 | |
---|
334 | |
---|
335 | |
---|
336 | |
---|