XnList.h
Go to the documentation of this file.
/****************************************************************************
* *
* OpenNI 1.x Alpha *
* Copyright (C) 2011 PrimeSense Ltd. *
* *
* This file is part of OpenNI. *
* *
* OpenNI is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* OpenNI is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with OpenNI. If not, see <http://www.gnu.org/licenses/>. *
* *
****************************************************************************/
#ifndef _XN_LIST_H
#define _XN_LIST_H
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include <XnDataTypes.h>
#include <IXnNodeAllocator.h>
#include <XnNodeAllocator.h>
#include <XnNode.h>
#include <XnStatusCodes.h>
//---------------------------------------------------------------------------
// Types
//---------------------------------------------------------------------------
class XnList
{
public:
class ConstIterator
{
public:
friend class XnList;
ConstIterator(const ConstIterator& other) : m_pCurrent(other.m_pCurrent) {}
ConstIterator& operator++()
{
m_pCurrent = m_pCurrent->Next();
return *this;
}
ConstIterator operator++(int)
{
ConstIterator other(m_pCurrent);
m_pCurrent = m_pCurrent->Next();
return other;
}
ConstIterator& operator--()
{
m_pCurrent = m_pCurrent->Previous();
return *this;
}
ConstIterator operator--(int)
{
ConstIterator other = *this;
--*this;
return other;
}
XnBool operator==(const ConstIterator& other) const
{
return m_pCurrent == other.m_pCurrent;
}
XnBool operator!=(const ConstIterator& other) const
{
return m_pCurrent != other.m_pCurrent;
}
const XnValue& operator*() const
{
return m_pCurrent->Data();
}
const XnNode* GetNode() const
{
return m_pCurrent;
}
XnNode* GetNode()
{
return m_pCurrent;
}
protected:
ConstIterator(XnNode* pNode) : m_pCurrent(pNode) {}
XnNode* m_pCurrent;
};
class Iterator : public ConstIterator
{
public:
friend class XnList;
inline Iterator(const Iterator& other) : ConstIterator(other) {}
inline Iterator& operator++()
{
++(*(ConstIterator*)this);
return (*this);
}
inline Iterator operator++(int)
{
Iterator result = *this;
++*this;
return (result);
}
inline Iterator& operator--()
{
--(*(ConstIterator*)this);
return (*this);
}
inline Iterator operator--(int)
{
Iterator result = *this;
--*this;
return (result);
}
inline XnValue& operator*() const { return ((XnValue&)**(ConstIterator*)this); }
protected:
inline Iterator(XnNode* pNode) : ConstIterator(pNode) {}
};
public:
XnList()
{
//Default node allocator is XnNodeAllocator
Init(XN_NEW(XnNodeAllocator));
m_bOwnsAllocator = TRUE;
}
virtual ~XnList()
{
Clear();
// Return base node to the pool
m_pNodeAllocator->Deallocate(m_pBase);
if (m_bOwnsAllocator)
{
//We created the allocator in this object, so we must release it
XN_DELETE(m_pNodeAllocator);
}
}
XnStatus AddFirst(const XnValue& value)
{
return Add(m_pBase, value);
}
XnStatus AddLast(const XnValue& value)
{
return Add(rbegin().m_pCurrent, value);
}
XnStatus AddAfter(ConstIterator where, const XnValue& val)
{
if (where == end())
{
return XN_STATUS_ILLEGAL_POSITION;
}
return Add(where.m_pCurrent, val);
}
XnStatus AddBefore(ConstIterator where, const XnValue& val)
{
if (where == end())
{
return XN_STATUS_ILLEGAL_POSITION;
}
return Add(where.m_pCurrent->Previous(), val);
}
Iterator Find(const XnValue& value)
{
if (IsEmpty())
{
return end();
}
Iterator iter = begin();
for (; iter != end(); ++iter)
{
if (*iter == value)
break;
}
return iter;
}
ConstIterator Find(const XnValue& value) const
{
if (IsEmpty())
{
return end();
}
ConstIterator iter = begin();
for (; iter != end(); ++iter)
{
if (*iter == value)
break;
}
return iter;
}
XnStatus Remove(ConstIterator where, XnValue& value)
{
value = *where;
return Remove(where);
}
virtual XnStatus Remove(ConstIterator where)
{
// Verify iterator is valid
if (where == end())
{
return XN_STATUS_ILLEGAL_POSITION;
}
if (IsEmpty())
{
return XN_STATUS_IS_EMPTY;
}
XnNode* pToRemove = where.m_pCurrent;
// Connect other nodes to bypass the one removed
pToRemove->Previous()->Next() = pToRemove->Next();
pToRemove->Next()->Previous() = pToRemove->Previous();
// Return removed node to the pool
m_pNodeAllocator->Deallocate(pToRemove);
return XN_STATUS_OK;
}
XnStatus Clear()
{
while (!IsEmpty())
Remove(begin());
return XN_STATUS_OK;
}
XnBool IsEmpty() const
{
return (begin() == end());
}
XnUInt32 Size() const
{
XnUInt32 nSize = 0;
for (ConstIterator iter = begin(); iter != end(); ++iter, ++nSize)
;
return nSize;
}
Iterator begin()
{
return Iterator(m_pBase->Next());
}
ConstIterator begin() const
{
return ConstIterator(m_pBase->Next());
}
Iterator end()
{
return Iterator(m_pBase);
}
ConstIterator end() const
{
return ConstIterator(m_pBase);
}
Iterator rbegin()
{
return Iterator(m_pBase->Previous());
}
ConstIterator rbegin() const
{
return ConstIterator(m_pBase->Previous());
}
Iterator rend()
{
return Iterator(m_pBase);
}
ConstIterator rend() const
{
return ConstIterator(m_pBase);
}
protected:
friend class XnNodeManager;
XnList(INiNodeAllocator* pNodeAllocator)
{
Init(pNodeAllocator);
m_bOwnsAllocator = FALSE;
}
void Init(INiNodeAllocator* pNodeAllocator)
{
m_pNodeAllocator = pNodeAllocator;
// Allocate a node to act as base node.
m_pBase = m_pNodeAllocator->Allocate();
if (m_pBase == NULL)
{
// OZOZ: Allocation failed in ctor...
}
m_pBase->Next() = m_pBase->Previous() = m_pBase;
}
XnStatus Add(XnNode* pWhere, const XnValue& val)
{
// Get a node from the pool for the entry
XnNode* pNewNode = m_pNodeAllocator->Allocate();
if (pNewNode == NULL)
{
return XN_STATUS_ALLOC_FAILED;
}
// push new node to position
pNewNode->Data() = val;
pNewNode->Next() = pWhere->Next();
pNewNode->Previous() = pWhere;
pWhere->Next()->Previous() = pNewNode;
pWhere->Next() = pNewNode;
return XN_STATUS_OK;
}
XnNode* m_pBase;
INiNodeAllocator* m_pNodeAllocator;
XnBool m_bOwnsAllocator;
private:
XN_DISABLE_COPY_AND_ASSIGN(XnList);
};
#define XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(decl, Type, ClassName, Translator) \
class decl ClassName : public XnList \
{ \
public: \
class decl ConstIterator : public XnList::ConstIterator \
{ \
public: \
friend class ClassName; \
inline ConstIterator(const ConstIterator& other) : XnList::ConstIterator(other) {} \
inline ConstIterator& operator++() \
{ \
++(*(XnList::ConstIterator*)this); \
return (*this); \
} \
inline ConstIterator operator++(int) \
{ \
ConstIterator result = *this; \
++*this; \
return result; \
} \
inline ConstIterator& operator--() \
{ \
--(*(XnList::ConstIterator*)this); \
return (*this); \
} \
inline ConstIterator operator--(int) \
{ \
ConstIterator result = *this; \
--*this; \
return result; \
} \
inline Type const& operator*() const \
{ \
return Translator::GetFromValue(**((XnList::ConstIterator*)this)); \
} \
inline Type const* operator->() const { return (&**this); } \
protected: \
inline ConstIterator(XnNode* pNode) : XnList::ConstIterator(pNode) {} \
inline ConstIterator(const XnList::ConstIterator& other) : \
XnList::ConstIterator(other) \
{} \
}; \
class decl Iterator : public ConstIterator \
{ \
public: \
friend class ClassName; \
Iterator(const Iterator& other) : ConstIterator(other) {} \
inline Iterator& operator++() \
{ \
++(*(ConstIterator*)this); \
return (*this); \
} \
inline Iterator operator++(int) \
{ \
Iterator result = *this; \
++*this; \
return result; \
} \
inline Iterator& operator--() \
{ \
--(*(ConstIterator*)this); \
return (*this); \
} \
inline Iterator operator--(int) \
{ \
Iterator result = *this; \
--*this; \
return result; \
} \
inline Type& operator*() const { return ((Type&)**(ConstIterator*)this); } \
inline Type* operator->() const { return (&**this); } \
protected: \
inline Iterator(XnNode* pNode) : ConstIterator(pNode) {} \
inline Iterator(const XnList::Iterator& other) : ConstIterator(other) {} \
}; \
public: \
ClassName() \
{ \
} \
~ClassName() \
{ \
while (!IsEmpty()) \
Remove(begin()); \
} \
inline XnStatus AddFirst(Type const& value) \
{ \
XnValue val = Translator::CreateValueCopy(value); \
XnStatus nRetVal = XnList::AddFirst(val); \
if (nRetVal != XN_STATUS_OK) \
{ \
Translator::FreeValue(val); \
return (nRetVal); \
} \
return XN_STATUS_OK; \
} \
inline XnStatus AddLast(Type const& value) \
{ \
XnValue val = Translator::CreateValueCopy(value); \
XnStatus nRetVal = XnList::AddLast(val); \
if (nRetVal != XN_STATUS_OK) \
{ \
Translator::FreeValue(val); \
return (nRetVal); \
} \
return XN_STATUS_OK; \
} \
inline XnStatus AddAfter(ConstIterator where, Type const& value) \
{ \
XnValue val = Translator::CreateValueCopy(value); \
XnStatus nRetVal = XnList::AddAfter(where, val); \
if (nRetVal != XN_STATUS_OK) \
{ \
Translator::FreeValue(val); \
return (nRetVal); \
} \
return XN_STATUS_OK; \
} \
inline XnStatus AddBefore(ConstIterator where, Type const& value) \
{ \
XnValue val = Translator::CreateValueCopy(value); \
XnStatus nRetVal = XnList::AddBefore(where, val); \
if (nRetVal != XN_STATUS_OK) \
{ \
Translator::FreeValue(val); \
return (nRetVal); \
} \
return XN_STATUS_OK; \
} \
inline ConstIterator Find(Type const& value) const \
{ \
XnValue _value = Translator::GetAsValue(value); \
return XnList::Find(_value); \
} \
inline Iterator Find(Type const& value) \
{ \
XnValue _value = Translator::GetAsValue(value); \
return XnList::Find(_value); \
} \
inline XnStatus Remove(ConstIterator where) \
{ \
XnValue val = Translator::GetAsValue(*where); \
XnStatus nRetVal = XnList::Remove(where); \
if (nRetVal != XN_STATUS_OK) return (nRetVal); \
Translator::FreeValue(val); \
return XN_STATUS_OK; \
} \
inline XnStatus Remove(Type const& value) \
{ \
Iterator it = Find(value); \
return Remove(it); \
} \
inline Iterator begin() { return XnList::begin(); } \
inline ConstIterator begin() const { return XnList::begin(); } \
inline Iterator end() { return XnList::end(); } \
inline ConstIterator end() const { return XnList::end(); } \
inline Iterator rbegin() { return XnList::rbegin(); } \
inline ConstIterator rbegin() const { return XnList::rbegin(); } \
inline Iterator rend() { return XnList::rend(); } \
inline ConstIterator rend() const { return XnList::rend(); } \
protected: \
virtual XnStatus Remove(XnList::ConstIterator where) \
{ \
return Remove(ConstIterator(where)); \
} \
private: \
XN_DISABLE_COPY_AND_ASSIGN(ClassName); \
};
#define XN_DECLARE_LIST_WITH_TRANSLATOR(Type, ClassName, Translator) \
XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(, Type, ClassName, Translator)
#define XN_DECLARE_LIST_DECL(decl, Type, ClassName) \
XN_DECLARE_DEFAULT_VALUE_TRANSLATOR_DECL(decl, Type, XN_DEFAULT_TRANSLATOR_NAME(ClassName)) \
XN_DECLARE_LIST_WITH_TRANSLATOR_DECL(decl, Type, ClassName, XN_DEFAULT_TRANSLATOR_NAME(ClassName))
#define XN_DECLARE_LIST(Type, ClassName) \
XN_DECLARE_LIST_DECL(, Type, ClassName)
#endif // _XN_LIST_H
IXnNodeAllocator.h
XnDataTypes.h
XnValue
void * XnValue
Definition XnDataTypes.h:36
XN_DISABLE_COPY_AND_ASSIGN
#define XN_DISABLE_COPY_AND_ASSIGN(TypeName)
Definition XnMacros.h:119
XnNode.h
XnNodeAllocator.h
XN_DELETE
#define XN_DELETE(p)
Definition XnOS.h:336
XN_NEW
#define XN_NEW(type,...)
Definition XnOS.h:326
TRUE
#define TRUE
Definition XnPlatform.h:96
FALSE
#define FALSE
Definition XnPlatform.h:100
XnStatus
XnUInt32 XnStatus
Definition XnStatus.h:34
XN_STATUS_OK
#define XN_STATUS_OK
Definition XnStatus.h:37
XnStatusCodes.h
INiNodeAllocator
Definition IXnNodeAllocator.h:34
XnList::ConstIterator
Definition XnList.h:45
XnList::ConstIterator::GetNode
XnNode * GetNode()
Definition XnList.h:133
XnList::ConstIterator::XnList
friend class XnList
Definition XnList.h:47
XnList::ConstIterator::operator--
ConstIterator & operator--()
Definition XnList.h:78
XnList::ConstIterator::operator++
ConstIterator operator++(int)
Definition XnList.h:68
XnList::ConstIterator::operator!=
XnBool operator!=(const ConstIterator &other) const
Definition XnList.h:108
XnList::ConstIterator::operator--
ConstIterator operator--(int)
Definition XnList.h:87
XnList::ConstIterator::m_pCurrent
XnNode * m_pCurrent
Definition XnList.h:147
XnList::ConstIterator::operator++
ConstIterator & operator++()
Definition XnList.h:59
XnList::ConstIterator::operator*
const XnValue & operator*() const
Definition XnList.h:116
XnList::ConstIterator::GetNode
const XnNode * GetNode() const
Definition XnList.h:125
XnList::ConstIterator::ConstIterator
ConstIterator(const ConstIterator &other)
Definition XnList.h:54
XnList::ConstIterator::operator==
XnBool operator==(const ConstIterator &other) const
Definition XnList.h:99
XnList::ConstIterator::ConstIterator
ConstIterator(XnNode *pNode)
Definition XnList.h:144
XnList::Iterator
Definition XnList.h:154
XnList::Iterator::operator--
Iterator & operator--()
Definition XnList.h:186
XnList::Iterator::XnList
friend class XnList
Definition XnList.h:156
XnList::Iterator::operator*
XnValue & operator*() const
Definition XnList.h:204
XnList::Iterator::operator++
Iterator operator++(int)
Definition XnList.h:176
XnList::Iterator::operator++
Iterator & operator++()
Definition XnList.h:168
XnList::Iterator::operator--
Iterator operator--(int)
Definition XnList.h:194
XnList::Iterator::Iterator
Iterator(const Iterator &other)
Definition XnList.h:163
XnList::Iterator::Iterator
Iterator(XnNode *pNode)
Definition XnList.h:212
XnList::m_bOwnsAllocator
XnBool m_bOwnsAllocator
Definition XnList.h:550
XnList::end
Iterator end()
Definition XnList.h:449
XnList::AddLast
XnStatus AddLast(const XnValue &value)
Definition XnList.h:262
XnList::Init
void Init(INiNodeAllocator *pNodeAllocator)
Definition XnList.h:506
XnList::~XnList
virtual ~XnList()
Definition XnList.h:229
XnList::Size
XnUInt32 Size() const
Definition XnList.h:421
XnList::XnList
XnList()
Definition XnList.h:219
XnList::end
ConstIterator end() const
Definition XnList.h:457
XnList::rbegin
ConstIterator rbegin() const
Definition XnList.h:473
XnList::m_pBase
XnNode * m_pBase
Definition XnList.h:547
XnList::rend
Iterator rend()
Definition XnList.h:481
XnList::Find
ConstIterator Find(const XnValue &value) const
Definition XnList.h:336
XnList::Remove
virtual XnStatus Remove(ConstIterator where)
Definition XnList.h:374
XnList::rend
ConstIterator rend() const
Definition XnList.h:489
XnList::Remove
XnStatus Remove(ConstIterator where, XnValue &value)
Definition XnList.h:361
XnList::AddFirst
XnStatus AddFirst(const XnValue &value)
Definition XnList.h:250
XnList::Clear
XnStatus Clear()
Definition XnList.h:402
XnList::rbegin
Iterator rbegin()
Definition XnList.h:465
XnList::Find
Iterator Find(const XnValue &value)
Definition XnList.h:312
XnList::XnList
XnList(INiNodeAllocator *pNodeAllocator)
Definition XnList.h:500
XnList::m_pNodeAllocator
INiNodeAllocator * m_pNodeAllocator
Definition XnList.h:549
XnList::begin
Iterator begin()
Definition XnList.h:433
XnList::AddAfter
XnStatus AddAfter(ConstIterator where, const XnValue &val)
Definition XnList.h:276
XnList::begin
ConstIterator begin() const
Definition XnList.h:441
XnList::AddBefore
XnStatus AddBefore(ConstIterator where, const XnValue &val)
Definition XnList.h:294
XnList::XnNodeManager
friend class XnNodeManager
Definition XnList.h:495
XnList::Add
XnStatus Add(XnNode *pWhere, const XnValue &val)
Definition XnList.h:527
XnList::IsEmpty
XnBool IsEmpty() const
Definition XnList.h:413
XnNodeAllocator
Definition XnNodeAllocator.h:29
XnNode
Definition XnNode.h:38
XnNode::Previous
XnNode *& Previous()
Definition XnNode.h:60
XnNode::Data
XnValue & Data()
Definition XnNode.h:69
XnNode::Next
XnNode *& Next()
Definition XnNode.h:51