comp.lang.ada
 help / color / mirror / Atom feed
From: adam@irvine.com (Adam Beneschan)
Subject: Re: Flags and Ada
Date: 1997/02/07
Date: 1997-02-07T00:00:00+00:00	[thread overview]
Message-ID: <5dgd14$mo@krusty.irvine.com> (raw)
In-Reply-To: 5dbqaj$uq8$1@A-abe.resnet.ucsb.edu


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




           reply	other threads:[~1997-02-07  0:00 UTC|newest]

Thread overview: expand[flat|nested]  mbox.gz  Atom feed
 [parent not found: <5dbqaj$uq8$1@A-abe.resnet.ucsb.edu>]
replies disabled

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