[80] | 1 | // |
---|
| 2 | // StrategyCollection.h |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/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 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 20 | // do so, all subject to the following: |
---|
| 21 | // |
---|
| 22 | // The copyright notices in the Software and this entire statement, including |
---|
| 23 | // the above license grant, this restriction and the following disclaimer, |
---|
| 24 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 25 | // all derivative works of the Software, unless such copies or derivative |
---|
| 26 | // works are solely in the form of machine-executable object code generated by |
---|
| 27 | // a source language processor. |
---|
| 28 | // |
---|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 35 | // DEALINGS IN THE SOFTWARE. |
---|
| 36 | // |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | #ifndef Foundation_StrategyCollection_INCLUDED |
---|
| 40 | #define Foundation_StrategyCollection_INCLUDED |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "Poco/KeyValueArgs.h" |
---|
| 44 | #include "Poco/ValidArgs.h" |
---|
| 45 | #include "Poco/AbstractStrategy.h" |
---|
| 46 | #include "Poco/SharedPtr.h" |
---|
| 47 | #include <vector> |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | namespace Poco { |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | template <class TKey, class TValue> |
---|
| 54 | class StrategyCollection: public AbstractStrategy<TKey, TValue> |
---|
| 55 | /// An StrategyCollection is a decorator masking n collections as a single one |
---|
| 56 | { |
---|
| 57 | public: |
---|
| 58 | typedef std::vector<SharedPtr<AbstractStrategy<TKey, TValue> > > Strategies; |
---|
| 59 | typedef typename Strategies::iterator Iterator; |
---|
| 60 | typedef typename Strategies::const_iterator ConstIterator; |
---|
| 61 | |
---|
| 62 | public: |
---|
| 63 | StrategyCollection() |
---|
| 64 | { |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | ~StrategyCollection() |
---|
| 68 | { |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | void pushBack(AbstractStrategy<TKey, TValue>* pStrat) |
---|
| 72 | /// Adds an AbstractStrategy to the collection. Class takes ownership of pointer |
---|
| 73 | { |
---|
| 74 | _strategies.push_back(SharedPtr<AbstractStrategy<TKey, TValue> >(pStrat)); |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | void popBack() |
---|
| 78 | /// Removes the last added AbstractStrategy from the collection. |
---|
| 79 | { |
---|
| 80 | _strategies.pop_back(); |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | void onAdd(const void* pSender, const KeyValueArgs <TKey, TValue>& key) |
---|
| 84 | /// Adds the key to the strategy. |
---|
| 85 | /// If for the key already an entry exists, it will be overwritten. |
---|
| 86 | { |
---|
| 87 | Iterator it = _strategies.begin(); |
---|
| 88 | Iterator endIt = _strategies.end(); |
---|
| 89 | for (; it != endIt; ++it) |
---|
| 90 | { |
---|
| 91 | (*it)->onAdd(pSender, key); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void onRemove(const void* pSender, const TKey& key) |
---|
| 96 | /// Removes an entry from the strategy. If the entry is not found |
---|
| 97 | /// the remove is ignored. |
---|
| 98 | { |
---|
| 99 | Iterator it = _strategies.begin(); |
---|
| 100 | Iterator endIt = _strategies.end(); |
---|
| 101 | for (; it != endIt; ++it) |
---|
| 102 | { |
---|
| 103 | (*it)->onRemove(pSender, key); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void onGet(const void* pSender, const TKey& key) |
---|
| 108 | { |
---|
| 109 | Iterator it = _strategies.begin(); |
---|
| 110 | Iterator endIt = _strategies.end(); |
---|
| 111 | for (; it != endIt; ++it) |
---|
| 112 | { |
---|
| 113 | (*it)->onGet(pSender, key); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void onClear(const void* pSender, const EventArgs& args) |
---|
| 118 | { |
---|
| 119 | Iterator it = _strategies.begin(); |
---|
| 120 | Iterator endIt = _strategies.end(); |
---|
| 121 | for (; it != endIt; ++it) |
---|
| 122 | { |
---|
| 123 | (*it)->onClear(pSender, args); |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | void onIsValid(const void* pSender, ValidArgs<TKey>& key) |
---|
| 128 | { |
---|
| 129 | Iterator it = _strategies.begin(); |
---|
| 130 | Iterator endIt = _strategies.end(); |
---|
| 131 | for (; it != endIt && key.isValid(); ++it) |
---|
| 132 | { |
---|
| 133 | (*it)->onIsValid(pSender, key); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | void onReplace(const void* pSender, std::set<TKey>& elemsToRemove) |
---|
| 138 | { |
---|
| 139 | Iterator it = _strategies.begin(); |
---|
| 140 | Iterator endIt = _strategies.end(); |
---|
| 141 | for (; it != endIt; ++it) |
---|
| 142 | { |
---|
| 143 | (*it)->onReplace(pSender, elemsToRemove); |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | protected: |
---|
| 148 | Strategies _strategies; |
---|
| 149 | }; |
---|
| 150 | |
---|
| 151 | |
---|
| 152 | } // namespace Poco |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | #endif |
---|