OpenMW
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
weakcache.hpp
Go to the documentation of this file.
1 #ifndef OPENMW_COMPONENTS_WEAKCACHE_HPP
2 #define OPENMW_COMPONENTS_WEAKCACHE_HPP
3 
4 #include <memory>
5 #include <unordered_map>
6 #include <vector>
7 
8 namespace Misc
9 {
12  template <typename Key, typename T>
13  class WeakCache
14  {
15  public:
16  using WeakPtr = std::weak_ptr<T>;
17  using StrongPtr = std::shared_ptr<T>;
18  using Map = std::unordered_map<Key, WeakPtr>;
19 
20  class iterator
21  {
22  public:
23  iterator(WeakCache* cache, typename Map::iterator current, typename Map::iterator end);
25  bool operator==(const iterator& other);
26  bool operator!=(const iterator& other);
28  private:
30  typename Map::iterator mCurrent, mEnd;
32  };
33 
35  void insert(Key key, StrongPtr value, bool prune=true);
36 
39  StrongPtr get(Key key);
40 
41  iterator begin();
42  iterator end();
43 
45  void prune();
46 
47  private:
49  std::vector<Key> mDirty;
50  };
51 
52 
53  template <typename Key, typename T>
54  WeakCache<Key, T>::iterator::iterator(WeakCache* cache, typename Map::iterator current, typename Map::iterator end)
55  : mCache(cache)
56  , mCurrent(current)
57  , mEnd(end)
58  {
59  // Move to 1st available valid item
60  for ( ; mCurrent != mEnd; ++mCurrent)
61  {
62  mPtr = mCurrent->second.lock();
63  if (mPtr) break;
64  else mCache->mDirty.push_back(mCurrent->first);
65  }
66  }
67 
68  template <typename Key, typename T>
70  {
71  auto next = mCurrent;
72  ++next;
73  return *this = iterator(mCache, next, mEnd);
74  }
75 
76  template <typename Key, typename T>
78  {
79  return mCurrent == other.mCurrent;
80  }
81 
82  template <typename Key, typename T>
84  {
85  return !(*this == other);
86  }
87 
88  template <typename Key, typename T>
90  {
91  return mPtr;
92  }
93 
94 
95  template <typename Key, typename T>
96  void WeakCache<Key, T>::insert(Key key, StrongPtr value, bool shouldPrune)
97  {
98  mData[key] = WeakPtr(value);
99  if (shouldPrune) prune();
100  }
101 
102  template <typename Key, typename T>
104  {
105  auto searchIt = mData.find(key);
106  if (searchIt != mData.end())
107  return searchIt->second.lock();
108  else
109  return StrongPtr();
110  }
111 
112  template <typename Key, typename T>
114  {
115  return iterator(this, mData.begin(), mData.end());
116  }
117 
118  template <typename Key, typename T>
120  {
121  return iterator(this, mData.end(), mData.end());
122  }
123 
124  template <typename Key, typename T>
126  {
127  // Remove empty entries
128  for (auto& key : mDirty)
129  {
130  auto it = mData.find(key);
131  if (it != mData.end() && it->second.use_count() == 0)
132  mData.erase(it);
133  }
134  mDirty.clear();
135  }
136 }
137 
138 #endif
std::unordered_map< std::string, WeakPtr > Map
Definition: weakcache.hpp:18
iterator end()
Definition: weakcache.hpp:119
StrongPtr get(Key key)
Definition: weakcache.hpp:103
Map::iterator mCurrent
Definition: weakcache.hpp:30
bool operator==(const iterator &other)
Definition: weakcache.hpp:77
iterator & operator++()
Definition: weakcache.hpp:69
StrongPtr mPtr
Definition: weakcache.hpp:31
void prune()
Removes known invalid entries.
Definition: weakcache.hpp:125
Definition: weakcache.hpp:20
Map mData
Definition: weakcache.hpp:48
std::weak_ptr< CSMWorld::ActorAdapter::RaceData > WeakPtr
Definition: weakcache.hpp:16
std::shared_ptr< CSMWorld::ActorAdapter::RaceData > StrongPtr
Definition: weakcache.hpp:17
Definition: weakcache.hpp:13
void insert(Key key, StrongPtr value, bool prune=true)
Stores a weak pointer to the item.
Definition: weakcache.hpp:96
Map::iterator mEnd
Definition: weakcache.hpp:30
std::vector< Key > mDirty
Definition: weakcache.hpp:49
iterator(WeakCache *cache, typename Map::iterator current, typename Map::iterator end)
Definition: weakcache.hpp:54
WeakCache * mCache
Definition: weakcache.hpp:29
bool operator!=(const iterator &other)
Definition: weakcache.hpp:83
StrongPtr operator*()
Definition: weakcache.hpp:89
iterator begin()
Definition: weakcache.hpp:113