OpenNI 1.0.0

XnOSCpp.h

Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.0 Alpha                                                          *
00004 *  Copyright (C) 2010 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 
00023 
00024 
00025 
00026 #ifndef __XN_OS_CPP_H__
00027 #define __XN_OS_CPP_H__
00028 
00029 //---------------------------------------------------------------------------
00030 // Includes
00031 //---------------------------------------------------------------------------
00032 #include <XnOS.h>
00033 
00034 //---------------------------------------------------------------------------
00035 // Types
00036 //---------------------------------------------------------------------------
00037 class XnAutoCSLocker
00038 {
00039 public:
00040     inline XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS) : m_hCS(hCS)
00041     {
00042         xnOSEnterCriticalSection(&m_hCS);
00043     }
00044 
00045     inline ~XnAutoCSLocker()
00046     {
00047         xnOSLeaveCriticalSection(&m_hCS);
00048     }
00049 
00050 private:
00051     XN_CRITICAL_SECTION_HANDLE m_hCS;
00052 };
00053 
00054 class XnAutoMutexLocker
00055 {
00056 public:
00057     inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
00058     {
00059         m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
00060     }
00061 
00062     XnStatus GetStatus() const 
00063     { 
00064         return m_nStatus; 
00065     }
00066 
00067     inline ~XnAutoMutexLocker()
00068     {
00069         if (m_nStatus == XN_STATUS_OK)
00070         {
00071             //Only unlock if we managed to lock in the first place
00072             xnOSUnLockMutex(m_hMutex);
00073         }
00074     }
00075 
00076 private:
00077     XN_MUTEX_HANDLE m_hMutex;
00078     XnStatus m_nStatus;
00079 };
00080 
00081 class XnOSEvent
00082 {
00083 public:
00084     XnOSEvent() : m_hEvent(NULL) {}
00085     
00086     ~XnOSEvent() 
00087     { 
00088         Close(); 
00089     }
00090 
00091     operator XN_EVENT_HANDLE() const 
00092     {
00093         return m_hEvent;
00094     }
00095 
00096     XnStatus Create(XnBool bManualReset)
00097     {
00098         return xnOSCreateEvent(&m_hEvent, bManualReset);
00099     }
00100 
00101     XnStatus Create(const XnChar* strName, XnBool bManualReset)
00102     {
00103         return xnOSCreateNamedEvent(&m_hEvent, strName, bManualReset);
00104     }
00105 
00106     XnStatus Open(const XnChar* strName)
00107     {
00108         return xnOSOpenNamedEvent(&m_hEvent, strName);
00109     }
00110 
00111     XnStatus Close()
00112     {
00113         return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
00114     }
00115 
00116     XnStatus Set()
00117     {
00118         return xnOSSetEvent(m_hEvent);
00119     }
00120 
00121     XnStatus Reset()
00122     {
00123         return xnOSResetEvent(m_hEvent);
00124     }
00125 
00126     XnStatus Wait(XnUInt32 nMilliseconds)
00127     {
00128         return xnOSWaitEvent(m_hEvent, nMilliseconds);
00129     }
00130 
00131 private:
00132     XN_EVENT_HANDLE m_hEvent;
00133 };
00134 
00135 #endif // __XN_OS_CPP_H__