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//---------------------------------------------------------------------------
34{
35public:
36 inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE)
37 {
38 Lock();
39 }
40
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
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
77private:
78 XN_CRITICAL_SECTION_HANDLE m_hCS;
79 XnBool m_bLocked;
80};
81
83{
84public:
85 inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
86 {
87 m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
88 }
89
91 {
92 return m_nStatus;
93 }
94
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
104private:
105 XN_MUTEX_HANDLE m_hMutex;
106 XnStatus m_nStatus;
107};
108
110{
111public:
112 XnOSEvent() : m_hEvent(NULL) {}
113
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
140 {
141 return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
142 }
143
145 {
146 return xnOSSetEvent(m_hEvent);
147 }
148
150 {
151 return xnOSResetEvent(m_hEvent);
152 }
153
154 XnStatus Wait(XnUInt32 nMilliseconds)
155 {
156 return xnOSWaitEvent(m_hEvent, nMilliseconds);
157 }
158
159private:
160 XN_EVENT_HANDLE m_hEvent;
161};
162
163#endif // __XN_OS_CPP_H__
XN_C_API XnStatus XN_C_DECL xnOSResetEvent(const XN_EVENT_HANDLE EventHandle)
XN_C_API XnStatus XN_C_DECL xnOSCreateNamedEventEx(XN_EVENT_HANDLE *pEventHandle, const XnChar *cpEventName, XnBool bManualReset, XnBool bAllowOtherUsers)
XN_C_API XnStatus XN_C_DECL xnOSEnterCriticalSection(XN_CRITICAL_SECTION_HANDLE *pCriticalSectionHandle)
XN_C_API XnStatus XN_C_DECL xnOSWaitEvent(const XN_EVENT_HANDLE EventHandle, XnUInt32 nMilliseconds)
XN_C_API XnStatus XN_C_DECL xnOSCloseEvent(XN_EVENT_HANDLE *pEventHandle)
XN_C_API XnStatus XN_C_DECL xnOSSetEvent(const XN_EVENT_HANDLE EventHandle)
XN_C_API XnStatus XN_C_DECL xnOSLeaveCriticalSection(XN_CRITICAL_SECTION_HANDLE *pCriticalSectionHandle)
XN_C_API XnStatus XN_C_DECL xnOSCreateEvent(XN_EVENT_HANDLE *pEventHandle, XnBool bManualReset)
XN_C_API XnStatus XN_C_DECL xnOSOpenNamedEventEx(XN_EVENT_HANDLE *pEventHandle, const XnChar *cpEventName, XnBool bAllowOtherUsers)
XN_C_API XnStatus XN_C_DECL xnOSUnLockMutex(const XN_MUTEX_HANDLE MutexHandle)
XN_C_API XnStatus XN_C_DECL xnOSLockMutex(const XN_MUTEX_HANDLE MutexHandle, XnUInt32 nMilliseconds)
#define TRUE
Definition: XnPlatform.h:93
#define FALSE
Definition: XnPlatform.h:97
XnUInt32 XnStatus
Definition: XnStatus.h:34
#define XN_STATUS_OK
Definition: XnStatus.h:37
Definition: XnOSCpp.h:34
XnAutoCSLocker & operator=(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:41
void Lock()
Definition: XnOSCpp.h:59
XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS)
Definition: XnOSCpp.h:49
XnAutoCSLocker(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:36
void Unlock()
Definition: XnOSCpp.h:68
~XnAutoCSLocker()
Definition: XnOSCpp.h:54
Definition: XnOSCpp.h:83
~XnAutoMutexLocker()
Definition: XnOSCpp.h:95
XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:85
XnStatus GetStatus() const
Definition: XnOSCpp.h:90
Definition: XnOSCpp.h:110
XnStatus Reset()
Definition: XnOSCpp.h:149
XnStatus Wait(XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:154
XnStatus Close()
Definition: XnOSCpp.h:139
~XnOSEvent()
Definition: XnOSCpp.h:114
XnOSEvent()
Definition: XnOSCpp.h:112
XnStatus Create(const XnChar *strName, XnBool bManualReset, XnBool bAllowOtherUsers=FALSE)
Definition: XnOSCpp.h:129
XnStatus Create(XnBool bManualReset)
Definition: XnOSCpp.h:124
XnStatus Set()
Definition: XnOSCpp.h:144
XnStatus Open(const XnChar *strName, XnBool bEnableOtherUsers=FALSE)
Definition: XnOSCpp.h:134