Cache_Map_Manager_T.h

Go to the documentation of this file.
00001 // -*- C++ -*-
00002 
00003 //=============================================================================
00004 /**
00005  *  @file    Cache_Map_Manager_T.h
00006  *
00007  *  Cache_Map_Manager_T.h,v 4.33 2005/11/05 13:00:45 jwillemsen Exp
00008  *
00009  *  @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
00010  */
00011 //=============================================================================
00012 
00013 #ifndef ACE_CACHE_MAP_MANAGER_T_H
00014 #define ACE_CACHE_MAP_MANAGER_T_H
00015 
00016 #include /**/ "ace/pre.h"
00017 
00018 #include "ace/config-all.h"
00019 
00020 #if !defined (ACE_LACKS_PRAGMA_ONCE)
00021 # pragma once
00022 #endif /* ACE_LACKS_PRAGMA_ONCE */
00023 
00024 #include "ace/Default_Constants.h"
00025 #include "ace/Global_Macros.h"
00026 #include "ace/Pair_T.h"
00027 
00028 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00029 
00030 // Forward declaration.
00031 class ACE_Allocator;
00032 
00033 #define ACE_Cache_Map_Iterator ACMI
00034 #define ACE_Cache_Map_Reverse_Iterator ACMRI
00035 
00036 template <class KEY, class VALUE, class IMPLEMENTATION, class CACHING_STRATEGY, class ATTRIBUTES>
00037 class ACE_Cache_Map_Iterator;
00038 
00039 template <class KEY, class VALUE, class REVERSE_IMPLEMENTATION, class CACHING_STRATEGY, class ATTRIBUTES>
00040 class ACE_Cache_Map_Reverse_Iterator;
00041 
00042 // For linkers that cant grok long names.
00043 #define ACE_Cache_Map_Manager ACMM
00044 
00045 /**
00046  * @class ACE_Cache_Map_Manager
00047  *
00048  * @brief Defines a abstraction that will purge entries from a map.
00049  *
00050  * The <ACE_Cache_Map_Manager> will manage the map it contains
00051  * and provide purging on demand from the map. The strategy for
00052  * caching is decided by the user and provided to the Cache
00053  * Manager.  The Cache Manager acts as a agent and communicates
00054  * between the Map and the Strategy for purging entries from the
00055  * map.
00056  * No locking mechanism provided since locking at this level
00057  * isn't efficient.  Locking has to be provided by the
00058  * application.
00059  */
00060 template <class KEY, class VALUE, class CMAP_TYPE, class ITERATOR_IMPL, class REVERSE_ITERATOR_IMPL, class CACHING_STRATEGY, class ATTRIBUTES>
00061 class ACE_Cache_Map_Manager
00062 {
00063 public:
00064 
00065   // = Traits.
00066   typedef KEY key_type;
00067   typedef VALUE mapped_type;
00068   typedef CMAP_TYPE map_type;
00069   typedef CACHING_STRATEGY caching_strategy_type;
00070 
00071   typedef ITERATOR_IMPL ITERATOR_IMPLEMENTATION;
00072   typedef REVERSE_ITERATOR_IMPL REVERSE_ITERATOR_IMPLEMENTATION;
00073 
00074   friend class ACE_Cache_Map_Iterator<KEY, VALUE, ITERATOR_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES>;
00075   friend class ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_ITERATOR_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES>;
00076 
00077   // = ACE-style iterator typedefs.
00078   typedef ACE_Cache_Map_Iterator<KEY, VALUE, ITERATOR_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES>
00079           ITERATOR;
00080   typedef ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_ITERATOR_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES>
00081           REVERSE_ITERATOR;
00082 
00083    // = STL-style iterator typedefs.
00084   typedef ITERATOR
00085           iterator;
00086   typedef REVERSE_ITERATOR
00087           reverse_iterator;
00088 
00089   /**
00090    * The actual value mapped to the key in the map. The <attributes>
00091    * are used by the strategy and is transparent to the user of this
00092    * class.
00093    */
00094   typedef ACE_Pair<VALUE, ATTRIBUTES> CACHE_VALUE;
00095 
00096   // = Initialization and termination methods.
00097 
00098   /// Initialize a <Cache_Map_Manager> with <caching_strategy> and
00099   /// <size> entries.
00100   ACE_Cache_Map_Manager (CACHING_STRATEGY &caching_strategy,
00101                          size_t size = ACE_DEFAULT_MAP_SIZE,
00102                          ACE_Allocator *alloc = 0);
00103 
00104   /// Close down a <Cache_Map_Manager> and release dynamically allocated
00105   /// resources.
00106   virtual ~ACE_Cache_Map_Manager (void);
00107 
00108   /// Initialize a cache with size <length>.
00109   int open (size_t length = ACE_DEFAULT_MAP_SIZE,
00110             ACE_Allocator *alloc = 0);
00111 
00112   /// Close down a cache and release dynamically allocated resources.
00113   int close (void);
00114 
00115   /**
00116    * Associate <key> with <value>.  If <key> is already in the CMAP_TYPE
00117    * then the ENTRY is not changed.  Returns 0 if a new entry is bound
00118    * successfully, returns 1 if an attempt is made to bind an existing
00119    * entry, and returns -1 if failures occur.
00120    */
00121   int bind (const KEY &key,
00122             const VALUE &value);
00123 
00124   /**
00125    * Lookup entry<key,value> in the cache. If it is not found, returns -1.
00126    * If the <key> is located in the CMAP_TYPE object, the CACHING_STRATEGY is
00127    * notified of it via notify_find (int result, ATTRIBUTES &attribute).
00128    * If notify_find also returns 0 (success), then this function returns
00129    * 0 (success) and sets the cached value in <value>.
00130    */
00131   int find (const KEY &key,
00132             VALUE &value);
00133 
00134   /**
00135    * Lookup entry<key,value> in the cache. If it is not found, returns -1.
00136    * If the <key> is located in the CMAP_TYPE object, the CACHING_STRATEGY is
00137    * notified of it via notify_find (int result, ATTRIBUTES &attribute).
00138    * If notify_find also returns 0 (success), then this function returns
00139    * 0 (success).
00140    */
00141   int find (const KEY &key);
00142 
00143   /**
00144    * Reassociate the <key> with <value>. If the <key> already exists
00145    * in the cache then returns 1, on a new bind returns 0 and returns
00146    * -1 in case of any failures.
00147    */
00148   int rebind (const KEY &key,
00149               const VALUE &value);
00150 
00151   /**
00152    * Reassociate <key> with <value>, storing the old value into the
00153    * "out" parameter <old_value>.  The function fails if <key> is not
00154    * in the cache for caches that do not allow user specified keys.
00155    * However, for caches that allow user specified keys, if the key is
00156    * not in the cache, a new <key>/<value> association is created.
00157    */
00158   int rebind (const KEY &key,
00159               const VALUE &value,
00160               VALUE &old_value);
00161 
00162   /**
00163    * Reassociate <key> with <value>, storing the old key and value
00164    * into the "out" parameters <old_key> and <old_value>.  The
00165    * function fails if <key> is not in the cache for caches that do
00166    * not allow user specified keys.  However, for caches that allow
00167    * user specified keys, if the key is not in the cache, a new
00168    * <key>/<value> association is created.
00169    */
00170   int rebind (const KEY &key,
00171               const VALUE &value,
00172               KEY &old_key,
00173               VALUE &old_value);
00174 
00175   /**
00176    * Associate <key> with <value> if and only if <key> is not in the
00177    * cache.  If <key> is already in the cache, then the <value>
00178    * parameter is overwritten with the existing value in the
00179    * cache. Returns 0 if a new <key>/<value> association is created.
00180    * Returns 1 if an attempt is made to bind an existing entry.  This
00181    * function fails for maps that do not allow user specified keys.
00182    */
00183   int trybind (const KEY &key,
00184                VALUE &value);
00185 
00186   /// Remove <key> from the cache.
00187   int unbind (const KEY &key);
00188 
00189   /// Remove <key> from the cache, and return the <value> associated with
00190   /// <key>.
00191   int unbind (const KEY &key,
00192               VALUE &value);
00193 
00194   /// Remove entries from the cache depending upon the strategy.
00195   int purge (void);
00196 
00197   /// Return the current size of the cache.
00198   size_t current_size (void) const;
00199 
00200   /// Return the total size of the cache.
00201   size_t total_size (void) const;
00202 
00203   /// Dumps the state of the object.
00204   void dump (void) const;
00205 
00206   // = STL styled iterator factory functions.
00207 
00208   /// Return forward iterator.
00209   ITERATOR begin (void);
00210   ITERATOR end (void);
00211 
00212   /// Return reverse iterator.
00213   REVERSE_ITERATOR rbegin (void);
00214   REVERSE_ITERATOR rend (void);
00215 
00216   /// The map managed by the Cache_Map_Manager.
00217   CMAP_TYPE &map (void);
00218 
00219   /// The caching strategy used on the cache.
00220   CACHING_STRATEGY &caching_strategy (void);
00221 
00222 protected:
00223 
00224   /// The underlying map which needs to be cached.
00225   CMAP_TYPE map_;
00226 
00227   /// The strategy to be followed for caching entries in the map.
00228   CACHING_STRATEGY &caching_strategy_;
00229 
00230 private:
00231 
00232   // = Disallow these operations.
00233   ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Cache_Map_Manager<KEY, VALUE, CMAP_TYPE, ITERATOR_IMPL, REVERSE_ITERATOR_IMPL, CACHING_STRATEGY, ATTRIBUTES> &))
00234   ACE_UNIMPLEMENTED_FUNC (ACE_Cache_Map_Manager (const ACE_Cache_Map_Manager<KEY, VALUE, CMAP_TYPE, ITERATOR_IMPL, REVERSE_ITERATOR_IMPL, CACHING_STRATEGY, ATTRIBUTES> &))
00235 
00236 };
00237 
00238 /**
00239  * @class ACE_Cache_Map_Iterator
00240  *
00241  * @brief Defines a iterator for the Cache_Map_Manager.
00242  *
00243  * Implementation to be provided by the iterator of the map
00244  * managed by the ACE_Cache_Map_Manager.
00245  */
00246 template <class KEY, class VALUE, class IMPLEMENTATION, class CACHING_STRATEGY, class ATTRIBUTES>
00247 class ACE_Cache_Map_Iterator
00248 {
00249 
00250 public:
00251 
00252   // = Traits.
00253   /// The actual value mapped to the key in the cache. The <attributes>
00254   /// are used by the strategy and is transperant to the cache user.
00255   typedef ACE_Reference_Pair<KEY, VALUE>
00256           value_type;
00257   typedef ACE_Pair <VALUE, ATTRIBUTES>
00258           CACHE_VALUE;
00259 
00260   // = Initialisation and termination methods.
00261 
00262   ACE_Cache_Map_Iterator (const IMPLEMENTATION &iterator_impl);
00263 
00264   /// Copy constructor.
00265   ACE_Cache_Map_Iterator (const ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs);
00266 
00267   virtual ~ACE_Cache_Map_Iterator (void);
00268 
00269   // = Iteration methods.
00270 
00271   /// assignment operator.
00272   ACE_Cache_Map_Iterator <KEY, VALUE, IMPLEMENTATION,
00273                           CACHING_STRATEGY, ATTRIBUTES> &operator=
00274       (const ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION,
00275                                     CACHING_STRATEGY, ATTRIBUTES> &rhs);
00276 
00277   /// Comparision operators.
00278   bool operator== (const ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs) const;
00279   bool operator!= (const ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs) const;
00280 
00281   /// Returns a reference to the internal element <this> is pointing
00282   /// to.
00283   ACE_Reference_Pair<KEY, VALUE> operator* (void) const;
00284 
00285   // = STL styled iteration, compare, and reference functions.
00286 
00287   /// Prefix advance
00288   ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &operator++ (void);
00289 
00290   /// Postfix advance.
00291   ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> operator++ (int);
00292 
00293   /// Prefix reverse.
00294   ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &operator-- (void);
00295 
00296   /// Postfix reverse.
00297   ACE_Cache_Map_Iterator<KEY, VALUE, IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> operator-- (int);
00298 
00299   /// Returns the iterator of the internal map in the custody of the
00300   /// Cache_Map_Manager.
00301   IMPLEMENTATION &iterator_implementation (void);
00302 
00303   /// Dump the state of an object.
00304   void dump (void) const;
00305 
00306   /// Declare the dynamic allocation hooks.
00307   ACE_ALLOC_HOOK_DECLARE;
00308 
00309 protected:
00310   /// The actual iterator which iterates internally on the map
00311   /// belonging to the Cache_Map_Manager.
00312   IMPLEMENTATION iterator_implementation_;
00313 };
00314 
00315 /**
00316  * @class ACE_Cache_Map_Reverse_Iterator
00317  *
00318  * @brief Defines a reverse iterator for the Cache_Map_Manager.
00319  *
00320  * Implementation to be provided by the reverse iterator of the map
00321  * managed by thr Cache_Map_manager.
00322  */
00323 template <class KEY, class VALUE, class REVERSE_IMPLEMENTATION, class CACHING_STRATEGY, class ATTRIBUTES>
00324 class ACE_Cache_Map_Reverse_Iterator
00325 {
00326 public:
00327 
00328   // = Traits.
00329   /// The actual value mapped to the key in the cache. The <attributes>
00330   /// are used by the strategy and is transperant to the cache user.
00331   typedef ACE_Reference_Pair<KEY, VALUE> value_type;
00332   typedef ACE_Pair <VALUE, ATTRIBUTES> CACHE_VALUE;
00333 
00334   // = Initialisation and termination methods.
00335 
00336   ACE_Cache_Map_Reverse_Iterator (const REVERSE_IMPLEMENTATION &iterator_impl);
00337 
00338   /// Copy constructor.
00339   ACE_Cache_Map_Reverse_Iterator (const ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs);
00340 
00341   ~ACE_Cache_Map_Reverse_Iterator (void);
00342 
00343   // = Iteration methods.
00344 
00345   /// Assignment operator.
00346   ACE_Cache_Map_Reverse_Iterator <KEY, VALUE, REVERSE_IMPLEMENTATION,
00347                                   CACHING_STRATEGY, ATTRIBUTES> &operator=
00348      (const ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION,
00349                                            CACHING_STRATEGY, ATTRIBUTES> &rhs);
00350 
00351   /// Comparision operators.
00352   bool operator== (const ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs) const;
00353   bool operator!= (const ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &rhs) const;
00354 
00355   /// Returns a reference to the internal element <this> is pointing
00356   /// to.
00357   ACE_Reference_Pair<KEY, VALUE> operator* (void) const;
00358 
00359   // = STL styled iteration, compare, and reference functions.
00360 
00361   /// Prefix advance
00362   ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &operator++ (void);
00363 
00364   /// Postfix advance.
00365   ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> operator++ (int);
00366 
00367   /// Prefix reverse.
00368   ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> &operator-- (void);
00369 
00370   /// Postfix reverse.
00371   ACE_Cache_Map_Reverse_Iterator<KEY, VALUE, REVERSE_IMPLEMENTATION, CACHING_STRATEGY, ATTRIBUTES> operator-- (int);
00372 
00373   /// Returns the iterator of the internal map in the custody of the
00374   /// Cache_Map_Manager.
00375   REVERSE_IMPLEMENTATION &iterator_implementation (void);
00376 
00377   /// Dump the state of an object.
00378   void dump (void) const;
00379 
00380   /// Declare the dynamic allocation hooks.
00381   ACE_ALLOC_HOOK_DECLARE;
00382 
00383 protected:
00384   /// The actual iterator which iterates internally on the map
00385   /// belonging to the Cache_Map_Manager.
00386   REVERSE_IMPLEMENTATION reverse_iterator_implementation_;
00387 };
00388 
00389 ACE_END_VERSIONED_NAMESPACE_DECL
00390 
00391 #if defined (__ACE_INLINE__)
00392 #include "ace/Cache_Map_Manager_T.inl"
00393 #endif /* __ACE_INLINE__ */
00394 
00395 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
00396 #include "ace/Cache_Map_Manager_T.cpp"
00397 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
00398 
00399 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
00400 #pragma implementation ("Cache_Map_Manager_T.cpp")
00401 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
00402 
00403 #include /**/ "ace/post.h"
00404 
00405 #endif /* ACE_CACHE_MAP_MANAGER_T_H */

Generated on Thu Nov 9 09:41:47 2006 for ACE by doxygen 1.3.6