1 //----------------------------------------------------------------------------- 2 // 3 // Wait.h 4 // 5 // Cross-platform abstract base class for objects we 6 // want to be able to wait for. 7 // 8 // Copyright (c) 2010 Mal Lansell <mal@lansell.org> 9 // All rights reserved. 10 // 11 // SOFTWARE NOTICE AND LICENSE 12 // 13 // This file is part of OpenZWave. 14 // 15 // OpenZWave is free software: you can redistribute it and/or modify 16 // it under the terms of the GNU Lesser General Public License as published 17 // by the Free Software Foundation, either version 3 of the License, 18 // or (at your option) any later version. 19 // 20 // OpenZWave is distributed in the hope that it will be useful, 21 // but WITHOUT ANY WARRANTY; without even the implied warranty of 22 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 23 // GNU Lesser General Public License for more details. 24 // 25 // You should have received a copy of the GNU Lesser General Public License 26 // along with OpenZWave. If not, see <http://www.gnu.org/licenses/>. 27 // 28 //----------------------------------------------------------------------------- 29 #ifndef _Wait_H 30 #define _Wait_H 31 32 #include <list> 33 #include "platform/Ref.h" 34 35 namespace OpenZWave 36 { 37 namespace Internal 38 { 39 namespace Platform 40 { 41 class WaitImpl; 42 43 /** \brief Platform-independent definition of Wait objects. 44 * \ingroup Platform 45 */ 46 class Wait: public Ref 47 { 48 friend class WaitImpl; 49 friend class ThreadImpl; 50 51 public: 52 enum 53 { 54 Timeout_Immediate = 0, 55 Timeout_Infinite = -1 56 }; 57 58 typedef void (*pfnWaitNotification_t)(void* _context); 59 60 /** 61 * Add a watcher to our object. The watcher will be triggered 62 * by the derived class, when it enters a certain state. 63 * \param _callback pointer to the function that will be called when the wait is over. 64 * \param _context pointer to custom data that will be sent with the callback. 65 */ 66 void AddWatcher(pfnWaitNotification_t _callback, void* _context); 67 68 /** 69 * Remove a watcher from our object. Both the _callback and _context pointers 70 * must match those used in a previous call to AddWatcher. 71 * \param _callback pointer to the function that will be called when the wait is over. 72 * \param _context pointer to custom data that will be sent with the callback. 73 */ 74 void RemoveWatcher(pfnWaitNotification_t _callback, void* _context); 75 76 /** 77 * Wait for a single object to become signalled. 78 * \param _object pointer to the object to wait on. 79 * \param _timeout optional maximum time to wait. Defaults to -1, which means wait forever. 80 * \return zero if the object was signalled, -1 if the wait timed out. 81 */ 82 static int32 Single(Wait* _object, int32 _timeout = -1) 83 { 84 return Multiple(&_object, 1, _timeout); 85 } 86 87 /** 88 * Wait for one of multiple objects to become signalled. If more than one object is in 89 * a signalled state, the lowest array index will be returned. 90 * \param _objects array of pointers to objects to wait on. 91 * \param _numObjects number of objects in the array. 92 * \param _timeout optional maximum time to wait. Defaults to -1, which means wait forever. 93 * \return index into the array of the object that was signalled, -1 if the wait timed out. 94 */ 95 static int32 Multiple(Wait** _objects, uint32 _numObjects, int32 _timeout = -1); 96 97 protected: 98 Wait(); 99 virtual ~Wait(); 100 101 /** 102 * Notify the watchers that the object is signalled. 103 */ 104 void Notify(); 105 106 /** 107 * Test whether an object is signalled. 108 */ 109 virtual bool IsSignalled() = 0; 110 111 private: 112 Wait(Wait const&); // prevent copy 113 Wait& operator =(Wait const&); // prevent assignment 114 115 WaitImpl* m_pImpl; // Pointer to an object that encapsulates the platform-specific implementation of a Wait object. 116 }; 117 } // namespace Platform 118 } // namespace Internal 119 } // namespace OpenZWave 120 121 #endif //_Wait_H 122 123