/******************************************************************************
Copyright (C) 2005 Matteo Lucarelli

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program 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 General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
******************************************************************************/

// dynaList is a Class to manage dynamic list
// current is a pointer that can be moved in the list
// payload is a user-defined void* (the object of the list)

#if !defined(_LISTMANAGER_H_INCLUDED_)
#define _LISTMANAGER_H_INCLUDED_

#include <stdlib.h>

class dynaList
{
public:
	// create a new dynamic list with initCount items;
	dynaList(int count=0);
	
	// free the list (NOT the payloads!)
	virtual ~dynaList();
	
	// delete all items in list 
	// WARNING: does not delete payload objects!
	void delAll();
	
	// get total items number
	int getCount();

	// functions to move current pointer
	// return value is true on success
	bool moveFirst();
	bool moveLast();
	bool moveTo(int index); // index starts from 1
	bool movePrev();
	bool moveNext();

	// functions to delete items
	// return value is NULL if list (or payload) is empty
	// else return value is pointer to deleted-item payload
	// CANNOT delete payload, then use something like
	// delete (myPayload*)(list.delLast());
	void *delFirst();
	void *delLast();
	void *delCurrent();

	// functions to add new items
	// return value is true on success
	// current moves to new item
	// optiona argument  is the payload of new item
	bool addNewLast(void *payload=NULL);
	bool addNewFirst(void *payload=NULL);
	bool addNewAfterCurrent(void *payload=NULL);
	bool addNewBeforeCurrent(void *payload=NULL);
	
	// set the payload of current item
	// return false if there is no current (thus there is no list)
	bool setCurrentPayload(void *obj);
	
	// get the payload of current item (NULL if not set)
	void *getCurrentPayload();
	
	// get index of current item
	// index start from 1, then 0 means error
	int getCurrentIndex();

private:

	// the list item structure
	struct listItem{	
		struct listItem *prev;  // pointer to next item
		struct listItem *next;  // pointer to previous item
		void* payload;          // user-defined pointer
	};
	
	struct listItem *m_first;   // pointer to first item
	struct listItem *m_last;    // pointer to last item
	struct listItem *m_current; // pointer to current item
};

#endif // !defined(_LISTMANAGER_H_INCLUDED_)