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=0.2 required=5.0 tests=BAYES_00,INVALID_MSGID, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,f4e1ae22e5bbdd9f X-Google-Attributes: gid103376,public From: Stephen Leake Subject: Re: Generic in out object parameters Date: 1997/04/17 Message-ID: <33561FCC.C38@gsfc.nasa.gov>#1/1 X-Deja-AN: 235461617 References: Organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA Reply-To: Stephen.Leake@gsfc.nasa.gov Newsgroups: comp.lang.ada Date: 1997-04-17T00:00:00+00:00 List-Id: petera@ece.uc.edu wrote: > > A generic unit may have an generic object parameter of mode in out. I understand that this acts as a renaming of the actual parameter supplied when the unit is instantiated. > > Can someone give me a good motivating example of why this is useful? > Here's a layer I use over Ada.Command_Lines: -- -- Purpose : -- Provide generic routines for reading parameters from the command -- line. -- -- Design : -- Each Get_* procedure reads a particular parameter type from -- Command_Line.Argument(Next_Arg) and then increments Next_Arg by -- one. PARAMETER_ERROR is raised for *any* error, after putting an -- appropriate message to Standard_Error. -- with Ada.Strings.Unbounded; package Command_Line_IO is PARAMETER_ERROR : exception; generic Default_Expecting : in STRING; Next_Arg : in out NATURAL; -- index of next argument to read procedure Get_String (Item : out Ada.Strings.Unbounded.Unbounded_String; Expecting : in String := Default_Expecting); generic type Discrete_Type is (<>); Default_Expecting : in STRING; Next_Arg : in out NATURAL; -- index of next argument to read procedure Get_Discrete (Item : out Discrete_Type; Expecting : in STRING := Default_Expecting); generic type Float_Type is digits <>; Default_Expecting : in STRING; Next_Arg : in out NATURAL; -- index of next argument to read procedure Get_Float (Item : out Float_Type; Expecting : in STRING := Default_Expecting); end Command_Line_IO; Next_Arg is a global for each Get; this makes the final code read eaiser: procedure Get_Options is Next_Arg : POSITIVE := 1; -- next argument to process procedure Get is new Command_Line_IO.Get_Discrete (Com_Port_ID_Type, "Com port id", Next_Arg); procedure Get is new Command_Line_IO.Get_Discrete (Interfaces.C.Unsigned_Long, "address", Next_Arg); procedure Get is new Command_Line_IO.Get_String ("String", Next_Arg); use type Ada.Strings.Unbounded.Unbounded_String; begin -- -- Process options. -- loop exit when (Ada.Command_Line.Argument_Count < Next_Arg); if Ada.Command_Line.Argument (Next_Arg) = "--help" or Ada.Command_Line.Argument (Next_Arg) = "-?" then Put_Usage; raise Fatal_Error; elsif Ada.Command_Line.Argument (Next_Arg) = "--com_port" or Ada.Command_Line.Argument (Next_Arg) = "-p" then Next_Arg := Next_Arg + 1; Get (Com_Port_ID); elsif Ada.Command_Line.Argument (Next_Arg) = "--verbose" or Ada.Command_Line.Argument (Next_Arg) = "-v" then Next_Arg := Next_Arg + 1; Verbose := True; else -- should be root file name Get (Root_File_Name, "root file name"); end if; end loop; end Get_Options; Clearly, I could have passed Next_Arg as a parameter to each procedure call, rather than to each instantiation. However, it is somewhat clearer this way that there must be exactly one Next_Arg, shared by all the Get procedures. Since I did not overload Ada.Command_Line.Argument, I admit the abstraction is muddy. I don't think I've used a generic "in out" parameter any where else. -- - Stephe