1--  Copyright 2006-2014 Simon Wright <simon@pushface.org>
2
3--  This package is free software; you can redistribute it and/or
4--  modify it under terms of the GNU General Public License as
5--  published by the Free Software Foundation; either version 2, or
6--  (at your option) any later version. This package is distributed in
7--  the hope that it will be useful, but WITHOUT ANY WARRANTY; without
8--  even the implied warranty of MERCHANTABILITY or FITNESS FOR A
9--  PARTICULAR PURPOSE. See the GNU General Public License for more
10--  details. You should have received a copy of the GNU General Public
11--  License distributed with this package; see file COPYING.  If not,
12--  write to the Free Software Foundation, 59 Temple Place - Suite
13--  330, Boston, MA 02111-1307, USA.
14
15--  As a special exception, if other files instantiate generics from
16--  this unit, or you link this unit with other files to produce an
17--  executable, this unit does not by itself cause the resulting
18--  executable to be covered by the GNU General Public License.  This
19--  exception does not however invalidate any other reasons why the
20--  executable file might be covered by the GNU Public License.
21
22with Ada.Finalization;
23
24generic
25   type T (<>) is limited private;
26   type P is access T;
27package BC.Support.Auto_Pointers is
28
29   pragma Preelaborate;
30
31   type Pointer is private;
32   --  A Pointer variable encapsulates an accessor of type P (to a T).
33   --
34   --  When a Pointer is assigned, ownership of the accessor and of
35   --  the T it designates is transferred.
36   --
37   --  When a Pointer that owns an accessor is finalized, the accessor
38   --  is deallocated.
39
40   function Create (Value : P) return Pointer;
41   --  Returns a new encapsulation. You must NOT deallocate the Value
42   --  passed; it will be deallocated when an owning Pointer is
43   --  finalized.
44
45   function Value (Ptr : Pointer) return P;
46   pragma Inline (Value);
47   --  Returns the encapsulated pointer, which will be null if
48   --  ownership has been assigned away.
49
50private
51
52   type Owner;
53   type Owner_P is access all Owner;
54
55   type Object is record
56      The_Owner : Owner_P;
57      The_P : P;
58   end record;
59   type Object_P is access Object;
60
61   type Owner is record
62      The_Object : Object_P;
63   end record;
64
65   type Pointer is new Ada.Finalization.Controlled with record
66      The_Owner : aliased Owner;
67   end record;
68
69   procedure Adjust (Obj : in out Pointer);
70   procedure Finalize (Obj : in out Pointer);
71
72end BC.Support.Auto_Pointers;
73