OpenNI 1.3.2
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
Include
XnThreadSafeQueue.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_THREAD_SAFE_QUEUE_H__
23
#define __XN_THREAD_SAFE_QUEUE_H__
24
25
//---------------------------------------------------------------------------
26
// Includes
27
//---------------------------------------------------------------------------
28
#include <
XnQueue.h
>
29
#include <
XnOS.h
>
30
31
//---------------------------------------------------------------------------
32
// Types
33
//---------------------------------------------------------------------------
37
class
XnThreadSafeQueue
:
public
XnQueue
38
{
39
public
:
40
XnThreadSafeQueue
() : m_hLock(NULL) {}
41
42
~XnThreadSafeQueue
()
43
{
44
xnOSCloseCriticalSection
(&m_hLock);
45
}
46
47
XnStatus
Init
()
48
{
49
XnStatus
nRetVal =
XN_STATUS_OK
;
50
51
nRetVal =
xnOSCreateCriticalSection
(&m_hLock);
52
XN_IS_STATUS_OK
(nRetVal);
53
54
return
(
XN_STATUS_OK
);
55
}
56
57
XnStatus
Push
(
XnValue
const
& value)
58
{
59
XnStatus
nRetVal =
XN_STATUS_OK
;
60
61
nRetVal =
xnOSEnterCriticalSection
(&m_hLock);
62
XN_IS_STATUS_OK
(nRetVal);
63
64
nRetVal =
XnQueue::Push
(value);
65
xnOSLeaveCriticalSection
(&m_hLock);
66
67
return
nRetVal;
68
}
69
70
XnStatus
Pop
(
XnValue
& value)
71
{
72
XnStatus
nRetVal =
XN_STATUS_OK
;
73
74
nRetVal =
xnOSEnterCriticalSection
(&m_hLock);
75
XN_IS_STATUS_OK
(nRetVal);
76
77
nRetVal =
XnQueue::Pop
(value);
78
xnOSLeaveCriticalSection
(&m_hLock);
79
80
return
nRetVal;
81
}
82
83
XnUInt32
Size
()
const
84
{
85
xnOSEnterCriticalSection
(&m_hLock);
86
XnUInt32 nSize =
XnQueue::Size
();
87
xnOSLeaveCriticalSection
(&m_hLock);
88
return
(nSize);
89
}
90
91
private
:
92
// NOTE: we declare the lock as mutable, as it may change on const methods.
93
mutable
XN_CRITICAL_SECTION_HANDLE m_hLock;
94
};
95
101
#define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, Translator) \
102
class decl ClassName : public XnThreadSafeQueue \
103
{ \
104
public: \
105
~ClassName() \
106
{ \
107
/* We do this using Pop() to make sure memory is freed. */
\
108
Type dummy; \
109
while (Size() != 0) \
110
Pop(dummy); \
111
} \
112
XnStatus Push(Type const& value) \
113
{ \
114
XnValue val = Translator::CreateValueCopy(value); \
115
XnStatus nRetVal = XnThreadSafeQueue::Push(val); \
116
if (nRetVal != XN_STATUS_OK) \
117
{ \
118
Translator::FreeValue(val); \
119
return (nRetVal); \
120
} \
121
return XN_STATUS_OK; \
122
} \
123
XnStatus Pop(Type& value) \
124
{ \
125
XnValue val; \
126
XnStatus nRetVal = XnThreadSafeQueue::Pop(val); \
127
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
128
value = Translator::GetFromValue(val); \
129
return XN_STATUS_OK; \
130
} \
131
};
132
138
#define XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR(Type, ClassName, Translator) \
139
XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(, Type, ClassName, Translator)
140
145
#define XN_DECLARE_THREAD_SAFE_QUEUE_DECL(decl, Type, ClassName) \
146
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, Type, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
147
XN_DECLARE_THREAD_SAFE_QUEUE_WITH_TRANSLATOR_DECL(decl, Type, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))
148
152
#define XN_DECLARE_THREAD_SAFE_QUEUE(Type, ClassName) \
153
XN_DECLARE_THREAD_SAFE_QUEUE_DECL(, Type, ClassName)
154
155
#endif //__XN_THREAD_SAFE_QUEUE_H__
Generated on Sat Nov 10 2012 07:20:44 for OpenNI 1.3.2 by
1.8.1.2