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