From: matthew_heaney@acm.org (Matthew Heaney)
Subject: Re: Are global or persistent variables in ADA?
Date: 1998/05/01
Date: 1998-05-01T00:00:00+00:00 [thread overview]
Message-ID: <matthew_heaney-ya023680000105980844230001@news.ni.net> (raw)
In-Reply-To: 35420cc3.0@news.pacifier.com
In article <35420cc3.0@news.pacifier.com>, "Steve Doiel"
<nospam_steved@pacifier.com> wrote:
(start of quote)
Fully global variables are
declared inside of a package spec (IMHO not a good practice).
(end of quote)
One idiom where "global" variables do make sense is in the construction of
a subsystem, a complex abstraction that comprises several packages, ie
package P is
<this is the public interface of the abstraction>
end P;
private package P.Q is
O : T;
--
-- O is a "global" variable, shared by children of P. However, it is only
-- global with the subsystem rooted at P.
end P.Q;
with P.Q;
package body P.R is
... Q.O ...
end P.R;
This is a handy technique to use when you have data shared among many
modules in a subsystem; perhaps a semaphore or something.
Another example of having a global data, not even in a private package,
would be a storage pool object. You may decide that all access types in an
application will use a non-default storage pool that you've defined, ie
package Storage_Pools is
type XYZ_Storage_Pool is
new Root_Storage_Pool with ...;
Pool : Storage_Pool;
end Storage_Pools;
As a matter a fact, I did something like this in some B-trees I just wrote.
One technique I like to use is to pass in a storage pool object to a
dynamic memory manager abstraction, ie
with System.Storage_Pools;
generic
System_Storage_Pool : in out System.Storage_Pools.Root_Storage_Pool'Class;
package ACL.Trees.Storage.BPlus_Tree.Dynamic_G is
pragma Elaborate_Body;
type Dynamic_Storage_Pool is
new Root_Storage_Pool with private;
...
package body ACL.Trees.Storage.BPlus_Tree.Dynamic_G is
type Dynamic_Page_Access is
access all Page_Base;
for Dynamic_Page_Access'Storage_Pool use System_Storage_Pool;
And I instantiate it as follows:
with ACL.Trees.Storage.BPlus_Tree.Dynamic_G;
package BPlus.Storage.Dynamic is
new BPlus.Storage.Dynamic_G (BPlus.Storage.Page_Access'Storage_Pool);
where I simply use the pool associated with another, already-existing
access type.
Actually, I do declare a global storage pool object, that I use to
instantiate the B-tree iteself;
with BPlus.Storage.Dynamic; use BPlus.Storage.Dynamic;
package BPlus.Storage_Pools is
Pool : Dynamic_Storage_Pool;
end;
with Ada.Finalization;
with ACL.Trees.Storage.BPlus_Tree;
generic
type B_Tree_Item is private;
with function "=" (L, R : B_Tree_Item) return Boolean is <>;
...
with package Storage is
new ACL.Trees.Storage.BPlus_Tree
(B_Tree_Item,
B_Tree_Item_Array,
B_Tree_Key,
B_Tree_Key_Array,
Index_Order,
Sequence_Order);
type B_Tree_Storage_Pool is
new Storage.Root_Storage_Pool with private;
Storage_Pool : in out B_Tree_Storage_Pool; -- pass in storage pool object
...
with ACL.Trees.BPlus_Trees;
with BPlus.Searches; use BPlus.Searches;
with BPlus.Storage.Dynamic;
with BPlus.Storage_Pools;
package BPlus.Trees is
new ACL.Trees.BPlus_Trees
(B_Tree_Item => Integer,
B_Tree_Item_Array => Searches.B_Tree_Item_Array,
B_Tree_Key => Integer,
B_Tree_Key_Array => Searches.B_Tree_Key_Array,
Index_Order => Index_Order,
Sequence_Order => Sequence_Order,
Storage => BPlus.Storage,
B_Tree_Storage_Pool => BPlus.Storage.Dynamic.Dynamic_Storage_Pool,
Storage_Pool => Storage_Pools.Pool, -- there it is...
Get_Key => BPlus.Get_Key);
So, from global data isn't necessarily bad, and is often required for
declaring static, system-wide data.
next prev parent reply other threads:[~1998-05-01 0:00 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
1998-04-25 0:00 Are global or persistent variables in ADA? Brian Franklin
1998-04-25 0:00 ` Brian Franklin
1998-04-25 0:00 ` Tom Moran
1998-04-25 0:00 ` Steve Doiel
1998-05-01 0:00 ` Matthew Heaney [this message]
1998-04-29 0:00 ` Alan E & Carmel J Brain
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox