comp.lang.ada
 help / color / mirror / Atom feed
From: emery@goldfinger.mitre.org (David Emery)
Subject: Re: Flags in Ada?
Date: 26 Oct 94 09:59:53
Date: 1994-10-26T09:59:53+00:00	[thread overview]
Message-ID: <EMERY.94Oct26095953@goldfinger.mitre.org> (raw)
In-Reply-To: tmoran@bix.com's message of 26 Oct 1994 03:19:57 GMT

Most of the techniques that have been proposed for this use packed
boolean and representation specs within the visibile part of the
package spec.  My experience is that this can be a dangerous approach,
particularly for portability.

The way we handle this in POSIX/Ada follows.  We tried to produce an
interface that could support a variety of implementation strategies,
but required no changes to using code.  

 package POSIX is 
  ...
    type Option_Set is private;
    function Empty_Set 
        return Option_Set;
    function "+" (L, R : Option_Set)
        return Option_Set;
    function "-" (L, R : Option_Set)
        return Option_Set;
  ...
 end POSIX;

 with POSIX,
      POSIX_Permissions;
 package POSIX_IO is
  ...
    type Open_Option_Set is new POSIX.Option_Set;
      -- Empty_Set, "+" and "-" are derived operations

    Non_Blocking 		: constant Open_Option_Set
				:= <implementation defined>;
    Append 			: constant Open_Option_Set
				:= <implementation defined>;
    Truncate 			: constant Open_Option_Set
				:= <implementation defined>;
    Exclusive 			: constant Open_Option_Set
				:= <implementation defined>;
    Not_Controlling_Terminal 	: constant Open_Option_Set
				:= <implementation defined>;
  ...
 end POSIX_IO;

Note the Ada9X-style use of derived types for 'inheritence'.  The type
Option_Set has no defined members (it's an 'abstract type').  

To define a particular set of options, such as the options used for
Open(), you derive a new type from POSIX.Option_Set, and then define
appropriate constants of that type.  

In Ada83, the definition of the option constants is a bit klugey, and
can involve unchecked_conversion.  Ada9X provides substantially better
features for this.  

In my prototype Ada83 implementation, I have the following:

 package POSIX is
  ...
 private
    type Option_Set is array (0..31) of boolean;
    pragma pack (Option_Set);
    for Option_Set'size use integer'size;
 end POSIX;

 with unchecked_conversion, POSIX;
 package option_munging is
   function to_option_set is new unchecked_conversion
		(integer, POSIX.option_set);
   function to_integer is new unchecked_conversion 
		(POSIX.option_set, integer);
 end option_munging;

 with POSIX,
      POSIX_Permissions;
 with option_munging;	-- implementation dependency
 package POSIX_IO is
  ...
    type Open_Option_Set is new POSIX.Option_Set;
      -- Empty_Set, "+" and "-" are derived operations

    Non_Blocking 		: constant Open_Option_Set
				:= Open_Option_Set
				    (option_munging.to_option_set (16#4000#));
    Append 			: constant Open_Option_Set
				:= Open_Option_Set
				    (option_munging.to_option_set (16#0008#));
    Truncate 			: constant Open_Option_Set
				:= Open_Option_Set
				    (option_munging.to_option_set (16#0400#));
    Exclusive 			: constant Open_Option_Set
				:= Open_Option_Set
				    (option_munging.to_option_set (16#0800#));
    Not_Controlling_Terminal 	: constant Open_Option_Set
				:= Open_Option_Set
				    (option_munging.to_option_set (16#8000#));
  ...
 end POSIX_IO;

In another implementation approach, I've completed the type Option_Set
as an integer, and changed the Option_Munging declaration to match.  

In Ada9X, I can eliminate the Option_Munging package altogether with a
'classic' use of child packages:

 package POSIX.Option_Values is
    Value_0 : constant Option_Set := (0 => true, others => false);
    Value_1 : constant Option_Set := (1 => true, others => false);
  ...
 end POSIX.Option_Values;

Then package POSIX_IO becomes

 with POSIX,
      POSIX_Permissions;
 with POSIX.Option_Values;
 package POSIX_IO is
  ...
    type Open_Option_Set is new POSIX.Option_Set;
      -- Empty_Set, "+" and "-" are derived operations

    Non_Blocking 		: constant Open_Option_Set
				:= Open_Option_Set
					(POSIX.Option_Values.Value_30);
    Append 			: constant Open_Option_Set
				:= Open_Option_Set
					(POSIX.Option_Values.value_8);
  ...
 end POSIX_IO;

Notice the type conversion from POSIX.Option_Set to
POSIX_IO.Open_Option_Set.  This is needed because the constants are
not 'inherited operations'.  

I've used this technique in other Ada bindings, and it works very
well.  

				dave
--
--The preceeding opinions do not necessarily reflect the opinions of
--The MITRE Corporation or its sponsors. 
-- "A good plan violently executed -NOW- is better than a perfect plan
--  next week"                                      George Patton
-- "Any damn fool can write a plan.  It's the execution that gets you
--  all screwed up"                              James Hollingsworth
-------------------------------------------------------------------------



  reply	other threads:[~1994-10-26  9:59 UTC|newest]

Thread overview: 33+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-10-25 10:36 Flags in Ada? Andre Spiegel
1994-10-25 10:07 ` David Emery
1994-10-25 16:19 ` Norman H. Cohen
1994-10-26  3:19   ` tmoran
1994-10-26  9:59     ` David Emery [this message]
1994-10-26 22:32       ` Robert Dewar
1994-10-27 13:24         ` Norman H. Cohen
1994-10-27 15:15         ` John Volan
1994-10-31  9:29         ` David Emery
1994-10-27 22:34       ` Henry G. Baker
1994-10-26 14:33     ` Robert Dewar
1994-10-26 17:43     ` Norman H. Cohen
1994-10-26 15:54   ` Andre Spiegel
1994-10-26  0:36 ` Dale Stanbrough
1994-10-26 11:01   ` Robert Dewar
1994-10-27  8:23 ` Henri Altarac
1994-10-27 23:00   ` Robert Dewar
1994-10-31  9:32     ` David Emery
  -- strict thread matches above, loose matches on Subject: below --
1994-10-25 16:22 tmoran
1994-10-27  5:05 tmoran
1994-10-27 13:29 ` Robert Dewar
1994-10-27 17:15 ` Norman H. Cohen
1994-10-28  3:51   ` Robert Dewar
1994-10-27  5:06 tmoran
1994-10-27 13:47 ` Robert Dewar
1994-10-28  2:41   ` Tucker Taft
1994-10-30 13:31     ` Robert Dewar
1994-10-28  3:59 tmoran
1994-10-28 13:43 ` Robert Dewar
1994-10-31 14:19   ` Norman H. Cohen
1994-11-02 14:06     ` Mats Weber
1994-11-03 23:08       ` Robert Dewar
1994-11-03 11:26     ` Robert Dewar
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox