OpenNI 1.5.7
XnOSCpp.h
Go to the documentation of this file.
1/*****************************************************************************
2* *
3* OpenNI 1.x Alpha *
4* Copyright (C) 2012 PrimeSense Ltd. *
5* *
6* This file is part of OpenNI. *
7* *
8* Licensed under the Apache License, Version 2.0 (the "License"); *
9* you may not use this file except in compliance with the License. *
10* You may obtain a copy of the License at *
11* *
12* http://www.apache.org/licenses/LICENSE-2.0 *
13* *
14* Unless required by applicable law or agreed to in writing, software *
15* distributed under the License is distributed on an "AS IS" BASIS, *
16* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
17* See the License for the specific language governing permissions and *
18* limitations under the License. *
19* *
20*****************************************************************************/
21#ifndef __XN_OS_CPP_H__
22#define __XN_OS_CPP_H__
23
24//---------------------------------------------------------------------------
25// Includes
26//---------------------------------------------------------------------------
27#include <XnOS.h>
28
29//---------------------------------------------------------------------------
30// Types
31//---------------------------------------------------------------------------
33{
34public:
35 inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE)
36 {
37 Lock();
38 }
39
41 {
42 Unlock();
43 m_hCS = other.m_hCS;
44 Lock();
45 return *this;
46 }
47
48 inline XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS) : m_hCS(hCS), m_bLocked(FALSE)
49 {
50 Lock();
51 }
52
54 {
55 Unlock();
56 }
57
58 inline void Lock()
59 {
60 if (!m_bLocked)
61 {
63 m_bLocked = TRUE;
64 }
65 }
66
67 inline void Unlock()
68 {
69 if (m_bLocked)
70 {
72 m_bLocked = FALSE;
73 }
74 }
75
76private:
77 XN_CRITICAL_SECTION_HANDLE m_hCS;
78 XnBool m_bLocked;
79};
80
82{
83public:
84 inline XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds) : m_hMutex(hMutex)
85 {
86 m_nStatus = xnOSLockMutex(m_hMutex, nMilliseconds);
87 }
88
90 {
91 return m_nStatus;
92 }
93
95 {
96 if (m_nStatus == XN_STATUS_OK)
97 {
98 //Only unlock if we managed to lock in the first place
99 xnOSUnLockMutex(m_hMutex);
100 }
101 }
102
103private:
104 XN_MUTEX_HANDLE m_hMutex;
105 XnStatus m_nStatus;
106};
107
109{
110public:
111 XnOSEvent() : m_hEvent(NULL) {}
112
114 {
115 Close();
116 }
117
118 operator XN_EVENT_HANDLE() const
119 {
120 return m_hEvent;
121 }
122
123 XnStatus Create(XnBool bManualReset)
124 {
125 return xnOSCreateEvent(&m_hEvent, bManualReset);
126 }
127
128 XnStatus Create(const XnChar* strName, XnBool bManualReset, XnBool bAllowOtherUsers = FALSE)
129 {
130 return xnOSCreateNamedEventEx(&m_hEvent, strName, bManualReset, bAllowOtherUsers);
131 }
132
133 XnStatus Open(const XnChar* strName, XnBool bEnableOtherUsers = FALSE)
134 {
135 return xnOSOpenNamedEventEx(&m_hEvent, strName, bEnableOtherUsers);
136 }
137
139 {
140 return (m_hEvent != NULL) ? xnOSCloseEvent(&m_hEvent) : XN_STATUS_OK;
141 }
142
144 {
145 return xnOSSetEvent(m_hEvent);
146 }
147
149 {
150 return xnOSResetEvent(m_hEvent);
151 }
152
153 XnStatus Wait(XnUInt32 nMilliseconds)
154 {
155 return xnOSWaitEvent(m_hEvent, nMilliseconds);
156 }
157
158private:
159 XN_EVENT_HANDLE m_hEvent;
160};
161
162#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:85
#define FALSE
Definition: XnPlatform.h:89
XnUInt32 XnStatus
Definition: XnStatus.h:33
#define XN_STATUS_OK
Definition: XnStatus.h:36
Definition: XnOSCpp.h:33
XnAutoCSLocker & operator=(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:40
void Lock()
Definition: XnOSCpp.h:58
XnAutoCSLocker(XN_CRITICAL_SECTION_HANDLE hCS)
Definition: XnOSCpp.h:48
XnAutoCSLocker(const XnAutoCSLocker &other)
Definition: XnOSCpp.h:35
void Unlock()
Definition: XnOSCpp.h:67
~XnAutoCSLocker()
Definition: XnOSCpp.h:53
Definition: XnOSCpp.h:82
~XnAutoMutexLocker()
Definition: XnOSCpp.h:94
XnAutoMutexLocker(XN_MUTEX_HANDLE hMutex, XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:84
XnStatus GetStatus() const
Definition: XnOSCpp.h:89
Definition: XnOSCpp.h:109
XnStatus Reset()
Definition: XnOSCpp.h:148
XnStatus Wait(XnUInt32 nMilliseconds)
Definition: XnOSCpp.h:153
XnStatus Close()
Definition: XnOSCpp.h:138
~XnOSEvent()
Definition: XnOSCpp.h:113
XnOSEvent()
Definition: XnOSCpp.h:111
XnStatus Create(const XnChar *strName, XnBool bManualReset, XnBool bAllowOtherUsers=FALSE)
Definition: XnOSCpp.h:128
XnStatus Create(XnBool bManualReset)
Definition: XnOSCpp.h:123
XnStatus Set()
Definition: XnOSCpp.h:143
XnStatus Open(const XnChar *strName, XnBool bEnableOtherUsers=FALSE)
Definition: XnOSCpp.h:133