TMorphLockedProducerBase.h
Go to the documentation of this file.
1 #ifndef iproc_TMorphLockedProducerBase_included
2 #define iproc_TMorphLockedProducerBase_included
3 
4 
5 // Qt includes
6 #include <QtCore/QList>
7 
8 // ACF includes
9 #include "istd/TDelPtr.h"
10 #include "iproc/TILockedProducer.h"
11 
12 
13 namespace iproc
14 {
15 
16 
17 template <class Key, class CacheObject, class SourceObject>
18 class TMorphLockedProducerBase: public TILockedProducer<Key, CacheObject>
19 {
20 public:
22 
23  double GetMaxCumulatedWeight() const;
24  void SetMaxCumulatedWeight(double value);
25 
26  // reimplemented (iproc::TILockedProducer)
27  virtual const CacheObject* ProduceLockedObject(const Key& key);
28  virtual void UnlockObject(const CacheObject* objectPtr);
29 
30 protected:
34  void CleanElementList();
35 
36  // abstract methods
41  virtual double CalcCacheObject(
42  const Key& key,
43  const SourceObject& source,
44  CacheObject& cache) const = 0;
45  virtual const SourceObject* LockSourceObject(const Key& key) = 0;
46  virtual void UnlockSourceObject(const Key& key, const SourceObject* sourcePtr) = 0;
47 
48 private:
49  double m_maxCumulatedWeight;
50 
51  mutable double m_cumulatedWeight;
52 
53  struct ListElement
54  {
55  bool operator==(const Key& key)
56  {
57  return this->key == key;
58  }
59 
60  Key key;
61  double weight;
63  int lockedCount;
64  };
65 
66  typedef QList<ListElement> CachedList;
67 
68  CachedList m_cachedList;
69 };
70 
71 
72 // public methods
73 
74 template <class Key, class CacheObject, class SourceObject>
76 : m_maxCumulatedWeight(10), m_cumulatedWeight(0)
77 {
78 }
79 
80 
81 template <class Key, class CacheObject, class SourceObject>
83 {
84  return m_maxCumulatedWeight;
85 }
86 
87 
88 template <class Key, class CacheObject, class SourceObject>
90 {
91  m_maxCumulatedWeight = value;
92 
93  CleanElementList();
94 }
95 
96 
97 // reimplemented (iproc::TILockedProducer)
98 
99 template <class Key, class CacheObject, class SourceObject>
101 {
102  typename CachedList::iterator foundIter = qFind(m_cachedList.begin(), m_cachedList.end(), key);
103  if (foundIter != m_cachedList.end()){
104  foundIter->lockedCount++;
105 
106  return foundIter->objectPtr.GetPtr();
107  }
108 
109  const SourceObject* sourcePtr = LockSourceObject(key);
110  if (sourcePtr != NULL){
111  istd::TDelPtr<CacheObject> newObjectPtr(new CacheObject);
112  if (!newObjectPtr.IsValid()){
113  return NULL;
114  }
115 
116  double weight = CalcCacheObject(key, *sourcePtr, *newObjectPtr);
117 
118  UnlockSourceObject(key, sourcePtr);
119 
120  if (weight < 0){
121  return NULL;
122  }
123 
124  m_cachedList.push_back(ListElement());
125 
126  ListElement& element = m_cachedList.back();
127 
128  element.key = key;
129  element.objectPtr.TakeOver(newObjectPtr);
130  element.weight = weight;
131  element.lockedCount = 1;
132 
133  m_cumulatedWeight += weight;
134 
135  CleanElementList();
136 
137  return element.objectPtr.GetPtr();
138  }
139 
140  return NULL;
141 }
142 
143 
144 template <class Key, class CacheObject, class SourceObject>
146 {
147  for ( typename CachedList::iterator iter = m_cachedList.begin();
148  iter != m_cachedList.end();
149  ++iter){
150  if (iter->objectPtr.GetPtr() == objectPtr){
151  if (--iter->lockedCount <= 0){
152  m_cachedList.erase(iter);
153  }
154 
155  break;
156  }
157  }
158 }
159 
160 
161 // protected methods
162 
163 template <class Key, class CacheObject, class SourceObject>
165 {
166  typename CachedList::iterator iter = m_cachedList.begin();
167  while ( (m_cumulatedWeight > m_maxCumulatedWeight) &&
168  (iter != m_cachedList.end())){
169  if (iter->lockedCount <= 0){
170  m_cumulatedWeight -= iter->weight;
171 
172  iter = m_cachedList.erase(iter);
173  }
174  else{
175  ++iter;
176  }
177  }
178 }
179 
180 
181 } // namespace iproc
182 
183 
184 #endif // !iproc_TMorphLockedProducerBase_included
185 
186 
virtual void UnlockObject(const CacheObject *objectPtr)
End of accessing to cached element.
void CleanElementList()
Remove elements from list if cumulated weight is above defined maximum.
bool IsValid() const
virtual const SourceObject * LockSourceObject(const Key &key)=0
Template interface for providers of cached data.
#define NULL
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 void UnlockSourceObject(const Key &key, const SourceObject *sourcePtr)=0

© 2007-2011 Witold Gantzke and Kirill Lepskiy