aboutsummaryrefslogtreecommitdiff
path: root/src/cache.h
blob: 344d1c6596e19e3128abbea2884161935b5b4ac5 (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
#ifndef BU_CACHE_H
#define BU_CACHE_H

#include "bu/cptr.h"
#include "bu/hash.h"

#define BU_TRACE
#include "bu/trace.h"

namespace Bu
{
	template<class obtype> class Cache;
	template<class obtype> long getCacheId( const obtype *o );

	template<class obtype>
	class Cache
	{
	friend class Bu::CPtr<obtype>;
	typedef Bu::CPtr<obtype> Ptr;
	public:
		typedef long cid;	/**< Cache ID type. Unique cache entry ID. */

	public:
		Cache()
		{
			TRACE();
		}

		virtual ~Cache()
		{
			TRACE();
		}

		Ptr insert( obtype *pData )
		{
			TRACE();
			CacheEntry e = {pData, 0};
			hEnt.insert( getCacheId( pData ), e );
			return Ptr( *this, pData );
		}

	private:
		void incRef( obtype *pData )
		{
			TRACE();
			hEnt.get( getCacheId( pData ) ).iRefs++;
		}

		void decRef( obtype *pData )
		{
			TRACE();
			CacheEntry &e = hEnt.get( getCacheId( pData ) );
			e.iRefs--;
		}

	private:
		typedef struct CacheEntry
		{
			obtype *pData;
			int iRefs;
		} CacheEntry;

		//typedef Bu::Hash<ptrdiff_t, int> RefHash;
		typedef Bu::Hash<cid, CacheEntry> CidHash;
		//RefHash hRefs;
		CidHash hEnt;
	};
};

#endif