xref: /haiku/src/tests/kits/app/bmessenger/Helpers.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 //------------------------------------------------------------------------------
2 //	Helpers.h
3 //
4 //------------------------------------------------------------------------------
5 
6 #ifndef HELPERS_H
7 #define HELPERS_H
8 
9 // Standard Includes -----------------------------------------------------------
10 
11 // System Includes -------------------------------------------------------------
12 #include <Looper.h>
13 
14 // Project Includes ------------------------------------------------------------
15 
16 // Local Includes --------------------------------------------------------------
17 
18 // Local Defines ---------------------------------------------------------------
19 
20 // Globals ---------------------------------------------------------------------
21 
22 
23 // helper class: quits a BLooper on destruction
24 class LooperQuitter {
25 public:
26 	inline LooperQuitter(BLooper *looper) : fLooper(looper) {}
27 	inline ~LooperQuitter()
28 	{
29 		fLooper->Lock();
30 		fLooper->Quit();
31 	}
32 
33 private:
34 	BLooper	*fLooper;
35 };
36 
37 // helper class: deletes an object on destruction
38 template<typename T>
39 class AutoDeleter {
40 public:
41 	inline AutoDeleter(T *object, bool array = false)
42 		: fObject(object), fArray(array) {}
43 	inline ~AutoDeleter()
44 	{
45 		if (fArray)
46 			delete[] fObject;
47 		else
48 			delete fObject;
49 	}
50 
51 protected:
52 	T		*fObject;
53 	bool	fArray;
54 };
55 
56 // helper class: deletes an BHandler on destruction
57 class HandlerDeleter : AutoDeleter<BHandler> {
58 public:
59 	inline HandlerDeleter(BHandler *handler)
60 		: AutoDeleter<BHandler>(handler) {}
61 	inline ~HandlerDeleter()
62 	{
63 		if (fObject) {
64 			if (BLooper *looper = fObject->Looper()) {
65 				looper->Lock();
66 				looper->RemoveHandler(fObject);
67 				looper->Unlock();
68 			}
69 		}
70 	}
71 };
72 
73 // helper function: return the this team's ID
74 static inline
75 team_id
76 get_this_team()
77 {
78 	thread_info info;
79 	get_thread_info(find_thread(NULL), &info);
80 	return info.team;
81 }
82 
83 #endif	// HELPERS_H
84