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.Unchecked_Deallocation; |
23 | with AdaCL.Pointer.Unique_With_Delete; |
24 | |
25 | --- |
26 | -- @summary |
27 | -- AdaCL: Ada Class Library — 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 | -- Simplified version which only need the element type |
34 | -- |
35 | --: Element_Type the element which are handled by the pointer |
36 | generic |
37 | type Element_Type (<>) is limited private; |
38 | package AdaCL.Pointer.Unique is |
39 | type Pointer is access Element_Type; |
40 | |
41 | procedure Deleter is new Ada.Unchecked_Deallocation (Object => Element_Type, Name => Pointer); |
42 | |
43 | package Delegate is new AdaCL.Pointer.Unique_With_Delete (Element_Type => Element_Type, Pointer => Pointer); |
44 | |
45 | subtype Object is Delegate.Object; |
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 renames Delegate.Create; |
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 renames Delegate.Exist; |
60 | |
61 | --- |
62 | -- gets pointer to element to perform operations on. Do not save the pointer. |
63 | -- |
64 | --: @param This Object itself. |
65 | --: @return pointer to element |
66 | function Get (This : in Object) return not null Pointer renames Delegate.Get; |
67 | |
68 | --- |
69 | -- gets pointer to element and releases ownership. You cans save this pointer. |
70 | -- |
71 | --: @param This Object itself. |
72 | --: @param Referent pointer to element |
73 | procedure Release (This : in out Object; Referent : out not null Pointer) renames Delegate.Release; |
74 | |
75 | --- |
76 | -- Replaces the managed object. |
77 | -- |
78 | --: @param This Object itself. |
79 | --: @param Referent new pointer to manage |
80 | procedure Reset (This : in out Object; Referent : in Pointer := null) renames Delegate.Reset; |
81 | |
82 | --- |
83 | -- swaps the managed objects |
84 | -- |
85 | --: @param This Object itself. |
86 | --: @param Other Object to swap Referents with |
87 | procedure Swap (This : in out Object; Other : in out Object) renames Delegate.Swap; |
88 | |
89 | end AdaCL.Pointer.Unique; |
90 | |
91 | ---------------------------------------------------------------- {{{ ---------- |
92 | --: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab : |
93 | --: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr : |
94 | --: vim: set spell spelllang=en_gb : |