OpenNI 1.5.7
XnCyclicStackT.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002 *                                                                            *
00003 *  OpenNI 1.x Alpha                                                          *
00004 *  Copyright (C) 2012 PrimeSense Ltd.                                        *
00005 *                                                                            *
00006 *  This file is part of OpenNI.                                              *
00007 *                                                                            *
00008 *  Licensed under the Apache License, Version 2.0 (the "License");           *
00009 *  you may not use this file except in compliance with the License.          *
00010 *  You may obtain a copy of the License at                                   *
00011 *                                                                            *
00012 *      http://www.apache.org/licenses/LICENSE-2.0                            *
00013 *                                                                            *
00014 *  Unless required by applicable law or agreed to in writing, software       *
00015 *  distributed under the License is distributed on an "AS IS" BASIS,         *
00016 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  *
00017 *  See the License for the specific language governing permissions and       *
00018 *  limitations under the License.                                            *
00019 *                                                                            *
00020 *****************************************************************************/
00021 #ifndef _XN_CYCLIC_STACK_T_H_
00022 #define _XN_CYCLIC_STACK_T_H_ 
00023 
00024 //---------------------------------------------------------------------------
00025 // Includes
00026 //---------------------------------------------------------------------------
00027 #include "XnStackT.h"
00028 
00029 //---------------------------------------------------------------------------
00030 // Code
00031 //---------------------------------------------------------------------------
00032 
00033 template<class T, XnUInt32 TDefaultMaxDepth, class TAlloc = XnLinkedNodeDefaultAllocatorT<T> >
00034 class XnCyclicStackT : protected XnStackT<T, TAlloc>
00035 {
00036 public:
00037     typedef XnStackT<T, TAlloc> Base;
00038 
00039     XnCyclicStackT(XnUInt32 nMaxDepth = TDefaultMaxDepth) : Base(), m_nMaxDepth(nMaxDepth) {}
00040 
00041     XnCyclicStackT(const XnCyclicStackT& other) : Base(other) 
00042     {
00043         *this = other;
00044     }
00045 
00046     XnCyclicStackT& operator=(const XnCyclicStackT& other)
00047     {
00048         Base::operator=(other);
00049         m_nMaxDepth = other.m_nMaxDepth;
00050         return *this;
00051     }
00052 
00053     ~XnCyclicStackT() {}
00054 
00055     using typename Base::ConstIterator;
00056     using Base::Remove;
00057     using Base::IsEmpty;
00058     using Base::Size;
00059 
00060     XnStatus SetMaxSize(XnUInt32 nMaxSize)
00061     {
00062         XnStatus nRetVal = XN_STATUS_OK;
00063 
00064         while (Size() > nMaxSize)
00065         {
00066             nRetVal = Remove(this->ReverseBegin());
00067             XN_IS_STATUS_OK(nRetVal);
00068         }
00069 
00070         m_nMaxDepth = nMaxSize;
00071 
00072         return (XN_STATUS_OK);
00073     }
00074 
00075     XnStatus Push(T const& value) 
00076     { 
00077         XnStatus nRetVal = XN_STATUS_OK;
00078         if (Size() == m_nMaxDepth)
00079         {
00080             nRetVal = Remove(this->ReverseBegin());
00081             XN_IS_STATUS_OK(nRetVal);
00082         }
00083 
00084         nRetVal = Base::Push(value); 
00085         XN_IS_STATUS_OK(nRetVal);
00086 
00087         return (XN_STATUS_OK);
00088     }
00089 
00090     using Base::Pop;
00091     using Base::Top;
00092     using Base::Begin;
00093     using Base::End;
00094 
00095 protected:
00096     XnUInt32 m_nMaxDepth;
00097 };
00098 
00099 
00100 #endif // _XN_CYCLIC_STACK_T_H_