Fix INT_MAX definition on some newer systems.
[Tsunagari.git] / src / cache-template.cpp
blob5815a2bec74af2e2f4911ddc9440fd88ca30d510
1 /***************************************
2 ** Tsunagari Tile Engine **
3 ** cache.cpp **
4 ** Copyright 2011-2013 PariahSoft LLC **
5 ***************************************/
7 // **********
8 // Permission is hereby granted, free of charge, to any person obtaining a copy
9 // of this software and associated documentation files (the "Software"), to
10 // deal in the Software without restriction, including without limitation the
11 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12 // sell copies of the Software, and to permit persons to whom the Software is
13 // furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 // IN THE SOFTWARE.
25 // **********
27 #include <vector>
29 #include "cache.h"
30 #include "client-conf.h"
31 #include "log.h"
32 #include "window.h"
34 #define IN_USE_NOW -1
36 template<class T>
37 T Cache<T>::momentaryRequest(const std::string& name)
39 if (conf.cacheEnabled) {
40 CacheMapIter it = map.find(name);
41 if (it != map.end()) {
42 // Log::info("Cache", name + ": requested (cached)");
43 CacheEntry& entry = it->second;
44 // Set lastUsed to now because it won't be used
45 // by the time garbageCollect() gets to it.
46 entry.lastUsed = GameWindow::instance().time();
47 return entry.resource;
50 Log::info("Cache", name + ": requested");
51 return T();
54 template<class T>
55 T Cache<T>::lifetimeRequest(const std::string& name)
57 if (conf.cacheEnabled) {
58 typename CacheMap::iterator it = map.find(name);
59 if (it != map.end()) {
60 // Log::info("Cache", name + ": requested (cached)");
61 CacheEntry& entry = it->second;
62 entry.lastUsed = IN_USE_NOW;
63 return entry.resource;
66 Log::info("Cache", name + ": requested");
67 return T();
70 template<class T>
71 void Cache<T>::momentaryPut(const std::string& name, T data)
73 if (!conf.cacheEnabled)
74 return;
75 CacheEntry entry;
76 entry.resource = data;
77 time_t now = GameWindow::instance().time();
78 entry.lastUsed = now;
79 map[name] = entry;
82 template<class T>
83 void Cache<T>::lifetimePut(const std::string& name, T data)
85 if (!conf.cacheEnabled)
86 return;
87 CacheEntry entry;
88 entry.resource = data;
89 entry.lastUsed = IN_USE_NOW;
90 map[name] = entry;
93 template<class T>
94 void Cache<T>::garbageCollect()
96 if (!conf.cacheEnabled)
97 return;
98 time_t now = GameWindow::instance().time();
99 typedef std::vector<std::string> StringVector;
100 StringVector dead;
101 for (CacheMapIter it = map.begin(); it != map.end(); it++) {
102 const std::string& name = it->first;
103 CacheEntry& cache = it->second;
104 bool unused = !cache.resource || cache.resource.unique();
105 if (!unused)
106 continue;
107 if (cache.lastUsed == IN_USE_NOW) {
108 cache.lastUsed = now;
109 // Log::info("Cache", name + ": unused");
111 else if (now > cache.lastUsed + conf.cacheTTL*1000) {
112 dead.push_back(name);
113 Log::info("Cache", name + ": purged");
116 for (StringVector::iterator it = dead.begin(); it != dead.end(); it++)
117 map.erase(*it);