xref: /haiku/src/apps/cortex/support/ObservableLooper.h (revision a0795c6fe30e25338049a952326c61deb7a343b6)
1 // ObservableLooper.h
2 // * PURPOSE
3 //   Implementation of an observable (target) derived
4 //   from BLooper.
5 //
6 // * HISTORY
7 //   e.moon		18aug99		Begun
8 
9 #ifndef __ObservableLooper_H__
10 #define __ObservableLooper_H__
11 
12 #include <Looper.h>
13 class BMessageRunner;
14 
15 #include "observe.h"
16 #include "IObservable.h"
17 #include "MultiInvoker.h"
18 
19 #include "cortex_defs.h"
20 __BEGIN_CORTEX_NAMESPACE
21 
22 class ObservableLooper :
23 	public	BLooper,
24 	public	IObservable,
25 	private	MultiInvoker {
26 
27 	typedef BLooper _inherited;
28 
29 public:											// *** deletion
30 	// clients must call release() rather than deleting,
31 	// to ensure that all observers are notified of the
32 	// object's demise.  if the object has already been
33 	// released, return an error.
34 
35 	virtual status_t release();
36 
37 public:											// *** ctor/dtor
38 	virtual ~ObservableLooper();
39 	ObservableLooper(
40 		const char*							name=0,
41 		int32										priority=B_NORMAL_PRIORITY,
42 		int32										portCapacity=B_LOOPER_PORT_DEFAULT_CAPACITY,
43 		bigtime_t								quitTimeout=B_INFINITE_TIMEOUT);
44 	ObservableLooper(
45 		BMessage*								archive);
46 
47 public:											// *** accessors
48 	// return true if release() has been called, false otherwise.
49 	bool isReleased() const;
50 
51 protected:									// *** hooks
52 	// sends M_OBSERVER_ADDED to the newly-added observer
53 	virtual void observerAdded(
54 		const BMessenger&				observer);
55 
56 	// sends M_OBSERVER_REMOVED to the newly-removed observer
57 	virtual void observerRemoved(
58 		const BMessenger&				observer);
59 
60 protected:									// *** internal operations
61 	// call to send the given message to all observers.
62 	// Responsibility for deletion of the message remains with
63 	// the caller.
64 	// * LOCKING: the BLooper must be locked.
65 	virtual status_t notify(
66 		BMessage*								message);
67 
68 	// sends M_RELEASE_OBSERVABLE
69 	virtual void notifyRelease();
70 
71 public:											// *** BLooper
72 	virtual void Quit();
73 	virtual bool QuitRequested();
74 
75 public:											// *** BHandler
76 	virtual void MessageReceived(
77 		BMessage*								message);
78 
79 public:											// *** BArchivable
80 	// * LOCKING: the BLooper must be locked.
81 	virtual status_t Archive(
82 		BMessage*								archive,
83 		bool										deep=true) const;
84 
85 private:										// implementation
86 	void _handleAddObserver(
87 		BMessage*								message);
88 
89 	void _handleRemoveObserver(
90 		BMessage*								message);
91 
92 private:										// members
93 	// how long to wait after being requested to shut down
94 	// before giving up on stuck observers
95 	bigtime_t									m_quitTimeout;
96 	BMessageRunner*						m_executioner;
97 
98 	// true if a quit has been requested
99 	bool											m_quitting;
100 };
101 
102 __END_CORTEX_NAMESPACE
103 #endif /*__ObservableLooper_H__*/