"Fossies" - the Fresh Open Source Software Archive 
Member "aspell-0.60.8/common/cache.hpp" (8 Oct 2019, 2444 Bytes) of package /linux/misc/aspell-0.60.8.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "cache.hpp" see the
Fossies "Dox" file reference documentation.
1 #ifndef ACOMMON_CACHE__HPP
2 #define ACOMMON_CACHE__HPP
3
4 #include "posib_err.hpp"
5
6 namespace acommon {
7
8 class GlobalCacheBase;
9 template <class Data> class GlobalCache;
10
11 // get_cache_data (both versions) and release_cache_data will acquires
12 // the cache's lock
13
14 template <class Data>
15 PosibErr<Data *> get_cache_data(GlobalCache<Data> *,
16 typename Data::CacheConfig *,
17 const typename Data::CacheKey &);
18 template <class Data>
19 PosibErr<Data *> get_cache_data(GlobalCache<Data> *,
20 typename Data::CacheConfig *,
21 typename Data::CacheConfig2 *,
22 const typename Data::CacheKey &);
23
24 class Cacheable;
25 void release_cache_data(GlobalCacheBase *, const Cacheable *);
26 static inline void release_cache_data(const GlobalCacheBase * c, const Cacheable * d)
27 {
28 release_cache_data(const_cast<GlobalCacheBase *>(c),d);
29 }
30
31 class Cacheable
32 {
33 public: // but don't use
34 Cacheable * next;
35 Cacheable * * prev;
36 mutable int refcount;
37 GlobalCacheBase * cache;
38 public:
39 bool attached() {return prev;}
40 void copy_no_lock() const {refcount++;}
41 void copy() const; // Acquires cache->lock
42 void release() const {release_cache_data(cache,this);} // Acquires cache->lock
43 Cacheable(GlobalCacheBase * c = 0) : next(0), prev(0), refcount(1), cache(c) {}
44 virtual ~Cacheable() {}
45 };
46
47 template <class Data>
48 class CachePtr
49 {
50 Data * ptr;
51
52 public:
53 void reset(Data * p) {
54 if (ptr) ptr->release();
55 ptr = p;
56 }
57 void copy(Data * p) {if (p) p->copy(); reset(p);}
58 Data * release() {Data * tmp = ptr; ptr = 0; return tmp;}
59
60 Data & operator* () const {return *ptr;}
61 Data * operator-> () const {return ptr;}
62 Data * get() const {return ptr;}
63 operator Data * () const {return ptr;}
64
65 CachePtr() : ptr(0) {}
66 CachePtr(const CachePtr & other) {ptr = other.ptr; if (ptr) ptr->copy();}
67 void operator=(const CachePtr & other) {copy(other.ptr);}
68 ~CachePtr() {reset(0);}
69 };
70
71 template <class Data>
72 PosibErr<void> setup(CachePtr<Data> & res,
73 GlobalCache<Data> * cache,
74 typename Data::CacheConfig * config,
75 const typename Data::CacheKey & key) {
76 PosibErr<Data *> pe = get_cache_data(cache, config, key);
77 if (pe.has_err()) return pe;
78 res.reset(pe.data);
79 return no_err;
80 }
81
82 bool reset_cache(const char * = 0);
83
84 }
85
86 #endif