OpenNI 1.5.4
Main Page
Related Pages
Modules
Namespaces
Classes
Files
File List
File Members
Include
XnList.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_LIST_H
23
#define _XN_LIST_H
24
25
//---------------------------------------------------------------------------
26
// Includes
27
//---------------------------------------------------------------------------
28
#include <
XnDataTypes.h
>
29
#include <
IXnNodeAllocator.h
>
30
#include <
XnNodeAllocator.h
>
31
#include <
XnNode.h
>
32
#include <
XnStatusCodes.h
>
33
34
//---------------------------------------------------------------------------
35
// Types
36
//---------------------------------------------------------------------------
37
41
class
XnList
42
{
43
public
:
44
class
ConstIterator
45
{
46
public
:
47
friend
class
XnList
;
48
54
ConstIterator
(
const
ConstIterator
& other) :
m_pCurrent
(other.
m_pCurrent
) {}
55
59
ConstIterator
&
operator++
()
60
{
61
m_pCurrent
=
m_pCurrent
->
Next
();
62
return
*
this
;
63
}
64
68
ConstIterator
operator++
(
int
)
69
{
70
ConstIterator
other(
m_pCurrent
);
71
m_pCurrent
=
m_pCurrent
->
Next
();
72
return
other;
73
}
74
78
ConstIterator
&
operator--
()
79
{
80
m_pCurrent
=
m_pCurrent
->
Previous
();
81
return
*
this
;
82
}
83
87
ConstIterator
operator--
(
int
)
88
{
89
ConstIterator
other = *
this
;
90
--*
this
;
91
return
other;
92
}
93
99
XnBool
operator==
(
const
ConstIterator
& other)
const
100
{
101
return
m_pCurrent
== other.
m_pCurrent
;
102
}
108
XnBool
operator!=
(
const
ConstIterator
& other)
const
109
{
110
return
m_pCurrent
!= other.
m_pCurrent
;
111
}
112
116
const
XnValue
&
operator*
()
const
117
{
118
return
m_pCurrent
->
Data
();
119
}
120
121
125
const
XnNode
*
GetNode
()
const
126
{
127
return
m_pCurrent
;
128
}
129
133
XnNode
*
GetNode
()
134
{
135
return
m_pCurrent
;
136
}
137
138
protected
:
144
ConstIterator
(
XnNode
* pNode) :
m_pCurrent
(pNode) {}
145
147
XnNode
*
m_pCurrent
;
148
};
149
153
class
Iterator
:
public
ConstIterator
154
{
155
public
:
156
friend
class
XnList
;
157
163
inline
Iterator
(
const
Iterator
& other) :
ConstIterator
(other) {}
164
168
inline
Iterator
&
operator++
()
169
{
170
++(*(
ConstIterator
*)
this
);
171
return
(*
this
);
172
}
176
inline
Iterator
operator++
(
int
)
177
{
178
Iterator
result = *
this
;
179
++*
this
;
180
return
(result);
181
}
182
186
inline
Iterator
&
operator--
()
187
{
188
--(*(
ConstIterator
*)
this
);
189
return
(*
this
);
190
}
194
inline
Iterator
operator--
(
int
)
195
{
196
Iterator
result = *
this
;
197
--*
this
;
198
return
(result);
199
}
200
204
inline
XnValue
&
operator*
()
const
{
return
((
XnValue
&)**(
ConstIterator
*)
this
); }
205
206
protected
:
212
inline
Iterator
(
XnNode
* pNode) :
ConstIterator
(pNode) {}
213
};
214
215
public
:
219
XnList
()
220
{
221
//Default node allocator is XnNodeAllocator
222
Init
(
XN_NEW
(
XnNodeAllocator
));
223
m_bOwnsAllocator
=
TRUE
;
224
}
225
229
virtual
~XnList
()
230
{
231
Clear
();
232
233
// Return base node to the pool
234
m_pNodeAllocator
->
Deallocate
(
m_pBase
);
235
236
if
(
m_bOwnsAllocator
)
237
{
238
//We created the allocator in this object, so we must release it
239
XN_DELETE
(
m_pNodeAllocator
);
240
}
241
}
242
250
XnStatus
AddFirst
(
const
XnValue
& value)
251
{
252
return
Add
(
m_pBase
, value);
253
}
254
262
XnStatus
AddLast
(
const
XnValue
& value)
263
{
264
return
Add
(
rbegin
().m_pCurrent, value);
265
}
266
276
XnStatus
AddAfter
(
ConstIterator
where,
const
XnValue
& val)
277
{
278
if
(where ==
end
())
279
{
280
return
XN_STATUS_ILLEGAL_POSITION;
281
}
282
283
return
Add
(where.
m_pCurrent
, val);
284
}
285
294
XnStatus
AddBefore
(
ConstIterator
where,
const
XnValue
& val)
295
{
296
if
(where ==
end
())
297
{
298
return
XN_STATUS_ILLEGAL_POSITION;
299
}
300
301
return
Add
(where.
m_pCurrent
->
Previous
(), val);
302
}
303
304
312
Iterator
Find
(
const
XnValue
& value)
313
{
314
if
(
IsEmpty
())
315
{
316
return
end
();
317
}
318
319
Iterator
iter =
begin
();
320
for
(; iter !=
end
(); ++iter)
321
{
322
if
(*iter == value)
323
break
;
324
}
325
return
iter;
326
}
327
328
336
ConstIterator
Find
(
const
XnValue
& value)
const
337
{
338
if
(
IsEmpty
())
339
{
340
return
end
();
341
}
342
343
ConstIterator
iter =
begin
();
344
for
(; iter !=
end
(); ++iter)
345
{
346
if
(*iter == value)
347
break
;
348
}
349
return
iter;
350
}
351
352
361
XnStatus
Remove
(
ConstIterator
where,
XnValue
& value)
362
{
363
value = *where;
364
return
Remove
(where);
365
}
366
374
virtual
XnStatus
Remove
(
ConstIterator
where)
375
{
376
// Verify iterator is valid
377
if
(where ==
end
())
378
{
379
return
XN_STATUS_ILLEGAL_POSITION;
380
}
381
if
(
IsEmpty
())
382
{
383
return
XN_STATUS_IS_EMPTY;
384
}
385
386
XnNode
* pToRemove = where.
m_pCurrent
;
387
388
// Connect other nodes to bypass the one removed
389
pToRemove->
Previous
()->
Next
() = pToRemove->
Next
();
390
pToRemove->
Next
()->
Previous
() = pToRemove->
Previous
();
391
392
// Return removed node to the pool
393
m_pNodeAllocator
->
Deallocate
(pToRemove);
394
395
return
XN_STATUS_OK
;
396
}
397
398
402
XnStatus
Clear
()
403
{
404
while
(!
IsEmpty
())
405
Remove
(
begin
());
406
407
return
XN_STATUS_OK
;
408
}
409
413
XnBool
IsEmpty
()
const
414
{
415
return
(
begin
() ==
end
());
416
}
417
421
XnUInt32
Size
()
const
422
{
423
XnUInt32 nSize = 0;
424
for
(
ConstIterator
iter =
begin
(); iter !=
end
(); ++iter, ++nSize)
425
;
426
427
return
nSize;
428
}
429
433
Iterator
begin
()
434
{
435
return
Iterator
(
m_pBase
->
Next
());
436
}
437
441
ConstIterator
begin
()
const
442
{
443
return
ConstIterator
(
m_pBase
->
Next
());
444
}
445
449
Iterator
end
()
450
{
451
return
Iterator
(
m_pBase
);
452
}
453
457
ConstIterator
end
()
const
458
{
459
return
ConstIterator
(
m_pBase
);
460
}
461
465
Iterator
rbegin
()
466
{
467
return
Iterator
(
m_pBase
->
Previous
());
468
}
469
473
ConstIterator
rbegin
()
const
474
{
475
return
ConstIterator
(
m_pBase
->
Previous
());
476
}
477
481
Iterator
rend
()
482
{
483
return
Iterator
(
m_pBase
);
484
}
485
489
ConstIterator
rend
()
const
490
{
491
return
ConstIterator
(
m_pBase
);
492
}
493
494
protected
:
495
friend
class
XnNodeManager
;
496
500
XnList
(
INiNodeAllocator
* pNodeAllocator)
501
{
502
Init
(pNodeAllocator);
503
m_bOwnsAllocator
=
FALSE
;
504
}
505
506
void
Init
(
INiNodeAllocator
* pNodeAllocator)
507
{
508
m_pNodeAllocator
= pNodeAllocator;
509
// Allocate a node to act as base node.
510
m_pBase
=
m_pNodeAllocator
->
Allocate
();
511
if
(
m_pBase
== NULL)
512
{
513
// OZOZ: Allocation failed in ctor...
514
}
515
516
m_pBase
->
Next
() =
m_pBase
->
Previous
() =
m_pBase
;
517
}
518
527
XnStatus
Add
(
XnNode
* pWhere,
const
XnValue
& val)
528
{
529
// Get a node from the pool for the entry
530
XnNode
* pNewNode =
m_pNodeAllocator
->
Allocate
();
531
if
(pNewNode == NULL)
532
{
533
return
XN_STATUS_ALLOC_FAILED;
534
}
535
// push new node to position
536
pNewNode->
Data
() = val;
537
pNewNode->
Next
() = pWhere->
Next
();
538
pNewNode->
Previous
() = pWhere;
539
pWhere->
Next
()->
Previous
() = pNewNode;
540
pWhere->
Next
() = pNewNode;
541
542
return
XN_STATUS_OK
;
543
}
544
545
547
XnNode
*
m_pBase
;
548
549
INiNodeAllocator
*
m_pNodeAllocator
;
550
XnBool
m_bOwnsAllocator
;
551
552
private
:
553
XN_DISABLE_COPY_AND_ASSIGN
(
XnList
);
554
};
555
560
#define XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(decl, Type, ClassName, Translator) \
561
class decl ClassName : public XnList \
562
{ \
563
public: \
564
class decl ConstIterator : public XnList::ConstIterator \
565
{ \
566
public: \
567
friend class ClassName; \
568
inline ConstIterator(const ConstIterator& other) : XnList::ConstIterator(other) {} \
569
inline ConstIterator& operator++() \
570
{ \
571
++(*(XnList::ConstIterator*)this); \
572
return (*this); \
573
} \
574
inline ConstIterator operator++(int) \
575
{ \
576
ConstIterator result = *this; \
577
++*this; \
578
return result; \
579
} \
580
inline ConstIterator& operator--() \
581
{ \
582
--(*(XnList::ConstIterator*)this); \
583
return (*this); \
584
} \
585
inline ConstIterator operator--(int) \
586
{ \
587
ConstIterator result = *this; \
588
--*this; \
589
return result; \
590
} \
591
inline Type const& operator*() const \
592
{ \
593
return Translator::GetFromValue(**((XnList::ConstIterator*)this)); \
594
} \
595
inline Type const* operator->() const { return (&**this); } \
596
protected: \
597
inline ConstIterator(XnNode* pNode) : XnList::ConstIterator(pNode) {} \
598
inline ConstIterator(const XnList::ConstIterator& other) : \
599
XnList::ConstIterator(other) \
600
{} \
601
}; \
602
class decl Iterator : public ConstIterator \
603
{ \
604
public: \
605
friend class ClassName; \
606
Iterator(const Iterator& other) : ConstIterator(other) {} \
607
inline Iterator& operator++() \
608
{ \
609
++(*(ConstIterator*)this); \
610
return (*this); \
611
} \
612
inline Iterator operator++(int) \
613
{ \
614
Iterator result = *this; \
615
++*this; \
616
return result; \
617
} \
618
inline Iterator& operator--() \
619
{ \
620
--(*(ConstIterator*)this); \
621
return (*this); \
622
} \
623
inline Iterator operator--(int) \
624
{ \
625
Iterator result = *this; \
626
--*this; \
627
return result; \
628
} \
629
inline Type& operator*() const { return ((Type&)**(ConstIterator*)this); } \
630
inline Type* operator->() const { return (&**this); } \
631
protected: \
632
inline Iterator(XnNode* pNode) : ConstIterator(pNode) {} \
633
inline Iterator(const XnList::Iterator& other) : ConstIterator(other) {} \
634
}; \
635
public: \
636
ClassName() \
637
{ \
638
} \
639
~ClassName() \
640
{ \
641
while (!IsEmpty()) \
642
Remove(begin()); \
643
} \
644
inline XnStatus AddFirst(Type const& value) \
645
{ \
646
XnValue val = Translator::CreateValueCopy(value); \
647
XnStatus nRetVal = XnList::AddFirst(val); \
648
if (nRetVal != XN_STATUS_OK) \
649
{ \
650
Translator::FreeValue(val); \
651
return (nRetVal); \
652
} \
653
return XN_STATUS_OK; \
654
} \
655
inline XnStatus AddLast(Type const& value) \
656
{ \
657
XnValue val = Translator::CreateValueCopy(value); \
658
XnStatus nRetVal = XnList::AddLast(val); \
659
if (nRetVal != XN_STATUS_OK) \
660
{ \
661
Translator::FreeValue(val); \
662
return (nRetVal); \
663
} \
664
return XN_STATUS_OK; \
665
} \
666
inline XnStatus AddAfter(ConstIterator where, Type const& value) \
667
{ \
668
XnValue val = Translator::CreateValueCopy(value); \
669
XnStatus nRetVal = XnList::AddAfter(where, val); \
670
if (nRetVal != XN_STATUS_OK) \
671
{ \
672
Translator::FreeValue(val); \
673
return (nRetVal); \
674
} \
675
return XN_STATUS_OK; \
676
} \
677
inline XnStatus AddBefore(ConstIterator where, Type const& value) \
678
{ \
679
XnValue val = Translator::CreateValueCopy(value); \
680
XnStatus nRetVal = XnList::AddBefore(where, val); \
681
if (nRetVal != XN_STATUS_OK) \
682
{ \
683
Translator::FreeValue(val); \
684
return (nRetVal); \
685
} \
686
return XN_STATUS_OK; \
687
} \
688
inline ConstIterator Find(Type const& value) const \
689
{ \
690
XnValue _value = Translator::GetAsValue(value); \
691
return XnList::Find(_value); \
692
} \
693
inline Iterator Find(Type const& value) \
694
{ \
695
XnValue _value = Translator::GetAsValue(value); \
696
return XnList::Find(_value); \
697
} \
698
inline XnStatus Remove(ConstIterator where) \
699
{ \
700
XnValue val = Translator::GetAsValue(*where); \
701
XnStatus nRetVal = XnList::Remove(where); \
702
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
703
Translator::FreeValue(val); \
704
return XN_STATUS_OK; \
705
} \
706
inline XnStatus Remove(Type const& value) \
707
{ \
708
Iterator it = Find(value); \
709
return Remove(it); \
710
} \
711
inline Iterator begin() { return XnList::begin(); } \
712
inline ConstIterator begin() const { return XnList::begin(); } \
713
inline Iterator end() { return XnList::end(); } \
714
inline ConstIterator end() const { return XnList::end(); } \
715
inline Iterator rbegin() { return XnList::rbegin(); } \
716
inline ConstIterator rbegin() const { return XnList::rbegin(); } \
717
inline Iterator rend() { return XnList::rend(); } \
718
inline ConstIterator rend() const { return XnList::rend(); } \
719
protected: \
720
virtual XnStatus Remove(XnList::ConstIterator where) \
721
{ \
722
return Remove(ConstIterator(where)); \
723
} \
724
private: \
725
XN_DISABLE_COPY_AND_ASSIGN(ClassName); \
726
};
727
731
#define XN_DECLARE_LIST_WITH_TRANSLATOR(Type, ClassName, Translator) \
732
XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(, Type, ClassName, Translator)
733
738
#define XN_DECLARE_LIST_DECL(decl, Type, ClassName) \
739
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, Type, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
740
XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(decl, Type, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))
741
745
#define XN_DECLARE_LIST(Type, ClassName) \
746
XN_DECLARE_LIST_DECL(, Type, ClassName)
747
748
#endif // _XN_LIST_H
749
Generated on Fri Dec 28 2012 12:04:02 for OpenNI 1.5.4 by
1.8.1.2