Source: src/ptrlist.h


Annotated List
Files
Globals
Hierarchy
Index
/***************************************************************************
	vim:tabstop=4
						ptrlist.h  -  description
                        	-------------------
	Template class to manage a list of pointer with Counter.
	begin				: Sat Nov 24 2001
	copyright			: (C) 2001 by Francois Biot
	email				: fbiot@free.fr
 ***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef	_ptrlist_h_
#define _ptrlist_h_

#include	
#include	
#include	

using namespace std;

/** :set
  * the T class must:
  * overload the ==  operator to ensure that the get
  * function works correctly.
  */
template < class T >
class ptrlist
{
public:
	ptrlist()	:	mbKill(false)		{};

	virtual ~ptrlist();

	class	item
	{
		public:
			item(	T*			pT,
					ptrlist*	pOwner)
					:
					mpT		(pT),
					mlCount	(0),
					mpOwner	(pOwner)	{};

			virtual ~item()	{	if (mpOwner)	mpOwner->Vanish(this);	};		

			long operator	++(int)	{	return mlCount++;	};
			long operator	--(int)	{	return mlCount--;	};
			operator	long()	{	return mlCount;	};
			bool operator	==(const item& o)	{	return (mpT==o.mpT) && (mlCount==o.mlCount);	};

			T*	ptr()	{	return mpT;	};
			
		private:
			item(const item&);
			item&	operator=(const item&);
			
			T*			mpT;		// The object pointed
			long		mlCount;	// Counter of occurences
			ptrlist	*mpOwner;	// Owner of this
	};
	
	// Add the object if it does not already exists.
	// return 0 if the object exists, else
	// return the ptr on the already existing object.
	// (in this case, ptr should be deleted.
	//
	// After this method call
	// pToUse is the ptr to use anyway.
	// ptr maybe deleted !!!
	// The use count  of the ptr is increased
	T*	Add(T* ptr,T* &pToUse);

	// Removes an occurence of a T*
	// The object is deleted only if the count of occurences is 0	
	void	Remove(T* ptr);

	typedef	CList	TContainer;

	ITERATORS_ALL(mlst);

	friend class item;

private:

	void	Vanish(const item *);
	
	bool		mbKill;
	TContainer	mlst;
	
};

template 
T* ptrlist::Add(T* p,T* &pToUse)
{
	const_iterator	oIt=begin();
	T*	pRet=0;

	while(oIt!=end())
	{
		if (*p==*oIt)
		{
			pRet=*oIt;
			pToUse=(T*)*oIt;
			break;
		}	
		oIt++;
	}

	// p does not exists, we add it
	if (pRet==0)
	{
		item	pItem=new item(p);

		mlst.push_front(pItem);

		pToUse=p;
	}
	else
		delete p;

	return	pRet;
}


template 
void	ptrlist::Vanish(const item *pItem)
{
	if (!mbKill)
	{
		iterator	oIt=begin();

		while(oIt!=end())
		{
			item	*p=&(*oIt);

			if (pItem==p)
			{
				item x(0,0);
				mlst.remove(x);
				return;
			}
		}

		cerr << "ptrlist::Vanish could not find a math !!!" << endl;
	}
}

template 
ptrlist::~ptrlist()
{
	mbKill=true;

	while(mlst.size())
	{
		T*	p=(*(mlst.begin())).ptr();
		
		delete p;

		mlst.pop_front;
	}

}

#endif

Generated by: saturn on FrancoisLinux on Sun Feb 3 20:01:44 2002, using kdoc 2.0a53.