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

namespace Bu
{
	template<class obtype> 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 CPtr
	{
	friend class Bu::Cache<obtype>;
	public:
		typedef long cid_t;	/**< Cache ID type. Unique cache entry ID. */
	private:
		CPtr( Cache<obtype> &rCache, obtype *pData ) :
			rCache( rCache ),
			pData( pData )
		{
			rCache.incRef( cId );
		}

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

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

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

	private:
		Bu::Cache<obtype> &rCache;
		obtype *pData;
		cid_t cId;
	};
};

#endif