Euphoria
cache.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <map>
4 #include <memory>
5 
6 
7 namespace eu::core
8 {
9  // todo(Gustav): support hotloading
10  // todo(Gustav): rename to something better?
11  // https://stackoverflow.com/questions/288400/guidelines-for-differences-between-cache-pool-cache-pool
12 
13  template <typename TKey, typename TData, typename TLoader>
14  struct Cache
15  {
16  std::map<TKey, std::weak_ptr<TData>> store;
17 
18  std::shared_ptr<TData> get(const TKey& key)
19  {
20  auto found = store.find(key);
21  if(found != store.end())
22  {
23  auto cached = found->second.lock();
24  if(cached)
25  {
26  return cached;
27  }
28  else
29  {
30  store.erase(found);
31  }
32  }
33 
34  std::shared_ptr<TData> data = static_cast<TLoader *>(this)->create(key);
35  store.insert(std::pair<TKey, std::weak_ptr<TData>>(key, data));
36  return data;
37  }
38  };
39 }
std::map< TKey, std::weak_ptr< TData > > store
Definition: cache.h:16
std::shared_ptr< TData > get(const TKey &key)
Definition: cache.h:18