comp.lang.ada
 help / color / mirror / Atom feed
* Abstraction over container iterators
@ 2012-08-03 12:01 ms
  2012-08-03 12:52 ` Dmitry A. Kazakov
  0 siblings, 1 reply; 3+ messages in thread
From: ms @ 2012-08-03 12:01 UTC (permalink / raw)


I'll let the code speak first:

-----------------------------------------------
with Ada.Containers.Hashed_Maps;                                                
with Ada.Strings.Unbounded;                                                     
with Ada.Strings.Unbounded.Hash_Case_Insensitive;                               
with Ada.Strings.Unbounded.Equal_Case_Insensitive;                              

package Environments is                                                         

   type Environment is tagged private;                                          

   function Variable (                                                          
      E    : in Environment;                                                    
      Name : in Ada.Strings.Unbounded.Unbounded_String                          
   )                                                                            
      return Ada.Strings.Unbounded.Unbounded_String                             
      with Inline;                                                              

   procedure Set_Variable (                                                     
      E     : in out Environment;                                               
      Name  : in Ada.Strings.Unbounded.Unbounded_String;                        
      Value : in Ada.Strings.Unbounded.Unbounded_String                         
   )                                                                            
      with Inline;                                                              

private                                                                         

   package Variable_Maps is new Ada.Containers.Hashed_Maps (                    
      Key_Type        => Ada.Strings.Unbounded.Unbounded_String,                
      Element_Type    => Ada.Strings.Unbounded.Unbounded_String,                
      Hash            => Ada.Strings.Unbounded.Hash_Case_Insensitive,           
      Equivalent_Keys => Ada.Strings.Unbounded.Equal_Case_Insensitive,          
      "="             => Ada.Strings.Unbounded."="                              
   );                                                                           

   type Environment is tagged record                                            
      Variables : Variable_Maps.Map;                                            
   end record;                                                                  

end Environments;
-----------------------------------------------

What we have here is an example package fairly well illustrating my problem. I'm storing some environment variables in Hashed_Map, but I want to build a abstraction layer over the standard container, so I can in future change the underlaying container without changing any code in my package's customers.

Getting and setting variables is easy - as declared above. The real problem is iterating. I'd like to let my package's customers to iterate over the environment and get both key and value for each element easily.

As I'm using Ada 2012 the best way would be to use iterators, but how? I could return a cursor to the underlaying container, but again, this cursor's interface would be container-dependent.

What would you say is the best way to achieve such abstraction over standard container iteration?



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Abstraction over container iterators
  2012-08-03 12:01 Abstraction over container iterators ms
@ 2012-08-03 12:52 ` Dmitry A. Kazakov
  2012-08-03 13:18   ` Georg Bauhaus
  0 siblings, 1 reply; 3+ messages in thread
From: Dmitry A. Kazakov @ 2012-08-03 12:52 UTC (permalink / raw)


On Fri, 3 Aug 2012 05:01:41 -0700 (PDT), ms wrote:

> What we have here is an example package fairly well illustrating my
> problem. I'm storing some environment variables in Hashed_Map, but I want
> to build a abstraction layer over the standard container, so I can in
> future change the underlaying container without changing any code in my
> package's customers.
> 
> Getting and setting variables is easy - as declared above. The real
> problem is iterating. I'd like to let my package's customers to iterate
> over the environment and get both key and value for each element easily.

Add the iteration abstraction to the package. The implementation would both
do both the container and the iterator.

> As I'm using Ada 2012 the best way would be to use iterators,

I prefer plain indices over iterators. Just have get/put an element by its
positive index. Most containers should have array interface.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Abstraction over container iterators
  2012-08-03 12:52 ` Dmitry A. Kazakov
@ 2012-08-03 13:18   ` Georg Bauhaus
  0 siblings, 0 replies; 3+ messages in thread
From: Georg Bauhaus @ 2012-08-03 13:18 UTC (permalink / raw)


On 03.08.12 14:52, Dmitry A. Kazakov wrote:
> On Fri, 3 Aug 2012 05:01:41 -0700 (PDT), ms wrote:
> 
>> What we have here is an example package fairly well illustrating my
>> problem. I'm storing some environment variables in Hashed_Map, but I want
>> to build a abstraction layer over the standard container, so I can in
>> future change the underlaying container without changing any code in my
>> package's customers.
>>
>> Getting and setting variables is easy - as declared above. The real
>> problem is iterating. I'd like to let my package's customers to iterate
>> over the environment and get both key and value for each element easily.
> 
> Add the iteration abstraction to the package. The implementation would both
> do both the container and the iterator.

2nd. The type has-a container, so it has-a cursor as well.

Your type could supply a call-back style iteration mechanism, too,
such as

 generic
   with Customery_Query (Name : Unbounded_String);
 procedure For_Every_Name (E : Environment);

Or use an anonymous procedure pointer instead of a template.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2012-08-10  4:06 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-08-03 12:01 Abstraction over container iterators ms
2012-08-03 12:52 ` Dmitry A. Kazakov
2012-08-03 13:18   ` Georg Bauhaus

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