1 /* 2 * Copyright (c) 1999-2000, Eric Moon. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions, and the following disclaimer. 11 * 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. 15 * 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. 18 * 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. 29 */ 30 31 32 // ObservableHandler.h 33 // * PURPOSE 34 // Implementation of an observable BHandler. 35 // 36 // * HISTORY 37 // e.moon 19aug99 Begun. 38 39 #ifndef __ObservableHandler_H__ 40 #define __ObservableHandler_H__ 41 42 #include <Handler.h> 43 44 #include "observe.h" 45 #include "IObservable.h" 46 #include "MultiInvoker.h" 47 48 #include "cortex_defs.h" 49 __BEGIN_CORTEX_NAMESPACE 50 51 class ObservableHandler : 52 public BHandler, 53 public IObservable, 54 private MultiInvoker { 55 56 typedef BHandler _inherited; 57 58 public: // *** deletion 59 // clients must call release() rather than deleting, 60 // to ensure that all observers are notified of the 61 // object's demise. if the object has already been 62 // released, return an error. 63 64 virtual status_t release(); 65 66 public: // *** ctor/dtor 67 virtual ~ObservableHandler(); 68 ObservableHandler( 69 const char* name=0); 70 ObservableHandler( 71 BMessage* archive); 72 73 public: // *** accessors 74 // return true if release() has been called, false otherwise. 75 bool isReleased() const; 76 77 protected: // *** hooks 78 // sends M_OBSERVER_ADDED to the newly-added observer 79 virtual void observerAdded( 80 const BMessenger& observer); 81 82 // sends M_OBSERVER_REMOVED to the newly-removed observer 83 virtual void observerRemoved( 84 const BMessenger& observer); 85 86 protected: // *** internal operations 87 // call to send the given message to all observers. 88 // Responsibility for deletion of the message remains with 89 // the caller. 90 // * LOCKING: the BLooper must be locked. 91 virtual status_t notify( 92 BMessage* message); 93 94 // sends M_RELEASE_OBSERVABLE 95 virtual void notifyRelease(); 96 97 public: // *** BHandler 98 virtual void MessageReceived( 99 BMessage* message); 100 101 public: // *** BArchivable 102 // * LOCKING: the BLooper must be locked. 103 virtual status_t Archive( 104 BMessage* archive, 105 bool deep=true) const; 106 107 private: // implementation 108 void _handleAddObserver( 109 BMessage* message); //nyi 110 111 void _handleRemoveObserver( 112 BMessage* message); //nyi 113 114 private: // members 115 bool m_released; 116 }; 117 118 __END_CORTEX_NAMESPACE 119 #endif /*__ObservableHandler_H__*/ 120 121