comp.lang.ada
 help / color / mirror / Atom feed
From: Lionel Draghi <lionel.draghi@gmail.com>
Subject: Re: Iterable container as generic parameter
Date: Sun, 28 Jan 2018 10:08:42 -0800 (PST)
Date: 2018-01-28T10:08:42-08:00	[thread overview]
Message-ID: <e51b188d-dab3-4c84-aa1e-319e4cf96179@googlegroups.com> (raw)
In-Reply-To: <3af9cfc4-d434-4d60-a4ed-17a16f75717f@googlegroups.com>

Le 26/01/2018 à 10:09, emmanuel... a écrit :
>> But I probably would just use your original spec and add the container type
>> and iterator function, something like:
>>
>> generic
>>     type Cursor is (<>);
>>     with function Image (C : Cursor) return String;
>>     type Container (<>) is private;
>>     with package Iterator_Interfaces is
>>       new Ada.Iterator_Interfaces (Cursor, others => <>);
>>     with function Iterator (C : Container) return
>> Iterator_Interfaces.Forward_Iterator'Class;
>> function List_Image return String;
>>
> 
> I think that's a good starting point.

Ok, I have something that works (https://github.com/LionelDraghi/List_Image).

Instantiating this is not as simple as I hoped, but not that much a nightmare :

   with List_Image;

   with Ada.Containers.Indefinite_Hashed_Sets;
   with Ada.Strings.Hash_Case_Insensitive;
   ...
      -- Example here with a set of identifier
      package Id_Sets is new Ada.Containers.Indefinite_Hashed_Sets
        (String, Ada.Strings.Hash_Case_Insensitive, "=");
      Id_Set : Id_Sets.Set;

      use Id_Sets;
      function Id_Set_Image is new List_Image.Image
           (Cursor      => Cursor,
            Image       => Element,
            Iterator_If => Set_Iterator_Interfaces,
            Container   => Set,
            Iterator    => Iterate,
            Style       => List_Image.Bracketed_List_Style);
      ...
      Id_Set.Insert ("Salt");
      Id_Set.Insert ("Pepper");
      Put_Line (Id_Set_Image (Id_Set));


It becomes slightly more complex for Containers that don't directly export an Iterate function returning Forward_Iterator'class (but Reversible_Iterator'class).
You need then to provide a simple function :

   function Iterate (L : List) return
     List_Iterator_Interfaces.Forward_Iterator'Class is 
       (Iterate (L));

The signature Style package is generally simple, and may be application wide, shared among all List_Image instantiation.

Any idea to simplify is welcome.

-- 
-- Lionel


  parent reply	other threads:[~2018-01-28 18:08 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-25  0:22 Iterable container as generic parameter Lionel Draghi
2018-01-25  3:36 ` Randy Brukardt
2018-01-25 14:58   ` Lionel Draghi
2018-01-25 18:26     ` briot.emmanuel
2018-01-26  4:50     ` Randy Brukardt
2018-01-26  9:09       ` briot.emmanuel
2018-01-26 22:32         ` Lionel Draghi
2018-01-27  7:03           ` Randy Brukardt
2018-01-27 11:38             ` Lionel Draghi
2018-01-27 14:31               ` Jere
2018-01-28 18:08         ` Lionel Draghi [this message]
2018-01-29 13:34           ` briot.emmanuel
2018-01-29 22:26             ` Lionel Draghi
2018-01-29 23:00               ` Randy Brukardt
2018-01-30 22:35                 ` Lionel Draghi
2018-01-30 23:32                   ` Lionel Draghi
2018-01-31  0:15                   ` Lionel Draghi
2018-01-30 15:13               ` Shark8
2018-01-26 23:07 ` Lionel Draghi
replies disabled

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