comp.lang.ada
 help / color / mirror / Atom feed
From: john@assen.demon.co.uk.nospam (John McCabe)
Subject: Re: Searching for an object
Date: Thu, 31 Aug 2000 21:55:15 GMT
Date: 2000-08-31T21:55:15+00:00	[thread overview]
Message-ID: <39aed239.14592091@news.demon.co.uk> (raw)
In-Reply-To: 87og2q9yyp.fsf@moon.mteege.de

Matthias Teege <matthias@mteege.de> wrote:

>I've defined the following type: 
>
>type customer is record
>   aname  : String( 1..25);
>   bname  : String( 1..25);
>end record;
>
>Then I create some objects of this type and save all of
>them in an table.
>
>Now I want to find a special object with a find procedure
>like this (not authentic Ada Code :-)):
>
>procedure find ( Field : in String;
>                 What  : in String;
>                 Result: out String) is
>
>while end_of_table is false loop
>  read(object);
>  if object.Field = What then -- field should be 'aname'
>                              -- or 'bname'
>     Result := object.Field;
>     exit; -- We have found it
>  end if
>end loop;
>end find;
>
>I want to search in all Record "Fields" depend on the
>procedure parameter.
>
>How is the Ada way?

If I understand the requirement correctly, a way to do it would be to
define:

   type Comparator_Type is access function (Left  : Customer;
                                            Right : String)
                                            return Boolean;

And define

   procedure find (What : in string;
                   Comparator : in Comparator_Type;
                   Result     : out String) is
     function "=" renames Comparator.all;
   begin
     ...
     if Object = What then
     ...
   end find;

So in a client of this package you would have:

   procedure client is
      function Match_Afield (Left : other_package.customer;
                             Right : string)
                             return boolean is
      begin
         return Left.afield = right;
      end Match_Afield;

      function Match_Bfield (Left : other_package.customer;
                             Right : string)
                             return boolean is
      begin
         return Left.bfield = right;
      end Match_Bfield;
   begin

      other_package.find (what => "blah",
                          comparator => Match_Afield'access,
                          result => local_result);
   end client;

Or something like that. This would all need to be modified and syntax
checked etc. You could also make the find function a generic that is
instantiated with an "=" operation.

John

Best Regards
John McCabe <john@assen.demon.co.uk>



      parent reply	other threads:[~2000-08-31 21:55 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-08-18  0:00 Searching for an object Matthias Teege
2000-08-21  0:00 ` Ted Dennison
2000-08-21  0:00   ` Matthias Teege
2000-08-21  0:00     ` tmoran
2000-08-22  0:00       ` Matthias Teege
2000-08-22  0:00         ` tmoran
2000-08-21  0:00     ` Ted Dennison
2000-08-21  0:00 ` Gerald Kasner
2000-08-21  0:00   ` Matthias Teege
2000-08-31 21:55 ` John McCabe [this message]
replies disabled

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