Copiryght note changed
[cassata.biscotto.git] / src / Cache.h
blob9e71ea4c9e7dcf397d1484f28926a36916c97710
1 //vim: expandtab:shiftwidth=4:fileencoding=utf-8 :
3 /* Copyright ® 2008-2009 Fulvio Satta
5 If you want contact me, send an email to Yota_VGA@users.sf.net
7 This file is part of Biscotto
9 Biscotto is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 Biscotto is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 #ifndef CACHE_H
25 #define CACHE_H
27 #include <set>
28 #include <map>
29 #include <boost/pool/pool_alloc.hpp>
30 #include <boost/thread.hpp>
32 class CacheObject;
34 /* Cache system
36 * The cache system work correctly only if the ids of the protected objects
37 * are all in the unsigned long int intervall size and if the ++ overflow of
38 * the unsigned long int give 0.
39 * This is sure in all the pratical cases.
41 class Cache
43 friend class CacheObject;
45 protected:
46 /* Functor for the monitor of the cache */
47 class Monitor
49 public:
50 Monitor() {}
52 void operator()();
55 /* Internal rappresentation of the cache */
56 static Cache m_cache;
58 /* Syncronization variables */
59 mutable boost::mutex mutex;
60 boost::condition_variable command;
61 boost::condition_variable_any waitdelfirst;
63 /* State of the cache */
64 unsigned long int position;
65 unsigned long int size;
66 unsigned long int first;
67 bool delfirst;
69 /* Maximum size of the cache, not strictly followed */
70 unsigned long int max;
72 /* Functor for the map order */
73 struct circular_order
75 inline bool operator()(unsigned long int a, unsigned long int b)
77 if (a < cache().position and b >= cache().position)
78 return true;
80 return a < b;
84 /* Order of the objects */
85 std::map<unsigned long int, CacheObject *,
86 circular_order,
87 boost::pool_allocator<
88 std::pair<unsigned long int, CacheObject *> > >
89 order;
91 /* Object collection */
92 std::set<CacheObject *, std::less<CacheObject *>,
93 boost::pool_allocator<CacheObject *> >
94 objects;
96 /* Internal costructor, is a singleton */
97 Cache();
99 /* Add an object in the cache */
100 void add(CacheObject &object);
102 /* Delete an object in the cache */
103 void del(CacheObject &object);
105 /* Resize an object */
106 void resize(long int delta);
108 /* Call this if an object is used */
109 void use(CacheObject &object);
111 public:
112 /* Return the cache (singleton) */
113 static inline Cache &cache()
115 return m_cache;
118 /* Set the cache limit (not strictly) */
119 void setLimit(unsigned long int imit);
121 /* Get the cache limit (not strictly) */
122 unsigned long int limit() const;
124 CacheObject *readPointer(CacheObject **ptr) const;
127 #include "CacheObject.h"
129 #endif