comp.lang.ada
 help / color / mirror / Atom feed
* Re: Flags and Ada
       [not found] <5dbqaj$uq8$1@A-abe.resnet.ucsb.edu>
@ 1997-02-07  0:00 ` Adam Beneschan
  0 siblings, 0 replies; only message in thread
From: Adam Beneschan @ 1997-02-07  0:00 UTC (permalink / raw)



Graham Hughes <graham.hughes@resnet.ucsb.edu> writes:

 >As a getting-used-to-Ada project, I'm trying to translate a regular
 >expression library from C.  I'm wondering what the idiomatic Ada way to
 >say some of the things the C library uses; specifically, it does things
 >like
 >
 >	regcomp(rx, yadda yadda, RX_FOO | RX_BAR);
 >
 >How would I do this with Ada?  Should I just have a whole bunch of
 >booleans in the function prototype defaulting to whatever the default
 >should be?

In addition to solutions posted by others, here's one method I like.
It's not the most efficient, but I like the readability I get at the
calling side.

    type Regcomp_Option is (RX_FOO, RX_BAR, RX_BAZ, RX_SHEESH, 
                            RX_WHATEVER, ...)

    type Regcomp_Option_Array is array (natural range <>) of Regcomp_Option;

    No_Options : constant Regcomp_Option_Array := (1 .. 0 => RX_FOO);
        -- this is a zero-length array

    procedure Regcomp (...);
    procedure Regcomp (..., Option : in Regcomp_Option);
    procedure Regcomp (..., Options : in Regcomp_Option_Array);


    procedure Regcomp (...) is
    begin
        Regcomp (..., No_Options);         -- calls the array version
                                           -- of Regcomp
    end Regcomp;

    procedure Regcomp (..., Option : in Regcomp_Option) is
    begin
        Regcomp (..., (1 => Option));      -- calls the array version
                                           -- of Regcomp
    end Regcomp;

In the body of Regcomp, you'd probably have an array type like:

    type Regcomp_Option_Set is array (Regcomp_Option) of boolean;

and start with

    Option_Set : Regcomp_Option_Set;
    ...
    Option_Set := (others => false);
    for I in Options'range loop
        Option_Set (Options (I)) := true;
    end loop;

so that you can refer to Option_Set(RX_FOO), Option_Set(RX_BAR), etc.,
in the procedure body.

Then, when calling Regcomp, you can say something like

    Regcomp (Rx, Yadda_Yadda, (RX_FOO, RX_BAR));      -- array of options
    Regcomp (Rx, Yadda_Yadda, RX_BAZ);                -- just one option
    Regcomp (Rx, Yadda_Yadda);                        -- no options

                                -- Adam




^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~1997-02-07  0:00 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <5dbqaj$uq8$1@A-abe.resnet.ucsb.edu>
1997-02-07  0:00 ` Flags and Ada Adam Beneschan

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