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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,196864e6c216ca4f X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-09-25 11:07:00 PST Path: archiver1.google.com!postnews1.google.com!not-for-mail From: taashlo@sandia.gov (Tad Ashlock) Newsgroups: comp.lang.ada Subject: Re: How to Emulate C++ Macro with Static Local Variable? Date: 25 Sep 2003 11:06:44 -0700 Organization: http://groups.google.com/ Message-ID: <5917f4d0.0309251006.29cd9421@posting.google.com> References: <17cd177c.0309250354.77444ccd@posting.google.com> NNTP-Posting-Host: 134.253.26.6 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit X-Trace: posting.google.com 1064513219 29969 127.0.0.1 (25 Sep 2003 18:06:59 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: 25 Sep 2003 18:06:59 GMT Xref: archiver1.google.com comp.lang.ada:42916 Date: 2003-09-25T18:06:59+00:00 List-Id: gautier_niouzes@hotmail.com (Gautier) wrote in message news:<17cd177c.0309250354.77444ccd@posting.google.com>... > Something like that: > > with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; > > procedure Test_Static_in_generic is > > type S is array(0..7) of Integer; > > generic > foo: Integer; > package P is > procedure BAR; > end; > > package body P is > initialized: Boolean:= False; > cache: S; > procedure BAR is > begin > if not initialized then > cache:= (others=> foo); > initialized:= True; > end if; > Put(cache(cache'last)); > end; > end; > > package P10 is new P(10); > package P20 is new P(20); > package P30 is new P(30); > > begin > for count in 1..4 loop > for i in 1..3 loop > case i is > when 1=> P10.BAR; > when 2=> P20.BAR; > when 3=> P30.BAR; > end case; > end loop; > New_Line; > end loop; > end; What happens to cache when Test_Static_in_generic exits? In the C++ implementation, the next time Test_Static_in_generic is entered, the individual cache's still retain their previous contents. Is that the case here? (Sorry, but I'm not familar enough with Ada to be certain about how a generic package defined within a procedure behaves exactly.) > ? > > BTW: what this solution with pointers is about ?! BAR() is actually used for performing static state transitions within a hierarchical state machine. See and look at Q_TRAN() (which I renamed BAR() for my example). The first time a particular transition is made, the path to the target state must be discovered. But subsequent transitions can use the cached information. This is how the current C++ implementation works. As an extension, I'd like to eventually find a way of "pre-caching" the transition information during initialization. But for a first attempt, I would like my Ada95 port to behave, as much as possible, like the original C++. Thank you, Tad