-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcache.cpp
61 lines (54 loc) · 1.72 KB
/
cache.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* Copyright (C) 2012 Giuliano Schneider <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "cache.hpp"
cache::cache() : _functions (new std::map<statusfunc_t *, _cacheFunction_t>)
{
}
cache::~cache()
{
delete _functions;
}
void cache::add(const statusfunc_t *function, unsigned int calls)
{
_cacheFunction_t tmp(calls);
_functions->insert(std::pair<const statusfunc_t *, _cacheFunction_t>(function, tmp));
}
std::string cache::get(const statusfunc_t *function, bool forceCall)
{
auto it = _functions->find(function);
_cacheFunction_t *tmp = &((*it).second);
if (tmp->getLastCall() % tmp->getCalls() == 0 || forceCall) {
tmp->setReturnValue(((*it).first)());
tmp->setLastCall(0);
}
tmp->incrementLastCall();
return tmp->getReturnValue();
}
void cache::refresh(const statusfunc_t *function)
{
auto it = _functions->find(function);
(*it).second.setLastCall(0);
}
bool cache::remove(const statusfunc_t *function)
{
auto it = _functions->find(function);
if (it != _functions->end()) {
_functions->erase(it);
return true;
}
return false;
}