OpenNI 1.5.4
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
41class XnList
42{
43public:
45 {
46 public:
47 friend class XnList;
48
55
60 {
62 return *this;
63 }
64
69 {
72 return other;
73 }
74
79 {
81 return *this;
82 }
83
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
134 {
135 return m_pCurrent;
136 }
137
138 protected:
144 ConstIterator(XnNode* pNode) : m_pCurrent(pNode) {}
145
148 };
149
153 class Iterator : public ConstIterator
154 {
155 public:
156 friend class XnList;
157
163 inline Iterator(const Iterator& other) : ConstIterator(other) {}
164
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
187 {
188 --(*(ConstIterator*)this);
189 return (*this);
190 }
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
215public:
220 {
221 //Default node allocator is XnNodeAllocator
224 }
225
229 virtual ~XnList()
230 {
231 Clear();
232
233 // Return base node to the pool
235
237 {
238 //We created the allocator in this object, so we must release it
240 }
241 }
242
251 {
252 return Add(m_pBase, value);
253 }
254
263 {
264 return Add(rbegin().m_pCurrent, value);
265 }
266
277 {
278 if (where == end())
279 {
280 return XN_STATUS_ILLEGAL_POSITION;
281 }
282
283 return Add(where.m_pCurrent, val);
284 }
285
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
362 {
363 value = *where;
364 return Remove(where);
365 }
366
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
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
434 {
435 return Iterator(m_pBase->Next());
436 }
437
442 {
443 return ConstIterator(m_pBase->Next());
444 }
445
450 {
451 return Iterator(m_pBase);
452 }
453
458 {
459 return ConstIterator(m_pBase);
460 }
461
466 {
467 return Iterator(m_pBase->Previous());
468 }
469
474 {
475 return ConstIterator(m_pBase->Previous());
476 }
477
482 {
483 return Iterator(m_pBase);
484 }
485
490 {
491 return ConstIterator(m_pBase);
492 }
493
494protected:
495 friend class XnNodeManager;
496
500 XnList(INiNodeAllocator* pNodeAllocator)
501 {
502 Init(pNodeAllocator);
504 }
505
506 void Init(INiNodeAllocator* pNodeAllocator)
507 {
508 m_pNodeAllocator = pNodeAllocator;
509 // Allocate a node to act as base node.
511 if (m_pBase == NULL)
512 {
513 // OZOZ: Allocation failed in ctor...
514 }
515
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
548
551
552private:
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
void * XnValue
Definition XnDataTypes.h:36
#define XN_DISABLE_COPY_AND_ASSIGN(TypeName)
Definition XnMacros.h:119
#define XN_DELETE(p)
Definition XnOS.h:336
#define XN_NEW(type,...)
Definition XnOS.h:326
#define TRUE
Definition XnPlatform.h:96
#define FALSE
Definition XnPlatform.h:100
XnUInt32 XnStatus
Definition XnStatus.h:34
#define XN_STATUS_OK
Definition XnStatus.h:37
Definition IXnNodeAllocator.h:34
virtual void Deallocate(XnNode *pNode)=0
virtual XnNode * Allocate()=0
Definition XnList.h:45
XnNode * GetNode()
Definition XnList.h:133
ConstIterator & operator--()
Definition XnList.h:78
ConstIterator operator++(int)
Definition XnList.h:68
XnBool operator!=(const ConstIterator &other) const
Definition XnList.h:108
ConstIterator operator--(int)
Definition XnList.h:87
XnNode * m_pCurrent
Definition XnList.h:147
ConstIterator & operator++()
Definition XnList.h:59
const XnValue & operator*() const
Definition XnList.h:116
const XnNode * GetNode() const
Definition XnList.h:125
ConstIterator(const ConstIterator &other)
Definition XnList.h:54
XnBool operator==(const ConstIterator &other) const
Definition XnList.h:99
ConstIterator(XnNode *pNode)
Definition XnList.h:144
Definition XnList.h:154
Iterator & operator--()
Definition XnList.h:186
XnValue & operator*() const
Definition XnList.h:204
Iterator operator++(int)
Definition XnList.h:176
Iterator & operator++()
Definition XnList.h:168
Iterator operator--(int)
Definition XnList.h:194
Iterator(const Iterator &other)
Definition XnList.h:163
Iterator(XnNode *pNode)
Definition XnList.h:212
Definition XnList.h:42
XnBool m_bOwnsAllocator
Definition XnList.h:550
Iterator end()
Definition XnList.h:449
XnStatus AddLast(const XnValue &value)
Definition XnList.h:262
void Init(INiNodeAllocator *pNodeAllocator)
Definition XnList.h:506
virtual ~XnList()
Definition XnList.h:229
XnUInt32 Size() const
Definition XnList.h:421
XnList()
Definition XnList.h:219
ConstIterator end() const
Definition XnList.h:457
ConstIterator rbegin() const
Definition XnList.h:473
XnNode * m_pBase
Definition XnList.h:547
Iterator rend()
Definition XnList.h:481
ConstIterator Find(const XnValue &value) const
Definition XnList.h:336
virtual XnStatus Remove(ConstIterator where)
Definition XnList.h:374
ConstIterator rend() const
Definition XnList.h:489
XnStatus Remove(ConstIterator where, XnValue &value)
Definition XnList.h:361
XnStatus AddFirst(const XnValue &value)
Definition XnList.h:250
XnStatus Clear()
Definition XnList.h:402
Iterator rbegin()
Definition XnList.h:465
Iterator Find(const XnValue &value)
Definition XnList.h:312
XnList(INiNodeAllocator *pNodeAllocator)
Definition XnList.h:500
INiNodeAllocator * m_pNodeAllocator
Definition XnList.h:549
Iterator begin()
Definition XnList.h:433
XnStatus AddAfter(ConstIterator where, const XnValue &val)
Definition XnList.h:276
ConstIterator begin() const
Definition XnList.h:441
XnStatus AddBefore(ConstIterator where, const XnValue &val)
Definition XnList.h:294
friend class XnNodeManager
Definition XnList.h:495
XnStatus Add(XnNode *pWhere, const XnValue &val)
Definition XnList.h:527
XnBool IsEmpty() const
Definition XnList.h:413
Definition XnNodeAllocator.h:29
Definition XnNode.h:38
XnNode *& Previous()
Definition XnNode.h:60
XnValue & Data()
Definition XnNode.h:69
XnNode *& Next()
Definition XnNode.h:51