DoublyLinkedList.cpp #3

  • //
  • guest/
  • eskopljak/
  • p4api.net/
  • main/
  • p4bridge/
  • DoublyLinkedList.cpp
  • View
  • Commits
  • Open Download .zip Download (3 KB)
#include "stdafx.h"
#include "DoublyLinkedList.h"

class DoublyLinkedList;

DoublyLinkedListItem::DoublyLinkedListItem(DoublyLinkedList *list)
{
	Id = -1;

    // Initialize the list pointers
    pNextItem = NULL;
    pPrevItem = NULL;

	pList = list;

	if (list)
	{
		list->Add(this);
	}
}

DoublyLinkedListItem::DoublyLinkedListItem(DoublyLinkedList *list, int newId)
{
	Id = newId;

    // Initialize the list pointers
    pNextItem = NULL;
    pPrevItem = NULL;

	pList = list;

	if (list)
	{
		list->Add(this);
	}
}

DoublyLinkedListItem::~DoublyLinkedListItem(void)
{
	Id = -1;

    // Initialize the list pointers
    pNextItem = NULL;
    pPrevItem = NULL;
}

DoublyLinkedList::DoublyLinkedList(ILockable* locker)
{
	Locker = locker;
	//InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x00000400); 
	
	pFirstItem = NULL;
	pLastItem = NULL;
	
	disposed = 0;

	itemCount = 0;
}

DoublyLinkedList::~DoublyLinkedList(void)
{
	if (disposed != 0)
	{
		return;
	}
	LOCK(Locker); 

	disposed = 1;

	DoublyLinkedListItem* curObj = pFirstItem;
	while (curObj != NULL)
	{	
		DoublyLinkedListItem* nextObj = curObj->pNextItem;
		delete curObj;
		curObj = nextObj;
	}
	pFirstItem = NULL;
	pLastItem = NULL;
}

void DoublyLinkedList::Add(DoublyLinkedListItem* object)
{
	LOCK(Locker); 
	int  cmdId = -1;

	// Initialize the list pointers
	object->pNextItem = NULL;
	object->pPrevItem = NULL;
	object->pList = this;

	// Add to the list of objects registered to be exported
	if(!pFirstItem)
	{
		// first object, initialize the list with this as the only element
		pFirstItem = object;
		pLastItem = object;
	}
	else
	{
		// add to the end of the list
		pLastItem->pNextItem = object;
		object->pPrevItem = pLastItem;
		pLastItem = object;
	}
	itemCount++;
}

void DoublyLinkedList::Remove(DoublyLinkedListItem* pObject, int deleteObj)
{
	LOCK(Locker); 
	// Remove from the list
	if ((pFirstItem == pObject) && (pLastItem == pObject))
	{
		// only object in the list, so NULL out the list head and tail pointers
		pFirstItem = NULL;
		pLastItem = NULL;
	}
	else if (pFirstItem == pObject)
	{
		// first object in list, set the head to the next object in the list
		pFirstItem = pObject->pNextItem;
		pFirstItem->pPrevItem = NULL;
	}
	else if (pLastItem == pObject)
	{
		// last object, set the tail to the pervious object in the list
		pLastItem = pObject->pPrevItem;
		pLastItem->pNextItem = NULL;
	}
	else 
	{
		// in the middle of the list, so link the pointers for the previous 
		//  and next objects.
		pObject->pPrevItem->pNextItem = pObject->pNextItem;
		pObject->pNextItem->pPrevItem = pObject->pPrevItem;
	}
	pObject->pPrevItem = NULL;
	pObject->pNextItem = NULL;

	if (deleteObj)
	{
		delete pObject;
	}
	itemCount--;
}

void DoublyLinkedList::Remove(int id)
{
	LOCK(Locker); 

	DoublyLinkedListItem* item = DoublyLinkedList::Find(id);

	Remove(item);
}

DoublyLinkedListItem* DoublyLinkedList::Find(int id)
{
	LOCK(Locker); 

	DoublyLinkedListItem* value = NULL;

	DoublyLinkedListItem* curObj = pFirstItem;
	while (curObj != NULL)
	{	
		if (curObj->Id == id)
		{
			value = curObj;
			curObj = NULL;
			break;
		}
		curObj = curObj->pNextItem;
	}
	return value;
}
# Change User Description Committed
#3 28480 eskopljak submit
#2 28479 eskopljak submit
#1 28441 eskopljak Merging using p4api.net_branch
//guest/perforce_software/p4api-net/main/p4bridge/DoublyLinkedList.cpp
#1 19043 Liz Lam Rename p4api.net to p4api-net
//guest/perforce_software/p4api.net/main/p4bridge/DoublyLinkedList.cpp
#1 19042 Liz Lam Rename/move file(s) to proper main branch.
//guest/perforce_software/p4api.net/p4bridge/DoublyLinkedList.cpp
#3 10191 Matt Attaway Bring Workshop version of p4api.net up-to-date with the 14.2 release.
#2 8964 Bill fix line endings
#1 8873 Matt Attaway Initial add of the P4API.NET source code