~ubuntu-branches/ubuntu/intrepid/enigma/intrepid

« back to all changes in this revision

Viewing changes to src/px/cache.hh

  • Committer: Bazaar Package Importer
  • Author(s): Erich Schubert
  • Date: 2005-08-28 15:30:09 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20050828153009-sky64kb6tcq37xt5
Tags: 0.92.1-1
* New upstream subversion checkout
* Remove menu.s3m, which we are allowed to distributed but not to modify
  also copyright notice is confusing... (Closes: #321669)
* Rebuild with new libzipios (Closes: #325405)
  I hope this works without a versioned build-dependency
* Added "enigma replaces enigma-data" for upgrades (Closes: #308558)
* Added notes about the fonts copyright.
* updated to policy 3.6.2.1 (no changes)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2002,2003 Daniel Heck
3
 
 *
4
 
 * This program is free software; you can redistribute it and/or
5
 
 * modify it under the terms of the GNU General Public License
6
 
 * as published by the Free Software Foundation; either version 2
7
 
 * of the License, or (at your option) any later version.
8
 
 *  
9
 
 * This program is distributed in the hope that it will be useful,
10
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 
 * GNU General Public License for more details.
13
 
 *
14
 
 * You should have received a copy of the GNU General Public License along
15
 
 * with this program; if not, write to the Free Software Foundation, Inc.,
16
 
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
17
 
 *
18
 
 * $Id: cache.hh,v 1.5 2004/04/24 11:46:01 dheck Exp $
19
 
 */
20
 
#ifndef PX_CACHE_HH
21
 
#define PX_CACHE_HH
22
 
 
23
 
/* -------------------- Cache -------------------- */
24
 
 
25
 
/*
26
 
  A generic class for caching external data.  Stored data is owned by
27
 
  the cache and is automatically `release'd on destruction.  Missing
28
 
  values are automatically retrieved using the `acquire' method.
29
 
*/
30
 
 
31
 
#include "dict.hh"
32
 
 
33
 
namespace px
34
 
{
35
 
    template <class T>
36
 
    class DeleteDisposer {
37
 
    public:
38
 
        static void dispose (T p) 
39
 
        { delete p; }
40
 
    };
41
 
 
42
 
    template <class T, class Disposer>
43
 
    class Cache {
44
 
    public:
45
 
        Cache();
46
 
        virtual ~Cache() {}
47
 
 
48
 
        // ---------- Methods ----------
49
 
        void     clear();
50
 
        T        get (const std::string &key);
51
 
        void     remove (const std::string &key);
52
 
        unsigned size() const;
53
 
        bool     has_key(const std::string &key) const;
54
 
    protected:
55
 
        T        store (const std::string &key, T value);
56
 
 
57
 
    private:
58
 
        Cache (const Cache &other);
59
 
        Cache &operator= (const Cache &other);
60
 
 
61
 
        void release (T value) {
62
 
            Disposer::dispose (value);
63
 
        }
64
 
 
65
 
        // ---------- Interface ----------
66
 
        virtual T    acquire (const std::string &name) = 0;
67
 
 
68
 
        // ---------- Variables ----------
69
 
        typedef px::Dict<T> Map;
70
 
        typedef typename Map::iterator iterator;
71
 
 
72
 
        Map cache;
73
 
    };
74
 
 
75
 
    template <class T, class D>
76
 
    Cache<T,D>::Cache() : cache(1223) {
77
 
    }
78
 
 
79
 
    template <class T, class D>
80
 
    void Cache<T, D>::clear() {
81
 
        for (iterator i=cache.begin(); i!=cache.end(); ++i) 
82
 
            release(i->second);
83
 
        cache.clear();
84
 
    }
85
 
 
86
 
    template <class T, class D>
87
 
    void Cache<T, D>::remove (const std::string &key)
88
 
    {
89
 
        cache.remove (key);
90
 
    }
91
 
 
92
 
 
93
 
    template <class T, class D>
94
 
    T Cache<T,D>::store (const std::string &key, T value) {
95
 
        cache.insert(key, value);
96
 
        return value;
97
 
    }
98
 
 
99
 
    template <class T, class D>
100
 
    T Cache<T,D>::get(const std::string &key) {
101
 
        iterator i=cache.find(key);
102
 
        if (i!=cache.end())
103
 
            return i->second;
104
 
        else
105
 
            return store (key, acquire(key));
106
 
    }
107
 
 
108
 
    template <class T, class D>
109
 
    unsigned Cache<T,D>::size() const { 
110
 
        return cache.size(); 
111
 
    }
112
 
    
113
 
    template <class T, class D>
114
 
    bool Cache<T, D>::has_key(const std::string &key) const {
115
 
        return cache.has_key(key);
116
 
    }
117
 
 
118
 
/* -------------------- PtrCache -------------------- */
119
 
 
120
 
    template <class T>
121
 
    class PtrCache : public Cache<T*, DeleteDisposer<T*> > {
122
 
    public:
123
 
        ~PtrCache() { this->clear(); }
124
 
 
125
 
//        void release (T *value) { delete value; }
126
 
    };
127
 
}
128
 
 
129
 
#endif