StrategyCollection.h 2.9 KB

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