OpenNI 1.5.4
XnOSCpp.h
Go to the documentation of this file.
1 /****************************************************************************
2 * *
3 * OpenNI 1.x 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 //---------------------------------------------------------------------------
33 class XnAutoCSLocker
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  {
64  m_bLocked = TRUE;
65  }
66  }
67 
68  inline void Unlock()
69  {
70  if (m_bLocked)
71  {
73  m_bLocked = FALSE;
74  }
75  }
76 
77 private:
78  XN_CRITICAL_SECTION_HANDLE m_hCS;
79  XnBool m_bLocked;
80 };
81 
83 {
84 public:
85  inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
86  {
87  m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
88  }
89 
90  XnStatus GetStatus() const
91  {
92  return m_nStatus;
93  }
94 
95  inline ~XnAutoMutexLocker()
96  {
97  if (m_nStatus == XN_STATUS_OK)
98  {
99  //Only unlock if we managed to lock in the first place
100  xnOSUnLockMutex(m_hMutex);
101  }
102  }
103 
104 private:
105  XN_MUTEX_HANDLE m_hMutex;
106  XnStatus m_nStatus;
107 };
108 
109 class XnOSEvent
110 {
111 public:
112  XnOSEvent() : m_hEvent(NULL) {}
113 
114  ~XnOSEvent()
115  {
116  Close();
117  }
118 
119  operator XN_EVENT_HANDLE() const
120  {
121  return m_hEvent;
122  }
123 
124  XnStatus Create(XnBool bManualReset)
125  {
126  return xnOSCreateEvent(&m_hEvent, bManualReset);
127  }
128 
129  XnStatus Create(const XnChar* strName, XnBool bManualReset, XnBool bAllowOtherUsers = FALSE)
130  {
131  return xnOSCreateNamedEventEx(&m_hEvent, strName, bManualReset, bAllowOtherUsers);
132  }
133 
134  XnStatus Open(const XnChar* strName, XnBool bEnableOtherUsers = FALSE)
135  {
136  return xnOSOpenNamedEventEx(&m_hEvent, strName, bEnableOtherUsers);
137  }
138 
139  XnStatus Close()
140  {
141  return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
142  }
143 
144  XnStatus Set()
145  {
146  return xnOSSetEvent(m_hEvent);
147  }
148 
149  XnStatus Reset()
150  {
151  return xnOSResetEvent(m_hEvent);
152  }
153 
154  XnStatus Wait(XnUInt32 nMilliseconds)
155  {
156  return xnOSWaitEvent(m_hEvent, nMilliseconds);
157  }
158 
159 private:
160  XN_EVENT_HANDLE m_hEvent;
161 };
162 
163 #endif // __XN_OS_CPP_H__
XnAutoCSLocker::Lock
void Lock()
Definition: XnOSCpp.h:77
XnOSEvent::Create
XnStatus Create(XnBool bManualReset)
Definition: XnOSCpp.h:123
xnOSCreateEvent
XN_C_API XnStatus XN_C_DECL xnOSCreateEvent(XN_EVENT_HANDLE *pEventHandle, XnBool bManualReset)
XnAutoMutexLocker::~XnAutoMutexLocker
~XnAutoMutexLocker()
Definition: XnOSCpp.h:94
XnOSEvent::~XnOSEvent
~XnOSEvent()
Definition: XnOSCpp.h:113
XnOS.h
XnOSEvent
Definition: XnOSCpp.h:108
XN_STATUS_OK
#define XN_STATUS_OK
Definition: XnStatus.h:36
FALSE
#define FALSE
Definition: XnPlatform.h:93
TRUE
#define TRUE
Definition: XnPlatform.h:89
xnOSUnLockMutex
XN_C_API XnStatus XN_C_DECL xnOSUnLockMutex(const XN_MUTEX_HANDLE MutexHandle)
XnStatus
XnUInt32 XnStatus
Definition: XnStatus.h:33
XnOSEvent::XnOSEvent
XnOSEvent()
Definition: XnOSCpp.h:111
XnAutoCSLocker::operator=
XnAutoCSLocker & operator=(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:59
XnAutoMutexLocker
Definition: XnOSCpp.h:81
XnAutoCSLocker
Definition: XnOSCpp.h:32
XnAutoCSLocker::Unlock
void Unlock()
Definition: XnOSCpp.h:86
xnOSOpenNamedEventEx
XN_C_API XnStatus XN_C_DECL xnOSOpenNamedEventEx(XN_EVENT_HANDLE *pEventHandle, const XnChar *cpEventName, XnBool bAllowOtherUsers)
xnOSCreateNamedEventEx
XN_C_API XnStatus XN_C_DECL xnOSCreateNamedEventEx(XN_EVENT_HANDLE *pEventHandle, const XnChar *cpEventName, XnBool bManualReset, XnBool bAllowOtherUsers)
XnAutoMutexLocker::XnAutoMutexLocker
XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:84
XnOSEvent::Close
XnStatus Close()
Definition: XnOSCpp.h:138
XnAutoMutexLocker::GetStatus
XnStatus GetStatus() const
Definition: XnOSCpp.h:89
xnOSCloseEvent
XN_C_API XnStatus XN_C_DECL xnOSCloseEvent(XN_EVENT_HANDLE *pEventHandle)
XnOSEvent::Set
XnStatus Set()
Definition: XnOSCpp.h:143
XnAutoCSLocker::XnAutoCSLocker
XnAutoCSLocker(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:54
XnOSEvent::Open
XnStatus Open(const XnChar *strName, XnBool bEnableOtherUsers=FALSE)
Definition: XnOSCpp.h:133
xnOSLockMutex
XN_C_API XnStatus XN_C_DECL xnOSLockMutex(const XN_MUTEX_HANDLE MutexHandle, XnUInt32 nMilliseconds)
xnOSWaitEvent
XN_C_API XnStatus XN_C_DECL xnOSWaitEvent(const XN_EVENT_HANDLE EventHandle, XnUInt32 nMilliseconds)
XnOSEvent::Wait
XnStatus Wait(XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:153
xnOSSetEvent
XN_C_API XnStatus XN_C_DECL xnOSSetEvent(const XN_EVENT_HANDLE EventHandle)
xnOSResetEvent
XN_C_API XnStatus XN_C_DECL xnOSResetEvent(const XN_EVENT_HANDLE EventHandle)
xnOSEnterCriticalSection
XN_C_API XnStatus XN_C_DECL xnOSEnterCriticalSection(XN_CRITICAL_SECTION_HANDLE *pCriticalSectionHandle)
XnOSEvent::Reset
XnStatus Reset()
Definition: XnOSCpp.h:148
xnOSLeaveCriticalSection
XN_C_API XnStatus XN_C_DECL xnOSLeaveCriticalSection(XN_CRITICAL_SECTION_HANDLE *pCriticalSectionHandle)
XnAutoCSLocker::~XnAutoCSLocker
~XnAutoCSLocker()
Definition: XnOSCpp.h:72