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