Frobby
0.10.0
Toggle main menu visibility
Loading...
Searching...
No Matches
src
ObjectCache.h
1
/* Frobby: Software for monomial ideal computations.
2
Copyright (C) 2009 University of Aarhus
3
Contact Bjarke Hammersholt Roune for license information (www.broune.com)
4
5
This program is free software; you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation; either version 2 of the License, or
8
(at your option) any later version.
9
10
This program is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see http://www.gnu.org/licenses/.
17
*/
18
#ifndef OBJECT_CACHE_GUARD
19
#define OBJECT_CACHE_GUARD
20
21
#include "ElementDeleter.h"
22
23
#include <vector>
24
34
template
<
class
T>
35
class
ObjectCache {
36
public
:
37
ObjectCache();
38
42
unique_ptr<T>
newObject
();
43
48
template
<
class
S>
49
unique_ptr<T>
newObjectCopy
(
const
S& copyOf);
50
52
template
<
class
S>
53
void
freeObject
(unique_ptr<S>
object
);
54
55
private
:
56
vector<T*> _cache;
57
ElementDeleter<vector<T*>
> _cacheDeleter;
58
};
59
60
// The implementation has to be inline because we are using templates.
61
62
template
<
class
T>
63
inline
ObjectCache<T>::ObjectCache():
64
_cache(),
65
_cacheDeleter(_cache) {
66
}
67
68
template
<
class
T>
69
unique_ptr<T>
ObjectCache<T>::newObject
() {
70
if
(_cache.empty())
71
return
unique_ptr<T>(FROBBY_NEW T());
72
73
unique_ptr<T> object(_cache.back());
74
_cache.pop_back();
75
return
object;
76
}
77
78
template
<
class
T>
template
<
class
S>
79
unique_ptr<T>
ObjectCache<T>::newObjectCopy
(
const
S& copyOf) {
80
if
(_cache.empty())
81
return
unique_ptr<T>(FROBBY_NEW T(copyOf));
82
83
unique_ptr<T> object(_cache.back());
84
_cache.pop_back();
85
*
object
= copyOf;
86
return
object;
87
}
88
89
template
<
class
T>
template
<
class
S>
90
void
ObjectCache<T>::freeObject
(unique_ptr<S>
object
) {
91
ASSERT(
dynamic_cast<
T*
>
(
object
.get()) != 0);
92
93
unique_ptr<T> casted(
static_cast<
T*
>
(
object
.release()));
94
noThrowPushBack(_cache, std::move(casted));
95
}
96
97
#endif
ElementDeleter
Definition
ElementDeleter.h:50
ObjectCache::newObjectCopy
unique_ptr< T > newObjectCopy(const S ©Of)
Definition
ObjectCache.h:79
ObjectCache::newObject
unique_ptr< T > newObject()
Definition
ObjectCache.h:69
ObjectCache::freeObject
void freeObject(unique_ptr< S > object)
Definition
ObjectCache.h:90
Generated by
1.17.0