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=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: "Jeffrey R. Carter" Newsgroups: comp.lang.ada Subject: Re: Good/best way to enforce library-level instantiation a generic package Date: Tue, 17 Mar 2020 12:21:44 +0100 Organization: Also freenews.netfront.net; news.tornevall.net; news.eternal-september.org Message-ID: References: <6bcc6133-8c8b-4252-a8e6-fd64a5eec2ca@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Tue, 17 Mar 2020 11:21:44 -0000 (UTC) Injection-Info: reader02.eternal-september.org; posting-host="5b2cc3fa5eedcfb9aea1b423f85ebb68"; logging-data="1314"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18nP9LLRe/NBxgR4TJJf4DT9LZU+WSewCE=" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.4.1 Cancel-Lock: sha1:6iyo0oxA4G1b9ZmIOTtP/DAPkgE= In-Reply-To: Content-Language: en-US Xref: reader01.eternal-september.org comp.lang.ada:58206 Date: 2020-03-17T12:21:44+01:00 List-Id: On 3/17/20 2:21 AM, Randy Brukardt wrote: > "Vincent Marciante" wrote in message > news:6bcc6133-8c8b-4252-a8e6-fd64a5eec2ca@googlegroups.com... >> I made a generic package that I want only to be instantiated at library >> level. >> I'm working on compile-time a way to enforce that desire which involves >> access >> type accessibility level checking but have not yet set it up correctly. >> Is there a better/standard way? > > For Ada 95, deriving from Controlled does the trick, but that was eliminated > (at substantial cost) in Ada 2005 and later. > > I suppose you could use type String_Access (which is a library-level access > type) for this: > > with Ada.Strings.Unbounded; > generic > ... > package My_Generic is > -- Real stuff here. > > Library-Level : constant aliased String := "Library-Level"; > Check : Ada.Strings.Unbounded.String_Access := Library_Level'Access; > -- 'Access is illegal if My_Generic is not instantiated at the > library level. > end My_Generic; Check should be constant, too. For the OP: You can also use Ada.Tags.Expanded_Name on a tagged type declared in the generic: type T is abstract tagged null record; Name : constant String := Ada.Tags.Expanded_Name (T); pragma Assert (Ada.Strings.Fixed.Index (Name (Name'First .. Name'Last - 2), ".") = 0, "Generic_Name must be instantiated at library level"); -- Jeff Carter "Gentlemen, you can't fight in here. This is the War Room!" Dr. Strangelove 30