OpenNI 1.5.7
XnCyclicStackT.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_CYCLIC_STACK_T_H_
22 #define _XN_CYCLIC_STACK_T_H_
23 
24 //---------------------------------------------------------------------------
25 // Includes
26 //---------------------------------------------------------------------------
27 #include "XnStackT.h"
28 
29 //---------------------------------------------------------------------------
30 // Code
31 //---------------------------------------------------------------------------
32 
33 template<class T, XnUInt32 TDefaultMaxDepth, class TAlloc = XnLinkedNodeDefaultAllocatorT<T> >
34 class XnCyclicStackT : protected XnStackT<T, TAlloc>
35 {
36 public:
37  typedef XnStackT<T, TAlloc> Base;
38 
39  XnCyclicStackT(XnUInt32 nMaxDepth = TDefaultMaxDepth) : Base(), m_nMaxDepth(nMaxDepth) {}
40 
41  XnCyclicStackT(const XnCyclicStackT& other) : Base(other)
42  {
43  *this = other;
44  }
45 
47  {
48  Base::operator=(other);
49  m_nMaxDepth = other.m_nMaxDepth;
50  return *this;
51  }
52 
53  ~XnCyclicStackT() {}
54 
55  using typename Base::ConstIterator;
56  using Base::Remove;
58  using Base::Size;
59 
60  XnStatus SetMaxSize(XnUInt32 nMaxSize)
61  {
62  XnStatus nRetVal = XN_STATUS_OK;
63 
64  while (Size() > nMaxSize)
65  {
66  nRetVal = Remove(this->ReverseBegin());
67  XN_IS_STATUS_OK(nRetVal);
68  }
69 
70  m_nMaxDepth = nMaxSize;
71 
72  return (XN_STATUS_OK);
73  }
74 
75  XnStatus Push(T const& value)
76  {
77  XnStatus nRetVal = XN_STATUS_OK;
78  if (Size() == m_nMaxDepth)
79  {
80  nRetVal = Remove(this->ReverseBegin());
81  XN_IS_STATUS_OK(nRetVal);
82  }
83 
84  nRetVal = Base::Push(value);
85  XN_IS_STATUS_OK(nRetVal);
86 
87  return (XN_STATUS_OK);
88  }
89 
90  using Base::Pop;
91  using Base::Top;
92  using Base::Begin;
93  using Base::End;
94 
95 protected:
96  XnUInt32 m_nMaxDepth;
97 };
98 
99 
100 #endif // _XN_CYCLIC_STACK_T_H_
XnStackT
Definition: XnStackT.h:32
XN_IS_STATUS_OK
#define XN_IS_STATUS_OK(x)
Definition: XnMacros.h:58
XN_STATUS_OK
#define XN_STATUS_OK
Definition: XnStatus.h:35
XnListT::Remove
XnStatus Remove(ConstIterator where)
Definition: XnListT.h:445
XnStackT::ConstIterator
Base::ConstIterator ConstIterator
Definition: XnStackT.h:55
XnStackT::operator=
XnStackT & operator=(const XnStackT &other)
Definition: XnStackT.h:64
XnStatus
XnUInt32 XnStatus
Definition: XnStatus.h:32
XnStackT::Begin
ConstIterator Begin() const
Definition: XnStackT.h:91
XnCyclicStackT::m_nMaxDepth
XnUInt32 m_nMaxDepth
Definition: XnCyclicStackT.h:114
XnStackT.h
XnCyclicStackT::Base
XnStackT< T, TAlloc > Base
Definition: XnCyclicStackT.h:55
XnStackT::IsEmpty
XnBool IsEmpty() const
Definition: XnStackT.h:73
XnCyclicStackT::Push
XnStatus Push(T const &value)
Definition: XnCyclicStackT.h:93
XnCyclicStackT::SetMaxSize
XnStatus SetMaxSize(XnUInt32 nMaxSize)
Definition: XnCyclicStackT.h:78
XnCyclicStackT::XnCyclicStackT
XnCyclicStackT(XnUInt32 nMaxDepth=TDefaultMaxDepth)
Definition: XnCyclicStackT.h:57
XnStackT::Push
XnStatus Push(T const &value)
Definition: XnStackT.h:75
XnListT::Size
XnUInt32 Size() const
Definition: XnListT.h:509
XnStackT::Top
T const & Top() const
Definition: XnStackT.h:88
XnCyclicStackT::operator=
XnCyclicStackT & operator=(const XnCyclicStackT &other)
Definition: XnCyclicStackT.h:64
XnListT::ReverseBegin
Iterator ReverseBegin()
Definition: XnListT.h:316
XnStackT::Pop
XnStatus Pop(T &value)
Definition: XnStackT.h:77
XnStackT::End
ConstIterator End() const
Definition: XnStackT.h:92
XnCyclicStackT
Definition: XnCyclicStackT.h:34
XnCyclicStackT::~XnCyclicStackT
~XnCyclicStackT()
Definition: XnCyclicStackT.h:71