16 #include <nn/pia/common/common_Definitions.h>
17 #include <nn/pia/common/common_OffsetList.h>
37 class ObjList :
public nn::pia::common::ListBase
57 Node& operator=(
const Node&);
62 NN_PIA_PRAGMA_PUSH_WARNINGS
63 NN_PIA_DISABLE_WARNING_CLANG_INVALID_OFFSETOF
64 c_LinkOffset = offsetof(Node, m_Link),
65 c_ItemOffset = offsetof(Node, m_Item)
66 NN_PIA_PRAGMA_POP_WARNINGS
82 c_NodeSize =
sizeof(Node)
90 : ListBase(), m_FreeList(), m_pBuffer(NULL), m_LimitNum(0)
92 NN_PIA_PRAGMA_PUSH_WARNINGS
93 NN_PIA_DISABLE_WARNING_CLANG_INVALID_OFFSETOF
94 m_FreeList.InitOffset(offsetof(Node, m_Link));
95 NN_PIA_PRAGMA_POP_WARNINGS
104 static uint32_t GetNeedBufferSize(uint32_t limitNum)
106 return limitNum * c_NodeSize;
118 void SetBuffer(uint32_t limitNum,
void* pBuffer)
124 m_pBuffer =
reinterpret_cast<Node*
>(pBuffer);
125 m_LimitNum = limitNum;
126 for (uint32_t i = 0; i < limitNum; ++i)
128 new (&m_pBuffer[i].m_Link) ListNode();
129 m_FreeList.PushBack(&m_pBuffer[i]);
134 PIA_ASSERTMSG(
false,
"pBuf is null");
139 PIA_ASSERTMSG(
false,
"limitNum[%d] must be larger than zero", limitNum);
149 bool IsBufferReady()
const
151 return m_pBuffer != NULL;
160 uint32_t MaxSize()
const
173 return m_FreeList.IsEmpty();
184 ListNode* pNode = Terminator()->Next();
185 while (pNode != Terminator())
187 ListNode* pN = pNode;
188 pNode = pNode->Next();
190 m_FreeList.PushFront(ListNodeToNode(pN));
207 PIA_ASSERTMSG(
false,
"buffer full");
210 Node* pNode = m_FreeList.PopBack();
211 new (&pNode->m_Item) T();
212 PushBackNode(&(pNode->m_Link));
213 return &(pNode->m_Item);
230 Node* pNode = m_FreeList.PopBack();
231 new (&pNode->m_Item) T();
232 PushBackNode(&(pNode->m_Link));
233 return &(pNode->m_Item);
248 PIA_ASSERTMSG(
false,
"buffer full");
251 Node* pNode = m_FreeList.PopBack();
252 new (&pNode->m_Item) T();
253 PushFrontNode(&(pNode->m_Link));
254 return &(pNode->m_Item);
271 Node* pNode = m_FreeList.PopBack();
272 new (&pNode->m_Item) T();
273 PushFrontNode(&(pNode->m_Link));
274 return &(pNode->m_Item);
287 T* BirthBefore(
const T* pBasis)
291 PIA_ASSERTMSG(
false,
"buffer full");
294 Node* pNode = m_FreeList.PopBack();
295 new (&pNode->m_Item) T();
296 InsertBeforeNode(&(ObjToNode(pBasis)->m_Link), &(pNode->m_Link));
297 return &(pNode->m_Item);
310 T* TryBirthBefore(
const T* pBasis)
316 Node* pNode = m_FreeList.PopBack();
317 new (&pNode->m_Item) T();
318 InsertBeforeNode(&(ObjToNode(pBasis)->m_Link), &(pNode->m_Link));
319 return &(pNode->m_Item);
332 T* BirthAfter(
const T* pBasis)
336 PIA_ASSERTMSG(
false,
"buffer full");
339 Node* pNode = m_FreeList.PopBack();
340 new (&pNode->m_Item) T();
341 InsertAfterNode(&(ObjToNode(pBasis)->m_Link), &(pNode->m_Link));
342 return &(pNode->m_Item);
355 T* TryBirthAfter(
const T* pBasis)
361 Node* pNode = m_FreeList.PopBack();
362 new (&pNode->m_Item) T();
363 InsertAfterNode(&(ObjToNode(pBasis)->m_Link), &(pNode->m_Link));
364 return &(pNode->m_Item);
377 Node* pNode = ObjToNode(pObj);
379 EraseNode(&(pNode->m_Link));
380 m_FreeList.PushFront(pNode);
392 ListNode* pLink = FrontNode();
397 return &(ListNodeToNode(pLink)->m_Item);
409 ListNode* pLink = BackNode();
414 return &(ListNodeToNode(pLink)->m_Item);
427 T* Prev(
const T* pObj)
429 ListNode* pPrevLink = ObjToNode(pObj)->m_Link.Prev();
430 if (pPrevLink == Terminator())
434 return &(ListNodeToNode(pPrevLink)->m_Item);
447 T* Next(
const T* pObj)
449 ListNode* pNextLink = ObjToNode(pObj)->m_Link.Next();
450 if (pNextLink == Terminator())
454 return &(ListNodeToNode(pNextLink)->m_Item);
465 bool IsInclude(
const T* pObj)
const
467 return IsIncludeNode(&(ObjToNode(pObj)->m_Link));
476 friend class ObjList<T>;
479 Iterator(
const ObjList<T>* pList,
typename ObjList<T>::Node* pNode)
480 : m_pList(pList), m_pNode(pNode)
485 Iterator& operator++()
487 m_pNode = m_pList->ListNodeToNode(m_pNode->m_Link.Next());
491 bool operator==(
const Iterator& x)
const
493 return m_pNode == x.m_pNode;
495 bool operator!=(
const Iterator& x)
const
497 return m_pNode != x.m_pNode;
502 return m_pNode->m_Item;
506 return &(m_pNode->m_Item);
510 const ObjList<T>* m_pList;
511 typename ObjList<T>::Node* m_pNode;
520 friend class ObjList<T>;
523 ConstIterator(
const ObjList<T>* pList,
typename ObjList<T>::Node* pNode)
524 : m_pList(pList), m_pNode(pNode)
529 ConstIterator& operator++()
531 m_pNode = m_pList->ListNodeToNode(m_pNode->m_Link.Next());
535 bool operator==(
const ConstIterator& x)
const
537 return m_pNode == x.m_pNode;
539 bool operator!=(
const ConstIterator& x)
const
541 return m_pNode != x.m_pNode;
544 const T& operator*()
const
546 return m_pNode->m_Item;
548 const T* operator->()
const
550 return &(m_pNode->m_Item);
554 const ObjList<T>* m_pList;
555 typename ObjList<T>::Node* m_pNode;
567 friend class ObjList<T>;
570 RobustIterator(
const ObjList<T>* pList,
typename ObjList<T>::Node* pNode)
571 : m_pList(pList), m_pNode(pNode)
573 m_pNextLink = m_pNode->m_Link.Next();
577 RobustIterator& operator++()
579 m_pNode = m_pList->ListNodeToNode(m_pNextLink);
580 m_pNextLink = m_pNode->m_Link.Next();
584 bool operator==(
const RobustIterator& x)
const
586 return m_pNode == x.m_pNode;
588 bool operator!=(
const RobustIterator& x)
const
590 return m_pNode != x.m_pNode;
595 return m_pNode->m_Item;
599 return &(m_pNode->m_Item);
603 const ObjList<T>* m_pList;
604 typename ObjList<T>::Node* m_pNode;
605 ListNode* m_pNextLink;
614 Iterator Begin()
const
616 return Iterator(
this, ListNodeToNode(Terminator()->Next()));
627 return Iterator(
this, ListNodeToNode(Terminator()));
636 ConstIterator ConstBegin()
const
638 return ConstIterator(
this, ListNodeToNode(Terminator()->Next()));
647 ConstIterator ConstEnd()
const
649 return ConstIterator(
this, ListNodeToNode(Terminator()));
658 RobustIterator RobustBegin()
const
660 return RobustIterator(
this, ListNodeToNode(Terminator()->Next()));
669 RobustIterator RobustEnd()
const
671 return RobustIterator(
this, ListNodeToNode(Terminator()));
675 static Node* ObjToNode(
const T* pObj)
677 return reinterpret_cast<Node*
>(PtrUtil::addOffset(pObj, -c_ItemOffset));
680 static Node* ListNodeToNode(
const ListNode* pListNode)
682 return reinterpret_cast<Node*
>(PtrUtil::addOffset(pListNode, -c_LinkOffset));
687 OffsetList<Node> m_FreeList;
691 ObjList(
const ObjList&);
692 ObjList& operator=(
const ObjList&);