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