xref: /haiku/headers/private/userlandfs/shared/ObjectTracker.h (revision c0509ccc2954628faa943034feab9e3fe15e3df1)
1*c0509cccSIngo Weinhold /*
2*c0509cccSIngo Weinhold  * Copyright 2001-2009, Ingo Weinhold, ingo_weinhold@gmx.de.
3*c0509cccSIngo Weinhold  * Distributed under the terms of the MIT License.
4*c0509cccSIngo Weinhold  */
583812f67SIngo Weinhold #ifndef USERLAND_FS_OBJECT_TRACKER_H
683812f67SIngo Weinhold #define USERLAND_FS_OBJECT_TRACKER_H
783812f67SIngo Weinhold 
8*c0509cccSIngo Weinhold #include <util/DoublyLinkedList.h>
9*c0509cccSIngo Weinhold 
1083812f67SIngo Weinhold #include "Locker.h"
1183812f67SIngo Weinhold 
1283812f67SIngo Weinhold namespace UserlandFSUtil {
1383812f67SIngo Weinhold 
1483812f67SIngo Weinhold class ObjectTracker;
1583812f67SIngo Weinhold class GetObjectTrackableLink;
1683812f67SIngo Weinhold 
1783812f67SIngo Weinhold // ObjectTrackable
1883812f67SIngo Weinhold class ObjectTrackable {
1983812f67SIngo Weinhold public:
2083812f67SIngo Weinhold 								ObjectTrackable();
2183812f67SIngo Weinhold 	virtual						~ObjectTrackable();
2283812f67SIngo Weinhold 
2383812f67SIngo Weinhold private:
2483812f67SIngo Weinhold 	friend class ObjectTracker;
2583812f67SIngo Weinhold 	friend class GetObjectTrackableLink;
2683812f67SIngo Weinhold 
27*c0509cccSIngo Weinhold 			DoublyLinkedListLink<ObjectTrackable> fLink;
2883812f67SIngo Weinhold };
2983812f67SIngo Weinhold 
3083812f67SIngo Weinhold // GetObjectTrackableLink
3183812f67SIngo Weinhold struct GetObjectTrackableLink {
operatorGetObjectTrackableLink32*c0509cccSIngo Weinhold 	inline DoublyLinkedListLink<ObjectTrackable> *operator()(
3383812f67SIngo Weinhold 		ObjectTrackable* trackable) const
3483812f67SIngo Weinhold 	{
3583812f67SIngo Weinhold 		return &trackable->fLink;
3683812f67SIngo Weinhold 	}
3783812f67SIngo Weinhold 
operatorGetObjectTrackableLink38*c0509cccSIngo Weinhold 	inline const DoublyLinkedListLink<ObjectTrackable> *operator()(
3983812f67SIngo Weinhold 		const ObjectTrackable* trackable) const
4083812f67SIngo Weinhold 	{
4183812f67SIngo Weinhold 		return &trackable->fLink;
4283812f67SIngo Weinhold 	}
4383812f67SIngo Weinhold };
4483812f67SIngo Weinhold 
4583812f67SIngo Weinhold // ObjectTracker
4683812f67SIngo Weinhold class ObjectTracker {
4783812f67SIngo Weinhold private:
4883812f67SIngo Weinhold 								ObjectTracker();
4983812f67SIngo Weinhold 								~ObjectTracker();
5083812f67SIngo Weinhold 
5183812f67SIngo Weinhold public:
5283812f67SIngo Weinhold 
5383812f67SIngo Weinhold 	static	ObjectTracker*		InitDefault();
5483812f67SIngo Weinhold 	static	void				ExitDefault();
5583812f67SIngo Weinhold 	static	ObjectTracker*		GetDefault();
5683812f67SIngo Weinhold 
5783812f67SIngo Weinhold private:
5883812f67SIngo Weinhold 			friend class ObjectTrackable;
5983812f67SIngo Weinhold 
6083812f67SIngo Weinhold 			void				AddTrackable(ObjectTrackable* trackable);
6183812f67SIngo Weinhold 			void				RemoveTrackable(ObjectTrackable* trackable);
6283812f67SIngo Weinhold 
6383812f67SIngo Weinhold private:
6483812f67SIngo Weinhold 			Locker				fLock;
65*c0509cccSIngo Weinhold 			DoublyLinkedList<ObjectTrackable, GetObjectTrackableLink>
66*c0509cccSIngo Weinhold 				fTrackables;
6783812f67SIngo Weinhold 
6883812f67SIngo Weinhold 	static	ObjectTracker*		sTracker;
6983812f67SIngo Weinhold };
7083812f67SIngo Weinhold 
7183812f67SIngo Weinhold }	// namespace UserlandFSUtil
7283812f67SIngo Weinhold 
7383812f67SIngo Weinhold using UserlandFSUtil::ObjectTrackable;
7483812f67SIngo Weinhold using UserlandFSUtil::ObjectTracker;
7583812f67SIngo Weinhold 
7683812f67SIngo Weinhold #ifdef DEBUG_OBJECT_TRACKING
7783812f67SIngo Weinhold #	define ONLY_OBJECT_TRACKABLE_BASE_CLASS		: private ObjectTrackable
7883812f67SIngo Weinhold #	define FIRST_OBJECT_TRACKABLE_BASE_CLASS	private ObjectTrackable,
7983812f67SIngo Weinhold #else
8083812f67SIngo Weinhold #	define ONLY_OBJECT_TRACKABLE_BASE_CLASS
8183812f67SIngo Weinhold #	define FIRST_OBJECT_TRACKABLE_BASE_CLASS
8283812f67SIngo Weinhold #endif
8383812f67SIngo Weinhold 
8483812f67SIngo Weinhold #endif	// USERLAND_FS_OBJECT_TRACKER_H
85