xref: /haiku/headers/private/userlandfs/shared/ObjectTracker.h (revision 83812f67529c88d4fb4b942162a9f21142e683fa)
1*83812f67SIngo Weinhold // ObjectTracker.h
2*83812f67SIngo Weinhold 
3*83812f67SIngo Weinhold #ifndef USERLAND_FS_OBJECT_TRACKER_H
4*83812f67SIngo Weinhold #define USERLAND_FS_OBJECT_TRACKER_H
5*83812f67SIngo Weinhold 
6*83812f67SIngo Weinhold #include "DLList.h"
7*83812f67SIngo Weinhold #include "Locker.h"
8*83812f67SIngo Weinhold 
9*83812f67SIngo Weinhold namespace UserlandFSUtil {
10*83812f67SIngo Weinhold 
11*83812f67SIngo Weinhold class ObjectTracker;
12*83812f67SIngo Weinhold class GetObjectTrackableLink;
13*83812f67SIngo Weinhold 
14*83812f67SIngo Weinhold // ObjectTrackable
15*83812f67SIngo Weinhold class ObjectTrackable {
16*83812f67SIngo Weinhold public:
17*83812f67SIngo Weinhold 								ObjectTrackable();
18*83812f67SIngo Weinhold 	virtual						~ObjectTrackable();
19*83812f67SIngo Weinhold 
20*83812f67SIngo Weinhold private:
21*83812f67SIngo Weinhold 	friend class ObjectTracker;
22*83812f67SIngo Weinhold 	friend class GetObjectTrackableLink;
23*83812f67SIngo Weinhold 
24*83812f67SIngo Weinhold 			DLListLink<ObjectTrackable> fLink;
25*83812f67SIngo Weinhold };
26*83812f67SIngo Weinhold 
27*83812f67SIngo Weinhold // GetObjectTrackableLink
28*83812f67SIngo Weinhold struct GetObjectTrackableLink {
29*83812f67SIngo Weinhold 	inline DLListLink<ObjectTrackable> *operator()(
30*83812f67SIngo Weinhold 		ObjectTrackable* trackable) const
31*83812f67SIngo Weinhold 	{
32*83812f67SIngo Weinhold 		return &trackable->fLink;
33*83812f67SIngo Weinhold 	}
34*83812f67SIngo Weinhold 
35*83812f67SIngo Weinhold 	inline const DLListLink<ObjectTrackable> *operator()(
36*83812f67SIngo Weinhold 		const ObjectTrackable* trackable) const
37*83812f67SIngo Weinhold 	{
38*83812f67SIngo Weinhold 		return &trackable->fLink;
39*83812f67SIngo Weinhold 	}
40*83812f67SIngo Weinhold };
41*83812f67SIngo Weinhold 
42*83812f67SIngo Weinhold // ObjectTracker
43*83812f67SIngo Weinhold class ObjectTracker {
44*83812f67SIngo Weinhold private:
45*83812f67SIngo Weinhold 								ObjectTracker();
46*83812f67SIngo Weinhold 								~ObjectTracker();
47*83812f67SIngo Weinhold 
48*83812f67SIngo Weinhold public:
49*83812f67SIngo Weinhold 
50*83812f67SIngo Weinhold 	static	ObjectTracker*		InitDefault();
51*83812f67SIngo Weinhold 	static	void				ExitDefault();
52*83812f67SIngo Weinhold 	static	ObjectTracker*		GetDefault();
53*83812f67SIngo Weinhold 
54*83812f67SIngo Weinhold private:
55*83812f67SIngo Weinhold 			friend class ObjectTrackable;
56*83812f67SIngo Weinhold 
57*83812f67SIngo Weinhold 			void				AddTrackable(ObjectTrackable* trackable);
58*83812f67SIngo Weinhold 			void				RemoveTrackable(ObjectTrackable* trackable);
59*83812f67SIngo Weinhold 
60*83812f67SIngo Weinhold private:
61*83812f67SIngo Weinhold 			Locker				fLock;
62*83812f67SIngo Weinhold 			DLList<ObjectTrackable, GetObjectTrackableLink> fTrackables;
63*83812f67SIngo Weinhold 
64*83812f67SIngo Weinhold 	static	ObjectTracker*		sTracker;
65*83812f67SIngo Weinhold };
66*83812f67SIngo Weinhold 
67*83812f67SIngo Weinhold }	// namespace UserlandFSUtil
68*83812f67SIngo Weinhold 
69*83812f67SIngo Weinhold using UserlandFSUtil::ObjectTrackable;
70*83812f67SIngo Weinhold using UserlandFSUtil::ObjectTracker;
71*83812f67SIngo Weinhold 
72*83812f67SIngo Weinhold #ifdef DEBUG_OBJECT_TRACKING
73*83812f67SIngo Weinhold #	define ONLY_OBJECT_TRACKABLE_BASE_CLASS		: private ObjectTrackable
74*83812f67SIngo Weinhold #	define FIRST_OBJECT_TRACKABLE_BASE_CLASS	private ObjectTrackable,
75*83812f67SIngo Weinhold #else
76*83812f67SIngo Weinhold #	define ONLY_OBJECT_TRACKABLE_BASE_CLASS
77*83812f67SIngo Weinhold #	define FIRST_OBJECT_TRACKABLE_BASE_CLASS
78*83812f67SIngo Weinhold #endif
79*83812f67SIngo Weinhold 
80*83812f67SIngo Weinhold #endif	// USERLAND_FS_OBJECT_TRACKER_H
81