1 | ------------------------------------------------------------------------------ |
---|---|
2 | --: Copyright © 2003 … 2023 Martin Krischik «krischik@users.sourceforge.net» |
3 | ------------------------------------------------------------------------------ |
4 | --: This library is free software; you can redistribute it and/or modify it |
5 | --: under the terms of the GNU Library General Public License as published by |
6 | --: the Free Software Foundation; either version 2 of the License, or (at your |
7 | --: option) any later version. |
8 | --: |
9 | --: This library is distributed in the hope that it will be useful, but |
10 | --: WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
11 | --: or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | --: License for more details. |
13 | --: |
14 | --: You should have received a copy of the GNU Library General Public License |
15 | --: along with this library; if not, write to the Free Software Foundation, |
16 | --: Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
17 | ---------------------------------------------------------------------------- |
18 | |
19 | pragma License (Modified_Gpl); |
20 | pragma Ada_2022; |
21 | |
22 | with Ada.Strings.Text_Buffers; |
23 | with AdaCL.Base; |
24 | |
25 | --- |
26 | -- @summary |
27 | -- AdaCL: Unique smart pointer. |
28 | -- |
29 | -- @description |
30 | -- Smart pointer with handover mechanism. Only one Unique pointer points to the referenced access. The referenced |
31 | -- element is deleted when the unique pointer is deleted. At handover the original is set null. |
32 | -- |
33 | --: Element_Type the element which are handled by the pointer |
34 | --: Pointer Access to the element type |
35 | --: Deleter function to delete an element instance |
36 | generic |
37 | type Element_Type (<>) is limited private; |
38 | type Pointer is access Element_Type; |
39 | with procedure Deleter (X : in out Pointer) is <> with |
40 | Post => X = null; |
41 | package AdaCL.Pointer.Unique_With_Delete is |
42 | --- |
43 | -- A Pointer to an unique element. |
44 | -- |
45 | type Object is new AdaCL.Base.Object with private; |
46 | |
47 | --- |
48 | -- Creates a new unique smart pointer from normal pointer. |
49 | -- |
50 | --: @param Referent Pointer to reference counted object |
51 | --: @return New smart pointer |
52 | function Create (Referent : in Pointer := null) return Object; |
53 | |
54 | --- |
55 | -- Checks if a pointers is set |
56 | -- |
57 | --: @param This Object itself. |
58 | --: @return true when pointer is not null |
59 | function Exist (This : in Object) return Boolean with |
60 | Pure_Function, Inline; |
61 | |
62 | --- |
63 | -- gets pointer to element to perform operations on. Do not save the pointer. |
64 | -- |
65 | --: @param This Object itself. |
66 | --: @return pointer to element |
67 | function Get (This : in Object) return not null Pointer with |
68 | Inline, Pre => (This.Exist), Post => (Get'Result /= null); |
69 | |
70 | --- |
71 | -- gets pointer to element and releases ownership. You cans save this pointer. |
72 | -- |
73 | --: @param This Object itself. |
74 | --: @param Referent pointer to element |
75 | procedure Release (This : in out Object; Referent : out not null Pointer) with |
76 | Pre => (This.Exist), Post => (Referent /= null); |
77 | |
78 | --- |
79 | -- Replaces the managed object. |
80 | -- |
81 | --: @param This Object itself. |
82 | --: @param Referent new pointer to manage |
83 | procedure Reset (This : in out Object; Referent : in Pointer := null); |
84 | |
85 | --- |
86 | -- swaps the managed objects |
87 | -- |
88 | --: @param This Object itself. |
89 | --: @param Other Object to swap Referents with |
90 | procedure Swap (This : in out Object; Other : in out Object); |
91 | |
92 | private |
93 | |
94 | --- |
95 | -- Self referencing type consisting of three components: This component holds the actual data. Details in the body. |
96 | -- |
97 | type Instance_Type; |
98 | |
99 | --- |
100 | -- Self referencing type consisting of three components: type of the self reference |
101 | -- |
102 | type Instance_Access is access Instance_Type; |
103 | |
104 | --- |
105 | -- A Pointer to an unique element. |
106 | -- |
107 | --: @field Self internal data kept as access to enable adjust to have Adjust to both instances. |
108 | type Object is new AdaCL.Base.Object with record |
109 | Self : Instance_Access; |
110 | end record with |
111 | Put_Image => Object_Image; |
112 | |
113 | procedure Object_Image (Output : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; This : Object); |
114 | |
115 | --- |
116 | -- Called when creating an object |
117 | -- |
118 | --: @param This Object itself. |
119 | overriding procedure Initialize (This : in out Object); |
120 | |
121 | --- |
122 | -- When adjusting we need hand over the pointer |
123 | -- |
124 | --: @param This Object itself. |
125 | overriding procedure Adjust (This : in out Object); |
126 | |
127 | --- |
128 | -- When finalizing we need delete the pointer |
129 | -- |
130 | --: @param This Object itself. |
131 | overriding procedure Finalize (This : in out Object); |
132 | |
133 | end AdaCL.Pointer.Unique_With_Delete; |
134 | |
135 | ---------------------------------------------------------------- {{{ ---------- |
136 | --: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab : |
137 | --: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr : |
138 | --: vim: set spell spelllang=en_gb : |