aboutsummaryrefslogtreecommitdiff
path: root/src/cptr.h
blob: 97f5e17748ef964a2cb68bc11a4978e8075bdcfe (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
#ifndef BU_C_PTR_H
#define BU_C_PTR_H

namespace Bu
{
	template<class obtype, class keytype> class Cache;

	/**
	 * Cache Pointer - Provides access to data that is held within the cache.
	 * This provides safe, refcounting access to data stored in the cache, with
	 * support for lazy loading.
	 */
	template<class obtype, class keytype>
	class CPtr
	{
	friend class Bu::Cache<obtype, keytype>;
	private:
		CPtr( Cache<obtype, keytype> &rCache, obtype *pData,
			const keytype &kId ) :
			rCache( rCache ),
			pData( pData ),
			kId( kId )
		{
			rCache.incRef( kId );
		}

	public:
		virtual ~CPtr()
		{
			rCache.decRef( kId );
		}

		obtype &operator*()
		{
			return *pData;
		}

		obtype *operator->()
		{
			return pData;
		}

		const keytype &getKey()
		{
			return kId;
		}

	private:
		Bu::Cache<obtype, keytype> &rCache;
		obtype *pData;
		keytype kId;
	};
};

#endif