aboutsummaryrefslogtreecommitdiff
path: root/src/unstable/cacheobject.h
blob: 47e09de00740630d4f24132466d2c4f297907f80 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * Copyright (C) 2007-2014 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>;
    //friend class CacheObject<keytype, obtype>::Initializer;
    public:
        CacheObject() :
            pCache( NULL ),
            pEntry( NULL ),
            bChanged( false )
        {
        }

        virtual ~CacheObject()
        {
        }

        typedef CacheObject<keytype, obtype> MyType;
        typedef CacheBase<keytype, obtype> CacheType;
        typedef CacheEntry<keytype, obtype> EntryType;

        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;
        }

        class Locker
        {
        public:
            Locker( const MyType *pObj ) :
                pObj( pObj )
            {
                pObj->lock();
            }

            ~Locker()
            {
                pObj->unlock();
            }

        private:
            const MyType *pObj;
        };

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

            this->bChanged = bChanged;
        }

        CacheType *getCache()
        {
            return pCache;
        }
    
    public:
        class Initializer
        {
        friend class CacheBase<keytype,obtype>;
        private:
            Initializer( CacheBase<keytype,obtype> *pCache ) :
                pCache( pCache )
            {
            }

        public:
            template<typename subtype>
            subtype *operator()( subtype *pNewObj )
            {
                pNewObj->setCache( pCache );
                return pNewObj;
            }
            
        private:
            CacheBase<keytype,obtype> *pCache;
        };


    private:
        void setCache( CacheType *pCache )
        {
            this->pCache = pCache;
        }

        void setCacheEntry( EntryType *pEntry )
        {
            this->pEntry = pEntry;
        }

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

#endif