OpenNI 1.3.2
|
00001 /**************************************************************************** 00002 * * 00003 * OpenNI 1.1 Alpha * 00004 * Copyright (C) 2011 PrimeSense Ltd. * 00005 * * 00006 * This file is part of OpenNI. * 00007 * * 00008 * OpenNI is free software: you can redistribute it and/or modify * 00009 * it under the terms of the GNU Lesser General Public License as published * 00010 * by the Free Software Foundation, either version 3 of the License, or * 00011 * (at your option) any later version. * 00012 * * 00013 * OpenNI is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00016 * GNU Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public License * 00019 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. * 00020 * * 00021 ****************************************************************************/ 00022 #ifndef __XN_OS_CPP_H__ 00023 #define __XN_OS_CPP_H__ 00024 00025 //--------------------------------------------------------------------------- 00026 // Includes 00027 //--------------------------------------------------------------------------- 00028 #include <XnOS.h> 00029 00030 //--------------------------------------------------------------------------- 00031 // Types 00032 //--------------------------------------------------------------------------- 00033 class XnAutoCSLocker 00034 { 00035 public: 00036 inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE) 00037 { 00038 Lock(); 00039 } 00040 00041 inline XnAutoCSLocker& operator=(const XnAutoCSLocker& other) 00042 { 00043 Unlock(); 00044 m_hCS = other.m_hCS; 00045 Lock(); 00046 return *this; 00047 } 00048 00049 inline XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS) : m_hCS(hCS), m_bLocked(FALSE) 00050 { 00051 Lock(); 00052 } 00053 00054 inline ~XnAutoCSLocker() 00055 { 00056 Unlock(); 00057 } 00058 00059 inline void Lock() 00060 { 00061 if (!m_bLocked) 00062 { 00063 XnStatus nRetVal = xnOSEnterCriticalSection(&m_hCS); 00064 XN_ASSERT(nRetVal == XN_STATUS_OK); 00065 m_bLocked = TRUE; 00066 } 00067 } 00068 00069 inline void Unlock() 00070 { 00071 if (m_bLocked) 00072 { 00073 XnStatus nRetVal = xnOSLeaveCriticalSection(&m_hCS); 00074 XN_ASSERT(nRetVal == XN_STATUS_OK); 00075 m_bLocked = FALSE; 00076 } 00077 } 00078 00079 private: 00080 XN_CRITICAL_SECTION_HANDLE m_hCS; 00081 XnBool m_bLocked; 00082 }; 00083 00084 class XnAutoMutexLocker 00085 { 00086 public: 00087 inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex) 00088 { 00089 m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds); 00090 } 00091 00092 XnStatus GetStatus() const 00093 { 00094 return m_nStatus; 00095 } 00096 00097 inline ~XnAutoMutexLocker() 00098 { 00099 if (m_nStatus == XN_STATUS_OK) 00100 { 00101 //Only unlock if we managed to lock in the first place 00102 xnOSUnLockMutex(m_hMutex); 00103 } 00104 } 00105 00106 private: 00107 XN_MUTEX_HANDLE m_hMutex; 00108 XnStatus m_nStatus; 00109 }; 00110 00111 class XnOSEvent 00112 { 00113 public: 00114 XnOSEvent() : m_hEvent(NULL) {} 00115 00116 ~XnOSEvent() 00117 { 00118 Close(); 00119 } 00120 00121 operator XN_EVENT_HANDLE() const 00122 { 00123 return m_hEvent; 00124 } 00125 00126 XnStatus Create(XnBool bManualReset) 00127 { 00128 return xnOSCreateEvent(&m_hEvent, bManualReset); 00129 } 00130 00131 XnStatus Create(const XnChar* strName, XnBool bManualReset) 00132 { 00133 return xnOSCreateNamedEvent(&m_hEvent, strName, bManualReset); 00134 } 00135 00136 XnStatus Open(const XnChar* strName) 00137 { 00138 return xnOSOpenNamedEvent(&m_hEvent, strName); 00139 } 00140 00141 XnStatus Close() 00142 { 00143 return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK; 00144 } 00145 00146 XnStatus Set() 00147 { 00148 return xnOSSetEvent(m_hEvent); 00149 } 00150 00151 XnStatus Reset() 00152 { 00153 return xnOSResetEvent(m_hEvent); 00154 } 00155 00156 XnStatus Wait(XnUInt32 nMilliseconds) 00157 { 00158 return xnOSWaitEvent(m_hEvent, nMilliseconds); 00159 } 00160 00161 private: 00162 XN_EVENT_HANDLE m_hEvent; 00163 }; 00164 00165 #endif // __XN_OS_CPP_H__