summaryrefslogtreecommitdiff
path: root/src/unstable/cacheobject.h
blob: 008868575078e474312a1f3c5ad52841e925019e (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
/*
 * 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 ),
            bChanged( false )
        {
        }

        virtual ~CacheObject()
        {
        }

        typedef CacheBase<keytype, obtype> CacheType;

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

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

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

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

        bool hasChanged() const
        {
            return bChanged;
        }

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

            this->bChanged = bChanged;
        }

    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