StrategyCollection.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. // StrategyCollection.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/StrategyCollection.h#1 $
  5. //
  6. // Library: Foundation
  7. // Package: Cache
  8. // Module: StrategyCollection
  9. //
  10. // Definition of the StrategyCollection class.
  11. //
  12. // Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_StrategyCollection_INCLUDED
  18. #define Foundation_StrategyCollection_INCLUDED
  19. #include "Poco/KeyValueArgs.h"
  20. #include "Poco/ValidArgs.h"
  21. #include "Poco/AbstractStrategy.h"
  22. #include "Poco/SharedPtr.h"
  23. #include <vector>
  24. namespace Poco {
  25. template <class TKey, class TValue>
  26. class StrategyCollection: public AbstractStrategy<TKey, TValue>
  27. /// An StrategyCollection is a decorator masking n collections as a single one.
  28. {
  29. public:
  30. typedef std::vector<SharedPtr<AbstractStrategy<TKey, TValue> > > Strategies;
  31. typedef typename Strategies::iterator Iterator;
  32. typedef typename Strategies::const_iterator ConstIterator;
  33. public:
  34. StrategyCollection()
  35. {
  36. }
  37. ~StrategyCollection()
  38. {
  39. }
  40. void pushBack(AbstractStrategy<TKey, TValue>* pStrat)
  41. /// Adds an AbstractStrategy to the collection. Class takes ownership of pointer
  42. {
  43. _strategies.push_back(SharedPtr<AbstractStrategy<TKey, TValue> >(pStrat));
  44. }
  45. void popBack()
  46. /// Removes the last added AbstractStrategy from the collection.
  47. {
  48. _strategies.pop_back();
  49. }
  50. void onAdd(const void* pSender, const KeyValueArgs <TKey, TValue>& key)
  51. /// Adds the key to the strategy.
  52. /// If for the key already an entry exists, it will be overwritten.
  53. {
  54. Iterator it = _strategies.begin();
  55. Iterator endIt = _strategies.end();
  56. for (; it != endIt; ++it)
  57. {
  58. (*it)->onAdd(pSender, key);
  59. }
  60. }
  61. void onRemove(const void* pSender, const TKey& key)
  62. /// Removes an entry from the strategy. If the entry is not found
  63. /// the remove is ignored.
  64. {
  65. Iterator it = _strategies.begin();
  66. Iterator endIt = _strategies.end();
  67. for (; it != endIt; ++it)
  68. {
  69. (*it)->onRemove(pSender, key);
  70. }
  71. }
  72. void onGet(const void* pSender, const TKey& key)
  73. {
  74. Iterator it = _strategies.begin();
  75. Iterator endIt = _strategies.end();
  76. for (; it != endIt; ++it)
  77. {
  78. (*it)->onGet(pSender, key);
  79. }
  80. }
  81. void onClear(const void* pSender, const EventArgs& args)
  82. {
  83. Iterator it = _strategies.begin();
  84. Iterator endIt = _strategies.end();
  85. for (; it != endIt; ++it)
  86. {
  87. (*it)->onClear(pSender, args);
  88. }
  89. }
  90. void onIsValid(const void* pSender, ValidArgs<TKey>& key)
  91. {
  92. Iterator it = _strategies.begin();
  93. Iterator endIt = _strategies.end();
  94. for (; it != endIt && key.isValid(); ++it)
  95. {
  96. (*it)->onIsValid(pSender, key);
  97. }
  98. }
  99. void onReplace(const void* pSender, std::set<TKey>& elemsToRemove)
  100. {
  101. Iterator it = _strategies.begin();
  102. Iterator endIt = _strategies.end();
  103. for (; it != endIt; ++it)
  104. {
  105. (*it)->onReplace(pSender, elemsToRemove);
  106. }
  107. }
  108. protected:
  109. Strategies _strategies;
  110. };
  111. } // namespace Poco
  112. #endif // Foundation_StrategyCollection_INCLUDED