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 // IObservable.h 33 // * PURPOSE 34 // Defines a general observable-object interface. 35 // * HISTORY 36 // e.moon 19aug99 Begun 37 38 #ifndef __IObservable_H__ 39 #define __IObservable_H__ 40 41 class BMessenger; 42 43 #include "cortex_defs.h" 44 __BEGIN_CORTEX_NAMESPACE 45 46 class IObservable { 47 48 public: IObservable()49 IObservable() { } ~IObservable()50 virtual ~IObservable() { } 51 public: // *** deletion 52 // clients must call release() rather than deleting, 53 // to ensure that all observers are notified of the 54 // object's demise. if the object has already been 55 // released, return an error. 56 virtual status_t release()=0; 57 58 public: // *** accessors 59 // return true if release() has been called, false otherwise. 60 virtual bool isReleased() const=0; 61 62 protected: // *** hooks 63 // must be called after an observer has been added (this is a good 64 // place to send an acknowledgement) observerAdded(const BMessenger & observer)65 virtual void observerAdded( 66 const BMessenger& observer) {TOUCH(observer);} 67 68 // must be called after an observer has been removed observerRemoved(const BMessenger & observer)69 virtual void observerRemoved( 70 const BMessenger& observer) {TOUCH(observer);} 71 72 // must be called once all observers have been removed releaseComplete()73 virtual void releaseComplete() {} 74 75 protected: // *** operations 76 // call to send the given message to all observers. 77 // Responsibility for deletion of the message remains with 78 // the caller. 79 virtual status_t notify( 80 BMessage* message)=0; 81 82 // must be called by release() if targets remain to be 83 // released. should notify all targets with an appropriate 84 // release-notification message (such as M_RELEASE_OBSERVABLE). 85 virtual void notifyRelease()=0; 86 }; 87 88 __END_CORTEX_NAMESPACE 89 #endif /*__IObservable_H__*/ 90