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
16
17#ifndef Foundation_StrategyCollection_INCLUDED
18#define Foundation_StrategyCollection_INCLUDED
19
20
21#include "Poco/KeyValueArgs.h"
22#include "Poco/ValidArgs.h"
23#include "Poco/AbstractStrategy.h"
24#include "Poco/SharedPtr.h"
25#include <vector>
26
27
28namespace Poco {
29
30
31template <class TKey, class TValue>
32class StrategyCollection: public AbstractStrategy<TKey, TValue>
33 /// An StrategyCollection is a decorator masking n collections as a single one.
34{
35public:
36 typedef std::vector<SharedPtr<AbstractStrategy<TKey, TValue> > > Strategies;
37 typedef typename Strategies::iterator Iterator;
38 typedef typename Strategies::const_iterator ConstIterator;
39
40public:
41 StrategyCollection()
42 {
43 }
44
45 ~StrategyCollection()
46 {
47 }
48
49 void pushBack(AbstractStrategy<TKey, TValue>* pStrat)
50 /// Adds an AbstractStrategy to the collection. Class takes ownership of pointer
51 {
52 _strategies.push_back(SharedPtr<AbstractStrategy<TKey, TValue> >(pStrat));
53 }
54
55 void popBack()
56 /// Removes the last added AbstractStrategy from the collection.
57 {
58 _strategies.pop_back();
59 }
60
61 void onAdd(const void* pSender, const KeyValueArgs <TKey, TValue>& key)
62 /// Adds the key to the strategy.
63 /// If for the key already an entry exists, it will be overwritten.
64 {
65 Iterator it = _strategies.begin();
66 Iterator endIt = _strategies.end();
67 for (; it != endIt; ++it)
68 {
69 (*it)->onAdd(pSender, key);
70 }
71 }
72
73 void onRemove(const void* pSender, const TKey& key)
74 /// Removes an entry from the strategy. If the entry is not found
75 /// the remove is ignored.
76 {
77 Iterator it = _strategies.begin();
78 Iterator endIt = _strategies.end();
79 for (; it != endIt; ++it)
80 {
81 (*it)->onRemove(pSender, key);
82 }
83 }
84
85 void onGet(const void* pSender, const TKey& key)
86 {
87 Iterator it = _strategies.begin();
88 Iterator endIt = _strategies.end();
89 for (; it != endIt; ++it)
90 {
91 (*it)->onGet(pSender, key);
92 }
93 }
94
95 void onClear(const void* pSender, const EventArgs& args)
96 {
97 Iterator it = _strategies.begin();
98 Iterator endIt = _strategies.end();
99 for (; it != endIt; ++it)
100 {
101 (*it)->onClear(pSender, args);
102 }
103 }
104
105 void onIsValid(const void* pSender, ValidArgs<TKey>& key)
106 {
107 Iterator it = _strategies.begin();
108 Iterator endIt = _strategies.end();
109 for (; it != endIt && key.isValid(); ++it)
110 {
111 (*it)->onIsValid(pSender, key);
112 }
113 }
114
115 void onReplace(const void* pSender, std::set<TKey>& elemsToRemove)
116 {
117 Iterator it = _strategies.begin();
118 Iterator endIt = _strategies.end();
119 for (; it != endIt; ++it)
120 {
121 (*it)->onReplace(pSender, elemsToRemove);
122 }
123 }
124
125protected:
126 Strategies _strategies;
127};
128
129
130} // namespace Poco
131
132
133#endif // Foundation_StrategyCollection_INCLUDED
134