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-28 01:42:13 PST Message-ID: <3F769EDE.D9D4744F@fakeaddress.nil> Date: Sun, 28 Sep 2003 10:42:06 +0200 From: Gautier Write-only X-Mailer: Mozilla 4.8 [en] (Windows NT 5.0; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Re: How to Emulate C++ Macro with Static Local Variable? References: <17cd177c.0309250354.77444ccd@posting.google.com> <5917f4d0.0309251006.29cd9421@posting.google.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 80.218.95.85 X-Trace: news.swissonline.ch 1064738532 80.218.95.85 (28 Sep 2003 10:42:12 +0200) X-Complaints-To: abuse@swissonline.ch Path: news1.google.com!newsfeed.stanford.edu!newsmi-us.news.garr.it!NewsITBone-GARR!news.mailgate.org!news-zh.switch.ch!switch.ch!news.swissonline.ch!not-for-mail Xref: news1.google.com comp.lang.ada:63 Date: 2003-09-28T10:42:06+02:00 List-Id: Tad Ashlock: > 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.) Since the cache is defined inside of "Test_Static_in_generic" it is effectively lost when at exit. If you define it outside of it (e.g. in a package at the same level as Test_Static_in_generic) it will remain. If Test_Static_in_generic is at "level 0" it could look like this: --8<----8<----8<----8<----8<----8<-- package BARs is type S is array(0..7) of Integer; generic foo: Integer; package P is procedure BAR; end; end; with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; package body BARs is package body P is initialized: Boolean:= False; cache: S; procedure BAR is begin if not initialized then cache:= (others=> foo); New_Line; Put_Line("[Filling cache for instance]" & Integer'image(foo)); initialized:= True; end if; Put(cache(cache'last)); end; end; end BARs; with BARs; pragma Elaborate_all(BARs); package Inst_BARs is package P10 is new BARs.P(10); package P20 is new BARs.P(20); package P30 is new BARs.P(30); end; with Ada.Text_IO, Ada.Integer_Text_IO; use Ada.Text_IO, Ada.Integer_Text_IO; with Inst_Bars; use Inst_Bars; procedure Test_Static_in_generic_2 is begin Put_Line("Starting..."); 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; with Test_Static_in_generic_2; procedure Test_Static_in_generic_3 is begin for i in 1..3 loop Test_Static_in_generic_2; end loop; end; --8<----8<----8<----8<----8<----8<-- But you also could avoid these instances (as translation of expanded macros) by making an array of caches, according to the efficiency tradeoff "place taken by the machine code of instances (and RAM caching!)" vs. "access to 1 cache via the array (once per call)". To be tested... HTH ________________________________________________________ Gautier -- http://www.mysunrise.ch/users/gdm/gsoft.htm NB: For a direct answer, e-mail address on the Web site!