Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Caching_Utility_T.cpp
blob2f0ea2c295a07f54f87a840111974d026c47323d
1 #ifndef ACE_CACHING_UTILITY_T_CPP
2 #define ACE_CACHING_UTILITY_T_CPP
4 #include "ace/Caching_Utility_T.h"
6 #if !defined (ACE_LACKS_PRAGMA_ONCE)
7 #pragma once
8 #endif /* ACE_LACKS_PRAGMA_ONCE */
10 #include "ace/ACE.h"
11 #include "ace/Min_Max.h"
12 #include "ace/OS_Memory.h"
13 #include "ace/Recyclable.h"
15 //////////////////////////////////////////////////////////////////////////////
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
20 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Pair_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
21 bool delete_cleanup_strategy)
22 : cleanup_strategy_ (cleanup_strategy),
23 delete_cleanup_strategy_ (delete_cleanup_strategy)
25 if (cleanup_strategy == 0)
27 ACE_NEW (this->cleanup_strategy_,
28 CLEANUP_STRATEGY);
29 this->delete_cleanup_strategy_ = true;
33 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
34 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Pair_Caching_Utility ()
36 if (this->delete_cleanup_strategy_)
37 delete this->cleanup_strategy_;
40 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
41 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
42 double purge_percent)
44 // Check that the purge_percent is non-zero.
45 if (ACE::is_equal (purge_percent, 0.0))
46 return 0;
48 // Get the number of entries in the container.
49 size_t current_map_size = container.current_size ();
51 // Also whether the number of entries in the cache!
52 // Oops! then there is no way out but exiting. So return an error.
53 if (current_map_size == 0)
54 return 0;
56 // Calculate the no of entries to remove from the cache depending
57 // upon the <purge_percent>.
58 size_t const entries_to_remove
59 = ACE_MAX (static_cast<size_t> (1),
60 static_cast<size_t> (static_cast<double> (purge_percent)
61 / 100 * current_map_size));
62 KEY *key_to_remove = 0;
63 VALUE *value_to_remove = 0;
65 for (size_t i = 0; i < entries_to_remove ; ++i)
67 this->minimum (container,
68 key_to_remove,
69 value_to_remove);
71 // Simply verifying that the key is non-zero.
72 // This is important for strategies where the minimum
73 // entry cant be found due to constraints on the type of entry
74 // to remove.
75 if (key_to_remove == 0)
76 return 0;
78 if (this->cleanup_strategy_->cleanup (container,
79 key_to_remove,
80 value_to_remove) == -1)
81 return -1;
84 return 0;
87 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
88 ACE_Pair_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
89 KEY *&key_to_remove,
90 VALUE *&value_to_remove)
92 // Starting values.
93 ITERATOR iter = container.begin ();
94 ITERATOR end = container.end ();
95 ATTRIBUTES min = (*iter).int_id_.second;
96 key_to_remove = &(*iter).ext_id_;
97 value_to_remove = &(*iter).int_id_;
99 // The iterator moves thru the container searching for the entry
100 // with the lowest ATTRIBUTES.
101 for (++iter;
102 iter != end;
103 ++iter)
105 if (min > (*iter).int_id_.second)
107 // Ah! an item with lower ATTTRIBUTES...
108 min = (*iter).int_id_.second;
109 key_to_remove = &(*iter).ext_id_;
110 value_to_remove = &(*iter).int_id_;
115 ////////////////////////////////////////////////////////////////////////////////////////////////////////
117 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
118 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
119 bool delete_cleanup_strategy)
120 : cleanup_strategy_ (cleanup_strategy),
121 delete_cleanup_strategy_ (delete_cleanup_strategy)
123 if (cleanup_strategy == 0)
125 ACE_NEW (this->cleanup_strategy_,
126 CLEANUP_STRATEGY);
127 this->delete_cleanup_strategy_ = true;
131 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
132 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Recyclable_Handler_Caching_Utility ()
134 if (this->delete_cleanup_strategy_)
135 delete this->cleanup_strategy_;
138 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
139 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
140 double purge_percent)
142 // Check that the purge_percent is non-zero.
143 if (ACE::is_equal (purge_percent, 0.0))
144 return 0;
146 // Get the number of entries in the container.
147 size_t current_map_size = container.current_size ();
149 // Also whether the number of entries in the cache is just one!
150 // Oops! then there is no way out but exiting. So return an error.
151 // if (current_map_size <= 1)
152 if (current_map_size == 0)
153 return 0;
155 // Calculate the no of entries to remove from the cache depending
156 // upon the <purge_percent>.
157 size_t const entries_to_remove
158 = ACE_MAX (static_cast<size_t> (1),
159 static_cast<size_t> (static_cast<double> (purge_percent)
160 / 100 * current_map_size));
162 KEY *key_to_remove = 0;
163 VALUE *value_to_remove = 0;
165 for (size_t i = 0; i < entries_to_remove ; ++i)
167 this->minimum (container,
168 key_to_remove,
169 value_to_remove);
171 // Simply verifying that the key is non-zero.
172 // This is important for strategies where the minimum
173 // entry cant be found due to constraints on the type of entry
174 // to remove.
175 if (key_to_remove == 0)
176 return 0;
178 if (this->cleanup_strategy_->cleanup (container,
179 key_to_remove,
180 value_to_remove) == -1)
181 return -1;
184 return 0;
187 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
188 ACE_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
189 KEY *&key_to_remove,
190 VALUE *&value_to_remove)
192 // Starting values.
193 ITERATOR end = container.end ();
194 ITERATOR iter = container.begin ();
195 ATTRIBUTES min = (*iter).int_id_.second;
196 key_to_remove = 0;
197 value_to_remove = 0;
198 // Found the minimum entry to be purged?
199 int found = 0;
201 // The iterator moves thru the container searching for the entry
202 // with the lowest ATTRIBUTES.
203 for (;
204 iter != end;
205 ++iter)
207 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
208 // the first entry which can be purged. This is the minimum with
209 // which you will compare the rest of the purgable entries.
210 if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
211 (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
213 if (found == 0)
215 min = (*iter).int_id_.second;
216 key_to_remove = &(*iter).ext_id_;
217 value_to_remove = &(*iter).int_id_;
218 found = 1;
220 else
222 // Ah! an entry with lower ATTTRIBUTES...
223 if (min > (*iter).int_id_.second)
225 min = (*iter).int_id_.second;
226 key_to_remove = &(*iter).ext_id_;
227 value_to_remove = &(*iter).int_id_;
234 ////////////////////////////////////////////////////////////////////////////////
236 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
237 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Refcounted_Recyclable_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
238 bool delete_cleanup_strategy)
239 : cleanup_strategy_ (cleanup_strategy),
240 delete_cleanup_strategy_ (delete_cleanup_strategy),
241 marked_as_closed_entries_ (0)
243 if (cleanup_strategy == 0)
245 ACE_NEW (this->cleanup_strategy_,
246 CLEANUP_STRATEGY);
247 this->delete_cleanup_strategy_ = true;
251 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
252 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Refcounted_Recyclable_Handler_Caching_Utility ()
254 if (this->delete_cleanup_strategy_)
255 delete this->cleanup_strategy_;
258 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
259 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
260 double purge_percent)
262 // Check that the purge_percent is non-zero.
263 if (ACE::is_equal (purge_percent, 0.0))
264 return 0;
266 // Get the number of entries in the container which can be considered for purging.
267 size_t const available_entries =
268 container.current_size () - this->marked_as_closed_entries_;
270 // Also whether the number of entries in the cache zero.
271 // Oops! then there is no way out but exiting.
272 if (available_entries <= 0)
273 return 0;
275 // Calculate the no of entries to remove from the cache depending
276 // upon the <purge_percent>.
277 size_t entries_to_remove
278 = ACE_MAX (static_cast<size_t> (1),
279 static_cast<size_t> (static_cast<double> (purge_percent)
280 / 100 * available_entries));
282 if (entries_to_remove >= available_entries || entries_to_remove == 0)
283 entries_to_remove = available_entries - 1;
285 KEY *key_to_remove = 0;
286 VALUE *value_to_remove = 0;
288 for (size_t i = 0; i < entries_to_remove ; ++i)
290 this->minimum (container,
291 key_to_remove,
292 value_to_remove);
294 // Simply verifying that the key is non-zero.
295 // This is important for strategies where the minimum
296 // entry cant be found due to constraints on the type of entry
297 // to remove.
298 if (key_to_remove == 0)
299 return 0;
301 if (this->cleanup_strategy_->cleanup (container,
302 key_to_remove,
303 value_to_remove) == -1)
304 return -1;
306 ++this->marked_as_closed_entries_;
309 return 0;
312 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
313 ACE_Refcounted_Recyclable_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
314 KEY *&key_to_remove,
315 VALUE *&value_to_remove)
317 // Starting values.
318 ITERATOR end = container.end ();
319 ITERATOR iter = container.begin ();
320 ATTRIBUTES min = (*iter).int_id_.second ();
321 key_to_remove = 0;
322 value_to_remove = 0;
323 // Found the minimum entry to be purged?
324 int found = 0;
326 // The iterator moves thru the container searching for the entry
327 // with the lowest ATTRIBUTES.
328 for (;
329 iter != end;
330 ++iter)
332 // If the <min> entry isnt IDLE_AND_PURGABLE continue until you reach
333 // the first entry which can be purged. This is the minimum with
334 // which you will compare the rest of the purgable entries.
335 if ((*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_IDLE_AND_PURGABLE ||
336 (*iter).ext_id_.recycle_state () == ACE_RECYCLABLE_PURGABLE_BUT_NOT_IDLE)
338 if (found == 0)
340 min = (*iter).int_id_.second ();
341 key_to_remove = &(*iter).ext_id_;
342 value_to_remove = &(*iter).int_id_;
343 found = 1;
345 else
347 // Ah! an entry with lower ATTTRIBUTES...
348 if (min > (*iter).int_id_.second ())
350 min = (*iter).int_id_.second ();
351 key_to_remove = &(*iter).ext_id_;
352 value_to_remove = &(*iter).int_id_;
359 ////////////////////////////////////////////////////////////////////////////////
361 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
362 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Handler_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
363 bool delete_cleanup_strategy)
364 : cleanup_strategy_ (cleanup_strategy),
365 delete_cleanup_strategy_ (delete_cleanup_strategy)
367 if (cleanup_strategy == 0)
369 ACE_NEW (this->cleanup_strategy_,
370 CLEANUP_STRATEGY);
371 this->delete_cleanup_strategy_ = true;
375 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
376 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Handler_Caching_Utility ()
378 if (this->delete_cleanup_strategy_)
379 delete this->cleanup_strategy_;
382 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
383 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
384 double purge_percent)
386 // Check that the purge_percent is non-zero.
387 if (ACE::is_equal (purge_percent, 0.0))
388 return 0;
390 // Get the number of entries in the container.
391 size_t current_map_size = container.current_size ();
393 // Also whether the number of entries in the cache is just one!
394 // Oops! then there is no way out but exiting. So return an error.
395 if (current_map_size == 0)
396 return 0;
398 // Calculate the no of entries to remove from the cache depending
399 // upon the <purge_percent>.
400 size_t entries_to_remove
401 = ACE_MAX (static_cast<size_t> (1),
402 static_cast<size_t> (static_cast<double> (purge_percent)
403 / 100 * current_map_size));
405 KEY *key_to_remove = 0;
406 VALUE *value_to_remove = 0;
408 for (size_t i = 0; i < entries_to_remove ; ++i)
410 this->minimum (container,
411 key_to_remove,
412 value_to_remove);
414 if (this->cleanup_strategy_->cleanup (container,
415 key_to_remove,
416 value_to_remove) == -1)
417 return -1;
420 return 0;
423 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
424 ACE_Handler_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
425 KEY *&key_to_remove,
426 VALUE *&value_to_remove)
428 // Starting values.
429 ITERATOR iter = container.begin ();
430 ITERATOR end = container.end ();
431 ATTRIBUTES min = (*iter).int_id_->caching_attributes ();
432 key_to_remove = &(*iter).ext_id_;
433 value_to_remove = &(*iter).int_id_;
435 // The iterator moves thru the container searching for the entry
436 // with the lowest ATTRIBUTES.
437 for (++iter;
438 iter != end;
439 ++iter)
441 if (min > (*iter).int_id_->caching_attributes () &&
442 (*iter).int_id_->active () != 1)
444 // Ah! an item with lower ATTTRIBUTES...
445 min = (*iter).int_id_->caching_attributes ();
446 key_to_remove = &(*iter).ext_id_;
447 value_to_remove = &(*iter).int_id_;
452 ////////////////////////////////////////////////////////////////////////////////////////////////////////
454 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
455 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::ACE_Null_Caching_Utility (ACE_Cleanup_Strategy<KEY, VALUE, CONTAINER> *cleanup_strategy,
456 bool delete_cleanup_strategy)
457 : cleanup_strategy_ (cleanup_strategy),
458 delete_cleanup_strategy_ (delete_cleanup_strategy)
460 if (cleanup_strategy == 0)
462 ACE_NEW (this->cleanup_strategy_,
463 CLEANUP_STRATEGY);
464 this->delete_cleanup_strategy_ = true;
468 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES>
469 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::~ACE_Null_Caching_Utility ()
471 if (this->delete_cleanup_strategy_)
472 delete this->cleanup_strategy_;
475 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> int
476 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::clear_cache (CONTAINER &container,
477 double purge_percent)
479 ACE_UNUSED_ARG (container);
480 ACE_UNUSED_ARG (purge_percent);
482 return 0;
485 template <class KEY, class VALUE, class CONTAINER, class ITERATOR, class ATTRIBUTES> void
486 ACE_Null_Caching_Utility<KEY, VALUE, CONTAINER, ITERATOR, ATTRIBUTES>::minimum (CONTAINER &container,
487 KEY *&key_to_remove,
488 VALUE *&value_to_remove)
490 ACE_UNUSED_ARG (container);
491 ACE_UNUSED_ARG (key_to_remove);
492 ACE_UNUSED_ARG (value_to_remove);
495 ACE_END_VERSIONED_NAMESPACE_DECL
497 #endif /* ACE_CACHING_UTILITY_T_CPP */