3 //=============================================================================
5 * @file Cache_Map_Manager_T.h
7 * @author Kirthika Parameswaran <kirthika@cs.wustl.edu>
9 //=============================================================================
11 #ifndef ACE_CACHE_MAP_MANAGER_T_H
12 #define ACE_CACHE_MAP_MANAGER_T_H
14 #include /**/ "ace/pre.h"
16 #include /**/ "ace/config-all.h"
18 #if !defined (ACE_LACKS_PRAGMA_ONCE)
20 #endif /* ACE_LACKS_PRAGMA_ONCE */
22 #include "ace/Default_Constants.h"
23 #include "ace/Global_Macros.h"
24 #include "ace/Pair_T.h"
26 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
28 // Forward declaration.
31 #define ACE_Cache_Map_Iterator ACMI
32 #define ACE_Cache_Map_Reverse_Iterator ACMRI
34 template <class KEY
, class VALUE
, class IMPLEMENTATION
, class CACHING_STRATEGY
, class ATTRIBUTES
>
35 class ACE_Cache_Map_Iterator
;
37 template <class KEY
, class VALUE
, class REVERSE_IMPLEMENTATION
, class CACHING_STRATEGY
, class ATTRIBUTES
>
38 class ACE_Cache_Map_Reverse_Iterator
;
40 // For linkers that cant grok long names.
41 #define ACE_Cache_Map_Manager ACMM
44 * @class ACE_Cache_Map_Manager
46 * @brief Defines a abstraction that will purge entries from a map.
48 * The <ACE_Cache_Map_Manager> will manage the map it contains
49 * and provide purging on demand from the map. The strategy for
50 * caching is decided by the user and provided to the Cache
51 * Manager. The Cache Manager acts as a agent and communicates
52 * between the Map and the Strategy for purging entries from the
54 * No locking mechanism provided since locking at this level
55 * isn't efficient. Locking has to be provided by the
58 template <class KEY
, class VALUE
, class CMAP_TYPE
, class ITERATOR_IMPL
, class REVERSE_ITERATOR_IMPL
, class CACHING_STRATEGY
, class ATTRIBUTES
>
59 class ACE_Cache_Map_Manager
64 typedef VALUE mapped_type
;
65 typedef CMAP_TYPE map_type
;
66 typedef CACHING_STRATEGY caching_strategy_type
;
68 typedef ITERATOR_IMPL ITERATOR_IMPLEMENTATION
;
69 typedef REVERSE_ITERATOR_IMPL REVERSE_ITERATOR_IMPLEMENTATION
;
71 friend class ACE_Cache_Map_Iterator
<KEY
, VALUE
, ITERATOR_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
>;
72 friend class ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_ITERATOR_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
>;
74 // = ACE-style iterator typedefs.
75 typedef ACE_Cache_Map_Iterator
<KEY
, VALUE
, ITERATOR_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
>
77 typedef ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_ITERATOR_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
>
80 // = STL-style iterator typedefs.
83 typedef REVERSE_ITERATOR
87 * The actual value mapped to the key in the map. The <attributes>
88 * are used by the strategy and is transparent to the user of this
91 typedef std::pair
<VALUE
, ATTRIBUTES
> CACHE_VALUE
;
93 /// Initialize a <Cache_Map_Manager> with <caching_strategy> and
95 ACE_Cache_Map_Manager (CACHING_STRATEGY
&caching_strategy
,
96 size_t size
= ACE_DEFAULT_MAP_SIZE
,
97 ACE_Allocator
*alloc
= 0);
99 /// Close down a <Cache_Map_Manager> and release dynamically allocated
101 virtual ~ACE_Cache_Map_Manager (void);
103 /// Initialize a cache with size @a length.
104 int open (size_t length
= ACE_DEFAULT_MAP_SIZE
,
105 ACE_Allocator
*alloc
= 0);
107 /// Close down a cache and release dynamically allocated resources.
111 * Associate @a key with @a value. If @a key is already in the CMAP_TYPE
112 * then the ENTRY is not changed. Returns 0 if a new entry is bound
113 * successfully, returns 1 if an attempt is made to bind an existing
114 * entry, and returns -1 if failures occur.
116 int bind (const KEY
&key
,
120 * Lookup entry<key,value> in the cache. If it is not found, returns -1.
121 * If the @a key is located in the CMAP_TYPE object, the CACHING_STRATEGY is
122 * notified of it via notify_find (int result, ATTRIBUTES &attribute).
123 * If notify_find also returns 0 (success), then this function returns
124 * 0 (success) and sets the cached value in @a value.
126 int find (const KEY
&key
,
130 * Lookup entry<key,value> in the cache. If it is not found, returns -1.
131 * If the @a key is located in the CMAP_TYPE object, the CACHING_STRATEGY is
132 * notified of it via notify_find (int result, ATTRIBUTES &attribute).
133 * If notify_find also returns 0 (success), then this function returns
136 int find (const KEY
&key
);
139 * Reassociate the @a key with @a value. If the @a key already exists
140 * in the cache then returns 1, on a new bind returns 0 and returns
141 * -1 in case of any failures.
143 int rebind (const KEY
&key
,
147 * Reassociate @a key with @a value, storing the old value into the
148 * "out" parameter @a old_value. The function fails if @a key is not
149 * in the cache for caches that do not allow user specified keys.
150 * However, for caches that allow user specified keys, if the key is
151 * not in the cache, a new @a key / @a value association is created.
153 int rebind (const KEY
&key
,
158 * Reassociate @a key with @a value, storing the old key and value
159 * into the "out" parameters @a old_key and @a old_value. The
160 * function fails if @a key is not in the cache for caches that do
161 * not allow user specified keys. However, for caches that allow
162 * user specified keys, if the key is not in the cache, a new
163 * @a key / @a value association is created.
165 int rebind (const KEY
&key
,
171 * Associate @a key with @a value if and only if @a key is not in the
172 * cache. If @a key is already in the cache, then the @a value
173 * parameter is overwritten with the existing value in the
174 * cache. Returns 0 if a new @a key / @a value association is created.
175 * Returns 1 if an attempt is made to bind an existing entry. This
176 * function fails for maps that do not allow user specified keys.
178 int trybind (const KEY
&key
,
181 /// Remove @a key from the cache.
182 int unbind (const KEY
&key
);
184 /// Remove @a key from the cache, and return the @a value associated with
186 int unbind (const KEY
&key
,
189 /// Remove entries from the cache depending upon the strategy.
192 /// Return the current size of the cache.
193 size_t current_size (void) const;
195 /// Return the total size of the cache.
196 size_t total_size (void) const;
198 /// Dumps the state of the object.
199 void dump (void) const;
201 // = STL styled iterator factory functions.
203 /// Return forward iterator.
204 ITERATOR
begin (void);
207 /// Return reverse iterator.
208 REVERSE_ITERATOR
rbegin (void);
209 REVERSE_ITERATOR
rend (void);
211 /// The map managed by the Cache_Map_Manager.
212 CMAP_TYPE
&map (void);
214 /// The caching strategy used on the cache.
215 CACHING_STRATEGY
&caching_strategy (void);
217 /// Declare the dynamic allocation hooks.
218 ACE_ALLOC_HOOK_DECLARE
;
222 /// The underlying map which needs to be cached.
225 /// The strategy to be followed for caching entries in the map.
226 CACHING_STRATEGY
&caching_strategy_
;
229 // = Disallow these operations.
230 ACE_UNIMPLEMENTED_FUNC (void operator= (const ACE_Cache_Map_Manager
<KEY
, VALUE
, CMAP_TYPE
, ITERATOR_IMPL
, REVERSE_ITERATOR_IMPL
, CACHING_STRATEGY
, ATTRIBUTES
> &))
231 ACE_UNIMPLEMENTED_FUNC (ACE_Cache_Map_Manager (const ACE_Cache_Map_Manager
<KEY
, VALUE
, CMAP_TYPE
, ITERATOR_IMPL
, REVERSE_ITERATOR_IMPL
, CACHING_STRATEGY
, ATTRIBUTES
> &))
235 * @class ACE_Cache_Map_Iterator
237 * @brief Defines a iterator for the Cache_Map_Manager.
239 * Implementation to be provided by the iterator of the map
240 * managed by the ACE_Cache_Map_Manager.
242 template <class KEY
, class VALUE
, class IMPLEMENTATION
, class CACHING_STRATEGY
, class ATTRIBUTES
>
243 class ACE_Cache_Map_Iterator
247 /// The actual value mapped to the key in the cache. The <attributes>
248 /// are used by the strategy and is transparent to the cache user.
249 typedef ACE_Reference_Pair
<KEY
, VALUE
>
251 typedef std::pair
<VALUE
, ATTRIBUTES
>
254 // = Initialisation and termination methods.
256 ACE_Cache_Map_Iterator (const IMPLEMENTATION
&iterator_impl
);
258 /// Copy constructor.
259 ACE_Cache_Map_Iterator (const ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
);
261 virtual ~ACE_Cache_Map_Iterator (void);
263 // = Iteration methods.
265 /// assignment operator.
266 ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
,
267 CACHING_STRATEGY
, ATTRIBUTES
> &operator=
268 (const ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
,
269 CACHING_STRATEGY
, ATTRIBUTES
> &rhs
);
271 /// Comparison operators.
272 bool operator== (const ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
) const;
273 bool operator!= (const ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
) const;
275 /// Returns a reference to the internal element @c this is pointing
277 ACE_Reference_Pair
<KEY
, VALUE
> operator* (void) const;
279 // = STL styled iteration, compare, and reference functions.
282 ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &operator++ (void);
285 ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> operator++ (int);
288 ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &operator-- (void);
291 ACE_Cache_Map_Iterator
<KEY
, VALUE
, IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> operator-- (int);
293 /// Returns the iterator of the internal map in the custody of the
294 /// Cache_Map_Manager.
295 IMPLEMENTATION
&iterator_implementation (void);
297 /// Dump the state of an object.
298 void dump (void) const;
300 /// Declare the dynamic allocation hooks.
301 ACE_ALLOC_HOOK_DECLARE
;
304 /// The actual iterator which iterates internally on the map
305 /// belonging to the Cache_Map_Manager.
306 IMPLEMENTATION iterator_implementation_
;
310 * @class ACE_Cache_Map_Reverse_Iterator
312 * @brief Defines a reverse iterator for the Cache_Map_Manager.
314 * Implementation to be provided by the reverse iterator of the map
315 * managed by thr Cache_Map_manager.
317 template <class KEY
, class VALUE
, class REVERSE_IMPLEMENTATION
, class CACHING_STRATEGY
, class ATTRIBUTES
>
318 class ACE_Cache_Map_Reverse_Iterator
322 /// The actual value mapped to the key in the cache. The <attributes>
323 /// are used by the strategy and is transparent to the cache user.
324 typedef ACE_Reference_Pair
<KEY
, VALUE
> value_type
;
325 typedef std::pair
<VALUE
, ATTRIBUTES
> CACHE_VALUE
;
327 // = Initialisation and termination methods.
329 ACE_Cache_Map_Reverse_Iterator (const REVERSE_IMPLEMENTATION
&iterator_impl
);
331 /// Copy constructor.
332 ACE_Cache_Map_Reverse_Iterator (const ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
);
334 ~ACE_Cache_Map_Reverse_Iterator (void);
336 // = Iteration methods.
338 /// Assignment operator.
339 ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
,
340 CACHING_STRATEGY
, ATTRIBUTES
> &operator=
341 (const ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
,
342 CACHING_STRATEGY
, ATTRIBUTES
> &rhs
);
344 /// Comparison operators.
345 bool operator== (const ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
) const;
346 bool operator!= (const ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &rhs
) const;
348 /// Returns a reference to the internal element @c this is pointing
350 ACE_Reference_Pair
<KEY
, VALUE
> operator* (void) const;
352 // = STL styled iteration, compare, and reference functions.
355 ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &operator++ (void);
358 ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> operator++ (int);
361 ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> &operator-- (void);
364 ACE_Cache_Map_Reverse_Iterator
<KEY
, VALUE
, REVERSE_IMPLEMENTATION
, CACHING_STRATEGY
, ATTRIBUTES
> operator-- (int);
366 /// Returns the iterator of the internal map in the custody of the
367 /// Cache_Map_Manager.
368 REVERSE_IMPLEMENTATION
&iterator_implementation (void);
370 /// Dump the state of an object.
371 void dump (void) const;
373 /// Declare the dynamic allocation hooks.
374 ACE_ALLOC_HOOK_DECLARE
;
377 /// The actual iterator which iterates internally on the map
378 /// belonging to the Cache_Map_Manager.
379 REVERSE_IMPLEMENTATION reverse_iterator_implementation_
;
382 ACE_END_VERSIONED_NAMESPACE_DECL
384 #if defined (__ACE_INLINE__)
385 #include "ace/Cache_Map_Manager_T.inl"
386 #endif /* __ACE_INLINE__ */
388 #if defined (ACE_TEMPLATES_REQUIRE_SOURCE)
389 #include "ace/Cache_Map_Manager_T.cpp"
390 #endif /* ACE_TEMPLATES_REQUIRE_SOURCE */
392 #if defined (ACE_TEMPLATES_REQUIRE_PRAGMA)
393 #pragma implementation ("Cache_Map_Manager_T.cpp")
394 #endif /* ACE_TEMPLATES_REQUIRE_PRAGMA */
396 #include /**/ "ace/post.h"
398 #endif /* ACE_CACHE_MAP_MANAGER_T_H */