OpenNI 1.5.7
XnOSCpp.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef __XN_OS_CPP_H__
00022 #define __XN_OS_CPP_H__
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include <XnOS.h>
00028 
00029 //---------------------------------------------------------------------------
00030 // Types
00031 //---------------------------------------------------------------------------
00032 class XnAutoCSLocker
00033 {
00034 public:
00035     inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE)
00036     {
00037         Lock();
00038     }
00039 
00040     inline XnAutoCSLocker& operator=(const XnAutoCSLocker& other)
00041     {
00042         Unlock();
00043         m_hCS = other.m_hCS;
00044         Lock();
00045         return *this;
00046     }
00047 
00048     inline XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS) : m_hCS(hCS), m_bLocked(FALSE)
00049     {
00050         Lock();
00051     }
00052 
00053     inline ~XnAutoCSLocker()
00054     {
00055         Unlock();
00056     }
00057 
00058     inline void Lock()
00059     {
00060         if (!m_bLocked)
00061         {
00062             xnOSEnterCriticalSection(&m_hCS);
00063             m_bLocked = TRUE;
00064         }
00065     }
00066 
00067     inline void Unlock()
00068     {
00069         if (m_bLocked)
00070         {
00071             xnOSLeaveCriticalSection(&m_hCS);
00072             m_bLocked = FALSE;
00073         }
00074     }
00075 
00076 private:
00077     XN_CRITICAL_SECTION_HANDLE m_hCS;
00078     XnBool m_bLocked;
00079 };
00080 
00081 class XnAutoMutexLocker
00082 {
00083 public:
00084     inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
00085     {
00086         m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
00087     }
00088 
00089     XnStatus GetStatus() const 
00090     { 
00091         return m_nStatus; 
00092     }
00093 
00094     inline ~XnAutoMutexLocker()
00095     {
00096         if (m_nStatus == XN_STATUS_OK)
00097         {
00098             //Only unlock if we managed to lock in the first place
00099             xnOSUnLockMutex(m_hMutex);
00100         }
00101     }
00102 
00103 private:
00104     XN_MUTEX_HANDLE m_hMutex;
00105     XnStatus m_nStatus;
00106 };
00107 
00108 class XnOSEvent
00109 {
00110 public:
00111     XnOSEvent() : m_hEvent(NULL) {}
00112     
00113     ~XnOSEvent() 
00114     { 
00115         Close(); 
00116     }
00117 
00118     operator XN_EVENT_HANDLE() const 
00119     {
00120         return m_hEvent;
00121     }
00122 
00123     XnStatus Create(XnBool bManualReset)
00124     {
00125         return xnOSCreateEvent(&m_hEvent, bManualReset);
00126     }
00127 
00128     XnStatus Create(const XnChar* strName, XnBool bManualReset, XnBool bAllowOtherUsers = FALSE)
00129     {
00130         return xnOSCreateNamedEventEx(&m_hEvent, strName, bManualReset, bAllowOtherUsers);
00131     }
00132 
00133     XnStatus Open(const XnChar* strName, XnBool bEnableOtherUsers = FALSE)
00134     {
00135         return xnOSOpenNamedEventEx(&m_hEvent, strName, bEnableOtherUsers);
00136     }
00137 
00138     XnStatus Close()
00139     {
00140         return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
00141     }
00142 
00143     XnStatus Set()
00144     {
00145         return xnOSSetEvent(m_hEvent);
00146     }
00147 
00148     XnStatus Reset()
00149     {
00150         return xnOSResetEvent(m_hEvent);
00151     }
00152 
00153     XnStatus Wait(XnUInt32 nMilliseconds)
00154     {
00155         return xnOSWaitEvent(m_hEvent, nMilliseconds);
00156     }
00157 
00158 private:
00159     XN_EVENT_HANDLE m_hEvent;
00160 };
00161 
00162 #endif // __XN_OS_CPP_H__