* Good/best way to enforce library-level instantiation a generic package
@ 2020-03-16 18:51 Vincent Marciante
2020-03-17 1:21 ` Randy Brukardt
2020-03-17 6:29 ` briot.emmanuel
0 siblings, 2 replies; 10+ messages in thread
From: Vincent Marciante @ 2020-03-16 18:51 UTC (permalink / raw)
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?
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-16 18:51 Good/best way to enforce library-level instantiation a generic package Vincent Marciante
@ 2020-03-17 1:21 ` Randy Brukardt
2020-03-17 10:11 ` Vincent Marciante
2020-03-17 11:21 ` Jeffrey R. Carter
2020-03-17 6:29 ` briot.emmanuel
1 sibling, 2 replies; 10+ messages in thread
From: Randy Brukardt @ 2020-03-17 1:21 UTC (permalink / raw)
"Vincent Marciante" <vincent.marciante@l3harris.com> 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;
String_Access is a silly type that isn't used in the spec of
Ada.Strings.Unbounded, and thus shouldn't be there, but it does work for
this use. :-)
You can of course use any library-level access type in your program for this
purpose; I picked this one 'cause it is already sitting around.
Hope this helps.
Randy.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-16 18:51 Good/best way to enforce library-level instantiation a generic package Vincent Marciante
2020-03-17 1:21 ` Randy Brukardt
@ 2020-03-17 6:29 ` briot.emmanuel
2020-03-17 10:15 ` Vincent Marciante
1 sibling, 1 reply; 10+ messages in thread
From: briot.emmanuel @ 2020-03-17 6:29 UTC (permalink / raw)
On Monday, March 16, 2020 at 7:51:29 PM UTC+1, Vincent Marciante wrote:
> 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?
The way I do this is using gnat-specific pragmas and attributes:
generic
package Generics is
pragma Compile_Time_Error
(not Generics'Library_Level, "must be at library level");
...
end Generics;
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-17 1:21 ` Randy Brukardt
@ 2020-03-17 10:11 ` Vincent Marciante
2020-03-17 11:21 ` Jeffrey R. Carter
1 sibling, 0 replies; 10+ messages in thread
From: Vincent Marciante @ 2020-03-17 10:11 UTC (permalink / raw)
On Monday, March 16, 2020 at 9:21:18 PM UTC-4, 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?
<snip>
> You can of course use any library-level access type in your program for this
> purpose; I picked this one 'cause it is already sitting around.
>
> Hope this helps.
>
> Randy.
Thanks for the suggestion. It was along that lines of what I was doing
and it hepled.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-17 6:29 ` briot.emmanuel
@ 2020-03-17 10:15 ` Vincent Marciante
0 siblings, 0 replies; 10+ messages in thread
From: Vincent Marciante @ 2020-03-17 10:15 UTC (permalink / raw)
On Tuesday, March 17, 2020 at 2:29:58 AM UTC-4, briot....@gmail.com wrote:
> On Monday, March 16, 2020 at 7:51:29 PM UTC+1, Vincent Marciante wrote:
> > 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?
>
> The way I do this is using gnat-specific pragmas and attributes:
>
> generic
> package Generics is
> pragma Compile_Time_Error
> (not Generics'Library_Level, "must be at library level");
> ...
> end Generics;
'Library_Level is nice and clean! It should be part of the Standard!
I am using GNAT but still have to be compatible with other compiler so
will have to go with something along the lines of Randy's suggestion.
Thanks.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-17 1:21 ` Randy Brukardt
2020-03-17 10:11 ` Vincent Marciante
@ 2020-03-17 11:21 ` Jeffrey R. Carter
2020-03-18 1:03 ` Randy Brukardt
1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey R. Carter @ 2020-03-17 11:21 UTC (permalink / raw)
On 3/17/20 2:21 AM, Randy Brukardt wrote:
> "Vincent Marciante" <vincent.marciante@l3harris.com> 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
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-17 11:21 ` Jeffrey R. Carter
@ 2020-03-18 1:03 ` Randy Brukardt
2020-03-18 9:23 ` Jeffrey R. Carter
2020-03-18 10:27 ` Vincent Marciante
0 siblings, 2 replies; 10+ messages in thread
From: Randy Brukardt @ 2020-03-18 1:03 UTC (permalink / raw)
"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message
news:r4qbs8$192$1@dont-email.me...
...
> 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");
That would reject an instance in a library-level child package, which
probably isn't what the OP wants.
Randy.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-18 1:03 ` Randy Brukardt
@ 2020-03-18 9:23 ` Jeffrey R. Carter
2020-03-20 20:37 ` Randy Brukardt
2020-03-18 10:27 ` Vincent Marciante
1 sibling, 1 reply; 10+ messages in thread
From: Jeffrey R. Carter @ 2020-03-18 9:23 UTC (permalink / raw)
On 3/18/20 2:03 AM, Randy Brukardt wrote:
> "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message
> news:r4qbs8$192$1@dont-email.me...
>>
>> 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");
>
> That would reject an instance in a library-level child package, which
> probably isn't what the OP wants.
An instance IN a child pkg is not a library-level instance, so the OP would want
to reject it. But an instance AS a library-level child pkg would be rejected, so
this doesn't work.
AFAICT, a child pkg can only be a library-level pkg, so "library-level child
pkg" seems like overqualification.
--
Jeff Carter
"My mind is aglow with whirling, transient nodes of
thought, careening through a cosmic vapor of invention."
Blazing Saddles
85
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-18 1:03 ` Randy Brukardt
2020-03-18 9:23 ` Jeffrey R. Carter
@ 2020-03-18 10:27 ` Vincent Marciante
1 sibling, 0 replies; 10+ messages in thread
From: Vincent Marciante @ 2020-03-18 10:27 UTC (permalink / raw)
On Tuesday, March 17, 2020 at 9:03:51 PM UTC-4, Randy Brukardt wrote:
> That would reject an instance in a library-level child package, which
> probably isn't what the OP wants.
>
> Randy.
Correct: I would want to instantiate into a library-level package, including a child. I just want to avoid repeated execution of elaboration code of an instance and have any such arrangement that would cause that be at least flagged/warned at compile-time.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: Good/best way to enforce library-level instantiation a generic package
2020-03-18 9:23 ` Jeffrey R. Carter
@ 2020-03-20 20:37 ` Randy Brukardt
0 siblings, 0 replies; 10+ messages in thread
From: Randy Brukardt @ 2020-03-20 20:37 UTC (permalink / raw)
"Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message
news:r4sp9u$m59$1@dont-email.me...
> On 3/18/20 2:03 AM, Randy Brukardt wrote:
>> "Jeffrey R. Carter" <spam.jrcarter.not@spam.not.acm.org> wrote in message
>> news:r4qbs8$192$1@dont-email.me...
>>>
>>> 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");
>>
>> That would reject an instance in a library-level child package, which
>> probably isn't what the OP wants.
>
> An instance IN a child pkg is not a library-level instance, so the OP
> would want to reject it. But an instance AS a library-level child pkg
> would be rejected, so this doesn't work.
??? Any declaration in a library-level *package* is a library-level
declaration. That's not true for other kinds units, of course. 3.10.2(22)
explains that "library-level" is determined by the accessibility of a
declaration, and a declaration in a package has the same accessibility as
the package.
Randy.
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2020-03-20 20:37 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-16 18:51 Good/best way to enforce library-level instantiation a generic package Vincent Marciante
2020-03-17 1:21 ` Randy Brukardt
2020-03-17 10:11 ` Vincent Marciante
2020-03-17 11:21 ` Jeffrey R. Carter
2020-03-18 1:03 ` Randy Brukardt
2020-03-18 9:23 ` Jeffrey R. Carter
2020-03-20 20:37 ` Randy Brukardt
2020-03-18 10:27 ` Vincent Marciante
2020-03-17 6:29 ` briot.emmanuel
2020-03-17 10:15 ` Vincent Marciante
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox