From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=0.1 required=5.0 tests=BAYES_00,FREEMAIL_FROM, PDS_OTHER_BAD_TLD autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,c6e5a27ff403a94d X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-12-24 13:57:06 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!wn11feed!worldnet.att.net!204.127.198.203!attbi_feed3!attbi.com!sccrnsc01.POSTED!not-for-mail From: Stapler Subject: Re: Package instantiation and automatic de-allocation. Newsgroups: comp.lang.ada References: User-Agent: Pan/0.11.2 (Unix) Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Comment-To: "Stapler" Message-ID: NNTP-Posting-Host: 12.241.145.39 X-Complaints-To: abuse@attbi.com X-Trace: sccrnsc01 1040767025 12.241.145.39 (Tue, 24 Dec 2002 21:57:05 GMT) NNTP-Posting-Date: Tue, 24 Dec 2002 21:57:05 GMT Organization: AT&T Broadband Date: Tue, 24 Dec 2002 21:57:05 GMT Xref: archiver1.google.com comp.lang.ada:32286 Date: 2002-12-24T21:57:05+00:00 List-Id: Alright, I've made some progress. All I want to do is declare a single instance of a generic stack within a declarative block(whether a procedure or inlined declare block). As a newbie, I probably overlooked some things.(As readers of my previous posts have probably already guessed.) ;-> I'm basically posting this up in hopes of getting feedback. To the Gurus among us, I'm posting under my own thread so as to allow your newsreader to scan past my hopeless, banal, novice ramblings. To those who find the stomach to look at it, thanks for not tossing your cookies. Heres what it looks like right now. generic Size : Positive; type Item is private; package gen_stack is Stack_Overflow : exception; Stack_Underflow : exception; procedure Push(X : in Item); procedure Pop(Y : out Item); function Is_Full return Boolean; function Is_Empty return Boolean; private subtype Stack_range is natural range 1..Size; type Item_array is array(positive range 1..Size) of Item; -- Make this a limited record? type Stack is record Elements : Item_Array; Top : Stack_range; Full, Empty : Boolean; end record; end gen_stack; package body gen_stack is Local : Stack; -- Using this variable to access the -- Stack in the Private part of the package. -- Assuming it should be visible to all procedures -- and functions in this package. -- Needs to be In/Out in procedure parameters? procedure Push(X : in Item) is separate; procedure Pop(Y : out Item) is separate; function Is_Full return Boolean is -- All this function does is exports the value -- in the full field of the record begin return Local.Full; -- A Boolean value end Is_Full; function Is_Empty return Boolean is -- Same as Is_Full, except exports Empty instead begin return Local.Empty; -- A Boolean value. end Is_Empty; end gen_stack; separate (gen_stack) procedure Pop(Y : out Item) is begin -- If the user attempts to Pop an Item off an -- empty stack, then raise Stack_Underflow -- Check using the public Is_Empty function; if Local.Empty = True then raise Stack_Underflow; end if; -- It seems like it shouldn't be necessary to -- set the Empty/Full flags explicitly. Local.Empty := False; Y := Local.Elements(Local.Top); if (Local.Top - 1) = 0 then Local.Empty := True; else -- Assuming my logic is correct -- If the stack is already on it's last -- element, the code should not reach here. Local.Top := Local.Top - 1; end if; end Pop; separate (gen_stack) procedure Push(X : in Item) is procedure Insert(X : in Item) is begin Local.Elements(Local.Top) := X; end Insert; begin -- If the user failed to check and see if the Stack is -- full via the Is_Full function, and the Stack is full -- then raise Stack_Overflow if Local.Full = True then raise Stack_Overflow; end if; -- If the Code reaches here, then the Full flag wasnt -- set to True, but set it explicitly anyways for safety. -- Is this needed? Local.Full := False; -- If there is one remaining element then -- Set the Full flag to true and Insert -- the Item into the remaining Element -- i.e. Top and Size should both be indexing -- the same element at the start of this -- "If" function. if Local.Top = Size then Local.Full := True; Insert(X); else if Local.Top < Size then Insert(X); Local.Top := Local.Top + 1; end if; end if; end Push;