Fillwave  10.0.0
TCache.h
1 #pragma once
2 
3 /*
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2018 Filip Wasil and Fillwave community members
7  *
8  * Permission is hereby granted, free of charge, to any person
9  * obtaining a copy of this software and associated documentation files (the "Software"),
10  * to deal in the Software without restriction, including without limitation the rights
11  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
18  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22  */
23 
24 #include <unordered_map>
25 
26 namespace flw {
27 namespace flf {
28 
29 constexpr size_t MAX_CACHE_SIZE = 1000;
30 
43 template <size_t M, class T, class K, typename ... P>
44 struct TCache final {
45  private:
46  std::unordered_map<K, std::unique_ptr<T>> mStored;
47 
48  public:
52  T *store(const K &key, const P&... parameters) {
53  if (mStored.find(key) != mStored.end()) {
54  return mStored[key].get();
55  }
56  if (mStored.size() >= M) {
57  return nullptr;
58  }
59  mStored[key] = std::make_unique<T>(parameters...);
60  return mStored[key].get();
61  }
62 
66  T *store(T *item, const K &key) {
67  if (mStored.find(key) != mStored.end()) {
68  if (nullptr != item) {
69  delete item;
70  }
71  return mStored[key].get();
72  }
73  if (mStored.size() >= M) {
74  return nullptr;
75  }
76  mStored[key] = std::unique_ptr<T>(item);
77  return mStored[key].get();
78  }
79 
83  T* get(const K &key) {
84  if (mStored.find(key) != mStored.end()) {
85  return mStored[key].get();
86  }
87  return nullptr;
88  }
89 
93  decltype(std::begin(mStored)) begin() {
94  return std::begin(mStored);
95  }
96 
100  decltype(std::end(mStored)) end() {
101  return std::end(mStored);
102  }
103 
107  decltype(mStored.erase(K())) erase (const K& key) {
108  return mStored.erase(key);
109  }
110 
114  size_t size() {
115  return mStored.size();
116  }
117 };
118 
119 } /* flf */
120 } /* flw */
Definition: Aliases.h:30
decltype(mStored.erase(K())) erase(const K &key)
erase
Definition: TCache.h:107
T * store(T *item, const K &key)
Add already allocated item to manager.
Definition: TCache.h:66
T * store(const K &key, const P &...parameters)
Add new allocated item to manager.
Definition: TCache.h:52
Basic manager of composites.
Definition: TCache.h:44
decltype(std::end(mStored)) end()
end
Definition: TCache.h:100
size_t size()
return current size
Definition: TCache.h:114
decltype(std::begin(mStored)) begin()
begin
Definition: TCache.h:93