File: XnEvent.h

package info (click to toggle)
openni2 2.2.0.33%2Bdfsg-15
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 22,232 kB
  • sloc: cpp: 111,183; ansic: 35,511; sh: 10,542; python: 1,313; java: 952; makefile: 575; xml: 12
file content (353 lines) | stat: -rw-r--r-- 9,316 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
/*****************************************************************************
*                                                                            *
*  PrimeSense PSCommon Library                                               *
*  Copyright (C) 2012 PrimeSense Ltd.                                        *
*                                                                            *
*  This file is part of PSCommon.                                            *
*                                                                            *
*  Licensed under the Apache License, Version 2.0 (the "License");           *
*  you may not use this file except in compliance with the License.          *
*  You may obtain a copy of the License at                                   *
*                                                                            *
*      http://www.apache.org/licenses/LICENSE-2.0                            *
*                                                                            *
*  Unless required by applicable law or agreed to in writing, software       *
*  distributed under the License is distributed on an "AS IS" BASIS,         *
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
*  See the License for the specific language governing permissions and       *
*  limitations under the License.                                            *
*                                                                            *
*****************************************************************************/
#ifndef _XN_EVENT_H_
#define _XN_EVENT_H_

#include "XnOSCpp.h"
#include "XnList.h"

namespace xnl
{

template <typename FuncPtr>
struct CallbackT
{
	CallbackT(FuncPtr func, void* pCookie) : pFunc(func), pCookie(pCookie) {}

	FuncPtr pFunc;
	void* pCookie;
};

template <typename FuncPtr>
class EventInterface
{
public:
	typedef FuncPtr HandlerPtr;
	typedef CallbackT<FuncPtr> Callback;
	typedef EventInterface Interface;

	~EventInterface()
	{
		Clear();
		xnOSCloseCriticalSection(&m_hLock);
	}
	XnStatus Register(FuncPtr pFunc, void* pCookie, XnCallbackHandle& handle)
	{
		XnStatus retVal = XN_STATUS_OK;

		XN_VALIDATE_INPUT_PTR(pFunc);

		Callback* pCallback = NULL;
		pCallback = XN_NEW(Callback, pFunc, pCookie);

		{
			AutoCSLocker locker(m_hLock);
			retVal = m_toAdd.AddLast(pCallback);
		}

		if (retVal != XN_STATUS_OK)
		{
			XN_DELETE(pCallback);
			return retVal;
		}

		handle = (XnCallbackHandle)pCallback;
		return XN_STATUS_OK;
	}
	XnStatus Unregister(XnCallbackHandle handle)
	{
		XnStatus retVal = XN_STATUS_OK;

		Callback* pCallback = (Callback*)handle;

		{
			AutoCSLocker locker(m_hLock);
			if (!RemoveCallback(m_toAdd, pCallback))
			{
				retVal = m_toRemove.AddLast(pCallback);
			}
		}
		return retVal;
	}
protected:
	typedef List<Callback*> CallbackPtrList;

	EventInterface()
	{
		Init();
	}
	EventInterface(const EventInterface& other)
	{
		Init();
		*this = other;
	}
	EventInterface& operator=(const EventInterface& other)
	{
		Clear();
		AutoCSLocker otherLocker(other.m_hLock);
		AutoCSLocker locker(m_hLock);

		m_callbacks = other.m_callbacks;
		m_toAdd = other.m_toAdd;
		m_toRemove = other.m_toRemove;

		ApplyListChanges();

		return *this;
	}
	XnStatus Clear()
	{
		AutoCSLocker locker(m_hLock);
		ApplyListChanges();

		for (typename CallbackPtrList::ConstIterator it = m_callbacks.Begin(); it != m_callbacks.End(); ++it)
		{
			Callback* pCallback = *it;
			XN_DELETE(pCallback);
		}

		m_callbacks.Clear();
		m_toRemove.Clear();
		m_toAdd.Clear();

		return XN_STATUS_OK;
	}

	XnStatus ApplyListChanges()
	{
		AutoCSLocker locker(m_hLock);

		for (typename CallbackPtrList::ConstIterator it = m_toAdd.Begin(); it != m_toAdd.End(); ++it)
		{
			m_callbacks.AddLast(*it);
		}
		m_toAdd.Clear();

		for (typename CallbackPtrList::ConstIterator it = m_toRemove.Begin(); it != m_toRemove.End(); ++it)
		{
			Callback* pCallback = *it;
			RemoveCallback(m_callbacks, pCallback);
		}
		m_toRemove.Clear();

		return XN_STATUS_OK;
	}

	bool RemoveCallback(CallbackPtrList& list, Callback* pCallback)
	{
		typename CallbackPtrList::Iterator it = list.Find(pCallback);
		if (it != list.End())
		{
			list.Remove(it);
			XN_DELETE(pCallback);
			return true;
		}
		return false;
	}
	XN_CRITICAL_SECTION_HANDLE m_hLock;
	CallbackPtrList m_callbacks;
	CallbackPtrList m_toAdd;
	CallbackPtrList m_toRemove;
private:
	void Init()
	{
		m_hLock = NULL;
		XnStatus retVal = xnOSCreateCriticalSection(&m_hLock);
		if (retVal != XN_STATUS_OK)
		{
			//XN_ASSERT(false);
		}
	}
};

struct HandlerFuncNoArgs
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(void* pCookie);
};

template <class Arg1>
struct HandlerFunc1Arg
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(Arg1 arg1, void* pCookie);
};

template <class Arg1, class Arg2>
struct HandlerFunc2Args
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(Arg1 arg1, Arg2 arg2, void* pCookie);
};

template <class Arg1, class Arg2, class Arg3>
struct HandlerFunc3Args
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(Arg1 arg1, Arg2 arg2, Arg3 arg3, void* pCookie);
};

template <class Arg1, class Arg2, class Arg3, class Arg4>
struct HandlerFunc4Args
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, void* pCookie);
};

template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
struct HandlerFunc5Args
{
	typedef void (XN_CALLBACK_TYPE* FuncPtr)(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5, void* pCookie);
};

template<class FuncPtr>
class EventBase : public EventInterface<FuncPtr>
{
public:
	using EventInterface<FuncPtr>::Clear;
};

class EventNoArgs : public EventBase<HandlerFuncNoArgs::FuncPtr>
{
public:
	XnStatus Raise()
	{
		AutoCSLocker locker(this->m_hLock);
		ApplyListChanges();

		for (CallbackPtrList::ConstIterator it = m_callbacks.Begin(); it != m_callbacks.End(); ++it)
		{
			Callback* pCallback = *it;
			pCallback->pFunc(pCallback->pCookie);
		}

		ApplyListChanges();
		return XN_STATUS_OK;
	}
};

template <class Arg1>
class Event1Arg : public EventBase<typename HandlerFunc1Arg<Arg1>::FuncPtr>
{
	typedef EventBase<typename HandlerFunc1Arg<Arg1>::FuncPtr> Base;
public:
	XnStatus Raise(Arg1 arg)
	{
		AutoCSLocker locker(this->m_hLock);
		this->ApplyListChanges();

		for (typename Base::CallbackPtrList::ConstIterator it = this->m_callbacks.Begin(); it != this->m_callbacks.End(); ++it)
		{
			typename Base::Callback* pCallback = *it;
			pCallback->pFunc(arg, pCallback->pCookie);
		}

		this->ApplyListChanges();
		return XN_STATUS_OK;
	}
};

template <class EventArgs>
class Event : public Event1Arg<const EventArgs&>
{};

template <class Arg1, class Arg2>
class Event2Args : public EventBase<typename HandlerFunc2Args<Arg1, Arg2>::FuncPtr>
{
	typedef EventBase<typename HandlerFunc2Args<Arg1, Arg2>::FuncPtr> Base;
public:
	XnStatus Raise(Arg1 arg1, Arg2 arg2)
	{
		AutoCSLocker locker(this->m_hLock);
		this->ApplyListChanges();

		for (typename Base::CallbackPtrList::ConstIterator it = this->m_callbacks.Begin(); it != this->m_callbacks.End(); ++it)
		{
			typename Base::Callback* pCallback = *it;
			pCallback->pFunc(arg1, arg2, pCallback->pCookie);
		}

		this->ApplyListChanges();
		return XN_STATUS_OK;
	}
};

template <class Arg1, class Arg2, class Arg3>
class Event3Args : public EventBase<typename HandlerFunc3Args<Arg1, Arg2, Arg3>::FuncPtr>
{
	typedef EventBase<typename HandlerFunc3Args<Arg1, Arg2, Arg3>::FuncPtr> Base;
public:
	XnStatus Raise(Arg1 arg1, Arg2 arg2, Arg3 arg3)
	{
		AutoCSLocker locker(this->m_hLock);
		this->ApplyListChanges();

		for (typename Base::CallbackPtrList::ConstIterator it = this->m_callbacks.Begin(); it != this->m_callbacks.End(); ++it)
		{
			typename Base::Callback* pCallback = *it;
			pCallback->pFunc(arg1, arg2, arg3, pCallback->pCookie);
		}

		this->ApplyListChanges();
		return XN_STATUS_OK;
	}
};

template <class Arg1, class Arg2, class Arg3, class Arg4>
class Event4Args : public EventBase<typename HandlerFunc4Args<Arg1, Arg2, Arg3, Arg4>::FuncPtr>
{
	typedef EventBase<typename HandlerFunc4Args<Arg1, Arg2, Arg3, Arg4>::FuncPtr> Base;
public:
	XnStatus Raise(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4)
	{
		AutoCSLocker locker(this->m_hLock);
		this->ApplyListChanges();

		for (typename Base::CallbackPtrList::ConstIterator it = this->m_callbacks.Begin(); it != this->m_callbacks.End(); ++it)
		{
			typename Base::Callback* pCallback = *it;
			pCallback->pFunc(arg1, arg2, arg3, arg4, pCallback->pCookie);
		}

		this->ApplyListChanges();
		return XN_STATUS_OK;
	}
};

template <class Arg1, class Arg2, class Arg3, class Arg4, class Arg5>
class Event5Args : public EventBase<typename HandlerFunc5Args<Arg1, Arg2, Arg3, Arg4, Arg5>::FuncPtr>
{
	typedef EventBase<typename HandlerFunc5Args<Arg1, Arg2, Arg3, Arg4, Arg5>::FuncPtr> Base;
public:
	XnStatus Raise(Arg1 arg1, Arg2 arg2, Arg3 arg3, Arg4 arg4, Arg5 arg5)
	{
		AutoCSLocker locker(this->m_hLock);
		this->ApplyListChanges();

		for (typename Base::CallbackPtrList::ConstIterator it = this->m_callbacks.Begin(); it != this->m_callbacks.End(); ++it)
		{
			typename Base::Callback* pCallback = *it;
			pCallback->pFunc(arg1, arg2, arg3, arg4, arg5, pCallback->pCookie);
		}

		this->ApplyListChanges();
		return XN_STATUS_OK;
	}
};

} // xnl

#endif // _XN_EVENT_H_