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