Last change
on this file was
12340,
checked in by westram, 10 years ago
|
- reimplement GB_keep_string using template Keeper<char*>
|
File size:
1.2 KB
|
Line | |
---|
1 | // =========================================================== // |
---|
2 | // // |
---|
3 | // File : Keeper.h // |
---|
4 | // Purpose : Keep allocated data until termination // |
---|
5 | // // |
---|
6 | // Coded by Ralf Westram (coder@reallysoft.de) in May 2014 // |
---|
7 | // http://www.arb-home.de/ // |
---|
8 | // // |
---|
9 | // =========================================================== // |
---|
10 | |
---|
11 | #ifndef KEEPER_H |
---|
12 | #define KEEPER_H |
---|
13 | |
---|
14 | #ifndef _GLIBCXX_LIST |
---|
15 | #include <list> |
---|
16 | #endif |
---|
17 | |
---|
18 | template <typename T> |
---|
19 | class Keeper { |
---|
20 | typedef std::list<T> kept; |
---|
21 | kept elems; |
---|
22 | |
---|
23 | void destroy(T elem); // needs to be declared for custom T |
---|
24 | |
---|
25 | public: |
---|
26 | Keeper() {} |
---|
27 | ~Keeper() { |
---|
28 | for (typename kept::iterator i = elems.begin(); i != elems.end(); ++i) { |
---|
29 | destroy(*i); |
---|
30 | } |
---|
31 | } |
---|
32 | void keep(T elem) { elems.push_back(elem); } |
---|
33 | }; |
---|
34 | |
---|
35 | // predefined specializations: |
---|
36 | template<> inline void Keeper<char*>::destroy(char *s) { free(s); } |
---|
37 | |
---|
38 | |
---|
39 | #else |
---|
40 | #error Keeper.h included twice |
---|
41 | #endif // KEEPER_H |
---|
Note: See
TracBrowser
for help on using the repository browser.