Changeset 36915 in vbox
- Timestamp:
- May 3, 2011 6:29:12 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/settings/UISettingsDefs.h
r36589 r36915 20 20 #define __UISettingsDefs_h__ 21 21 22 /* Qt includes: */ 23 #include <QPair> 24 #include <QMap> 25 22 26 /* VBox includes: */ 23 27 #include "COMDefs.h" … … 39 43 } 40 44 45 /* Template to operate settings cache item: */ 46 template <class CacheData> class UISettingsCache 47 { 48 public: 49 50 /* Creates empty cache item: */ 51 UISettingsCache() { m_value = qMakePair(CacheData(), CacheData()); } 52 53 /* Returns the NON-modifiable REFERENCE to the initial cached data: */ 54 const CacheData& base() const { return m_value.first; } 55 /* Returns the NON-modifiable REFERENCE to the current cached data: */ 56 const CacheData& data() const { return m_value.second; } 57 58 /* We assume that old cache item was removed if 59 * initial data was set but current data was NOT set. 60 * Returns 'true' if that cache item was removed: */ 61 virtual bool wasRemoved() const { return base() != CacheData() && data() == CacheData(); } 62 63 /* We assume that new cache item was created if 64 * initial data was NOT set but current data was set. 65 * Returns 'true' if that cache item was created: */ 66 virtual bool wasCreated() const { return base() == CacheData() && data() != CacheData(); } 67 68 /* We assume that old cache item was updated if 69 * current and initial data were both set and not equal to each other. 70 * Returns 'true' if that cache item was updated: */ 71 virtual bool wasUpdated() const { return base() != CacheData() && data() != CacheData() && data() != base(); } 72 73 /* We assume that old cache item was actually changed if 74 * 1. this item was removed or 75 * 2. this item was created or 76 * 3. this item was updated. 77 * Returns 'true' if that cache item was actually changed: */ 78 virtual bool wasChanged() const { return wasRemoved() || wasCreated() || wasUpdated(); } 79 80 /* Set initial cache item data: */ 81 void cacheInitialData(const CacheData &initialData) { m_value.first = initialData; } 82 /* Set current cache item data: */ 83 void cacheCurrentData(const CacheData ¤tData) { m_value.second = currentData; } 84 85 /* Reset the current data to be empty: */ 86 void reset() { m_value.second = CacheData(); } 87 88 /* Reset the initial and the current data to be both empty: */ 89 void clear() { m_value.first = CacheData(); m_value.second = CacheData(); } 90 91 private: 92 93 /* Data: */ 94 QPair<CacheData, CacheData> m_value; 95 }; 96 97 /* Template to operate settings cache item with children: */ 98 template <class ParentCacheData, class ChildCacheData> class UISettingsCachePool : public UISettingsCache<ParentCacheData> 99 { 100 public: 101 102 /* Typedefs: */ 103 typedef QMap<QString, ChildCacheData> UISettingsCacheChildMap; 104 typedef QMapIterator<QString, ChildCacheData> UISettingsCacheChildIterator; 105 106 /* Creates empty cache item: */ 107 UISettingsCachePool() : UISettingsCache<ParentCacheData>() {} 108 109 /* Returns the modifiable REFERENCE to the particular child cached data. 110 * If child with such key or index is NOT present, 111 * both those methods will create the new one to return: */ 112 ChildCacheData& child(const QString &strChildKey) { return m_children[strChildKey]; } 113 ChildCacheData& child(int iIndex) { return child(indexToKey(iIndex)); } 114 115 /* Returns the NON-modifiable COPY to the particular child cached data. 116 * If child with such key or index is NOT present, 117 * both those methods will create the new one to return: */ 118 const ChildCacheData child(const QString &strChildKey) const { return m_children[strChildKey]; } 119 const ChildCacheData child(int iIndex) const { return child(indexToKey(iIndex)); } 120 121 /* Children count: */ 122 int childCount() const { return m_children.size(); } 123 124 /* We assume that old cache item was updated if 125 * current and initial data were both set and not equal to each other. 126 * Takes into account all the children. 127 * Returns 'true' if that cache item was updated: */ 128 bool wasUpdated() const 129 { 130 /* First of all, cache item is considered to be updated if parent data was updated: */ 131 bool fWasUpdated = UISettingsCache<ParentCacheData>::wasUpdated(); 132 /* If parent data was NOT updated but also was NOT created or removed too (e.j. was NOT changed at all), 133 * we have to check children too: */ 134 if (!fWasUpdated && !UISettingsCache<ParentCacheData>::wasRemoved() && !UISettingsCache<ParentCacheData>::wasCreated()) 135 { 136 for (int iChildIndex = 0; !fWasUpdated && iChildIndex < childCount(); ++iChildIndex) 137 if (child(iChildIndex).wasChanged()) 138 fWasUpdated = true; 139 } 140 return fWasUpdated; 141 } 142 143 /* Reset the current data to be empty. 144 * Perform the same for all the children: */ 145 void reset() 146 { 147 UISettingsCache<ParentCacheData>::reset(); 148 for (int iChildIndex = 0; iChildIndex < childCount(); ++iChildIndex) 149 child(iChildIndex).reset(); 150 } 151 152 /* Reset the initial and the current data to be both empty. 153 * Perform the same for all the children: */ 154 void clear() 155 { 156 UISettingsCache<ParentCacheData>::clear(); 157 for (int iChildIndex = 0; iChildIndex < childCount(); ++iChildIndex) 158 child(iChildIndex).clear(); 159 } 160 161 private: 162 163 QString indexToKey(int iIndex) const 164 { 165 UISettingsCacheChildIterator childIterator(m_children); 166 for (int iChildIndex = 0; childIterator.hasNext(); ++iChildIndex) 167 { 168 childIterator.next(); 169 if (iChildIndex == iIndex) 170 return childIterator.key(); 171 } 172 return QString::number(iIndex); 173 } 174 175 /* Children: */ 176 UISettingsCacheChildMap m_children; 177 }; 178 41 179 #endif /* __UISettingsDefs_h__ */ 42 180
Note:
See TracChangeset
for help on using the changeset viewer.