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 {
34 public:
35  inline XnAutoCSLocker(const XnAutoCSLocker& other) : m_hCS(other.m_hCS), m_bLocked(FALSE)
36  {
37  Lock();
38  }
39 
40  inline XnAutoCSLocker& operator=(const XnAutoCSLocker& other)
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 
53  inline ~XnAutoCSLocker()
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 
76 private:
77  XN_CRITICAL_SECTION_HANDLE m_hCS;
78  XnBool m_bLocked;
79 };
80 
82 {
83 public:
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 
103 private:
104  XN_MUTEX_HANDLE m_hMutex;
105  XnStatus m_nStatus;
106 };
107 
109 {
110 public:
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 
158 private:
159  XN_EVENT_HANDLE m_hEvent;
160 };
161 
162 #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:89
TRUE
#define TRUE
Definition: XnPlatform.h:85
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::Create
XnStatus Create(const XnChar *strName, XnBool bManualReset, XnBool bAllowOtherUsers=FALSE)
Definition: XnOSCpp.h:128
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