comp.lang.ada
 help / color / mirror / Atom feed
From: ms <m.siedlarek@nctz.net>
Subject: Abstraction over container iterators
Date: Fri, 3 Aug 2012 05:01:41 -0700 (PDT)
Date: 2012-08-03T05:01:41-07:00	[thread overview]
Message-ID: <75425fa8-7885-48d8-9eeb-8f90a1bfb128@googlegroups.com> (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?



             reply	other threads:[~2012-08-07  6:29 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-03 12:01 ms [this message]
2012-08-03 12:52 ` Abstraction over container iterators Dmitry A. Kazakov
2012-08-03 13:18   ` Georg Bauhaus
replies disabled

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