1/* 2Open Tracker License 3 4Terms and Conditions 5 6Copyright (c) 1991-2000, Be Incorporated. All rights reserved. 7 8Permission is hereby granted, free of charge, to any person obtaining a copy of 9this software and associated documentation files (the "Software"), to deal in 10the Software without restriction, including without limitation the rights to 11use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 12of the Software, and to permit persons to whom the Software is furnished to do 13so, subject to the following conditions: 14 15The above copyright notice and this permission notice applies to all licensees 16and shall be included in all copies or substantial portions of the Software. 17 18THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF TITLE, MERCHANTABILITY, 20FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 21BE INCORPORATED BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 22AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF, OR IN CONNECTION 23WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 24 25Except as contained in this notice, the name of Be Incorporated shall not be 26used in advertising or otherwise to promote the sale, use or other dealings in 27this Software without prior written authorization from Be Incorporated. 28 29Tracker(TM), Be(R), BeOS(R), and BeIA(TM) are trademarks or registered trademarks 30of Be Incorporated in the United States and other countries. Other brand product 31names are registered trademarks or trademarks of their respective holders. 32All rights reserved. 33*/ 34 35#ifndef _AUTO_LOCK_H 36#define _AUTO_LOCK_H 37 38template<class T> 39class AutoLock 40{ 41 42// exception safe locking mechanism, allocate on stack and have 43// destructor unlock for you whenever the lock goes out of scope 44 45public: 46 AutoLock(T *lock, bool lockNow = true) 47 // use <lockLater> for lazy locking 48 : fLock(lock), 49 fHasLock(false) 50 { 51 if (lockNow) 52 fHasLock = fLock->Lock(); 53 } 54 55 AutoLock(T &lock, bool lockNow = true) 56 : fLock(&lock), 57 fHasLock(false) 58 { 59 if (lockNow) 60 fHasLock = fLock->Lock(); 61 } 62 63 ~AutoLock() 64 { 65 if (fHasLock) 66 fLock->Unlock(); 67 } 68 69 bool operator!() const 70 { return !fHasLock; } 71 72 bool IsLocked() const 73 { return fHasLock; } 74 75 // explicit Lock/Unlock calls are only used in special cases 76 // for unlocking before lock goes out of scope and successive 77 // re-locking 78 // usually constructor/destructor locks/unlocks are sufficient 79 void Unlock() 80 { 81 if (fHasLock) { 82 fLock->Unlock(); 83 fHasLock = false; 84 } 85 } 86 87 bool Lock() 88 { 89 if (!fHasLock) 90 fHasLock = fLock->Lock(); 91 92 return fHasLock; 93 } 94 95 // convenience call used when passing the AutoLock and the locked object 96 // around 97 T *LockedItem() const 98 { return fLock; } 99 100private: 101 T *fLock; 102 bool fHasLock; 103}; 104 105#endif 106