OpenNI 1.3.2
XnOSCpp.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.1 Alpha *
4 * Copyright (C) 2011 PrimeSense Ltd. *
5 * *
6 * This file is part of OpenNI. *
7 * *
8 * OpenNI is free software: you can redistribute it and/or modify *
9 * it under the terms of the GNU Lesser General Public License as published *
10 * by the Free Software Foundation, either version 3 of the License, or *
11 * (at your option) any later version. *
12 * *
13 * OpenNI is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
16 * GNU Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public License *
19 * along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
20 * *
21 ****************************************************************************/
22 #ifndef __XN_OS_CPP_H__
23 #define __XN_OS_CPP_H__
24 
25 //---------------------------------------------------------------------------
26 // Includes
27 //---------------------------------------------------------------------------
28 #include <XnOS.h>
29 
30 //---------------------------------------------------------------------------
31 // Types
32 //---------------------------------------------------------------------------
34 {
35 public:
36  inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE)
37  {
38  Lock();
39  }
40 
41  inline XnAutoCSLocker& operator=(const XnAutoCSLocker& other)
42  {
43  Unlock();
44  m_hCS = other.m_hCS;
45  Lock();
46  return *this;
47  }
48 
49  inline XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS) : m_hCS(hCS), m_bLocked(FALSE)
50  {
51  Lock();
52  }
53 
54  inline ~XnAutoCSLocker()
55  {
56  Unlock();
57  }
58 
59  inline void Lock()
60  {
61  if (!m_bLocked)
62  {
63  XnStatus nRetVal = xnOSEnterCriticalSection(&m_hCS);
64  XN_ASSERT(nRetVal == XN_STATUS_OK);
65  m_bLocked = TRUE;
66  }
67  }
68 
69  inline void Unlock()
70  {
71  if (m_bLocked)
72  {
73  XnStatus nRetVal = xnOSLeaveCriticalSection(&m_hCS);
74  XN_ASSERT(nRetVal == XN_STATUS_OK);
75  m_bLocked = FALSE;
76  }
77  }
78 
79 private:
80  XN_CRITICAL_SECTION_HANDLE m_hCS;
81  XnBool m_bLocked;
82 };
83 
85 {
86 public:
87  inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
88  {
89  m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
90  }
91 
93  {
94  return m_nStatus;
95  }
96 
98  {
99  if (m_nStatus == XN_STATUS_OK)
100  {
101  //Only unlock if we managed to lock in the first place
102  xnOSUnLockMutex(m_hMutex);
103  }
104  }
105 
106 private:
107  XN_MUTEX_HANDLE m_hMutex;
108  XnStatus m_nStatus;
109 };
110 
112 {
113 public:
114  XnOSEvent() : m_hEvent(NULL) {}
115 
117  {
118  Close();
119  }
120 
121  operator XN_EVENT_HANDLE() const
122  {
123  return m_hEvent;
124  }
125 
126  XnStatus Create(XnBool bManualReset)
127  {
128  return xnOSCreateEvent(&m_hEvent, bManualReset);
129  }
130 
131  XnStatus Create(const XnChar* strName, XnBool bManualReset)
132  {
133  return xnOSCreateNamedEvent(&m_hEvent, strName, bManualReset);
134  }
135 
136  XnStatus Open(const XnChar* strName)
137  {
138  return xnOSOpenNamedEvent(&m_hEvent, strName);
139  }
140 
142  {
143  return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
144  }
145 
147  {
148  return xnOSSetEvent(m_hEvent);
149  }
150 
152  {
153  return xnOSResetEvent(m_hEvent);
154  }
155 
156  XnStatus Wait(XnUInt32 nMilliseconds)
157  {
158  return xnOSWaitEvent(m_hEvent, nMilliseconds);
159  }
160 
161 private:
162  XN_EVENT_HANDLE m_hEvent;
163 };
164 
165 #endif // __XN_OS_CPP_H__