summaryrefslogtreecommitdiff
path: root/src/unstable/cacheobject.h
blob: 4606493bfe454a0130810590bac6d7e6f137aaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
 * Copyright (C) 2007-2013 Xagasoft, All rights reserved.
 *
 * This file is part of the libbu++ library and is released under the
 * terms of the license contained in the file LICENSE.
 */

#ifndef BU_CACHE_OBJECT_H
#define BU_CACHE_OBJECT_H

#ifndef NULL
#define NULL 0
#endif

namespace Bu
{
    template<typename keytype, typename obtype> class CacheBase;
    template<typename keytype, typename obtype> class CacheEntry;

    template<typename keytype, typename obtype>
    class CacheObject
    {
    friend class CacheBase<keytype, obtype>;
    public:
        CacheObject() :
            pCache( NULL ),
            pEntry( NULL ),
            bChanged( false )
        {
        }

        virtual ~CacheObject()
        {
        }

        typedef CacheBase<keytype, obtype> CacheType;

        virtual keytype getKey() const=0;
        virtual int getPersistenceScore() const { return 0; }

        void lock() const
        {
            if( pEntry )
                pEntry->lock();
        }

        void unlock() const
        {
            if( pEntry )
                pEntry->unlock();
        }

        int getRefCount() const
        {
            if( pEntry )
                return pEntry->getRefCount();
            return -1;
        }

        bool hasChanged() const
        {
            return bChanged;
        }

    protected:
        void changed( bool bChanged=true )
        {
            if( this->bChanged == false && bChanged == true && pCache )
                pCache->objectChanged( getKey() );

            this->bChanged = bChanged;
        }

        CacheType *getCache()
        {
            return pCache;
        }

    private:
        typedef CacheEntry<keytype, obtype> Entry;
        void setCache( CacheType *pCache, Entry *pEntry )
        {
            this->pCache = pCache;
            this->pEntry = pEntry;
        }

    private:
        CacheType *pCache;
        Entry *pEntry;
        bool bChanged;
    };
}

#endif