comp.lang.ada
 help / color / mirror / Atom feed
From: "James S. Rogers" <jimmaureenrogers@worldnet.att.net>
Subject: Re: Package instantiation and automatic de-allocation.
Date: Mon, 23 Dec 2002 05:09:31 GMT
Date: 2002-12-23T05:09:31+00:00	[thread overview]
Message-ID: <fUwN9.9541$p_6.746832@bgtnsc04-news.ops.worldnet.att.net> (raw)
In-Reply-To: xuwN9.431253$P31.150740@rwcrnsc53

"Stapler" <spam.magnet@yahoo.com> wrote in message
news:xuwN9.431253$P31.150740@rwcrnsc53...
> Alright, I'm running into a bit of wall here.
>
> I can get my package to work, but I cant get it to work the way I want it
> to.
>
> Basically, as I understand it, if I declare something such as
>
> package foo is new generic_stack(Size, Integer);
>
> Then it wont be de-allocated when it goes out of scope(like at the end of
> a declare block) like it should be. This is assuming I've declared it in
> the declare section of the declare block.
>
> Further more I can't "use" generic_stack and then declare a variable such
> as
>
> My_Stack : Stack(Size, Integer);
>
> because language rules wont allow me to "use" a generic_package.
>
> I've tried all sorts of things...

Your package below does not work because it lacks a public
declaration for your Stack type. Once you have a name for the
type you must pass an instance of that type to each function and
procedure. Ada does not have an impicit "this" variable like C++ or
Java.

> with gen_stack;
>
> My_Stack : gen_stack.Stack(Size, Integer);
>
> My_Stack : new gen_stack.Stack(Size, Integer);
>
> My_Stack : Blah blah blah.
>
>
> Heres the package spec, any pointers would be helpful.
>
>
> generic
> Size : Positive;
> type Item is private;
>
> package gen_stack is
>
   type Stack is private;

> Stack_Overflow : exception;
> Stack_Underflow : exception;
>
 procedure Push( X : in Item : Onto : in out Stack);
function Pop(From : Stack) return Item;
function Is_Full(Item : Stack) return Boolean; -- These functions simply
export
function Is_Empty(Item : Stack) return Boolean;-- The Full, Empty fields of
the stack.
>
> private
>
> subtype Stack_range is positive range 1..Size;
> type Item_array is array(positive range 1..Size) of Item;
>
> type Stack is limited record
> Elements : Item_Array;
> Top : Stack_range;
> Full, Empty : Boolean;
> end record;
>
> end gen_stack;
>
>
> Basically I want to figure out how to instantiate this within a program
> without having to dick with Unchecked_Deallocation. i.e. I want to be
> able to assume that it will be de-allocated at then "end" of a declare
> block, whether a procedure or an inline "declare" region.

In the example above an instantiation does not allocate an object.
Creating an instance of the generic package is separate from
creating an instance of a stack object from an instantiated package.

>
> Currently I have to go ...
>
> package My_Stack is new gen_stack(Size, Foo_Type);

This is correct. It instantiates the package with a size of Size and
a type of Foo_Type. Next you want to create an instance of
the stack from that package:

My_Foo_Stack : My_Stack.Stack;

This will be deallocated when the block it is declared in goes
out of scope.

Jim Rogers





  reply	other threads:[~2002-12-23  5:09 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-12-23  4:42 Package instantiation and automatic de-allocation Stapler
2002-12-23  5:09 ` James S. Rogers [this message]
2002-12-23  6:22   ` tmoran
2002-12-24 21:57 ` Stapler
     [not found]   ` <j1cbua.fpc.ln@beastie.ix.netcom.com>
2002-12-25 23:56     ` Stapler
     [not found]       ` <amndua.f24.ln@beastie.ix.netcom.com>
2003-01-13 15:43         ` John English
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox