ICF 3.0.5.47
Technical documentation of ICF Libraries
TMorphLockedProducerBase.h
Go to the documentation of this file.
1/********************************************************************************
2** This file is part of the ICF Framework. Copyright (C) Witold Gantzke & Kirill Lepskiy
3** ICF Framework may be used under the terms of the LGPL License v. 2.1 by the Free Software Foundation.
4********************************************************************************/
5
6#ifndef iproc_TMorphLockedProducerBase_included
7#define iproc_TMorphLockedProducerBase_included
8
9
10// Qt includes
11#include <QtCore/QList>
12
13// ICF includes
15
16
17namespace iproc
18{
19
20
21template <class Key, class CacheObject, class SourceObject>
22class TMorphLockedProducerBase: public TILockedProducer<Key, CacheObject>
23{
24public:
26
27 double GetMaxCumulatedWeight() const;
28 void SetMaxCumulatedWeight(double value);
29
30 // reimplemented (iproc::TILockedProducer)
31 virtual const CacheObject* ProduceLockedObject(const Key& key);
32 virtual void UnlockObject(const CacheObject* objectPtr);
33
34protected:
38 void CleanElementList();
39
40 // abstract methods
45 virtual double CalcCacheObject(
46 const Key& key,
47 const SourceObject& source,
48 CacheObject& cache) const = 0;
49 virtual const SourceObject* LockSourceObject(const Key& key) = 0;
50 virtual void UnlockSourceObject(const Key& key, const SourceObject* sourcePtr) = 0;
51
52private:
53 double m_maxCumulatedWeight;
54
55 mutable double m_cumulatedWeight;
56
57 struct ListElement
58 {
59 bool operator==(const Key& key)
60 {
61 return this->key == key;
62 }
63
64 Key key;
65 double weight;
66 std::unique_ptr<CacheObject> objectPtr;
67 int lockedCount;
68 };
69
70 typedef QList<ListElement> CachedList;
71
72 CachedList m_cachedList;
73};
74
75
76// public methods
77
78template <class Key, class CacheObject, class SourceObject>
80: m_maxCumulatedWeight(10), m_cumulatedWeight(0)
81{
82}
83
84
85template <class Key, class CacheObject, class SourceObject>
87{
88 return m_maxCumulatedWeight;
89}
90
91
92template <class Key, class CacheObject, class SourceObject>
94{
95 m_maxCumulatedWeight = value;
96
97 CleanElementList();
98}
99
100
101// reimplemented (iproc::TILockedProducer)
102
103template <class Key, class CacheObject, class SourceObject>
105{
106 typename CachedList::iterator foundIter = std::find(m_cachedList.begin(), m_cachedList.end(), key);
107 if (foundIter != m_cachedList.end()){
108 foundIter->lockedCount++;
109
110 return foundIter->objectPtr.get();
111 }
112
113 const SourceObject* sourcePtr = LockSourceObject(key);
114 if (sourcePtr != nullptr){
115 std::unique_ptr<CacheObject> newObjectPtr(new CacheObject);
116 if (newObject == nullptr){
117 return nullptr;
118 }
119
120 double weight = CalcCacheObject(key, *sourcePtr, *newObjectPtr);
121
122 UnlockSourceObject(key, sourcePtr);
123
124 if (weight < 0){
125 return nullptr;
126 }
127
128 m_cachedList.push_back(ListElement());
129
130 ListElement& element = m_cachedList.back();
131
132 element.key = key;
133 element.objectPtr = std::move(newObjectPtr);
134 element.weight = weight;
135 element.lockedCount = 1;
136
137 m_cumulatedWeight += weight;
138
139 CleanElementList();
140
141 return element.objectPtr.get();
142 }
143
144 return nullptr;
145}
146
147
148template <class Key, class CacheObject, class SourceObject>
150{
151 for ( typename CachedList::iterator iter = m_cachedList.begin();
152 iter != m_cachedList.end();
153 ++iter){
154 if (iter->objectPtr.get() == objectPtr){
155 if (--iter->lockedCount <= 0){
156 m_cachedList.erase(iter);
157 }
158
159 break;
160 }
161 }
162}
163
164
165// protected methods
166
167template <class Key, class CacheObject, class SourceObject>
169{
170 typename CachedList::iterator iter = m_cachedList.begin();
171 while ( (m_cumulatedWeight > m_maxCumulatedWeight) &&
172 (iter != m_cachedList.end())){
173 if (iter->lockedCount <= 0){
174 m_cumulatedWeight -= iter->weight;
175
176 iter = m_cachedList.erase(iter);
177 }
178 else{
179 ++iter;
180 }
181 }
182}
183
184
185} // namespace iproc
186
187
188#endif // !iproc_TMorphLockedProducerBase_included
189
190
Template interface for providers of cached data.
virtual double CalcCacheObject(const Key &key, const SourceObject &source, CacheObject &cache) const =0
Calculate cache object from source object.
virtual const CacheObject * ProduceLockedObject(const Key &key)
Begin of accessing to cached element.
virtual const SourceObject * LockSourceObject(const Key &key)=0
void CleanElementList()
Remove elements from list if cumulated weight is above defined maximum.
virtual void UnlockObject(const CacheObject *objectPtr)
End of accessing to cached element.
virtual void UnlockSourceObject(const Key &key, const SourceObject *sourcePtr)=0
This namespace containes interfaces and implementation of data processing concepts.

© Witold Gantzke and Kirill Lepskiy