TCachedUpdateManagerWrap.h
Go to the documentation of this file.
1 #ifndef istd_TCachedUpdateManagerWrap_included
2 #define istd_TCachedUpdateManagerWrap_included
3 
4 
5 #include <istd/IChangeable.h>
6 
7 
8 namespace istd
9 {
10 
11 
15 template <class Base>
16 class TCachedUpdateManagerWrap: virtual public Base
17 {
18 public:
19  typedef Base BaseClass;
20 
22 
23  // reimplemented (istd::IChangeable)
24  virtual void BeginChanges(const IChangeable::ChangeSet& changeSet);
25  virtual void EndChanges(const IChangeable::ChangeSet& changeSet);
26 
27 protected:
32  bool EnsureCacheValid() const;
37  void InvalidateCache(const IChangeable::ChangeSet& changeSet);
41  void SetCacheValid();
48  virtual bool CalculateCache(const IChangeable::ChangeSet& changeSet);
49 
50 private:
51  int m_changesCounter;
52  mutable bool m_isCacheValid;
53  mutable IChangeable::ChangeSet m_cumulatedSet;
54 };
55 
56 
57 // public inline methods
58 
59 // reimplemented (istd::IChangeable)
60 
61 template <class Base>
63 {
64  Q_ASSERT(m_changesCounter >= 0);
65 
66  InvalidateCache(changeSet);
67 
68  BaseClass::BeginChanges(changeSet);
69 
70  ++m_changesCounter;
71 }
72 
73 
74 template <class Base>
76 {
77  --m_changesCounter;
78  Q_ASSERT(m_changesCounter >= 0);
79 
80  BaseClass::EndChanges(changeSet);
81 }
82 
83 
84 // protected inline methods
85 
86 template <class Base>
88 {
89  if (!m_isCacheValid && (m_changesCounter <= 0)){
90  m_isCacheValid = true; // set to avoid recursion
91 
92  m_isCacheValid = const_cast<TCachedUpdateManagerWrap<Base>*>(this)->CalculateCache(m_cumulatedSet);
93 
94  m_cumulatedSet.Reset();
95  }
96 
97  return m_isCacheValid;
98 }
99 
100 
101 template <class Base>
103 {
104  m_cumulatedSet += changeSet;
105 
106  m_isCacheValid = false;
107 }
108 
109 
110 template <class Base>
112 {
113  m_cumulatedSet.Reset();
114 
115  m_isCacheValid = true;
116 }
117 
118 
119 template <class Base>
121 {
122  return true;
123 }
124 
125 
126 // public methods
127 
128 template <class Base>
130 : m_changesCounter(0),
131  m_isCacheValid(false)
132 {
133 }
134 
135 
136 } // namespace istd
137 
138 
139 #endif // !istd_TCachedUpdateManagerWrap_included
140 
141 
void InvalidateCache(const IChangeable::ChangeSet &changeSet)
Make cache invalid.
bool EnsureCacheValid() const
Ensures cache is valid.
void SetCacheValid()
Say that cache is valid.
virtual void BeginChanges(const IChangeable::ChangeSet &changeSet)
Help wrapper class supporting of cached parts.
virtual bool CalculateCache(const IChangeable::ChangeSet &changeSet)
Calculate cache.
Set of change flags (its IDs).
Definition: IChangeable.h:30
virtual void EndChanges(const IChangeable::ChangeSet &changeSet)

© 2007-2017 Witold Gantzke and Kirill Lepskiy