2 * Copyright (c) 1999-2000, Eric Moon.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions, and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
26 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
27 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // Implementation of an observable (target) derived
38 // e.moon 18aug99 Begun
40 #ifndef __ObservableLooper_H__
41 #define __ObservableLooper_H__
47 #include "IObservable.h"
48 #include "MultiInvoker.h"
50 #include "cortex_defs.h"
51 __BEGIN_CORTEX_NAMESPACE
53 class ObservableLooper
:
56 private MultiInvoker
{
58 typedef BLooper _inherited
;
60 public: // *** deletion
61 // clients must call release() rather than deleting,
62 // to ensure that all observers are notified of the
63 // object's demise. if the object has already been
64 // released, return an error.
66 virtual status_t
release();
68 public: // *** ctor/dtor
69 virtual ~ObservableLooper();
72 int32 priority
=B_NORMAL_PRIORITY
,
73 int32 portCapacity
=B_LOOPER_PORT_DEFAULT_CAPACITY
,
74 bigtime_t quitTimeout
=B_INFINITE_TIMEOUT
);
78 public: // *** accessors
79 // return true if release() has been called, false otherwise.
80 bool isReleased() const;
82 protected: // *** hooks
83 // sends M_OBSERVER_ADDED to the newly-added observer
84 virtual void observerAdded(
85 const BMessenger
& observer
);
87 // sends M_OBSERVER_REMOVED to the newly-removed observer
88 virtual void observerRemoved(
89 const BMessenger
& observer
);
91 protected: // *** internal operations
92 // call to send the given message to all observers.
93 // Responsibility for deletion of the message remains with
95 // * LOCKING: the BLooper must be locked.
96 virtual status_t
notify(
99 // sends M_RELEASE_OBSERVABLE
100 virtual void notifyRelease();
102 public: // *** BLooper
104 virtual bool QuitRequested();
106 public: // *** BHandler
107 virtual void MessageReceived(
110 public: // *** BArchivable
111 // * LOCKING: the BLooper must be locked.
112 virtual status_t
Archive(
114 bool deep
=true) const;
116 private: // implementation
117 void _handleAddObserver(
120 void _handleRemoveObserver(
124 // how long to wait after being requested to shut down
125 // before giving up on stuck observers
126 bigtime_t m_quitTimeout
;
127 BMessageRunner
* m_executioner
;
129 // true if a quit has been requested
133 __END_CORTEX_NAMESPACE
134 #endif /*__ObservableLooper_H__*/