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 | pragma License (Modified_Gpl); |
19 | pragma Ada_2022; |
20 | |
21 | with AdaCL.Limited_Base; |
22 | with Ada.Strings.Text_Buffers; |
23 | |
24 | --- |
25 | -- @summary |
26 | -- Ada Class Library |
27 | -- Base Class for Reference Counted Instances. |
28 | -- |
29 | -- @description |
30 | -- Element and Holder: A reference counted smart pointer for tagged types where the reference is kept inside the |
31 | -- tagged type. This is slightly faster, uses less memory and is more reliable. However it can only be uses for |
32 | -- limited tagged types and you need to implement the interface. |
33 | package AdaCL.Pointer.Element is |
34 | |
35 | ---------- Interface ------------------------------------------------------ |
36 | |
37 | --- |
38 | -- Interface Class for Reference Counted Instances |
39 | -- |
40 | -- Use the interface in when using the base class is not possible. |
41 | -- |
42 | -- Type for which we want to supply a reference counter. Since, in Ada one can not overload the ":=" operator the |
43 | -- type need to be limited so the counter is not damaged by assignment. |
44 | -- |
45 | -- Mind you, in C++ I almost always make the operator = private in in Reference counted classes as well. |
46 | -- |
47 | type Object_Interface is limited interface and AdaCL.Limited_Base.Object_Interface; |
48 | |
49 | --- |
50 | -- Add a reference |
51 | -- |
52 | --: @param This Object itself. |
53 | procedure Add_Reference (This : in out Object_Interface) is abstract; |
54 | |
55 | --- |
56 | -- Remove a reference. However since an object and a pointer is passed the instance is not deleted. |
57 | -- |
58 | -- The caller is responsible to call Use_Count and check if delete is needed. |
59 | -- |
60 | -- Concurrency: Guarded |
61 | -- |
62 | --: @param This Object itself. |
63 | procedure Remove_Reference (This : in out Object_Interface) is abstract; |
64 | |
65 | --- |
66 | -- Current reference counter. |
67 | -- |
68 | --: @param This Object itself. |
69 | function Use_Count (This : in Object_Interface) return Natural is abstract; |
70 | |
71 | --- |
72 | -- Get name of Class. |
73 | -- |
74 | --: @param This Object itself. |
75 | overriding function Get_Name (This : in Object_Interface) return String is abstract; |
76 | |
77 | ---------- Class ---------------------------------------------------------- |
78 | |
79 | --- |
80 | -- Base Class for Reference Counted Instances. |
81 | -- |
82 | type Object is new AdaCL.Limited_Base.Object and Object_Interface with private; |
83 | |
84 | type Object_Class is access Object'Class; |
85 | |
86 | --- |
87 | -- Add a reference |
88 | -- |
89 | --: @param This Object itself. |
90 | overriding procedure Add_Reference (This : in out Object) with |
91 | Inline; |
92 | |
93 | --- |
94 | -- Remove a reference. However since an object and a pointer is passed the instance is not deleted. |
95 | -- |
96 | -- The caller is responsible to call Use_Count and check if delete is needed. |
97 | -- |
98 | --: @param This Object itself. |
99 | overriding procedure Remove_Reference (This : in out Object); |
100 | |
101 | --- |
102 | -- Current reference counter. |
103 | -- |
104 | --: @param This Object itself. |
105 | overriding function Use_Count (This : in Object) return Natural with |
106 | Inline; |
107 | |
108 | private |
109 | |
110 | --- |
111 | -- Base class for Reference Counted Instances. |
112 | -- |
113 | --: @field RefCount Reference Counter. |
114 | type Object is new AdaCL.Limited_Base.Object and Object_Interface with record |
115 | RefCount : Natural := 0; |
116 | end record with |
117 | Put_Image => Object_Image; |
118 | |
119 | procedure Object_Image (Output : in out Ada.Strings.Text_Buffers.Root_Buffer_Type'Class; This : Object); |
120 | |
121 | --- |
122 | -- Remove a reference and if the use count reaches 0 the instance is deleted. |
123 | -- |
124 | --: @param This Object itself. |
125 | --: @return current use count |
126 | overriding function Use_Count (This : in Object) return Natural is (This.RefCount); |
127 | |
128 | end AdaCL.Pointer.Element; |
129 | |
130 | ---------------------------------------------------------------- {{{ ---------- |
131 | --: vim: set textwidth=0 nowrap tabstop=8 shiftwidth=3 softtabstop=3 expandtab : |
132 | --: vim: set filetype=ada fileencoding=utf-8 fileformat=unix foldmethod=expr : |
133 | --: vim: set spell spelllang=en_gb : |